View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.campagnebp.service;
35  
36  import java.sql.Timestamp;
37  import java.text.Normalizer;
38  import java.text.SimpleDateFormat;
39  import java.util.Collection;
40  import java.util.Date;
41  import java.util.HashMap;
42  import java.util.Map;
43  
44  import fr.paris.lutece.plugins.campagnebp.business.Campagne;
45  import fr.paris.lutece.plugins.campagnebp.business.CampagneHome;
46  import fr.paris.lutece.plugins.campagnebp.business.CampagnePhase;
47  import fr.paris.lutece.plugins.campagnebp.business.CampagnePhaseHome;
48  import fr.paris.lutece.plugins.campagnebp.utils.Constants;
49  import fr.paris.lutece.portal.service.spring.SpringContextService;
50  import fr.paris.lutece.portal.service.util.AppLogService;
51  
52  /**
53   * 
54   */
55  public class CampagnesService implements ICampagneService
56  {
57  
58      // Attributes
59  
60      private Map<String, Timestamp> _cache = null;
61  
62      // ***********************************************************************************
63      // * SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON *
64      // * SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON *
65      // ***********************************************************************************
66  
67      private static final String BEAN_CAMPAGNE_SERVICE = "campagnebp.campagneService";
68  
69      private static ICampagneService _singleton;
70  
71      /**
72       * @return ICampagneService singleton
73       */
74      public static ICampagneService getInstance( )
75      {
76          if ( _singleton == null )
77          {
78              _singleton = SpringContextService.getBean( BEAN_CAMPAGNE_SERVICE );
79          }
80          return _singleton;
81      }
82  
83      // ***********************************************************************************
84      // * CACHE CACHE CACHE CACHE CACHE CACHE CACHE CACHE CACHE CACHE CACHE CACHE CACHE C *
85      // * CACHE CACHE CACHE CACHE CACHE CACHE CACHE CACHE CACHE CACHE CACHE CACHE CACHE C *
86      // ***********************************************************************************
87  
88      private Map<String, Timestamp> getCache( )
89      {
90          if ( _cache == null )
91              reset( );
92  
93          return _cache;
94      }
95  
96      private String getKey( String campain, String phase, String datetimeType )
97      {
98          return campain + "-" + phase + "-" + datetimeType;
99      }
100 
101     public void reset( )
102     {
103         AppLogService.debug( "CampagnePhase cache reset" );
104 
105         Map<String, Timestamp> cache = new HashMap<String, Timestamp>( );
106 
107         Collection<CampagnePhase> phases = CampagnePhaseHome.getCampagnePhasesList( );
108         for ( CampagnePhase phase : phases )
109         {
110             String beginningKey = getKey( phase.getCodeCampagne( ), phase.getCodePhaseType( ), Constants.BEGINNING_DATETIME );
111             String endKey = getKey( phase.getCodeCampagne( ), phase.getCodePhaseType( ), Constants.END_DATETIME );
112 
113             cache.put( beginningKey, phase.getStart( ) );
114             cache.put( endKey, phase.getEnd( ) );
115 
116             AppLogService.debug(
117                     "  -> Added '" + phase.getCodeCampagne( ) + "-" + phase.getCodePhaseType( ) + "' = '" + phase.getStart( ) + "/" + phase.getEnd( ) + "'." );
118         }
119 
120         _cache = cache;
121     }
122 
123     // ***********************************************************************************
124     // * CAMPAIN CAMPAIN CAMPAIN CAMPAIN CAMPAIN CAMPAIN CAMPAIN CAMPAIN CAMPAIN CAMPAIN *
125     // * CAMPAIN CAMPAIN CAMPAIN CAMPAIN CAMPAIN CAMPAIN CAMPAIN CAMPAIN CAMPAIN CAMPAIN *
126     // ***********************************************************************************
127 
128     public Campagne getLastCampagne( )
129     {
130         return CampagneHome.getLastCampagne( );
131     }
132 
133     // ***********************************************************************************
134     // * PHASE PHASE PHASE PHASE PHASE PHASE PHASE PHASE PHASE PHASE PHASE PHASE PHASE P *
135     // * PHASE PHASE PHASE PHASE PHASE PHASE PHASE PHASE PHASE PHASE PHASE PHASE PHASE P *
136     // ***********************************************************************************
137 
138     private Timestamp getTimestamp( String campain, String phase, String timestampType )
139     {
140         String key = getKey( campain, phase, timestampType );
141         Timestamp timeStamp = getCache( ).get( key );
142         if ( timeStamp == null )
143         {
144             String errorMsg = "Null datetime for campagne '" + campain + "' and phase '" + phase + "' and timestampType '" + timestampType + "'. ";
145             AppLogService.error( errorMsg );
146             throw new NoSuchPhaseException( errorMsg );
147         }
148         return timeStamp;
149     }
150 
151     public boolean isBeforeBeginning( String campain, String phase )
152     {
153         Timestamp timeStamp = getTimestamp( campain, phase, Constants.BEGINNING_DATETIME );
154         Date date = new Date( );
155         boolean result = date.before( timeStamp );
156         return result;
157     }
158 
159     public boolean isBeforeEnd( String campain, String phase )
160     {
161         Timestamp timeStamp = getTimestamp( campain, phase, Constants.END_DATETIME );
162         Date date = new Date( );
163         boolean result = date.before( timeStamp );
164         return result;
165     }
166 
167     public boolean isDuring( String campain, String phase )
168     {
169         Timestamp beginningTimeStamp = getTimestamp( campain, phase, Constants.BEGINNING_DATETIME );
170         Timestamp endTimeStamp = getTimestamp( campain, phase, Constants.END_DATETIME );
171 
172         Date date = new Date( );
173 
174         boolean result = date.after( beginningTimeStamp ) && date.before( endTimeStamp );
175         return result;
176     }
177 
178     public boolean isAfterBeginning( String campain, String phase )
179     {
180         Timestamp timeStamp = getTimestamp( campain, phase, Constants.BEGINNING_DATETIME );
181         Date date = new Date( );
182         boolean result = date.after( timeStamp );
183         return result;
184     }
185 
186     public boolean isAfterEnd( String campain, String phase )
187     {
188         Timestamp timeStamp = getTimestamp( campain, phase, Constants.END_DATETIME );
189         Date date = new Date( );
190         boolean result = date.after( timeStamp );
191         return result;
192     }
193 
194     public Timestamp start( String campain, String phase )
195     {
196         Timestamp timeStamp = getTimestamp( campain, phase, Constants.BEGINNING_DATETIME );
197         return timeStamp;
198     }
199 
200     public Timestamp end( String campain, String phase )
201     {
202         Timestamp timeStamp = getTimestamp( campain, phase, Constants.END_DATETIME );
203         return timeStamp;
204     }
205 
206     public String startStr( String campain, String phase, String format, boolean withAccents )
207     {
208         Timestamp ts = start( campain, phase );
209         Date date = new Date( );
210         date.setTime( ts.getTime( ) );
211         String formattedDate = new SimpleDateFormat( format ).format( date );
212         return toSoLovelyString( formattedDate, withAccents );
213     }
214 
215     public String endStr( String campain, String phase, String format, boolean withAccents )
216     {
217         Timestamp ts = end( campain, phase );
218         Date date = new Date( );
219         date.setTime( ts.getTime( ) );
220         String formattedDate = new SimpleDateFormat( format ).format( date );
221         return toSoLovelyString( formattedDate, withAccents );
222     }
223 
224     // Same methods than preceding, for last campagne
225 
226     public boolean isBeforeBeginning( String phase )
227     {
228         return isBeforeBeginning( getLastCampagne( ).getCode( ), phase );
229     }
230 
231     public boolean isBeforeEnd( String phase )
232     {
233         return isBeforeEnd( getLastCampagne( ).getCode( ), phase );
234     }
235 
236     public boolean isDuring( String phase )
237     {
238         return isDuring( getLastCampagne( ).getCode( ), phase );
239     }
240 
241     public boolean isAfterBeginning( String phase )
242     {
243         return isAfterBeginning( getLastCampagne( ).getCode( ), phase );
244     }
245 
246     public boolean isAfterEnd( String phase )
247     {
248         return isAfterEnd( getLastCampagne( ).getCode( ), phase );
249     }
250 
251     public Timestamp start( String phase )
252     {
253         return start( getLastCampagne( ).getCode( ), phase );
254     }
255 
256     public Timestamp end( String phase )
257     {
258         return end( getLastCampagne( ).getCode( ), phase );
259     }
260 
261     public String startStr( String phase, String format, boolean withAccents )
262     {
263         return startStr( getLastCampagne( ).getCode( ), phase, format, withAccents );
264     }
265 
266     public String endStr( String phase, String format, boolean withAccents )
267     {
268         return endStr( getLastCampagne( ).getCode( ), phase, format, withAccents );
269     }
270 
271     // ***********************************************************************************
272     // * UTILS UTILS UTILS UTILS UTILS UTILS UTILS UTILS UTILS UTILS UTILS UTILS UTILS U *
273     // * UTILS UTILS UTILS UTILS UTILS UTILS UTILS UTILS UTILS UTILS UTILS UTILS UTILS U *
274     // ***********************************************************************************
275 
276     private String toSoLovelyString( String msg, boolean withAccents )
277     {
278         String soLovelyStr = null;
279 
280         // Deleting accents
281         if ( !withAccents )
282         {
283             soLovelyStr = Normalizer.normalize( msg, Normalizer.Form.NFD ).replaceAll( "\\p{InCombiningDiacriticalMarks}+", "" );
284         }
285         else
286         {
287             soLovelyStr = msg;
288         }
289 
290         // Replacing "01" by "1er"
291         if ( soLovelyStr.startsWith( "01" ) )
292         {
293             soLovelyStr = "1er " + soLovelyStr.substring( 3 );
294         }
295 
296         // Deleting "0" if first char
297         if ( soLovelyStr.charAt( 0 ) == '0' )
298         {
299             soLovelyStr = soLovelyStr.substring( 1 );
300         }
301 
302         return soLovelyStr;
303     }
304 
305 }