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