View Javadoc
1   /*
2    * Copyright (c) 2002-2018, 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.aot.modules.declarationimmat.business;
35  
36  import java.io.IOException;
37  import java.io.Serializable;
38  import java.sql.Timestamp;
39  import java.text.SimpleDateFormat;
40  import java.util.ArrayList;
41  import java.util.Date;
42  import java.util.List;
43  
44  import javax.validation.Valid;
45  import javax.validation.constraints.NotNull;
46  import javax.xml.bind.annotation.XmlElement;
47  import javax.xml.bind.annotation.XmlRootElement;
48  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
49  
50  import org.hibernate.validator.constraints.NotEmpty;
51  
52  import fr.paris.lutece.plugins.aot.modules.declarationimmat.utils.JaxbJacksonObjectMapper;
53  import fr.paris.lutece.plugins.aot.modules.declarationimmat.utils.TimestampAdapter;
54  import fr.paris.lutece.portal.service.i18n.I18nService;
55  import fr.paris.lutece.portal.web.l10n.LocaleService;
56  
57  /**
58   * This is the business class for the object Authorization
59   */ 
60  @XmlRootElement( name = "authorization" )
61  public class Authorization implements Serializable
62  {
63      private static final long serialVersionUID = 1L;
64  
65      private static final String PROPERTY_STATUS_DEFAULT = "module.aot.declarationimmat.status.default";
66      private static final String PROPERTY_STATUS_ACTIVE = "module.aot.declarationimmat.status.active";
67      private static final String PROPERTY_STATUS_EXPIRED = "module.aot.declarationimmat.status.expired";
68      private static final String PROPERTY_STATUS_CANCELLED = "module.aot.declarationimmat.status.cancelled";
69  
70      private int _nId;
71      private String _strNumber;
72      private Timestamp _dateStartDate;
73      private Timestamp _dateEndDate;
74      private String _strToken;
75      private String _strLabel;
76      private String _strAddress;
77      private String _strContactEmail;
78      private String _strProfile;
79      private String _strOriginApplicationCode;
80      private String _typeRight;
81      private String _strUrl;
82      private Boolean _bCancelled = false;
83      private List<Site> _sites = new ArrayList<>( );
84  
85      @XmlElement( name = "id" )
86      public int getId( )
87      {
88          return _nId;
89      }
90  
91      public void setId( int nId )
92      {
93          _nId = nId;
94      }
95  
96      @NotNull
97      @NotEmpty
98      @XmlElement( name = "number" )
99      public String getNumber( )
100     {
101         return _strNumber;
102     }
103 
104     public void setNumber( String strNumber )
105     {
106         _strNumber = strNumber;
107     }
108 
109     @XmlElement( name = "label" )
110     public String getLabel( )
111     {
112         return _strLabel;
113     }
114 
115     public void setLabel( String strLabel )
116     {
117         _strLabel = strLabel;
118     }
119 
120     @XmlElement( name = "address" )
121     public String getAddress( )
122     {
123         return _strAddress;
124     }
125 
126     public void setAddress( String strAddress )
127     {
128         _strAddress = strAddress;
129     }
130 
131     @XmlElement( name = "contactEmail" )
132     public String getContactEmail( )
133     {
134         return _strContactEmail;
135     }
136 
137     public void setContactEmail( String strContactEmail )
138     {
139         _strContactEmail = strContactEmail;
140     }
141 
142     @XmlElement( name = "profile" )
143     public String getProfile( )
144     {
145         return _strProfile;
146     }
147 
148     public void setProfile( String strProfile )
149     {
150         _strProfile = strProfile;
151     }
152 
153     @NotNull
154     @NotEmpty
155     @XmlElement( name = "originApplicationCode" )
156     public String getOriginApplicationCode( )
157     {
158         return _strOriginApplicationCode;
159     }
160 
161     public void setOriginApplicationCode( String originApplicationCode )
162     {
163         _strOriginApplicationCode = originApplicationCode;
164     }
165 
166     @XmlElement( name = "typeRight" )
167     public String getTypeRight( )
168     {
169         return _typeRight;
170     }
171 
172     public void setTypeRight( String typeRight )
173     {
174         this._typeRight = typeRight;
175     }
176 
177     @NotNull
178     @NotEmpty
179     @Valid
180     @XmlElement( name = "sites" )
181     public List<Site> getSites( )
182     {
183         return _sites;
184     }
185 
186     public void setSites( List<Site> sites )
187     {
188         this._sites = sites;
189     }
190 
191     @XmlJavaTypeAdapter( TimestampAdapter.class )
192     @XmlElement( name = "startDate" )
193     public Timestamp getStartDate( )
194     {
195         return _dateStartDate;
196     }
197 
198     public void setStartDate( Timestamp startDate )
199     {
200         this._dateStartDate = startDate;
201     }
202 
203     @XmlJavaTypeAdapter( TimestampAdapter.class )
204     @XmlElement( name = "endDate" )
205     public Timestamp getEndDate() {
206         return _dateEndDate;
207     }
208 
209     public void setEndDate( Timestamp endDate )
210     {
211         this._dateEndDate = endDate;
212     }
213 
214     @XmlElement( name = "cancelled" )
215     public Boolean getCancelled( )
216     {
217         return _bCancelled;
218     }
219 
220     public void setCancelled( Boolean cancelled )
221     {
222         _bCancelled = cancelled;
223     }
224 
225     @XmlElement( name = "token" )
226     public String getToken( )
227     {
228         return _strToken;
229     }
230 
231     public void setToken( String token )
232     {
233         _strToken = token;
234     }
235 
236     @XmlElement( name = "url" )
237     public String getUrl( )
238     {        
239         return _strUrl;
240     }
241 
242     public void setUrl( String url )
243     {
244         _strUrl = url;
245     }
246 
247     @XmlElement( name = "status" )
248     public String getStatus( )
249     {
250         Date startDate = getStartDate( );
251         Date endDate = getEndDate( );
252         Date now = new Date( );
253 
254         if ( _bCancelled )
255         {
256             return I18nService.getLocalizedString( PROPERTY_STATUS_CANCELLED, LocaleService.getDefault( ) );
257         }
258 
259         if ( endDate != null && now.after( endDate ) )
260         {
261             return I18nService.getLocalizedString( PROPERTY_STATUS_EXPIRED, LocaleService.getDefault( ) );
262         }
263 
264         if ( startDate != null && now.after( startDate ) )
265         {
266             return I18nService.getLocalizedString( PROPERTY_STATUS_ACTIVE, LocaleService.getDefault( ) );
267         }
268 
269         return I18nService.getLocalizedString( PROPERTY_STATUS_DEFAULT, LocaleService.getDefault( ) );
270     }
271 
272     public String toJson() throws IOException {
273         JaxbJacksonObjectMapper mapper = new JaxbJacksonObjectMapper( );
274         mapper.setDateFormat( new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssXXX" ) );
275         return mapper.writeValueAsString( this );
276     }
277 }