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.w3c.dom.Node;
024
025import org.ametys.core.util.dom.AbstractWrappingAmetysElement;
026import org.ametys.core.util.dom.AmetysAttribute;
027import org.ametys.odf.enumeration.OdfReferenceTableEntry;
028
029/**
030 * DOM layer for {@link OdfReferenceTableEntry}.
031 */
032public class OdfReferenceTableEntryElement extends AbstractWrappingAmetysElement<OdfReferenceTableEntry>
033{
034    private String _lang;
035    
036    /**
037     * Constructor.
038     * @param entry the underlying {@link OdfReferenceTableEntry}.
039     * @param lang the language
040     */
041    public OdfReferenceTableEntryElement(OdfReferenceTableEntry entry, String lang)
042    {
043        super(entry);
044        _lang = lang;
045    }
046    
047    /**
048     * Constructor.
049     * @param entry the underlying {@link OdfReferenceTableEntry}.
050     * @param lang the language
051     * @param parent the parent {@link OdfReferenceTableElement}.
052     */
053    public OdfReferenceTableEntryElement(OdfReferenceTableEntry entry, String lang, OdfReferenceTableElement parent)
054    {
055        super(entry, parent);
056        _lang = lang;
057    }
058    
059    @Override
060    public String getTagName()
061    {
062        return "item";
063    }
064    
065    @Override
066    protected Map<String, AmetysAttribute> _lookupAttributes()
067    {
068        Map<String, AmetysAttribute> result = new HashMap<>();
069        
070        result.put("title", new AmetysAttribute("title", "title", null, _object.getLabel(_lang), this));
071        result.put("code", new AmetysAttribute("code", "code", null, _object.getCode(), this));
072        result.put("id", new AmetysAttribute("id", "id", null, _object.getId(), this));
073        
074        return result;
075    }
076    
077    @Override
078    public Node getNextSibling()
079    {
080        if (_parent == null)
081        {
082            return null;
083        }
084        
085        List<OdfReferenceTableEntry> children = ((OdfReferenceTableElement) _parent).getChildren();
086        
087        boolean isNext = false;
088        OdfReferenceTableEntry nextSibling = null;
089        Iterator<OdfReferenceTableEntry> it = children.iterator();
090        
091        while (nextSibling == null && it.hasNext())
092        {
093            OdfReferenceTableEntry child = it.next();
094            
095            if (isNext)
096            {
097                nextSibling = child;
098            }
099            else if (_object.getId().equals(child.getId()))
100            {
101                isNext = true;
102            }
103        }
104        
105        return nextSibling == null ? null : new OdfReferenceTableEntryElement(nextSibling, _lang, (OdfReferenceTableElement) _parent);
106    }
107}