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.List;
020import java.util.Map;
021
022import org.w3c.dom.Node;
023
024import org.ametys.core.util.dom.AbstractAmetysElement;
025import org.ametys.core.util.dom.AmetysAttribute;
026import org.ametys.odf.enumeration.OdfReferenceTableEntry;
027import org.ametys.odf.enumeration.OdfReferenceTableHelper;
028import org.ametys.odf.enumeration.OdfReferenceTableHelper.SortField;
029
030/**
031 * DOM layer on reference table elements
032 */
033public class OdfReferenceTableElement extends AbstractAmetysElement
034{
035    private String _lang;
036    private OdfReferenceTableHelper _refTableHelper;
037    private String _refTableId;
038    private boolean _ordered;
039    
040    private List<OdfReferenceTableEntry> _children;
041    
042    
043    /**
044     * Constructor
045     * @param refTableId The id of reference table
046     * @param refTableHelper the reference table helper
047     * @param lang the language to use for entry
048     */
049    public OdfReferenceTableElement(String refTableId, OdfReferenceTableHelper refTableHelper, String lang)
050    {
051        this(refTableId, refTableHelper, lang, false);
052    }
053    
054    /**
055     * Constructor
056     * @param refTableId The id of reference table
057     * @param refTableHelper the reference table helper
058     * @param lang the language to use for entry
059     * @param ordered true to get items sorted by their 'order' attribute
060     */
061    public OdfReferenceTableElement(String refTableId, OdfReferenceTableHelper refTableHelper, String lang, boolean ordered)
062    {
063        _refTableHelper = refTableHelper;
064        _refTableId = refTableId;
065        _lang = lang;
066        _ordered = ordered;
067    }
068    
069    @Override
070    public String getTagName()
071    {
072        return "items";
073    }
074    
075    List<OdfReferenceTableEntry> getChildren()
076    {
077        if (_children == null)
078        {
079            _children = _ordered ? _refTableHelper.getItems(_refTableId, new SortField("order", true)) : _refTableHelper.getItems(_refTableId);
080        }
081        
082        return _children;
083    }
084    
085    @Override
086    protected Map<String, AmetysAttribute> _lookupAttributes()
087    {
088        Map<String, AmetysAttribute> result = new HashMap<>();
089        
090        result.put("id", new AmetysAttribute("id", "id", null, _refTableId, this));
091        
092        return result;
093    }
094    
095    @Override
096    public Node getFirstChild()
097    {
098        if (_children == null)
099        {
100            _children = _ordered ? _refTableHelper.getItems(_refTableId, new SortField("order", true)) : _refTableHelper.getItems(_refTableId);
101        }
102        
103        if (_children.isEmpty())
104        {
105            return null;
106        }
107        
108        return new OdfReferenceTableEntryElement(_children.get(0), _lang, this);
109    }
110}