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.portal.business.user.attribute;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.portal.service.template.AppTemplateService;
38  import fr.paris.lutece.util.html.HtmlTemplate;
39  
40  import java.util.ArrayList;
41  import java.util.HashMap;
42  import java.util.List;
43  import java.util.Locale;
44  import java.util.Map;
45  
46  /**
47   *
48   * Attribute
49   *
50   */
51  public abstract class AbstractAttribute implements IAttribute
52  {
53      // MARKS
54      private static final String MARK_ATTRIBUTE = "attribute";
55      private static final String MARK_DEFAULT_VALUES_LIST = "default_values_list";
56      private static final String MARK_USER_FIELD = "user_field";
57      private int _nIdAttribute;
58      private boolean _bMandatory;
59      private String _strTitle;
60      private String _strHelpMessage;
61      private int _nPosition;
62      private AttributeType _attributeType;
63      private List<AttributeField> _listAttributeFields;
64      private Plugin _plugin;
65      private boolean _bIsShownInSearch;
66      private boolean _bIsFieldInLine;
67      private boolean _bIsAttributeImage;
68      private boolean _bIsShownInResultList;
69      private boolean _bAnonymize;
70  
71      /**
72       * Constructor
73       */
74      public AbstractAttribute( )
75      {
76      }
77  
78      /**
79       * Get ID Attribute
80       * 
81       * @return ID attribute
82       */
83      @Override
84      public int getIdAttribute( )
85      {
86          return _nIdAttribute;
87      }
88  
89      /**
90       * Set ID Attribute
91       * 
92       * @param nIdAttribute
93       *            ID Attribute
94       */
95      @Override
96      public void setIdAttribute( int nIdAttribute )
97      {
98          _nIdAttribute = nIdAttribute;
99      }
100 
101     /**
102      * Get Mandatory
103      * 
104      * @return true if it's mandatory, false otherwise
105      */
106     @Override
107     public boolean isMandatory( )
108     {
109         return _bMandatory;
110     }
111 
112     /**
113      * Set mandatory
114      * 
115      * @param bMandatory
116      *            true if it's mandatory, false otherwise
117      */
118     @Override
119     public void setMandatory( boolean bMandatory )
120     {
121         _bMandatory = bMandatory;
122     }
123 
124     /**
125      * Get list fields
126      * 
127      * @return list fields
128      */
129     @Override
130     public List<AttributeField> getListAttributeFields( )
131     {
132         return _listAttributeFields;
133     }
134 
135     /**
136      * Set list fields
137      * 
138      * @param listAttributeFields
139      *            list fields
140      */
141     @Override
142     public void setListAttributeFields( List<AttributeField> listAttributeFields )
143     {
144         _listAttributeFields = listAttributeFields;
145     }
146 
147     /**
148      * Get title
149      * 
150      * @return title
151      */
152     @Override
153     public String getTitle( )
154     {
155         return _strTitle;
156     }
157 
158     /**
159      * Set title
160      * 
161      * @param strTitle
162      *            title
163      */
164     @Override
165     public void setTitle( String strTitle )
166     {
167         _strTitle = strTitle;
168     }
169 
170     /**
171      * Get help Message
172      * 
173      * @return help message
174      */
175     @Override
176     public String getHelpMessage( )
177     {
178         return _strHelpMessage;
179     }
180 
181     /**
182      * Set help message
183      * 
184      * @param strHelpMessage
185      *            help message
186      */
187     @Override
188     public void setHelpMessage( String strHelpMessage )
189     {
190         _strHelpMessage = strHelpMessage;
191     }
192 
193     /**
194      * Get position
195      * 
196      * @return position
197      */
198     @Override
199     public int getPosition( )
200     {
201         return _nPosition;
202     }
203 
204     /**
205      * Set position
206      * 
207      * @param nPosition
208      *            position
209      */
210     @Override
211     public void setPosition( int nPosition )
212     {
213         _nPosition = nPosition;
214     }
215 
216     /**
217      * Get attribute type
218      * 
219      * @return attribute type
220      */
221     @Override
222     public AttributeType getAttributeType( )
223     {
224         return _attributeType;
225     }
226 
227     /**
228      * Set attribute Type
229      * 
230      * @param attributeType
231      *            attribute type
232      */
233     @Override
234     public void setAttributeType( AttributeType attributeType )
235     {
236         _attributeType = attributeType;
237     }
238 
239     /**
240      * Get the anonymize status of the attribute
241      * 
242      * @return True if the attribute should be anonymize, false otherwise.
243      */
244     @Override
245     public boolean getAnonymize( )
246     {
247         return _bAnonymize;
248     }
249 
250     /**
251      * Set the anonymize status of the attribute
252      * 
253      * @param bAnonymize
254      *            New anonymize status. True if the attribute should be anonymize, false otherwise.
255      */
256     @Override
257     public void setAnonymize( boolean bAnonymize )
258     {
259         _bAnonymize = bAnonymize;
260     }
261 
262     /**
263      * Get Html form
264      * 
265      * @param locale
266      *            locale
267      * @return html form
268      */
269     public String getHtmlFormAttribute( Locale locale )
270     {
271         Map<String, Object> model = new HashMap<>( );
272         model.put( MARK_ATTRIBUTE, this );
273 
274         HtmlTemplate template = AppTemplateService.getTemplate( getTemplateHtmlFormAttribute( ), locale, model );
275 
276         return template.getHtml( );
277     }
278 
279     /**
280      * Get Html form
281      * 
282      * @param locale
283      *            locale
284      * @param listDefaultValues
285      *            the list of default values
286      * @return html form
287      */
288     public String getHtmlFormAttribute( Locale locale, Object listDefaultValues )
289     {
290         Map<String, Object> model = new HashMap<>( );
291         model.put( MARK_ATTRIBUTE, this );
292         model.put( MARK_DEFAULT_VALUES_LIST, listDefaultValues );
293 
294         HtmlTemplate template = AppTemplateService.getTemplate( getTemplateHtmlFormAttribute( ), locale, model );
295 
296         return template.getHtml( );
297     }
298 
299     /**
300      * Get Html form
301      * 
302      * @param auFieldFilter
303      *            The admin user field filter
304      * @param locale
305      *            locale
306      * @return html form
307      */
308     public String getHtmlFormSearchAttribute( AdminUserFieldFilter auFieldFilter, Locale locale )
309     {
310         Map<String, Object> model = new HashMap<>( );
311         List<AdminUserField> listUserFields = auFieldFilter.getListUserFields( );
312         List<AdminUserField> selectedUserFields = null;
313 
314         if ( ( listUserFields != null ) && ( !listUserFields.isEmpty( ) ) )
315         {
316             selectedUserFields = new ArrayList<>( );
317 
318             for ( AdminUserField userField : listUserFields )
319             {
320                 if ( userField.getAttribute( ).getIdAttribute( ) == _nIdAttribute )
321                 {
322                     selectedUserFields.add( userField );
323                 }
324             }
325         }
326 
327         model.put( MARK_DEFAULT_VALUES_LIST, selectedUserFields );
328         model.put( MARK_ATTRIBUTE, this );
329 
330         HtmlTemplate template = AppTemplateService.getTemplate( getTemplateHtmlFormSearchAttribute( ), locale, model );
331 
332         return template.getHtml( );
333     }
334 
335     /**
336      * Get Html value
337      * 
338      * @param locale
339      *            Locale
340      * @param userField
341      *            User field
342      * @return the html
343      */
344     public String getHtmlValue( Locale locale, AdminUserField userField )
345     {
346         Map<String, Object> model = new HashMap<>( );
347 
348         model.put( MARK_ATTRIBUTE, this );
349         model.put( MARK_USER_FIELD, userField );
350 
351         HtmlTemplate template = AppTemplateService.getTemplate( getTemplateHtmlValue( ), locale, model );
352 
353         return template.getHtml( );
354     }
355 
356     /**
357      * Get plugin
358      * 
359      * @return plugin
360      */
361     @Override
362     public Plugin getPlugin( )
363     {
364         return _plugin;
365     }
366 
367     /**
368      * Set plugin
369      * 
370      * @param plugin
371      *            plugin
372      */
373     @Override
374     public void setPlugin( Plugin plugin )
375     {
376         _plugin = plugin;
377     }
378 
379     /**
380      * Check if the attribute is shown in search
381      * 
382      * @return true if it is, false otherwise
383      */
384     @Override
385     public boolean isShownInSearch( )
386     {
387         return _bIsShownInSearch;
388     }
389 
390     /**
391      * Set isShownInSearch
392      * 
393      * @param bIsShownInSearch
394      *            shown in search
395      */
396     @Override
397     public void setShownInSearch( boolean bIsShownInSearch )
398     {
399         _bIsShownInSearch = bIsShownInSearch;
400     }
401 
402     /**
403      * Check if the attribute is to be shown in line
404      * 
405      * @return true if it is shown in line, false otherwise
406      */
407     @Override
408     public boolean isFieldInLine( )
409     {
410         return _bIsFieldInLine;
411     }
412 
413     /**
414      * Set isFieldInLine
415      * 
416      * @param bIsFieldInLine
417      *            shown in line
418      */
419     @Override
420     public void setFieldInLine( boolean bIsFieldInLine )
421     {
422         _bIsFieldInLine = bIsFieldInLine;
423     }
424 
425     /**
426      * Check if it is an attribute image
427      * 
428      * @return true if it is, false otherwise
429      */
430     @Override
431     public boolean isAttributeImage( )
432     {
433         return _bIsAttributeImage;
434     }
435 
436     /**
437      * Set the attribute as an attribute image
438      * 
439      * @param bIsAttributeImage
440      *            true if it is an image, false otherwise
441      */
442     @Override
443     public void setAttributeImage( boolean bIsAttributeImage )
444     {
445         _bIsAttributeImage = bIsAttributeImage;
446     }
447 
448     /**
449      * Check if the attribute is shown in result list
450      * 
451      * @return true if it is, false otherwise
452      */
453     @Override
454     public boolean isShownInResultList( )
455     {
456         return _bIsShownInResultList;
457     }
458 
459     /**
460      * Set isShownInResultList
461      * 
462      * @param bIsShownInResultList
463      *            shown in result list
464      */
465     @Override
466     public void setShownInResultList( boolean bIsShownInResultList )
467     {
468         _bIsShownInResultList = bIsShownInResultList;
469     }
470 }