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.calendar.business.category;
35  
36  import fr.paris.lutece.portal.service.image.ImageResource;
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  
40  import java.util.ArrayList;
41  import java.util.Collection;
42  
43  
44  /**
45   * This class provides Data Access methods for Category objects
46   */
47  public final class CategoryDAO implements ICategoryDAO
48  {
49      // Constants
50      private static final String SQL_QUERY_MAX_PK = " SELECT MAX(id_category) FROM calendar_category ";
51      private static final String SQL_QUERY_SELECT_BY_NAME = " SELECT id_category, description, icon_content, icon_mime_type, workgroup_key FROM calendar_category WHERE calendar_category_name = ? ";
52      private static final String SQL_QUERY_SELECTALL = " SELECT id_category, calendar_category_name, description, icon_content, icon_mime_type, workgroup_key FROM calendar_category ORDER BY calendar_category_name";
53      private static final String SQL_QUERY_INSERT = " INSERT INTO calendar_category ( id_category, calendar_category_name, description, icon_content, icon_mime_type, workgroup_key ) VALUES ( ?, ?, ?, ?, ?, ? )";
54      private static final String SQL_QUERY_SELECT = " SELECT calendar_category_name, description, icon_content, icon_mime_type ,workgroup_key FROM calendar_category WHERE id_category = ? ";
55      private static final String SQL_QUERY_DELETE = " DELETE FROM calendar_category WHERE id_category = ? ";
56      private static final String SQL_QUERY_UPDATE = " UPDATE calendar_category SET calendar_category_name = ?, description = ?, icon_content = ?, icon_mime_type = ?, workgroup_key= ? WHERE id_category = ?";
57      private static final String SQL_QUERY_DELETE_LINK_CATEGORY_CALENDAR = " DELETE FROM calendar_category WHERE id_category = ? ";
58      private static final String SQL_QUERY_SELECTALL_ID_EVENT = " SELECT a.id_event FROM calendar_category_link a WHERE a.id_category = ? ";
59      private static final String SQL_QUERY_DELETE_LINKS_CATEGORY = " DELETE FROM calendar_category_link WHERE id_category = ? ";
60      private static final String SQL_QUERY_SELECT_COUNT_OF_EVENT_ID = " SELECT COUNT(*) FROM calendar_category_link WHERE id_category = ?";
61      private static final String SQL_QUERY_SELECT_EVENT_CATEGORIES = " SELECT a.id_category, a.calendar_category_name, a.description, a.icon_content, a.icon_mime_type , a.workgroup_key" +
62          " FROM calendar_category a, calendar_category_link b" +
63          " WHERE a.id_category = b.id_category AND b.id_event = ?";
64  
65      // ImageResource queries
66      private static final String SQL_QUERY_SELECT_RESOURCE_IMAGE = " SELECT icon_content, icon_mime_type FROM calendar_category WHERE id_category = ? ";
67  
68      ///////////////////////////////////////////////////////////////////////////////////////
69      //Access methods to data
70  
71      /**
72       * Load the list of Category
73  	 * @param plugin Plugin
74       * @return The Collection of Category (empty collection is no result)
75       */
76      public Collection<Category> selectAll( Plugin plugin )
77      {
78          int nParam;
79          Collection<Category> listCategory = new ArrayList<Category>(  );
80          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
81          daoUtil.executeQuery(  );
82  
83          while ( daoUtil.next(  ) )
84          {
85              nParam = 0;
86  
87              Category category = new Category(  );
88              category.setId( daoUtil.getInt( ++nParam ) );
89              category.setName( daoUtil.getString( ++nParam ) );
90              category.setDescription( daoUtil.getString( ++nParam ) );
91              category.setIconContent( daoUtil.getBytes( ++nParam ) );
92              category.setIconMimeType( daoUtil.getString( ++nParam ) );
93              category.setWorkgroup( daoUtil.getString( ++nParam ) );
94  
95              listCategory.add( category );
96          }
97  
98          daoUtil.free(  );
99  
100         return listCategory;
101     }
102 
103     /**
104      * Insert a new Category
105      * @param category The object category to insert
106      * @param plugin Plugin
107      */
108     public void insert( Category category, Plugin plugin )
109     {
110         int nParam = 0;
111         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
112 
113         daoUtil.setInt( ++nParam, getNewPrimaryKey( plugin ) );
114         daoUtil.setString( ++nParam, category.getName(  ) );
115         daoUtil.setString( ++nParam, category.getDescription(  ) );
116         daoUtil.setBytes( ++nParam, category.getIconContent(  ) );
117         daoUtil.setString( ++nParam, category.getIconMimeType(  ) );
118         daoUtil.setString( ++nParam, category.getWorkgroup(  ) );
119 
120         daoUtil.executeUpdate(  );
121         daoUtil.free(  );
122     }
123 
124     /**
125      * Auto increment the primary key for the new category
126      * @param plugin Plugin
127      * @return the new primary key for category
128      */
129     private int getNewPrimaryKey( Plugin plugin )
130     {
131         int nNewPrimaryKey = -1;
132         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_MAX_PK, plugin );
133         daoUtil.executeQuery(  );
134 
135         if ( daoUtil.next(  ) )
136         {
137             nNewPrimaryKey = daoUtil.getInt( 1 );
138         }
139 
140         daoUtil.free(  );
141 
142         return ++nNewPrimaryKey;
143     }
144 
145     /**
146      * Load the data of Category from the table
147      * @param nIdCategory The identifier of the category
148      * @param plugin Plugin
149      * @return The Instance of the object Category
150      */
151     public Category load( int nIdCategory, Plugin plugin )
152     {
153         int nParam;
154         Category category = null;
155         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
156         daoUtil.setInt( 1, nIdCategory );
157 
158         daoUtil.executeQuery(  );
159 
160         if ( daoUtil.next(  ) )
161         {
162             nParam = 0;
163             category = new Category(  );
164             category.setId( nIdCategory );
165             category.setName( daoUtil.getString( ++nParam ) );
166             category.setDescription( daoUtil.getString( ++nParam ) );
167             category.setIconContent( daoUtil.getBytes( ++nParam ) );
168             category.setIconMimeType( daoUtil.getString( ++nParam ) );
169             category.setWorkgroup( daoUtil.getString( ++nParam ) );
170         }
171 
172         daoUtil.free(  );
173 
174         return category;
175     }
176 
177     /**
178      * Load the data of Category for an event from the table
179      * @param nIdEvent The identifier of the event
180      * @param plugin Plugin
181      * @return The Instance of the object Category
182      */
183     public Collection<Category> selectByEvent( int nIdEvent, Plugin plugin )
184     {
185         int nParam;
186         Collection<Category> listCategory = new ArrayList<Category>(  );
187         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_EVENT_CATEGORIES, plugin );
188         daoUtil.setInt( 1, nIdEvent );
189         daoUtil.executeQuery(  );
190 
191         while ( daoUtil.next(  ) )
192         {
193             nParam = 0;
194 
195             Category category = new Category(  );
196             category.setId( daoUtil.getInt( ++nParam ) );
197             category.setName( daoUtil.getString( ++nParam ) );
198             category.setDescription( daoUtil.getString( ++nParam ) );
199             category.setIconContent( daoUtil.getBytes( ++nParam ) );
200             category.setIconMimeType( daoUtil.getString( ++nParam ) );
201             category.setWorkgroup( daoUtil.getString( ++nParam ) );
202 
203             listCategory.add( category );
204         }
205 
206         daoUtil.free(  );
207 
208         return listCategory;
209     }
210 
211     /**
212      * Delete a record from the table
213      * @param nIdCategory The identifier of the object Category
214      * @param plugin Plugin
215      */
216     public void delete( int nIdCategory, Plugin plugin )
217     {
218         int nParam = 0;
219         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
220         daoUtil.setInt( ++nParam, nIdCategory );
221         daoUtil.executeUpdate(  );
222         daoUtil.free(  );
223     }
224 
225     /**
226      * Update the record in the table
227      * @param category The instance of the Category to update
228      * @param plugin Plugin
229      */
230     public void store( Category category, Plugin plugin )
231     {
232         int nParam = 0;
233         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
234         daoUtil.setString( ++nParam, category.getName(  ) );
235         daoUtil.setString( ++nParam, category.getDescription(  ) );
236         daoUtil.setBytes( ++nParam, category.getIconContent(  ) );
237         daoUtil.setString( ++nParam, category.getIconMimeType(  ) );
238         daoUtil.setString( ++nParam, category.getWorkgroup(  ) );
239         daoUtil.setInt( ++nParam, category.getId(  ) );
240 
241         daoUtil.executeUpdate(  );
242         daoUtil.free(  );
243     }
244 
245     /**
246      * Load the data of Category from the table
247      * @param strCategoryName The name of the category
248      * @param plugin Plugin
249      * @return The Collection of Category (empty collection is no result)
250      */
251     public Collection<Category> selectByName( String strCategoryName, Plugin plugin )
252     {
253         int nParam;
254         Collection<Category> listCategory = new ArrayList<Category>(  );
255         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_NAME, plugin );
256         daoUtil.setString( 1, strCategoryName );
257         daoUtil.executeQuery(  );
258 
259         while ( daoUtil.next(  ) )
260         {
261             nParam = 0;
262 
263             Category category = new Category(  );
264             category.setId( daoUtil.getInt( ++nParam ) );
265             category.setName( strCategoryName );
266             category.setDescription( daoUtil.getString( ++nParam ) );
267             category.setIconContent( daoUtil.getBytes( ++nParam ) );
268             category.setIconMimeType( daoUtil.getString( ++nParam ) );
269             category.setWorkgroup( daoUtil.getString( ++nParam ) );
270 
271             listCategory.add( category );
272         }
273 
274         daoUtil.free(  );
275 
276         return listCategory;
277     }
278 
279     /**
280      * Delete a link between category and event
281      * @param nIdCategory The identifier of the object Category
282      * @param nIdEvent The id of document
283      * @param plugin Plugin
284      */
285     public void deleteLinkCategoryEvent( int nIdCategory, int nIdEvent, Plugin plugin )
286     {
287         int nParam = 0;
288         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_LINK_CATEGORY_CALENDAR, plugin );
289         daoUtil.setInt( ++nParam, nIdEvent );
290         daoUtil.setInt( ++nParam, nIdCategory );
291         daoUtil.executeUpdate(  );
292         daoUtil.free(  );
293     }
294 
295     /**
296      * Select a list of Id Events for a specified category
297      * @param nIdCategory The category name
298      * @param plugin Plugin
299      * @return The array of Id Event
300      */
301     public int[] selectAllIdEvent( int nIdCategory, Plugin plugin )
302     {
303         Collection<Integer> listIdEvent = new ArrayList<Integer>(  );
304         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ID_EVENT, plugin );
305         daoUtil.setInt( 1, nIdCategory );
306         daoUtil.executeQuery(  );
307 
308         while ( daoUtil.next(  ) )
309         {
310             listIdEvent.add( daoUtil.getInt( 1 ) );
311         }
312 
313         daoUtil.free(  );
314 
315         // Convert ArrayList to Int[]
316         int[] arrayIdEvent = new int[listIdEvent.size(  )];
317         int i = 0;
318 
319         for ( Integer nIdDocument : listIdEvent )
320         {
321             arrayIdEvent[i++] = nIdDocument.intValue(  );
322         }
323 
324         return arrayIdEvent;
325     }
326 
327     /**
328      * Delete all links for a category
329      * @param nIdCategory The identifier of the object Category
330      * @param plugin Plugin
331      */
332     public void deleteLinksCategory( int nIdCategory, Plugin plugin )
333     {
334         int nParam = 0;
335         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_LINKS_CATEGORY, plugin );
336         daoUtil.setInt( ++nParam, nIdCategory );
337         daoUtil.executeUpdate(  );
338         daoUtil.free(  );
339     }
340 
341     /**
342      * Return the number of Events linked to a category
343      * @param nIdCategory The category name
344      * @param plugin Plugin
345      * @return count of id event
346      */
347     public int selectCountIdEvents( int nIdCategory, Plugin plugin )
348     {
349         int nCountEventsId = -1;
350         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_COUNT_OF_EVENT_ID, plugin );
351         daoUtil.setInt( 1, nIdCategory );
352         daoUtil.executeQuery(  );
353 
354         while ( daoUtil.next(  ) )
355         {
356             nCountEventsId = daoUtil.getInt( 1 );
357         }
358 
359         daoUtil.free(  );
360 
361         return nCountEventsId;
362     }
363 
364     /**
365      * Return the image resource corresponding to the category id
366      * @param nCategoryId The identifier of Category object
367      * @param plugin Plugin
368      * @return The image resource
369      */
370     public ImageResource loadImageResource( int nCategoryId, Plugin plugin )
371     {
372         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_RESOURCE_IMAGE, plugin );
373         daoUtil.setInt( 1, nCategoryId );
374         daoUtil.executeQuery(  );
375 
376         ImageResource image = null;
377 
378         if ( daoUtil.next(  ) )
379         {
380             image = new ImageResource(  );
381             image.setImage( daoUtil.getBytes( 1 ) );
382             image.setMimeType( daoUtil.getString( 2 ) );
383         }
384 
385         daoUtil.free(  );
386 
387         return image;
388     }
389 }