View Javadoc
1   /*
2    * Copyright (c) 2002-2023, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.document.service.attributes;
35  
36  import fr.paris.lutece.plugins.document.business.attributes.DocumentAttribute;
37  import fr.paris.lutece.plugins.document.business.attributes.DocumentAttributeHome;
38  import fr.paris.lutece.util.xml.XmlUtil;
39  
40  import java.util.Collection;
41  
42  
43  /**
44   * Document Attributes management Service.
45   */
46  public class DocumentAttributesService
47  {
48      private static final String TAG_ATTRIBUTES = "attributes";
49      private static final String TAG_ATTRIBUTE = "attribute";
50      private static final String TAG_ATTRIBUTE_ID = "id_document_attr";
51      private static final String TAG_CODE_ATTRIBUTE_TYPE = "code_attr_type";
52      private static final String TAG_CODE_ATTRIBUTE = "code";
53      private static final String TAG_DOCUMENT_TYPE_ATTRIBUTE_NAME = "document_type_attr_name";
54      private static final String TAG_ATTRIBUTE_DESCRIPTION = "description";
55      private static DocumentAttributesServicettributes/DocumentAttributesService.html#DocumentAttributesService">DocumentAttributesService _singleton = new DocumentAttributesService(  );
56  
57      /** Creates a new instance of DocumentSpacesService */
58      private DocumentAttributesService(  )
59      {
60      }
61  
62      /**
63       * Returns the unique instance of the service
64       * @return the unique instance of the service
65       */
66      public static DocumentAttributesService getInstance(  )
67      {
68          return _singleton;
69      }
70  
71      /**
72       * Gets attributes list of a type of document as an XML document
73       * @param strCodeType The code for the document type
74       * @return An XML document containing attributes list
75       */
76      public String getXmlAttributesList( String strCodeType )
77      {
78          StringBuffer sbXML = new StringBuffer(  );
79          XmlUtil.beginElement( sbXML, TAG_ATTRIBUTES );
80  
81          for ( DocumentAttribute attribute : getAttributesOfDocumentType( strCodeType ) )
82          {
83              findDocumentAttributeByCodeType( sbXML, attribute.getId(  ) );
84          }
85  
86          XmlUtil.endElement( sbXML, TAG_ATTRIBUTES );
87  
88          return sbXML.toString(  );
89      }
90  
91      /**
92       * Get a attributes list of a selected code document type
93       * @param codeDocumentType The code document type
94       * @return The attributes list
95       */
96      private Collection<DocumentAttribute> getAttributesOfDocumentType( String codeDocumentType )
97      {
98          Collection<DocumentAttribute> listAttributes = DocumentAttributeHome.selectAllAttributesOfDocumentType( codeDocumentType );
99  
100         return listAttributes;
101     }
102 
103     /**
104     * Build recursively the XML document containing the arborescence of spaces
105     *
106     * @param sbXML The buffer in which adding the current space of the arborescence
107     * @param nAttributeId The attribute ID
108     */
109     private void findDocumentAttributeByCodeType( StringBuffer sbXML, int nAttributeId )
110     {
111         DocumentAttribute attribute = DocumentAttributeHome.findByPrimaryKey( nAttributeId );
112 
113         XmlUtil.beginElement( sbXML, TAG_ATTRIBUTE );
114         XmlUtil.addElement( sbXML, TAG_ATTRIBUTE_ID, attribute.getId(  ) );
115         XmlUtil.addElement( sbXML, TAG_CODE_ATTRIBUTE_TYPE, attribute.getCodeAttributeType(  ) );
116         XmlUtil.addElement( sbXML, TAG_CODE_ATTRIBUTE, attribute.getCode(  ) );
117         XmlUtil.addElement( sbXML, TAG_DOCUMENT_TYPE_ATTRIBUTE_NAME, attribute.getName(  ) );
118         XmlUtil.addElement( sbXML, TAG_ATTRIBUTE_DESCRIPTION, attribute.getDescription(  ) );
119         XmlUtil.endElement( sbXML, TAG_ATTRIBUTE );
120     }
121 }