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.appstore.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.util.ArrayList;
40  import java.util.Collection;
41  
42  /**
43   * This class provides Data Access methods for Category objects
44   */
45  public final class CategoryDAO implements ICategoryDAO
46  {
47      // Constants
48      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_category ) FROM appstore_category";
49      private static final String SQL_QUERY_SELECT = "SELECT id_category, name, id_order FROM appstore_category WHERE id_category = ?";
50      private static final String SQL_QUERY_INSERT = "INSERT INTO appstore_category ( id_category, name, id_order ) VALUES ( ?, ?, ? ) ";
51      private static final String SQL_QUERY_DELETE = "DELETE FROM appstore_category WHERE id_category = ? ";
52      private static final String SQL_QUERY_UPDATE = "UPDATE appstore_category SET id_category = ?, name = ?, id_order = ? WHERE id_category = ?";
53      private static final String SQL_QUERY_SELECTALL = "SELECT id_category, name, id_order FROM appstore_category";
54  
55      /**
56       * Generates a new primary key
57       * 
58       * @param plugin
59       *            The Plugin
60       * @return The new primary key
61       */
62      public int newPrimaryKey( Plugin plugin )
63      {
64          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
65          daoUtil.executeQuery( );
66  
67          int nKey;
68  
69          if ( !daoUtil.next( ) )
70          {
71              // if the table is empty
72              nKey = 1;
73          }
74  
75          nKey = daoUtil.getInt( 1 ) + 1;
76          daoUtil.free( );
77  
78          return nKey;
79      }
80  
81      /**
82       * Insert a new record in the table.
83       * 
84       * @param category
85       *            instance of the Category object to insert
86       * @param plugin
87       *            The plugin
88       */
89      public void insert( Category category, Plugin plugin )
90      {
91          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
92  
93          category.setId( newPrimaryKey( plugin ) );
94  
95          daoUtil.setInt( 1, category.getId( ) );
96          daoUtil.setString( 2, category.getName( ) );
97          daoUtil.setInt( 3, category.getOrder( ) );
98  
99          daoUtil.executeUpdate( );
100         daoUtil.free( );
101     }
102 
103     /**
104      * Load the data of the category from the table
105      * 
106      * @param nId
107      *            The identifier of the category
108      * @param plugin
109      *            The plugin
110      * @return the instance of the Category
111      */
112     public Category load( int nId, Plugin plugin )
113     {
114         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
115         daoUtil.setInt( 1, nId );
116         daoUtil.executeQuery( );
117 
118         Category category = null;
119 
120         if ( daoUtil.next( ) )
121         {
122             category = new Category( );
123 
124             category.setId( daoUtil.getInt( 1 ) );
125             category.setName( daoUtil.getString( 2 ) );
126             category.setOrder( daoUtil.getInt( 3 ) );
127         }
128 
129         daoUtil.free( );
130 
131         return category;
132     }
133 
134     /**
135      * Delete a record from the table
136      * 
137      * @param nCategoryId
138      *            The identifier of the category
139      * @param plugin
140      *            The plugin
141      */
142     public void delete( int nCategoryId, Plugin plugin )
143     {
144         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
145         daoUtil.setInt( 1, nCategoryId );
146         daoUtil.executeUpdate( );
147         daoUtil.free( );
148     }
149 
150     /**
151      * Update the record in the table
152      * 
153      * @param category
154      *            The reference of the category
155      * @param plugin
156      *            The plugin
157      */
158     public void store( Category category, Plugin plugin )
159     {
160         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
161 
162         daoUtil.setInt( 1, category.getId( ) );
163         daoUtil.setString( 2, category.getName( ) );
164         daoUtil.setInt( 3, category.getOrder( ) );
165         daoUtil.setInt( 4, category.getId( ) );
166 
167         daoUtil.executeUpdate( );
168         daoUtil.free( );
169     }
170 
171     /**
172      * Load the data of all the categorys and returns them as a collection
173      * 
174      * @param plugin
175      *            The plugin
176      * @return The Collection which contains the data of all the categorys
177      */
178     public Collection<Category> selectCategorysList( Plugin plugin )
179     {
180         Collection<Category> categoryList = new ArrayList<Category>( );
181         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
182         daoUtil.executeQuery( );
183 
184         while ( daoUtil.next( ) )
185         {
186             Category category = new Category( );
187 
188             category.setId( daoUtil.getInt( 1 ) );
189             category.setName( daoUtil.getString( 2 ) );
190             category.setOrder( daoUtil.getInt( 3 ) );
191 
192             categoryList.add( category );
193         }
194 
195         daoUtil.free( );
196 
197         return categoryList;
198     }
199 }