001/*
002 *  Copyright 2019 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.odf.xslt;
017
018import java.util.HashMap;
019import java.util.Iterator;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.commons.lang3.StringUtils;
024import org.w3c.dom.Node;
025
026import org.ametys.core.util.dom.AbstractWrappingAmetysElement;
027import org.ametys.core.util.dom.AmetysAttribute;
028import org.ametys.odf.enumeration.OdfReferenceTableEntry;
029
030/**
031 * DOM layer for {@link OdfReferenceTableEntry}.
032 */
033public class OdfReferenceTableEntryElement extends AbstractWrappingAmetysElement<OdfReferenceTableEntry>
034{
035    private String _lang;
036    
037    /**
038     * Constructor.
039     * @param entry the underlying {@link OdfReferenceTableEntry}.
040     * @param lang the language
041     */
042    public OdfReferenceTableEntryElement(OdfReferenceTableEntry entry, String lang)
043    {
044        super(entry);
045        _lang = lang;
046    }
047    
048    /**
049     * Constructor.
050     * @param entry the underlying {@link OdfReferenceTableEntry}.
051     * @param lang the language
052     * @param parent the parent {@link OdfReferenceTableElement}.
053     */
054    public OdfReferenceTableEntryElement(OdfReferenceTableEntry entry, String lang, OdfReferenceTableElement parent)
055    {
056        super(entry, parent);
057        _lang = lang;
058    }
059    
060    @Override
061    public String getTagName()
062    {
063        return "item";
064    }
065    
066    @Override
067    protected Map<String, AmetysAttribute> _lookupAttributes()
068    {
069        Map<String, AmetysAttribute> result = new HashMap<>();
070        
071        result.put("title", new AmetysAttribute("title", "title", null, _object.getLabel(_lang), this));
072        result.put("code", new AmetysAttribute("code", "code", null, _object.getCode(), this));
073        result.put("id", new AmetysAttribute("id", "id", null, _object.getId(), this));
074        result.put("order", new AmetysAttribute("order", "order", null, _object.getOrder().toString(), this));
075        result.put("cdmValue", new AmetysAttribute("cdmValue", "cdmValue", null, _object.getCdmValue(), this));
076        result.put("archived", new AmetysAttribute("archived", "archived", null, _object.isArchived().toString(), this));
077        if (_object.getContent().hasDefinition("shortLabel"))
078        {
079            result.put("shortLabel", new AmetysAttribute("shortLabel", "shortLabel", null, _object.getContent().getValue("shortLabel", false, StringUtils.EMPTY), this));
080        }
081        
082        return result;
083    }
084    
085    @Override
086    public Node getNextSibling()
087    {
088        if (_parent == null)
089        {
090            return null;
091        }
092        
093        List<OdfReferenceTableEntry> children = ((OdfReferenceTableElement) _parent).getChildren();
094        
095        boolean isNext = false;
096        OdfReferenceTableEntry nextSibling = null;
097        Iterator<OdfReferenceTableEntry> it = children.iterator();
098        
099        while (nextSibling == null && it.hasNext())
100        {
101            OdfReferenceTableEntry child = it.next();
102            
103            if (isNext)
104            {
105                nextSibling = child;
106            }
107            else if (_object.getId().equals(child.getId()))
108            {
109                isNext = true;
110            }
111        }
112        
113        return nextSibling == null ? null : new OdfReferenceTableEntryElement(nextSibling, _lang, (OdfReferenceTableElement) _parent);
114    }
115}