View Javadoc
1   /*
2    * Copyright (c) 2002-2018, Mairie de 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.modules.rest.util.builderxml;
35  
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  
40  import org.apache.commons.beanutils.locale.LocaleBeanUtils;
41  
42  import fr.paris.lutece.plugins.document.business.DocumentType;
43  import fr.paris.lutece.plugins.document.business.DocumentTypeHome;
44  import fr.paris.lutece.plugins.document.business.attributes.AttributeTypeParameter;
45  import fr.paris.lutece.plugins.document.business.attributes.DocumentAttribute;
46  import fr.paris.lutece.plugins.document.business.attributes.DocumentAttributeHome;
47  import fr.paris.lutece.plugins.document.modules.rest.util.constants.DocumentRestConstants;
48  import fr.paris.lutece.portal.service.util.AppPropertiesService;
49  import fr.paris.lutece.util.xml.XmlUtil;
50  
51  /**
52   * 
53   * FieldsToCreateDocumentBuilderXml
54   *
55   */
56  public class FieldsToCreateDocumentBuilderXml
57  {
58  
59      /**
60       * Constructor
61       */
62      FieldsToCreateDocumentBuilderXml( )
63      {
64      }
65  
66      /**
67       * Build the xml
68       * 
69       * @param strCodeDocumentType
70       *            code type of document
71       * @return the xml response
72       */
73      public static String buildXml( String strCodeDocumentType )
74      {
75          StringBuffer sbXml = new StringBuffer( AppPropertiesService.getProperty( DocumentRestConstants.PROPERTIES_XML_HEADER ) );
76          XmlUtil.addElement( sbXml, DocumentRestConstants.TAG_STATUS, DocumentRestConstants.STATUS_SUCCESS );
77          XmlUtil.beginElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_FORM );
78  
79          DocumentType documentType = DocumentTypeHome.findByPrimaryKey( strCodeDocumentType );
80  
81          XmlUtil.beginElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_FORM_MAINFIELDS );
82          mainFieldsBuildXml( sbXml );
83          XmlUtil.endElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_FORM_MAINFIELDS );
84  
85          if ( documentType.getMetadataHandler( ).equals( "dublincore" ) )
86          {
87              XmlUtil.beginElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_FORM_DUBLINCORE_FIELDS );
88              dublinCodeMetaDatasFieldsBuildXml( sbXml );
89              XmlUtil.endElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_FORM_DUBLINCORE_FIELDS );
90          }
91  
92          if ( ( documentType.getAttributes( ) != null ) && !documentType.getAttributes( ).isEmpty( ) )
93          {
94              XmlUtil.beginElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_FORM_ATTRIBUTS_FIELDS );
95              documentAttributsFieldsBuildXml( documentType.getAttributes( ), sbXml );
96              XmlUtil.endElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_FORM_ATTRIBUTS_FIELDS );
97          }
98  
99          XmlUtil.endElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_FORM );
100 
101         return sbXml.toString( );
102     }
103 
104     /**
105      * Build the xml of main fiels
106      * 
107      * @param sbXml
108      *            xml
109      */
110     private static void mainFieldsBuildXml( StringBuffer sbXml )
111     {
112         Map<String, String> mapAttributsList = new HashMap<String, String>( );
113         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_NAME, DocumentRestConstants.PARAMETER_DOCUMENT_TITLE );
114         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_TYPE, "text" );
115         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_MAXLENGTH, "255" );
116         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_SITE, "72" );
117         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_ISREQUIRED, "1" );
118         XmlUtil.addEmptyElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_TEXT, mapAttributsList );
119 
120         mapAttributsList = new HashMap<String, String>( );
121         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_NAME, DocumentRestConstants.PARAMETER_DOCUMENT_SUMMARY );
122         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_TYPE, "text" );
123         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_ROWS, "2" );
124         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_COLS, "60" );
125         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_ISREQUIRED, "1" );
126         XmlUtil.addEmptyElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_TEXTAREA, mapAttributsList );
127 
128         mapAttributsList = new HashMap<String, String>( );
129         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_NAME, DocumentRestConstants.PARAMETER_DOCUMENT_COMMENT );
130         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_TYPE, "text" );
131         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_ROWS, "2" );
132         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_COLS, "60" );
133         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_ISREQUIRED, "0" );
134         XmlUtil.addEmptyElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_TEXTAREA, mapAttributsList );
135 
136         mapAttributsList = new HashMap<String, String>( );
137         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_NAME, DocumentRestConstants.PARAMETER_PAGE_TEMPLATE_DOCUMENT_ID );
138         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_TYPE, "radio" );
139         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_ROWS, "2" );
140         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_COLS, "60" );
141         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_ISREQUIRED, "0" );
142         XmlUtil.beginElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_RADIO, mapAttributsList );
143         XmlUtil.addElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_RADIO_VALUE, "0" );
144         XmlUtil.addElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_RADIO_VALUE, "1" );
145         XmlUtil.addElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_RADIO_VALUE, "2" );
146 
147         mapAttributsList = new HashMap<String, String>( );
148         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_NAME, DocumentRestConstants.PARAMETER_VALIDITY_BEGIN );
149         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_TYPE, "text" );
150         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_SITE, "10" );
151         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_ISREQUIRED, "0" );
152         XmlUtil.addEmptyElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_TEXT, mapAttributsList );
153 
154         mapAttributsList = new HashMap<String, String>( );
155         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_NAME, DocumentRestConstants.PARAMETER_VALIDITY_END );
156         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_TYPE, "text" );
157         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_SITE, "10" );
158         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_ISREQUIRED, "0" );
159         XmlUtil.addEmptyElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_TEXT, mapAttributsList );
160 
161         mapAttributsList = new HashMap<String, String>( );
162         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_NAME, DocumentRestConstants.PARAMETER_ACCEPT_SITE_COMMENTS );
163         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_TYPE, "checkbox" );
164         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_VALUE, "1" );
165         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_ISREQUIRED, "0" );
166         XmlUtil.addEmptyElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_CHECKBOX, mapAttributsList );
167 
168         mapAttributsList = new HashMap<String, String>( );
169         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_NAME, DocumentRestConstants.PARAMETER_IS_MODERATED_COMMENT );
170         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_TYPE, "checkbox" );
171         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_VALUE, "1" );
172         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_ISREQUIRED, "0" );
173         XmlUtil.addEmptyElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_CHECKBOX, mapAttributsList );
174     }
175 
176     /**
177      * Build the xml of dublin code meta datas
178      * 
179      * @param sbXml
180      *            xml
181      */
182     private static void dublinCodeMetaDatasFieldsBuildXml( StringBuffer sbXml )
183     {
184         XmlUtil.addElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_TEXT, "",
185                 getMapAttributDublinCodeMetaDatasFields( DocumentRestConstants.PARAMETER_DUBLIN_CORE_META_DATA_TITLE ) );
186         XmlUtil.addElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_TEXT, "Mairie de Paris",
187                 getMapAttributDublinCodeMetaDatasFields( DocumentRestConstants.PARAMETER_DUBLIN_CORE_META_DATA_CREATOR ) );
188         XmlUtil.addElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_TEXT, "lutece;portal;xml;java",
189                 getMapAttributDublinCodeMetaDatasFields( DocumentRestConstants.PARAMETER_DUBLIN_CORE_META_DATA_SUBJECT ) );
190         XmlUtil.addElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_TEXT, "",
191                 getMapAttributDublinCodeMetaDatasFields( DocumentRestConstants.PARAMETER_DUBLIN_CORE_META_DATA_DESCRIPTION ) );
192         XmlUtil.addElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_TEXT, "Mairie de Paris",
193                 getMapAttributDublinCodeMetaDatasFields( DocumentRestConstants.PARAMETER_DUBLIN_CORE_META_DATA_PUBLISHER ) );
194         XmlUtil.addElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_TEXT, "",
195                 getMapAttributDublinCodeMetaDatasFields( DocumentRestConstants.PARAMETER_DUBLIN_CORE_META_DATA_CONTRIBUTOR ) );
196         XmlUtil.addElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_TEXT, "",
197                 getMapAttributDublinCodeMetaDatasFields( DocumentRestConstants.PARAMETER_DUBLIN_CORE_META_DATA_DATE ) );
198         XmlUtil.addElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_TEXT, "",
199                 getMapAttributDublinCodeMetaDatasFields( DocumentRestConstants.PARAMETER_DUBLIN_CORE_META_DATA_TYPE ) );
200         XmlUtil.addElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_TEXT, "",
201                 getMapAttributDublinCodeMetaDatasFields( DocumentRestConstants.PARAMETER_DUBLIN_CORE_META_DATA_FORMAT ) );
202         XmlUtil.addElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_TEXT, "",
203                 getMapAttributDublinCodeMetaDatasFields( DocumentRestConstants.PARAMETER_DUBLIN_CORE_META_DATA_IDENTIFIER ) );
204         XmlUtil.addElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_TEXT, "",
205                 getMapAttributDublinCodeMetaDatasFields( DocumentRestConstants.PARAMETER_DUBLIN_CORE_META_DATA_SOURCE ) );
206         XmlUtil.addElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_TEXT, "fr",
207                 getMapAttributDublinCodeMetaDatasFields( DocumentRestConstants.PARAMETER_DUBLIN_CORE_META_DATA_LANGUAGE ) );
208         XmlUtil.addElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_TEXT, "",
209                 getMapAttributDublinCodeMetaDatasFields( DocumentRestConstants.PARAMETER_DUBLIN_CORE_META_DATA_RELATION ) );
210         XmlUtil.addElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_TEXT, "",
211                 getMapAttributDublinCodeMetaDatasFields( DocumentRestConstants.PARAMETER_DUBLIN_CORE_META_DATA_COVERAGE ) );
212         XmlUtil.addElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_TEXT, "Copyrights (c) Mairie de Paris",
213                 getMapAttributDublinCodeMetaDatasFields( DocumentRestConstants.PARAMETER_DUBLIN_CORE_META_DATA_RIGHTS ) );
214     }
215 
216     /**
217      * Add attribut for dublin code meta datas
218      * 
219      * @param strName
220      *            name of parameters
221      * @return mapAttributsList map of attributs
222      */
223     private static Map<String, String> getMapAttributDublinCodeMetaDatasFields( String strName )
224     {
225         Map<String, String> mapAttributsList = new HashMap<String, String>( );
226         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_TYPE, "text" );
227         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_SITE, "50" );
228         mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_NAME, strName );
229 
230         return mapAttributsList;
231     }
232 
233     /**
234      * Build the xml of document attributs fields
235      * 
236      * @param listDocumentAttribute
237      *            list of attributs
238      * @param sbXml
239      *            xml
240      */
241     private static void documentAttributsFieldsBuildXml( List<DocumentAttribute> listDocumentAttribute, StringBuffer sbXml )
242     {
243         for ( DocumentAttribute documentAttribute : listDocumentAttribute )
244         {
245             Map<String, String> mapAttributsList = new HashMap<String, String>( );
246             mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_NAME, documentAttribute.getName( ) );
247             mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_TYPE, documentAttribute.getCodeAttributeType( ) );
248             mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_CODE, documentAttribute.getCode( ) );
249             if ( documentAttribute.isRequired( ) )
250             {
251                 mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_ISREQUIRED, "1" );
252             }
253             else
254             {
255                 mapAttributsList.put( DocumentRestConstants.ATTRIBUTS_ISREQUIRED, "0" );
256             }
257             XmlUtil.beginElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_FORM_ATTRIBUT, mapAttributsList );
258 
259             List<AttributeTypeParameter> listAttributeTypeParameter = (List<AttributeTypeParameter>) DocumentAttributeHome.getAttributeParametersValues(
260                     documentAttribute.getId( ), LocaleBeanUtils.getDefaultLocale( ) );
261 
262             if ( ( listAttributeTypeParameter != null ) && !listAttributeTypeParameter.isEmpty( ) )
263             {
264                 for ( AttributeTypeParameter attributeTypeParameter : listAttributeTypeParameter )
265                 {
266                     Map<String, String> mapParametersAttributsList = new HashMap<String, String>( );
267                     mapParametersAttributsList.put( DocumentRestConstants.ATTRIBUTS_NAME, attributeTypeParameter.getName( ) );
268                     XmlUtil.beginElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_FORM_PARAMETER_ATTRIBUT, mapParametersAttributsList );
269 
270                     if ( ( attributeTypeParameter.getValueList( ) != null ) && !attributeTypeParameter.getValueList( ).isEmpty( ) )
271                     {
272                         for ( String strValueParameter : attributeTypeParameter.getValueList( ) )
273                         {
274                             XmlUtil.addElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_FORM_PARAMETER_ATTRIBUT_VALUE, strValueParameter );
275                         }
276                     }
277 
278                     XmlUtil.endElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_FORM_PARAMETER_ATTRIBUT );
279                 }
280             }
281 
282             XmlUtil.endElement( sbXml, DocumentRestConstants.TAG_CREATE_DOCUMENT_FIELDS_FORM_ATTRIBUT );
283         }
284     }
285 }