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