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.appointment.service;
35  
36  import java.io.FileInputStream;
37  import java.io.IOException;
38  import java.time.Instant;
39  import java.time.LocalDate;
40  import java.time.ZoneId;
41  import java.util.ArrayList;
42  import java.util.HashSet;
43  import java.util.Iterator;
44  import java.util.List;
45  
46  import org.apache.commons.fileupload.FileItem;
47  import org.apache.commons.io.FilenameUtils;
48  import org.apache.commons.lang.StringUtils;
49  import org.apache.poi.ss.usermodel.Cell;
50  import org.apache.poi.ss.usermodel.Row;
51  import org.apache.poi.ss.usermodel.Sheet;
52  import org.apache.poi.ss.usermodel.Workbook;
53  import org.apache.poi.xssf.usermodel.XSSFWorkbook;
54  
55  import fr.paris.lutece.plugins.appointment.business.planning.ClosingDay;
56  import fr.paris.lutece.plugins.appointment.business.planning.ClosingDayHome;
57  
58  /**
59   * Service class for the closing day
60   * 
61   * @author Laurent Payen
62   *
63   */
64  public final class ClosingDayService
65  {
66  
67      private static final String MARK_EXCEL_EXTENSION_XLSX = "xlsx";
68      private static final String MARK_FORMAT_DATE_REGEX = "([0-9]{2})/([0-9]{2})/([0-9]{4})";
69  
70      /**
71       * Private constructor - this class does not need to be instantiated
72       */
73      private ClosingDayService( )
74      {
75      }
76  
77      /**
78       * Find all the closing dates of the form on a given period
79       * 
80       * @param nIdForm
81       *            the form Id
82       * @param startingDate
83       *            the starting date
84       * @param endingDate
85       *            the ending date
86       * @return a list of closing dates
87       */
88      public static List<LocalDate> findListDateOfClosingDayByIdFormAndDateRange( int nIdForm, LocalDate startingDate, LocalDate endingDate )
89      {
90          List<LocalDate> listDate = new ArrayList<>( );
91          List<ClosingDay> listClosingDay = ClosingDayHome.findByIdFormAndDateRange( nIdForm, startingDate, endingDate );
92          for ( ClosingDay closingDay : listClosingDay )
93          {
94              listDate.add( closingDay.getDateOfClosingDay( ) );
95          }
96          return listDate;
97      }
98  
99      /**
100      * Find, if it exists, the closing day of a form for a given date
101      * 
102      * @param nIdForm
103      *            the form Id
104      * @param date
105      *            the date
106      * @return the closing day, if it exists
107      */
108     public static ClosingDay findClosingDayByIdFormAndDateOfClosingDay( int nIdForm, LocalDate dateOfClosingDay )
109     {
110         return ClosingDayHome.findByIdFormAndDateOfCLosingDay( nIdForm, dateOfClosingDay );
111     }
112 
113     /**
114      * Find all the dates of the closing days of the form
115      * 
116      * @param nIdForm
117      *            the form Id
118      * @return the list of closing dates
119      */
120     public static List<LocalDate> findListDateOfClosingDayByIdForm( int nIdForm )
121     {
122         List<LocalDate> listDate = new ArrayList<>( );
123         List<ClosingDay> listClosingDay = ClosingDayHome.findByIdForm( nIdForm );
124         for ( ClosingDay closingDay : listClosingDay )
125         {
126             listDate.add( closingDay.getDateOfClosingDay( ) );
127         }
128         return listDate;
129     }
130 
131     /**
132      * Returns a list of the closing days of a form
133      * 
134      * @param nIdForm
135      *            the form id
136      * @return a list of the closing days of the form
137      */
138     public static List<ClosingDay> findListClosingDay( int nIdForm )
139     {
140         return ClosingDayHome.findByIdForm( nIdForm );
141     }
142 
143     /**
144      * Save the closing days of a form
145      * 
146      * @param nIdForm
147      *            the form id
148      * @param listClosingDate
149      *            the closing dates to save
150      */
151     public static void saveListClosingDay( int nIdForm, List<LocalDate> listClosingDate )
152     {
153 
154         for ( LocalDate closingDate : listClosingDate )
155         {
156             saveClosingDay( nIdForm, closingDate );
157         }
158     }
159 
160     /**
161      * Save a closing day of a form
162      * 
163      * @param nIdForm
164      *            the form Id
165      * @param closingDate
166      *            the closing date
167      */
168     public static void saveClosingDay( int nIdForm, LocalDate closingDate )
169     {
170         ClosingDay closingDay = new ClosingDay( );
171         closingDay.setIdForm( nIdForm );
172         closingDay.setDateOfClosingDay( closingDate );
173         ClosingDayHome.create( closingDay );
174     }
175 
176     /**
177      * Save a closing day of a form
178      * 
179      * @param closingDay
180      *            the closing day to save
181      */
182     public static void saveClosingDay( ClosingDay closingDay )
183     {
184         ClosingDayHome.create( closingDay );
185     }
186 
187     /**
188      * Remove a closing day
189      * 
190      * @param closingDay
191      *            the closing day to remove
192      */
193     public static void removeClosingDay( ClosingDay closingDay )
194     {
195         ClosingDayHome.delete( closingDay.getIdClosingDay( ) );
196     }
197 
198     /**
199      * Import the closing dates of a given file
200      * 
201      * @param item
202      *            the file in input
203      * @return the list of the closing dates in the file
204      * @throws IOException
205      *             if error during reading file
206      */
207     public static List<LocalDate> getImportClosingDays( FileItem item ) throws IOException
208     {
209         HashSet<LocalDate> listDays = new HashSet<LocalDate>( );
210         FileInputStream fis = null;
211         Workbook workbook = null;
212         String strExtension = FilenameUtils.getExtension( item.getName( ) );
213         if ( StringUtils.equals( MARK_EXCEL_EXTENSION_XLSX, strExtension ) )
214         {
215             try
216             {
217                 fis = (FileInputStream) item.getInputStream( );
218                 // Using XSSF for xlsx format, for xls use HSSF
219                 workbook = new XSSFWorkbook( fis );
220                 int numberOfSheets = workbook.getNumberOfSheets( );
221                 // looping over each workbook sheet
222                 for ( int i = 0; i < numberOfSheets; i++ )
223                 {
224                     Sheet sheet = workbook.getSheetAt( i );
225                     Iterator<Row> rowIterator = sheet.iterator( );
226                     // iterating over each row
227                     while ( rowIterator.hasNext( ) )
228                     {
229                         Row row = (Row) rowIterator.next( );
230                         if ( row.getRowNum( ) > 1 )
231                         {
232                             Iterator<Cell> cellIterator = row.cellIterator( );
233                             // Iterating over each cell (column wise) in a
234                             // particular row.
235                             while ( cellIterator.hasNext( ) )
236                             {
237                                 Cell cell = (Cell) cellIterator.next( );
238                                 // The Cell Containing String will is name.
239                                 if ( cell.getColumnIndex( ) == 3 )
240                                 {
241                                     String strdate = StringUtils.EMPTY;
242                                     if ( cell.getCellType( ) == 0 )
243                                     {
244                                         Instant instant = cell.getDateCellValue( ).toInstant( );
245                                         LocalDate localDate = instant.atZone( ZoneId.systemDefault( ) ).toLocalDate( );
246                                         strdate = localDate.format( Utilities.getFormatter( ) );
247                                     }
248                                     if ( StringUtils.isNotEmpty( strdate ) && strdate.matches( MARK_FORMAT_DATE_REGEX ) )
249                                     {
250                                         LocalDate date = LocalDate.parse( strdate, Utilities.getFormatter( ) );
251                                         listDays.add( date );
252                                     }
253                                 }
254                             }
255                         }
256                     }
257                 }
258             }
259             finally
260             {
261                 if ( fis != null )
262                 {
263                     fis.close( );
264                 }
265                 if ( workbook != null )
266                 {
267                     workbook.close( );
268                 }
269             }
270         }
271         return new ArrayList<LocalDate>( listDays );
272     }
273 
274 }