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.digglike.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.List;
41  
42  
43  /**
44   *
45   *class  CategoryDAO
46   *
47   */
48  public class CategoryDAO implements ICategoryDAO
49  {
50      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_category) FROM digglike_category";
51      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_category,title,color FROM digglike_category WHERE id_category=?";
52      private static final String SQL_QUERY_SELECT = "SELECT id_category,title,color FROM digglike_category ORDER BY title";
53      private static final String SQL_QUERY_INSERT = "INSERT INTO digglike_category (id_category,title,color )VALUES(?,?,?)";
54      private static final String SQL_QUERY_UPDATE = "UPDATE digglike_category SET id_category=?,title=?,color=? WHERE id_category=?";
55      private static final String SQL_QUERY_DELETE = "DELETE FROM digglike_category WHERE id_category = ? ";
56      private static final String SQL_QUERY_COUNT_NUMBER_OF_DIGG_ASSOCIATE_TO_THE_CATEGORY = "select COUNT(id_category) " +
57          " FROM digglike_digg_category WHERE id_category=? ";
58      private static final String SQL_QUERY_SELECT_BY_ID_DIGG = "SELECT c.id_category,c.title,c.color FROM digglike_category c,digglike_digg_category dc " +
59          "WHERE c.id_category=dc.id_category AND dc.id_digg=? ORDER BY title";
60      private static final String SQL_QUERY_DELETE_ASSOCIATED_CATEGORIE = "DELETE FROM digglike_digg_category WHERE id_digg = ? and id_category= ? ";
61      private static final String SQL_QUERY_INSERT_ASSOCIATED_CATEGORY = "INSERT INTO digglike_digg_category(id_digg,id_category) VALUES(?,?) ";
62  
63      /**
64       * Generates a new primary key
65       *
66       * @param plugin the plugin
67       * @return The new primary key
68       */
69      public int newPrimaryKey( Plugin plugin )
70      {
71          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
72          daoUtil.executeQuery(  );
73  
74          int nKey;
75  
76          if ( !daoUtil.next(  ) )
77          {
78              // if the table is empty
79              nKey = 1;
80          }
81  
82          nKey = daoUtil.getInt( 1 ) + 1;
83          daoUtil.free(  );
84  
85          return nKey;
86      }
87  
88      /**
89       * Insert a new record in the table.
90       *
91       * @param category instance of the Category object to insert
92       * @param plugin the plugin
93       */
94      public void insert( Category category, Plugin plugin )
95      {
96          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
97          category.setIdCategory( newPrimaryKey( plugin ) );
98          daoUtil.setInt( 1, category.getIdCategory(  ) );
99          daoUtil.setString( 2, category.getTitle(  ) );
100         daoUtil.setString( 3, category.getColor(  ) );
101         daoUtil.executeUpdate(  );
102         daoUtil.free(  );
103     }
104 
105     /**
106      * update record in the table.
107      *
108      * @param category instance of the Category object to update
109      * @param plugin the plugin
110      */
111     public void store( Category category, Plugin plugin )
112     {
113         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
114 
115         daoUtil.setInt( 1, category.getIdCategory(  ) );
116         daoUtil.setString( 2, category.getTitle(  ) );
117         daoUtil.setString( 3, category.getColor(  ) );
118         daoUtil.setInt( 4, category.getIdCategory(  ) );
119         daoUtil.executeUpdate(  );
120         daoUtil.free(  );
121     }
122 
123     /**
124      * Load the data of the category from the table
125      *
126      * @param idKey The identifier of the category
127      * @param plugin the plugin
128      * @return the instance of the Category
129      */
130     public Category load( int idKey, Plugin plugin )
131     {
132         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin );
133         daoUtil.setInt( 1, idKey );
134         daoUtil.executeQuery(  );
135 
136         Category category = null;
137 
138         if ( daoUtil.next(  ) )
139         {
140             category = new Category(  );
141             category.setIdCategory( daoUtil.getInt( 1 ) );
142             category.setTitle( daoUtil.getString( 2 ) );
143             category.setColor( daoUtil.getString( 3 ) );
144         }
145 
146         daoUtil.free(  );
147 
148         return category;
149     }
150 
151     /**
152      * Load the data of all category returns them in a  list
153      * @param plugin the plugin
154      * @return  the list of category
155      */
156     public List<Category> select( Plugin plugin )
157     {
158         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
159         daoUtil.executeQuery(  );
160 
161         Category category = null;
162         List<Category> listCategory = new ArrayList<Category>(  );
163 
164         while ( daoUtil.next(  ) )
165         {
166             category = new Category(  );
167             category.setIdCategory( daoUtil.getInt( 1 ) );
168             category.setTitle( daoUtil.getString( 2 ) );
169             category.setColor( daoUtil.getString( 3 ) );
170             listCategory.add( category );
171         }
172 
173         daoUtil.free(  );
174 
175         return listCategory;
176     }
177 
178     /**
179      * Delete a record from the table
180      *
181      * @param  nIdCategory The identifier of the category
182      * @param plugin the plugin
183      */
184     public void delete( int nIdCategory, Plugin plugin )
185     {
186         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
187         daoUtil.setInt( 1, nIdCategory );
188         daoUtil.executeUpdate(  );
189         daoUtil.free(  );
190     }
191 
192     /**
193      * true if there is a  digg associate to the category
194      * @param nIdCategory the key of the category
195      * @param plugin the plugin
196      * @return true if there is a digg associate to the category
197      */
198     public boolean isAssociateToDigg( int nIdCategory, Plugin plugin )
199     {
200         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_COUNT_NUMBER_OF_DIGG_ASSOCIATE_TO_THE_CATEGORY, plugin );
201         daoUtil.setInt( 1, nIdCategory );
202         daoUtil.executeQuery(  );
203 
204         if ( daoUtil.next(  ) )
205         {
206             if ( daoUtil.getInt( 1 ) != 0 )
207             {
208                 daoUtil.free(  );
209 
210                 return true;
211             }
212         }
213 
214         daoUtil.free(  );
215 
216         return false;
217     }
218 
219     @Override
220     public List<Category> select( int nIdDigg, Plugin plugin )
221     {
222         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_ID_DIGG, plugin );
223         daoUtil.setInt( 1, nIdDigg );
224         daoUtil.executeQuery(  );
225 
226         Category category = null;
227         List<Category> listCategory = new ArrayList<Category>(  );
228 
229         while ( daoUtil.next(  ) )
230         {
231             category = new Category(  );
232             category.setIdCategory( daoUtil.getInt( 1 ) );
233             category.setTitle( daoUtil.getString( 2 ) );
234             category.setColor( daoUtil.getString( 3 ) );
235             listCategory.add( category );
236         }
237 
238         daoUtil.free(  );
239 
240         return listCategory;
241     }
242 
243     /**
244      * Delete an association between digg and categories
245      *
246      * @param nIdDigg The identifier of the digg
247      * @param nIdCategory The identifier of the category
248      * @param plugin the plugin
249      */
250     public void deleteDiggAssociation( int nIdDigg, int nIdCategory, Plugin plugin )
251     {
252         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_ASSOCIATED_CATEGORIE, plugin );
253         daoUtil.setInt( 1, nIdDigg );
254         daoUtil.setInt( 2, nIdCategory );
255 
256         daoUtil.executeUpdate(  );
257         daoUtil.free(  );
258     }
259 
260     /**
261      * insert an association between  digg and a  category
262      *
263      * @param nIdDigg The identifier of the category
264      * @param nIdCategory The identifier of the category
265      * @param plugin the plugin
266      */
267     public void insertDiggAssociation( int nIdDigg, int nIdCategory, Plugin plugin )
268     {
269         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_ASSOCIATED_CATEGORY, plugin );
270         daoUtil.setInt( 1, nIdDigg );
271         daoUtil.setInt( 2, nIdCategory );
272         daoUtil.executeUpdate(  );
273         daoUtil.free(  );
274     }
275 }