View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.genericattributes.business;
35  
36  import fr.paris.lutece.plugins.genericattributes.service.entrytype.IEntryTypeService;
37  import fr.paris.lutece.portal.service.util.AppLogService;
38  
39  import java.io.Serializable;
40  
41  import java.util.List;
42  
43  /**
44   *
45   * class Entry
46   *
47   */
48  public class Entry implements Serializable, Cloneable
49  {
50      private static final long serialVersionUID = 7623715927165156626L;
51  
52      // Other constants
53      private int _nIdEntry;
54      private String _strTitle;
55      private String _strCode;
56      private String _strHelpMessage;
57      private String _strComment;
58      private boolean _bMandatory;
59      private boolean _bFieldInLine;
60      private int _nPosition;
61      private int _nIdResource;
62      private String _strResourceType;
63      private EntryType _entryType;
64      private Entry _entryParent;
65      private List<Entry> _listEntryChildren;
66      private List<Field> _listFields;
67      private Field _fieldDepend;
68      private int _nNumberConditionalQuestion;
69      private boolean _bUnique;
70      private GenericAttributeError _error;
71      private String _strCSSClass;
72      private String _strErrorMessage;
73      private boolean _bOnlyDisplayInBack;
74      private boolean _bIndexed;
75  
76      /**
77       * Get the list of children of this entry
78       * 
79       * @return the list of entry who are insert in the group
80       */
81      public List<Entry> getChildren( )
82      {
83          return _listEntryChildren;
84      }
85  
86      /**
87       * Get the comment of this entry
88       * 
89       * @return the entry comment
90       */
91      public String getComment( )
92      {
93          return _strComment;
94      }
95  
96      /**
97       * Get the type of the entry
98       * 
99       * @return the type of the entry
100      */
101     public EntryType getEntryType( )
102     {
103         return _entryType;
104     }
105 
106     /**
107      * Get the list of fields of this entry
108      * 
109      * @return the list of field who are associate to the entry
110      */
111     public List<Field> getFields( )
112     {
113         return _listFields;
114     }
115 
116     /**
117      * Get the help message of this entry
118      * 
119      * @return The help message of this entry
120      */
121     public String getHelpMessage( )
122     {
123         return _strHelpMessage;
124     }
125 
126     /**
127      * Get the id of this entry
128      * 
129      * @return the id of entry
130      */
131     public int getIdEntry( )
132     {
133         return _nIdEntry;
134     }
135 
136     /**
137      * @return parent entry if the entry is insert in a group
138      */
139     public Entry getParent( )
140     {
141         return _entryParent;
142     }
143 
144     /**
145      * Get the position of the entry
146      * 
147      * @return position entry
148      */
149     public int getPosition( )
150     {
151         return _nPosition;
152     }
153 
154     /**
155      * Get the title of this entry
156      * 
157      * @return The title of this entry
158      */
159     public String getTitle( )
160     {
161         return _strTitle;
162     }
163 
164     /**
165      * Check if generated fields must be displayed in line
166      * 
167      * @return true if the field associate must be display in line
168      */
169     public boolean isFieldInLine( )
170     {
171         return _bFieldInLine;
172     }
173 
174     /**
175      * Check if this entry is mandatory or not
176      * 
177      * @return true if the question is mandatory
178      */
179     public boolean isMandatory( )
180     {
181         return _bMandatory;
182     }
183 
184     /**
185      * Set the list of entry who are insert in the group
186      * 
187      * @param children
188      *            the list of entry
189      */
190     public void setChildren( List<Entry> children )
191     {
192         _listEntryChildren = children;
193     }
194 
195     /**
196      * Set entry comment
197      * 
198      * @param strComment
199      *            entry comment
200      */
201     public void setComment( String strComment )
202     {
203         _strComment = strComment;
204     }
205 
206     /**
207      * Set the type of the entry
208      * 
209      * @param entryType
210      *            the type of the entry
211      */
212     public void setEntryType( EntryType entryType )
213     {
214         _entryType = entryType;
215     }
216 
217     /**
218      * Set true if the field associate must be display in line
219      * 
220      * @param bFieldInLine
221      *            true if the field associate must be display in line
222      */
223     public void setFieldInLine( boolean bFieldInLine )
224     {
225         _bFieldInLine = bFieldInLine;
226     }
227 
228     /**
229      * Set the list of field who are associate to the entry
230      * 
231      * @param fields
232      *            the list of field
233      */
234     public void setFields( List<Field> fields )
235     {
236         _listFields = fields;
237     }
238 
239     /**
240      * Set the entry help message
241      * 
242      * @param strHelpMessage
243      *            the entry help message
244      */
245     public void setHelpMessage( String strHelpMessage )
246     {
247         _strHelpMessage = strHelpMessage;
248     }
249 
250     /**
251      * Set the id of the entry
252      * 
253      * @param nIdEntry
254      *            the id of the entry
255      */
256     public void setIdEntry( int nIdEntry )
257     {
258         _nIdEntry = nIdEntry;
259     }
260 
261     /**
262      * Set true if the question is mandatory
263      * 
264      * @param bMandatory
265      *            true if the question is mandatory
266      */
267     public void setMandatory( boolean bMandatory )
268     {
269         _bMandatory = bMandatory;
270     }
271 
272     /**
273      * Set parent entry if the entry is insert in a group
274      * 
275      * @param parent
276      *            parent entry
277      */
278     public void setParent( Entry parent )
279     {
280         _entryParent = parent;
281     }
282 
283     /**
284      * Set position entry
285      * 
286      * @param position
287      *            position entry
288      */
289     public void setPosition( int position )
290     {
291         _nPosition = position;
292     }
293 
294     /**
295      * Set title entry
296      * 
297      * @param strTitle
298      *            title
299      */
300     public void setTitle( String strTitle )
301     {
302         _strTitle = strTitle;
303     }
304 
305     /**
306      * Get the id of the resource associated with this entry
307      * 
308      * @return The id of the resource associated with this entry
309      */
310     public int getIdResource( )
311     {
312         return _nIdResource;
313     }
314 
315     /**
316      * Set the id of the resource associated with this entry
317      * 
318      * @param nIdResource
319      *            The id of the resource associated with this entry
320      */
321     public void setIdResource( int nIdResource )
322     {
323         this._nIdResource = nIdResource;
324     }
325 
326     /**
327      * Get the type of the resource associated with this entry
328      * 
329      * @return The type of the resource associated with this entry
330      */
331     public String getResourceType( )
332     {
333         return _strResourceType;
334     }
335 
336     /**
337      * Set the type of the resource associated with this entry
338      * 
339      * @param strResourceType
340      *            The type of the resource associated with this entry
341      */
342     public void setResourceType( String strResourceType )
343     {
344         this._strResourceType = strResourceType;
345     }
346 
347     /**
348      * Get the field depend of this entry
349      * 
350      * @return the field if the entry is a conditional question
351      */
352     public Field getFieldDepend( )
353     {
354         return _fieldDepend;
355     }
356 
357     /**
358      * Set the field if the entry is a conditional question
359      * 
360      * @param depend
361      *            depend the field if the entry is a conditional question
362      */
363     public void setFieldDepend( Field depend )
364     {
365         _fieldDepend = depend;
366     }
367 
368     /**
369      * Get the number of conditional questions associated with the entry
370      * 
371      * @return the number of conditional questions associated with the entry
372      */
373     public int getNumberConditionalQuestion( )
374     {
375         return _nNumberConditionalQuestion;
376     }
377 
378     /**
379      * Set the number of conditional questions who are associated with the entry
380      * 
381      * @param numberConditionalQuestion
382      *            the number of conditional questions which are associated with the entry
383      *
384      */
385     public void setNumberConditionalQuestion( int numberConditionalQuestion )
386     {
387         _nNumberConditionalQuestion = numberConditionalQuestion;
388     }
389 
390     /**
391      * Set to true if the value of the response to this question must be unique
392      * 
393      * @param bUnique
394      *            true if the value of the response to this question must be unique, false otherwise
395      */
396     public void setUnique( boolean bUnique )
397     {
398         this._bUnique = bUnique;
399     }
400 
401     /**
402      * Check if the value of the response must be unique
403      * 
404      * @return true if the value of the response to this question must be unique
405      */
406     public boolean isUnique( )
407     {
408         return _bUnique;
409     }
410 
411     /**
412      * Get the error associated to the entry
413      * 
414      * @return the error
415      */
416     public GenericAttributeError getError( )
417     {
418         return _error;
419     }
420 
421     /**
422      * Set the error associated to the entry
423      * 
424      * @param error
425      *            the error
426      */
427     public void setError( GenericAttributeError error )
428     {
429         _error = error;
430     }
431 
432     /**
433      * Set the CSS class of the generated fields
434      * 
435      * @param strCSSClass
436      *            The CSS class to set
437      */
438     public void setCSSClass( String strCSSClass )
439     {
440         this._strCSSClass = strCSSClass;
441     }
442 
443     /**
444      * Get the CSS class of the generated fields
445      * 
446      * @return The CSS class
447      */
448     public String getCSSClass( )
449     {
450         return _strCSSClass;
451     }
452 
453     /**
454      * Get the error message associated with this entry. This error message should be used by the right entry type. For example, EntryTypeCheckBox use it as a
455      * message to indicates that this field is mandatory.
456      * 
457      * @return The error message of this entry
458      */
459     public String getErrorMessage( )
460     {
461         return _strErrorMessage;
462     }
463 
464     /**
465      * Set the error message associated with this entry. This error message should be used by the right entry type. For example, EntryTypeCheckBox use it as a
466      * message to indicates that this field is mandatory.
467      * 
468      * @param strErrorMessage
469      *            The error message of this entry
470      */
471     public void setErrorMessage( String strErrorMessage )
472     {
473         this._strErrorMessage = strErrorMessage;
474     }
475 
476     /**
477      * @return the _strCode
478      */
479     public String getCode( )
480     {
481         return _strCode;
482     }
483 
484     /**
485      * @param strCode
486      *            the code to set
487      */
488     public void setCode( String strCode )
489     {
490         this._strCode = strCode;
491     }
492 
493     /**
494      * Creates and returns a copy of this object.
495      * 
496      * @return a clone of this instance.
497      * @see java.lang.Object#clone()
498      */
499     @Override
500     public Object clone( )
501     {
502         try
503         {
504             return super.clone( );
505         }
506         catch( CloneNotSupportedException e )
507         {
508             AppLogService.error( e.getMessage( ), e );
509 
510             return new Entry( );
511         }
512     }
513 
514     public boolean isOnlyDisplayInBack( )
515     {
516         return _bOnlyDisplayInBack;
517     }
518 
519     public void setOnlyDisplayInBack( boolean onlyDisplayInBack )
520     {
521         this._bOnlyDisplayInBack = onlyDisplayInBack;
522     }
523 
524     public boolean isIndexed( )
525     {
526         return _bIndexed;
527     }
528 
529     public void setIndexed( boolean bIndexed )
530     {
531         _bIndexed = bIndexed;
532     }
533 
534     /**
535      * @return true if the field exportable is present and set to true
536      */
537     public boolean isExportable( )
538     {
539         Field fieldExportable = getFieldByCode( IEntryTypeService.FIELD_EXPORTABLE );
540         return fieldExportable != null && Boolean.valueOf( fieldExportable.getValue( ) );
541     }
542 
543     /**
544      * @return true if the field exportable_pdf is present and set to true
545      */
546     public boolean isExportablePdf( )
547     {
548         Field fieldExportablePdf = getFieldByCode( IEntryTypeService.FIELD_EXPORTABLE_PDF );
549         return fieldExportablePdf != null && Boolean.valueOf( fieldExportablePdf.getValue( ) );
550     }
551 
552     /**
553      * @return true if the field published is present and set to true
554      */
555     public boolean isPublished( )
556     {
557         Field fieldPublished = getFieldByCode( IEntryTypeService.FIELD_PUBLISHED );
558         return fieldPublished != null && Boolean.valueOf( fieldPublished.getValue( ) );
559     }
560 
561     /**
562      * @return true if the field disabled is present and set to true
563      */
564     public boolean isDisabled( )
565     {
566         Field fieldDisabled = getFieldByCode( IEntryTypeService.FIELD_DISABLED );
567         return fieldDisabled != null && Boolean.valueOf( fieldDisabled.getValue( ) );
568     }
569 
570     /**
571      * Get the selected map provider
572      * 
573      * @see IMapProvider
574      * @return the select map provider
575      */
576     public IMapProvider getMapProvider( )
577     {
578         Field fieldProvider = getFieldByCode( IEntryTypeService.FIELD_PROVIDER );
579         if ( fieldProvider == null )
580         {
581             return null;
582         }
583         return MapProviderManager.getMapProvider( fieldProvider.getValue( ) );
584     }
585 
586     /**
587      * Get the field by its code.
588      * 
589      * @param strCode
590      * @return
591      */
592     public Field getFieldByCode( String strCode )
593     {
594         if ( _listFields == null || _listFields.isEmpty( ) )
595         {
596             return null;
597         }
598 
599         return _listFields.stream( ).filter( field -> field.getCode( ).equals( strCode ) ).findFirst( ).orElse( null );
600     }
601 }