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 private boolean _includeArchived; 040 041 private List<OdfReferenceTableEntry> _children; 042 043 044 /** 045 * Constructor 046 * @param refTableId The id of reference table 047 * @param refTableHelper the reference table helper 048 * @param lang the language to use for entry 049 */ 050 public OdfReferenceTableElement(String refTableId, OdfReferenceTableHelper refTableHelper, String lang) 051 { 052 this(refTableId, refTableHelper, lang, false, true); 053 } 054 055 /** 056 * Constructor 057 * @param refTableId The id of reference table 058 * @param refTableHelper the reference table helper 059 * @param lang the language to use for entry 060 * @param ordered true to get items sorted by their 'order' attribute 061 * @param includeArchived true to include archived items 062 */ 063 public OdfReferenceTableElement(String refTableId, OdfReferenceTableHelper refTableHelper, String lang, boolean ordered, boolean includeArchived) 064 { 065 _refTableHelper = refTableHelper; 066 _refTableId = refTableId; 067 _lang = lang; 068 _ordered = ordered; 069 _includeArchived = includeArchived; 070 } 071 072 @Override 073 public String getTagName() 074 { 075 return "items"; 076 } 077 078 List<OdfReferenceTableEntry> getChildren() 079 { 080 if (_children == null) 081 { 082 _children = _ordered ? _refTableHelper.getItems(_refTableId, _includeArchived, new SortField("order", true)) : _refTableHelper.getItems(_refTableId, _includeArchived); 083 } 084 085 return _children; 086 } 087 088 @Override 089 protected Map<String, AmetysAttribute> _lookupAttributes() 090 { 091 Map<String, AmetysAttribute> result = new HashMap<>(); 092 093 result.put("id", new AmetysAttribute("id", "id", null, _refTableId, this)); 094 095 return result; 096 } 097 098 @Override 099 public Node getFirstChild() 100 { 101 if (_children == null) 102 { 103 _children = _ordered ? _refTableHelper.getItems(_refTableId, _includeArchived, new SortField("order", true)) : _refTableHelper.getItems(_refTableId, _includeArchived); 104 } 105 106 if (_children.isEmpty()) 107 { 108 return null; 109 } 110 111 return new OdfReferenceTableEntryElement(_children.get(0), _lang, this); 112 } 113}