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