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;
028
029/**
030 * DOM layer on reference table elements
031 */
032public class OdfReferenceTableElement extends AbstractAmetysElement
033{
034    private String _lang;
035    private OdfReferenceTableHelper _refTableHelper;
036    private String _refTableId;
037    
038    private List<OdfReferenceTableEntry> _children;
039    
040    
041    /**
042     * Constructor
043     * @param refTableId The id of reference table
044     * @param refTableHelper the reference table helper
045     * @param lang the language to use for entry
046     */
047    public OdfReferenceTableElement(String refTableId, OdfReferenceTableHelper refTableHelper, String lang)
048    {
049        _refTableHelper = refTableHelper;
050        _refTableId = refTableId;
051        _lang = lang;
052    }
053    
054    @Override
055    public String getTagName()
056    {
057        return "items";
058    }
059    
060    List<OdfReferenceTableEntry> getChildren()
061    {
062        if (_children == null)
063        {
064            _children = _refTableHelper.getItems(_refTableId);
065        }
066        
067        return _children;
068    }
069    
070    @Override
071    protected Map<String, AmetysAttribute> _lookupAttributes()
072    {
073        Map<String, AmetysAttribute> result = new HashMap<>();
074        
075        result.put("id", new AmetysAttribute("id", "id", null, _refTableId, this));
076        
077        return result;
078    }
079    
080    @Override
081    public Node getFirstChild()
082    {
083        if (_children == null)
084        {
085            _children = _refTableHelper.getItems(_refTableId);
086        }
087        
088        if (_children.isEmpty())
089        {
090            return null;
091        }
092        
093        return new OdfReferenceTableEntryElement(_children.get(0), _lang, this);
094    }
095}