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.plugins.crm.business.demand;
35  
36  import com.fasterxml.jackson.annotation.JsonIgnore;
37  import com.fasterxml.jackson.annotation.JsonProperty;
38  import java.util.Date;
39  import java.util.Locale;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  import org.apache.commons.lang3.StringUtils;
44  
45  import fr.paris.lutece.plugins.crm.util.CrmUtils;
46  import fr.paris.lutece.plugins.crm.util.TargetEnum;
47  import fr.paris.lutece.plugins.crm.util.constants.CRMConstants;
48  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupResource;
49  import fr.paris.lutece.util.date.DateUtil;
50  import fr.paris.lutece.util.xml.XmlUtil;
51  
52  /**
53   *
54   * DemandType
55   *
56   */
57  public class DemandType implements AdminWorkgroupResource
58  {
59      public static final String ROLE_NONE = "none";
60  
61      @JsonProperty( "id_demand_type" )
62      private int _nIdDemandType;
63      @JsonProperty( "label" )
64      private String _strLabel;
65      @JsonProperty( "url" )
66      private String _strUrlResource;
67      @JsonIgnore
68      private String _strUrlInfo;
69      @JsonIgnore
70      private String _strUrlContact;
71      @JsonIgnore
72      private int _nOrder;
73      @JsonIgnore
74      private int _nIdCategory;
75      @JsonIgnore
76      private Date _dateBegin;
77      @JsonIgnore
78      private Date _dateEnd;
79      @JsonIgnore
80      private String _strWorkgroupKey;
81      @JsonIgnore
82      private String _strRoleKey;
83      @JsonIgnore
84      private TargetEnum _target;
85      @JsonIgnore
86      private String _strUrlDelete;
87      @JsonIgnore
88      private boolean _bIncludeIdCrmUser;
89      @JsonIgnore
90      private boolean _bNeedAuthentication;
91      @JsonIgnore
92      private boolean _bNeedValidation;
93  
94      /**
95       * Constructor
96       */
97      public DemandType( )
98      {
99          _nIdDemandType = 0;
100         _strLabel = StringUtils.EMPTY;
101         _strUrlResource = StringUtils.EMPTY;
102         _strUrlInfo = StringUtils.EMPTY;
103         _strUrlContact = StringUtils.EMPTY;
104         _nOrder = 1;
105         _nIdCategory = -1;
106         _strWorkgroupKey = StringUtils.EMPTY;
107         _strRoleKey = StringUtils.EMPTY;
108         _target = TargetEnum.SELF;
109         _strUrlDelete = StringUtils.EMPTY;
110         _bIncludeIdCrmUser = false;
111         _bNeedAuthentication = false;
112         _bNeedValidation = false;
113 
114     }
115 
116     /**
117      * Get the id demand type
118      * 
119      * @return the id demand type
120      */
121     @JsonProperty( "id_demand_type" )
122     public int getIdDemandType( )
123     {
124         return _nIdDemandType;
125     }
126 
127     /**
128      * Set the id demand type
129      * 
130      * @param nIdDemandType
131      *            the id deman type
132      */
133     @JsonProperty( "id_demand_type" )
134     public void setIdDemandType( int nIdDemandType )
135     {
136         _nIdDemandType = nIdDemandType;
137     }
138 
139     /**
140      * Get the label
141      * 
142      * @return the label
143      */
144     @JsonProperty( "label" )
145     public String getLabel( )
146     {
147         return _strLabel;
148     }
149 
150     /**
151      * Set the label
152      * 
153      * @param strLabel
154      *            the label
155      */
156     public void setLabel( String strLabel )
157     {
158         _strLabel = strLabel;
159     }
160 
161     /**
162      * Get the url of the resource
163      * 
164      * @return the url of the resource
165      */
166 
167     @JsonProperty( "url" )
168     public String getUrlResource( )
169     {
170         return _strUrlResource;
171     }
172 
173     /**
174      * Set the url of the form
175      * 
176      * @param strUrlResource
177      *            the url of the form
178      */
179     public void setUrlResource( String strUrlResource )
180     {
181         _strUrlResource = strUrlResource;
182     }
183 
184     /**
185      * Get the url of the information
186      * 
187      * @return the url of the information
188      */
189     @JsonIgnore
190     public String getUrlInfo( )
191     {
192         return _strUrlInfo;
193     }
194 
195     /**
196      * Set the url of the information
197      * 
198      * @param strUrlInfo
199      *            the url of the information
200      */
201     public void setUrlInfo( String strUrlInfo )
202     {
203         _strUrlInfo = strUrlInfo;
204     }
205 
206     /**
207      * Get the url of the contact
208      * 
209      * @return the url of the contact
210      */
211     @JsonIgnore
212     public String getUrlContact( )
213     {
214         return _strUrlContact;
215     }
216 
217     /**
218      * Set the url of the contact
219      * 
220      * @param strUrlContact
221      *            the url of the contact
222      */
223     @JsonIgnore
224     public void setUrlContact( String strUrlContact )
225     {
226         _strUrlContact = strUrlContact;
227     }
228 
229     /**
230      * Get the order
231      * 
232      * @return the order
233      */
234     @JsonIgnore
235     public int getOrder( )
236     {
237         return _nOrder;
238     }
239 
240     /**
241      * Set the order
242      * 
243      * @param nOrder
244      *            the order
245      */
246     public void setOrder( int nOrder )
247     {
248         _nOrder = nOrder;
249     }
250 
251     /**
252      * Get the id category
253      * 
254      * @return the ID category
255      */
256     @JsonIgnore
257     public int getIdCategory( )
258     {
259         return _nIdCategory;
260     }
261 
262     /**
263      * Set the ID category
264      * 
265      * @param nIdCategory
266      *            the id category
267      */
268     public void setIdCategory( int nIdCategory )
269     {
270         _nIdCategory = nIdCategory;
271     }
272 
273     /**
274      * Get the beginning date
275      * 
276      * @return the beginning date
277      */
278     @JsonIgnore
279     public Date getDateBegin( )
280     {
281         return _dateBegin;
282     }
283 
284     /**
285      * Set the beginning date
286      * 
287      * @param dateBegin
288      *            the beginning date
289      */
290     public void setDateBegin( Date dateBegin )
291     {
292         _dateBegin = dateBegin;
293     }
294 
295     /**
296      * Get the closing date
297      * 
298      * @return the closing date
299      */
300     @JsonIgnore
301     public Date getDateEnd( )
302     {
303         return _dateEnd;
304     }
305 
306     /**
307      * Set the closing date
308      * 
309      * @param dateEnd
310      *            the closing date
311      */
312     public void setDateEnd( Date dateEnd )
313     {
314         _dateEnd = dateEnd;
315     }
316 
317     /**
318      * Set the workgroup
319      * 
320      * @param strWorkgroupKey
321      *            the workgroup key
322      */
323     public void setWorkgroup( String strWorkgroupKey )
324     {
325         _strWorkgroupKey = strWorkgroupKey;
326     }
327 
328     /**
329      * Get the workgroup key
330      * 
331      * @return the workgroup key
332      */
333     @JsonIgnore
334     public String getWorkgroup( )
335     {
336         return _strWorkgroupKey;
337     }
338 
339     /**
340      * Set the role
341      * 
342      * @param strRoleKey
343      *            the role key
344      */
345     public void setRole( String strRoleKey )
346     {
347         _strRoleKey = strRoleKey;
348     }
349 
350     /**
351      * Get the role
352      * 
353      * @return the role key
354      */
355     @JsonIgnore
356     public String getRole( )
357     {
358         return _strRoleKey;
359     }
360 
361     /**
362      * Check if the demand type is open or not
363      * 
364      * @return true if it is open, false otherwise
365      */
366     @JsonIgnore
367     public boolean isOpen( )
368     {
369         boolean bIsDateBeginCorrect = false;
370         boolean bIsDateEndCorrect = false;
371         Date dateToday = new Date( );
372 
373         if ( _dateBegin != null )
374         {
375             if ( _dateBegin.before( dateToday ) )
376             {
377                 bIsDateBeginCorrect = true;
378             }
379         }
380         else
381         {
382             bIsDateBeginCorrect = true;
383         }
384 
385         if ( _dateEnd != null )
386         {
387             if ( _dateEnd.after( dateToday ) )
388             {
389                 bIsDateEndCorrect = true;
390             }
391         }
392         else
393         {
394             bIsDateEndCorrect = true;
395         }
396 
397         return bIsDateBeginCorrect && bIsDateEndCorrect;
398     }
399 
400     /**
401      * Check if the demand type is closed
402      * 
403      * @return true if it is closed, false otherwise
404      */
405     @JsonIgnore
406     public boolean isClosed( )
407     {
408         return !isOpen( );
409     }
410 
411     /**
412      * Set target
413      * 
414      * @param target
415      *            the target
416      */
417     public void setTarget( TargetEnum target )
418     {
419         _target = target;
420     }
421 
422     /**
423      * Get target
424      * 
425      * @return the target
426      */
427     @JsonIgnore
428     public TargetEnum getTarget( )
429     {
430         return _target;
431     }
432 
433     /**
434      * Get the url to delete the demand
435      * 
436      * @return the url to delete the demand
437      */
438     @JsonIgnore
439     public String getUrlDelete( )
440     {
441         return _strUrlDelete;
442     }
443 
444     /**
445      * Set the url to delete the demand
446      * 
447      * @param strUrlDelete
448      *            the url to delete the demand
449      */
450     public void setUrlDelete( String strUrlDelete )
451     {
452         _strUrlDelete = strUrlDelete;
453     }
454 
455     /**
456      * Returns the xml of this Demande Type
457      *
458      * @param request
459      *            The HTTP Servlet request
460      * @param locale
461      *            the Locale
462      * @return the xml of this Demande Type
463      */
464     @JsonIgnore
465     public String getXml( HttpServletRequest request, Locale locale )
466     {
467         StringBuffer strXml = new StringBuffer( );
468         XmlUtil.beginElement( strXml, CRMConstants.TAG_DEMAND_TYPE );
469         XmlUtil.addElement( strXml, CRMConstants.TAG_DEMAND_TYPE_ID, _nIdDemandType );
470         XmlUtil.addElement( strXml, CRMConstants.TAG_DEMAND_TYPE_CATEGORY, _nIdCategory );
471         XmlUtil.addElement( strXml, CRMConstants.TAG_DEMAND_TYPE_ORDER, _nOrder );
472         CrmUtils.addElementHtml( strXml, CRMConstants.TAG_DEMAND_TYPE_LABEL, _strLabel );
473         CrmUtils.addElementHtml( strXml, CRMConstants.TAG_DEMAND_TYPE_URL_RESOURCE, _strUrlResource );
474         CrmUtils.addElementHtml( strXml, CRMConstants.TAG_DEMAND_TYPE_URL_INFO, _strUrlInfo );
475         CrmUtils.addElementHtml( strXml, CRMConstants.TAG_DEMAND_TYPE_URL_CONTACT, _strUrlContact );
476         CrmUtils.addElementHtml( strXml, CRMConstants.TAG_DEMAND_TYPE_TARGET, _target.toString( ) );
477         CrmUtils.addElementHtml( strXml, CRMConstants.TAG_DEMAND_TYPE_URL_DELETE, _strUrlDelete );
478         CrmUtils.addElementHtml( strXml, CRMConstants.TAG_DEMAND_TYPE_DATE_BEGIN, _dateBegin != null ? DateUtil.getDateString( _dateBegin, locale ) : null );
479         CrmUtils.addElementHtml( strXml, CRMConstants.TAG_DEMAND_TYPE_DATE_END, _dateEnd != null ? DateUtil.getDateString( _dateEnd, locale ) : null );
480         CrmUtils.addElementHtml( strXml, CRMConstants.TAG_DEMAND_TYPE_NEED_AUTHENTICATION, new Boolean( _bNeedAuthentication ).toString( ) );
481         CrmUtils.addElementHtml( strXml, CRMConstants.TAG_DEMAND_TYPE_NEED_VALIDATION, new Boolean( _bNeedValidation ).toString( ) );
482         XmlUtil.endElement( strXml, CRMConstants.TAG_DEMAND_TYPE );
483         return strXml.toString( );
484     }
485 
486     /**
487      * 
488      * @return true if the crm user id must be added to the list of _strUrlResource parameters
489      */
490     @JsonIgnore
491     public boolean isIncludeIdCrmUser( )
492     {
493         return _bIncludeIdCrmUser;
494     }
495 
496     /**
497      * 
498      * @param _bUseIdCrmUser
499      *            true if the crm user id must be added to the list of _strUrlResource parameters
500      */
501     public void setIncludeIdCrmUser( boolean _bUseIdCrmUser )
502     {
503         this._bIncludeIdCrmUser = _bUseIdCrmUser;
504     }
505 
506     /**
507      * 
508      * @return true if the demand type need authentication access
509      */
510     @JsonIgnore
511     public boolean isNeedAuthentication( )
512     {
513         return _bNeedAuthentication;
514     }
515 
516     /**
517      * 
518      * @param _bNeedAuthentication
519      *            true if the demand type need authentication access
520      */
521     public void setNeedAuthentication( boolean _bNeedAuthentication )
522     {
523         this._bNeedAuthentication = _bNeedAuthentication;
524     }
525 
526     /**
527      * 
528      * @return true if the demand type need validation access
529      */
530     @JsonIgnore
531     public boolean isNeedValidation( )
532     {
533         return _bNeedValidation;
534     }
535 
536     /**
537      * 
538      * @param _bNeedAuthentication
539      *            true if the demand type need validation access
540      */
541     public void setNeedValidation( boolean _bNeedValidation )
542     {
543         this._bNeedValidation = _bNeedValidation;
544     }
545 
546 }