View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.business;
35  
36  import fr.paris.lutece.plugins.document.business.attributes.DocumentAttribute;
37  import fr.paris.lutece.plugins.document.service.metadata.MetadataHandler;
38  import fr.paris.lutece.plugins.document.service.metadata.MetadataService;
39  import fr.paris.lutece.portal.service.rbac.RBACResource;
40  import fr.paris.lutece.portal.service.spring.SpringContextService;
41  import fr.paris.lutece.portal.service.template.AppTemplateService;
42  import fr.paris.lutece.util.UniqueIDGenerator;
43  import fr.paris.lutece.util.html.HtmlTemplate;
44  
45  import java.io.ByteArrayInputStream;
46  import java.io.StringReader;
47  
48  import java.util.ArrayList;
49  import java.util.HashMap;
50  import java.util.List;
51  import java.util.Locale;
52  import java.util.Map;
53  
54  import javax.xml.transform.Source;
55  import javax.xml.transform.stream.StreamSource;
56  
57  
58  /**
59   * This class represents the business object DocumentType
60   */
61  public class DocumentType implements RBACResource
62  {
63      public static final String RESOURCE_TYPE = "DOCUMENT_TYPE";
64      private static final String TEMPLATE_ADMIN_DEFAULT_XSL = "/admin/plugins/document/document_admin_default_xsl.html";
65      private static final String TEMPLATE_CONTENT_SERVICE_DEFAULT_XSL = "/skin/plugins/document/document_content_service_default_xsl.html";
66      private static final String MARK_DOCUMENT_TYPE = "document_type";
67      private static final int MODE_ADMIN = 1;
68  
69      //Style prefix Id
70      private static final String STYLE_PREFIX_ID = UniqueIDGenerator.getNewId(  ) + "document-";
71      private static final String STYLE_PREFIX_DEFAULT = UniqueIDGenerator.getNewId(  ) + "default-";
72      private static final String STYLE_PREFIX_CONTENT_SERVICE = STYLE_PREFIX_ID + "content-";
73      private static final String STYLE_PREFIX_ADMIN_SERVICE = STYLE_PREFIX_ID + "admin-";
74      private static final String STYLE_PREFIX_DEFAULT_CONTENT_SERVICE = STYLE_PREFIX_CONTENT_SERVICE +
75          STYLE_PREFIX_DEFAULT;
76      private static final String STYLE_PREFIX_DEFAULT_ADMIN_SERVICE = STYLE_PREFIX_ADMIN_SERVICE + STYLE_PREFIX_DEFAULT;
77  
78      // Variables declarations
79      private String _strCode;
80      private String _strOldCode;
81      private String _strName;
82      private String _strDescription;
83      private byte[] _baAdminXsl;
84      private byte[] _baContentServiceXsl;
85      private ArrayList<DocumentAttribute> _listAttributes = new ArrayList<DocumentAttribute>(  );
86      private int _nThumbnailAttributeId;
87      private String _strDefaultThumbnailUrl;
88      private String _strMetadataHandler;
89  
90      /**
91       * Returns the Code
92       *
93       * @return The Code
94       */
95      public String getCode(  )
96      {
97          return _strCode;
98      }
99  
100     /**
101      * Sets the Code
102      *
103      * @param strCode The Code
104      */
105     public void setCode( String strCode )
106     {
107         _strCode = strCode;
108     }
109 
110     /**
111      * Returns the old code
112      *
113      * @return The old code
114      */
115     public String getOldCode(  )
116     {
117         return _strOldCode;
118     }
119 
120     /**
121      * Sets the old code
122      *
123      * @param strCode The old code
124      */
125     public void setOldCode( String strOldCode )
126     {
127         _strOldCode = strOldCode;
128     }
129 
130     /**
131      * Returns the Name
132      *
133      * @return The Name
134      */
135     public String getName(  )
136     {
137         return _strName;
138     }
139 
140     /**
141      * Sets the Name
142      *
143      * @param strName The Name
144      */
145     public void setName( String strName )
146     {
147         _strName = strName;
148     }
149 
150     /**
151      * Returns the Description
152      *
153      * @return The Description
154      */
155     public String getDescription(  )
156     {
157         return _strDescription;
158     }
159 
160     /**
161      * Sets the Description
162      *
163      * @param strDescription The Description
164      */
165     public void setDescription( String strDescription )
166     {
167         _strDescription = strDescription;
168     }
169 
170     /**
171      * Returns the Xsl for the Administration module
172      *
173      * @return The Xsl
174      */
175     public byte[] getAdminXsl(  )
176     {
177         return _baAdminXsl;
178     }
179 
180     /**
181      * Sets the Xsl for the Administration module
182      *
183      * @param baXsl The Xsl
184      */
185     public void setAdminXsl( byte[] baXsl )
186     {
187         _baAdminXsl = baXsl;
188     }
189 
190     /**
191      * Returns the Xsl for the Document ContentService
192      *
193      * @return The Xsl
194      */
195     public byte[] getContentServiceXsl(  )
196     {
197         return _baContentServiceXsl;
198     }
199 
200     /**
201      * Sets the Xsl for the Document ContentService
202      *
203      * @param baXsl The Xsl
204      */
205     public void setContentServiceXsl( byte[] baXsl )
206     {
207         _baContentServiceXsl = baXsl;
208     }
209 
210     /**
211      * Add a document attribute to this type of document
212      * @param documentAttribute The documentAttribute to add
213      */
214     public void addAttribute( DocumentAttribute documentAttribute )
215     {
216         _listAttributes.add( documentAttribute );
217     }
218 
219     /**
220      * Gets attributes list for the document type
221      * @return The attrubutes list
222      */
223     public List<DocumentAttribute> getAttributes(  )
224     {
225         return _listAttributes;
226     }
227 
228     /**
229      * Returns the ThumbnailAttributeId
230      *
231      * @return The ThumbnailAttributeId
232      */
233     public int getThumbnailAttributeId(  )
234     {
235         return _nThumbnailAttributeId;
236     }
237 
238     /**
239      * Sets the ThumbnailAttributeId
240      *
241      * @param nThumbnailAttributeId The ThumbnailAttributeId
242      */
243     public void setThumbnailAttributeId( int nThumbnailAttributeId )
244     {
245         _nThumbnailAttributeId = nThumbnailAttributeId;
246     }
247 
248     /**
249      * Returns the DefaultThumbnailUrl
250      *
251      * @return The DefaultThumbnailUrl
252      */
253     public String getDefaultThumbnailUrl(  )
254     {
255         return ( _strDefaultThumbnailUrl != null ) ? _strDefaultThumbnailUrl : "";
256     }
257 
258     /**
259      * Sets the DefaultThumbnailUrl
260      *
261      * @param strDefaultThumbnailUrl The DefaultThumbnailUrl
262      */
263     public void setDefaultThumbnailUrl( String strDefaultThumbnailUrl )
264     {
265         _strDefaultThumbnailUrl = strDefaultThumbnailUrl;
266     }
267 
268     /**
269      * RBAC resource implementation
270      * @return The resource type code
271      */
272     public String getResourceTypeCode(  )
273     {
274         return RESOURCE_TYPE;
275     }
276 
277     /**
278      * RBAC resource implementation
279      * @return The resourceId
280      */
281     public String getResourceId(  )
282     {
283         return getCode(  );
284     }
285 
286     /**
287      * Returns a default XSL stylesheet to display the document into the
288      * backoffice
289      * @return An XSL stylesheet as a String
290      */
291     private String getAdminDefaultXsl(  )
292     {
293         Map<String, Object> model = new HashMap<String, Object>(  );
294         model.put( MARK_DOCUMENT_TYPE, this );
295 
296         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ADMIN_DEFAULT_XSL, Locale.getDefault(  ), model );
297 
298         return template.getHtml(  );
299     }
300 
301     /**
302      * Returns a default XSL stylesheet to display the document into the
303      * frontoffice
304      * @return An XSL stylesheet as a String
305      */
306     private String getContentServiceDefaultXsl(  )
307     {
308         Map<String, Object> model = new HashMap<String, Object>(  );
309         model.put( MARK_DOCUMENT_TYPE, this );
310 
311         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CONTENT_SERVICE_DEFAULT_XSL,
312                 Locale.getDefault(  ), model );
313 
314         return template.getHtml(  );
315     }
316 
317     /**
318      * Return the admin xsl source : if the admin xsl is null, a default one is
319      * generated and returned
320      * @return the xsl stylesheet as a source
321      */
322     public Source getAdminXslSource(  )
323     {
324         Source xslSource;
325 
326         if ( getAdminXsl(  ) != null )
327         {
328             xslSource = new StreamSource( new ByteArrayInputStream( getAdminXsl(  ) ) );
329         }
330         else
331         {
332             StringReader xslStringReader = new StringReader( getAdminDefaultXsl(  ) );
333             xslSource = new StreamSource( xslStringReader );
334         }
335 
336         return xslSource;
337     }
338 
339     /**
340      * Return the xsl source to display the document into the frontoffice :
341      * if the admin xsl is null, a default one is generated and returned.
342      * @return the xsl stylesheet as a source
343      */
344     public Source getContentServiceXslSource(  )
345     {
346         Source xslSource;
347 
348         if ( ( getContentServiceXsl(  ) != null ) && ( getContentServiceXsl(  ).length > 0 ) )
349         {
350             xslSource = new StreamSource( new ByteArrayInputStream( getContentServiceXsl(  ) ) );
351         }
352         else
353         {
354             StringReader xslStringReader = new StringReader( getContentServiceDefaultXsl(  ) );
355             xslSource = new StreamSource( xslStringReader );
356         }
357 
358         return xslSource;
359     }
360 
361     /**
362      * Return the StyleSheet unique Id
363      * @param nMode The current mode.
364      * @return the StyleSheet unique Id
365      */
366     public String getStyleSheetId( int nMode )
367     {
368         String strResult = null;
369 
370         if ( nMode == MODE_ADMIN )
371         {
372             strResult = this.getAdminStyleSheetId(  );
373         }
374         else
375         {
376             strResult = this.getContentServiceStyleSheetId(  );
377         }
378 
379         return strResult;
380     }
381 
382     /**
383      * Return the content service StyleSheet unique Id
384      * @return The id
385      */
386     public String getContentServiceStyleSheetId(  )
387     {
388         String strStyleSheetId;
389 
390         if ( ( getContentServiceXsl(  ) != null ) && ( getContentServiceXsl(  ).length > 0 ) )
391         {
392             strStyleSheetId = STYLE_PREFIX_CONTENT_SERVICE + this.getResourceId(  );
393         }
394         else
395         {
396             strStyleSheetId = STYLE_PREFIX_DEFAULT_CONTENT_SERVICE;
397         }
398 
399         return strStyleSheetId;
400     }
401 
402     /**
403      * Return the admin StyleSheet unique Id
404      * @return The id
405      */
406     public String getAdminStyleSheetId(  )
407     {
408         String strStyleSheetId;
409 
410         if ( ( getAdminXsl(  ) != null ) && ( getAdminXsl(  ).length > 0 ) )
411         {
412             strStyleSheetId = STYLE_PREFIX_ADMIN_SERVICE + this.getResourceId(  );
413         }
414         else
415         {
416             strStyleSheetId = STYLE_PREFIX_DEFAULT_ADMIN_SERVICE;
417         }
418 
419         return strStyleSheetId;
420     }
421 
422     /**
423      * Returns the MetadataHandler name
424      *
425      * @return The MetadataHandler
426      */
427     public String getMetadataHandler(  )
428     {
429         return _strMetadataHandler;
430     }
431 
432     /**
433      * Sets the MetadataHandler name
434      *
435      * @param strMetadataHandler The MetadataHandler
436      */
437     public void setMetadataHandler( String strMetadataHandler )
438     {
439         _strMetadataHandler = strMetadataHandler;
440     }
441 
442     /**
443      * Returns the metahandler
444      * @return the metahandler
445      */
446     public MetadataHandler metadataHandler(  )
447     {
448         MetadataHandler handler = null;
449 
450         if ( ( _strMetadataHandler != null ) && ( !_strMetadataHandler.equals( "" ) ) &&
451                 ( !_strMetadataHandler.equals( MetadataService.NO_HANDLER ) ) )
452         {
453             String strBeanName = MetadataService.getBeanName( _strMetadataHandler );
454             handler = SpringContextService.getBean( strBeanName );
455         }
456 
457         return handler;
458     }
459 }