View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.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.poi.ss.usermodel.Cell;
49  import org.apache.poi.ss.usermodel.CellType;
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  
69      /**
70       * Private constructor - this class does not need to be instantiated
71       */
72      private ClosingDayService( )
73      {
74      }
75  
76      /**
77       * Find all the closing dates of the form on a given period
78       * 
79       * @param nIdForm
80       *            the form Id
81       * @param startingDate
82       *            the starting date
83       * @param endingDate
84       *            the ending date
85       * @return a list of closing dates
86       */
87      public static List<LocalDate> findListDateOfClosingDayByIdFormAndDateRange( int nIdForm, LocalDate startingDate, LocalDate endingDate )
88      {
89          List<LocalDate> listDate = new ArrayList<>( );
90          List<ClosingDay> listClosingDay = ClosingDayHome.findByIdFormAndDateRange( nIdForm, startingDate, endingDate );
91          for ( ClosingDay closingDay : listClosingDay )
92          {
93              listDate.add( closingDay.getDateOfClosingDay( ) );
94          }
95          return listDate;
96      }
97  
98      /**
99       * Find, if it exists, the closing day of a form for a given date
100      * 
101      * @param nIdForm
102      *            the form Id
103      * @param date
104      *            the date
105      * @return the closing day, if it exists
106      */
107     public static ClosingDay findClosingDayByIdFormAndDateOfClosingDay( int nIdForm, LocalDate dateOfClosingDay )
108     {
109         return ClosingDayHome.findByIdFormAndDateOfCLosingDay( nIdForm, dateOfClosingDay );
110     }
111 
112     /**
113      * Find all the dates of the closing days of the form
114      * 
115      * @param nIdForm
116      *            the form Id
117      * @return the list of closing dates
118      */
119     public static List<LocalDate> findListDateOfClosingDayByIdForm( int nIdForm )
120     {
121         List<LocalDate> listDate = new ArrayList<>( );
122         List<ClosingDay> listClosingDay = ClosingDayHome.findByIdForm( nIdForm );
123         for ( ClosingDay closingDay : listClosingDay )
124         {
125             listDate.add( closingDay.getDateOfClosingDay( ) );
126         }
127         return listDate;
128     }
129 
130     /**
131      * Returns a list of the closing days of a form
132      * 
133      * @param nIdForm
134      *            the form id
135      * @return a list of the closing days of the form
136      */
137     public static List<ClosingDay> findListClosingDay( int nIdForm )
138     {
139         return ClosingDayHome.findByIdForm( nIdForm );
140     }
141 
142     /**
143      * Save the closing days of a form
144      * 
145      * @param nIdForm
146      *            the form id
147      * @param listClosingDate
148      *            the closing dates to save
149      */
150     public static void saveListClosingDay( int nIdForm, List<LocalDate> listClosingDate )
151     {
152 
153         for ( LocalDate closingDate : listClosingDate )
154         {
155             saveClosingDay( nIdForm, closingDate );
156         }
157     }
158 
159     /**
160      * Save a closing day of a form
161      * 
162      * @param nIdForm
163      *            the form Id
164      * @param closingDate
165      *            the closing date
166      */
167     public static void saveClosingDay( int nIdForm, LocalDate closingDate )
168     {
169         ClosingDaytment/business/planning/ClosingDay.html#ClosingDay">ClosingDay closingDay = new ClosingDay( );
170         closingDay.setIdForm( nIdForm );
171         closingDay.setDateOfClosingDay( closingDate );
172         ClosingDayHome.create( closingDay );
173     }
174 
175     /**
176      * Save a closing day of a form
177      * 
178      * @param closingDay
179      *            the closing day to save
180      */
181     public static void saveClosingDay( ClosingDay closingDay )
182     {
183         ClosingDayHome.create( closingDay );
184     }
185 
186     /**
187      * Remove a closing day
188      * 
189      * @param closingDay
190      *            the closing day to remove
191      */
192     public static void removeClosingDay( ClosingDay closingDay )
193     {
194         ClosingDayHome.delete( closingDay.getIdClosingDay( ) );
195     }
196 
197     /**
198      * Import the closing dates of a given file
199      * 
200      * @param item
201      *            the file in input
202      * @return the list of the closing dates in the file
203      * @throws IOException
204      *             if error during reading file
205      */
206     public static List<LocalDate> getImportClosingDays( FileItem item ) throws IOException
207     {
208         HashSet<LocalDate> listDays = new HashSet<>( );
209         String strExtension = FilenameUtils.getExtension( item.getName( ) );
210         if ( !MARK_EXCEL_EXTENSION_XLSX.equals( strExtension ) )
211         {
212             return new ArrayList<>( );
213         }
214         // Using XSSF for xlsx format, for xls use HSSF
215         try ( FileInputStream fis = (FileInputStream) item.getInputStream( ) ; Workbook workbook = new XSSFWorkbook( fis ) )
216         {
217             int numberOfSheets = workbook.getNumberOfSheets( );
218             // looping over each workbook sheet
219             for ( int i = 0; i < numberOfSheets; i++ )
220             {
221                 Sheet sheet = workbook.getSheetAt( i );
222                 Iterator<Row> rowIterator = sheet.iterator( );
223                 // iterating over each row
224                 while ( rowIterator.hasNext( ) )
225                 {
226                     Row row = rowIterator.next( );
227                     if ( row.getRowNum( ) > 1 )
228                     {
229                         Iterator<Cell> cellIterator = row.cellIterator( );
230                         // Iterating over each cell (column wise) in a
231                         // particular row.
232                         while ( cellIterator.hasNext( ) )
233                         {
234                             Cell cell = cellIterator.next( );
235                             // The Cell Containing String will is name.
236                             if ( cell.getColumnIndex( ) == 3 && cell.getCellType( ) == CellType.NUMERIC )
237                             {
238                                 Instant instant = cell.getDateCellValue( ).toInstant( );
239                                 LocalDate localDate = instant.atZone( ZoneId.systemDefault( ) ).toLocalDate( );
240                                 listDays.add( localDate );
241                             }
242                         }
243                     }
244                 }
245             }
246         }
247         return new ArrayList<>( listDays );
248     }
249 
250 }