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   * This class provides Data Access methods for  objects DiggSubmitDAO
45   */
46  public final class DiggSubmitTypeDAO implements IDiggSubmitTypeDAO
47  {
48      // Constants
49      private static final String SQL_ORDER_BY_NAME = "ORDER BY t.name";
50      private static final String SQL_QUERY_NEW_PK = "SELECT MAX( id_type ) FROM digglike_digg_submit_type";
51      private static final String SQL_SELECT_DIGG_SUBMIT_TYPE = "SELECT t.id_type, t.name, t.color, t.parameterizable, t.id_xsl,t.id_resource_image FROM digglike_digg_submit_type t ";
52      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = SQL_SELECT_DIGG_SUBMIT_TYPE + "WHERE t.id_type=? ";
53      private static final String SQL_QUERY_INSERT = "INSERT INTO digglike_digg_submit_type ( id_type,name,color, " +
54          "parameterizable, id_xsl,id_resource_image ) VALUES(?,?,?,?,?,?)";
55      private static final String SQL_QUERY_DELETE = "DELETE FROM digglike_digg_submit_type WHERE id_type = ? ";
56      private static final String SQL_QUERY_UPDATE = "UPDATE digglike_digg_submit_type SET " +
57          "name=?,color=?,parameterizable=?, id_xsl=?,id_resource_image=? WHERE id_type=? ";
58      private static final String SQL_QUERY_FIND_ALL = SQL_SELECT_DIGG_SUBMIT_TYPE + SQL_ORDER_BY_NAME;
59      private static final String SQL_QUERY_FIND_BY_ID_DIGG = SQL_SELECT_DIGG_SUBMIT_TYPE +
60          ",digglike_digg_digg_submit_type dt WHERE t.id_type=dt.id_type AND dt.id_digg= ? " + SQL_ORDER_BY_NAME;
61      private static final String SQL_QUERY_COUNT_NUMBER_OF_DIGG_ASSOCIATE_TO_THE_DIGG_SUBMIT_TYPE = "select COUNT(id_digg) " +
62          " FROM digglike_digg_digg_submit_type WHERE id_type=? ";
63      private static final String SQL_QUERY_DELETE_ASSOCIATION_DIGG = "DELETE FROM digglike_digg_digg_submit_type WHERE id_digg = ? and id_type= ? ";
64      private static final String SQL_QUERY_INSERT_ASSOCIATION_DIGG = "INSERT INTO digglike_digg_digg_submit_type(id_digg,id_type) VALUES(?,?) ";
65  
66      /**
67      * Generates a new primary key
68      *
69      * @param plugin the plugin
70      * @return The new primary key
71      */
72      private int newPrimaryKey( Plugin plugin )
73      {
74          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
75          daoUtil.executeQuery(  );
76  
77          int nKey;
78  
79          if ( !daoUtil.next(  ) )
80          {
81              // if the table is empty
82              nKey = 1;
83          }
84  
85          nKey = daoUtil.getInt( 1 ) + 1;
86          daoUtil.free(  );
87  
88          return nKey;
89      }
90  
91      /**
92       * Insert a new record in the table.
93       *
94       * @param diggSubmitType instance of the Digg Submit Type object to insert
95       * @param plugin the plugin
96       * @return the id of the new Digg
97       */
98      public int insert( DiggSubmitType diggSubmitType, Plugin plugin )
99      {
100         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
101         diggSubmitType.setIdType( newPrimaryKey( plugin ) );
102 
103         daoUtil.setInt( 1, diggSubmitType.getIdType(  ) );
104         daoUtil.setString( 2, diggSubmitType.getName(  ) );
105         daoUtil.setString( 3, diggSubmitType.getColor(  ) );
106         daoUtil.setBoolean( 4, diggSubmitType.getParameterizableInFO(  ) );
107         daoUtil.setInt( 5, diggSubmitType.getIdXSLStyleSheet(  ) );
108 
109         if ( diggSubmitType.getIdImageResource(  ) != null )
110         {
111             daoUtil.setInt( 6, diggSubmitType.getIdImageResource(  ) );
112         }
113         else
114         {
115             daoUtil.setIntNull( 6 );
116         }
117 
118         daoUtil.executeUpdate(  );
119         daoUtil.free(  );
120 
121         return diggSubmitType.getIdType(  );
122     }
123 
124     /**
125      * Load the data of the diggSubmitType from the table
126      *
127      * @param nIdDiggSubmitType The identifier of the formResponse
128      * @param plugin the plugin
129      * @return the instance of the diggSubmitType
130      */
131     public DiggSubmitType load( int nIdDiggSubmitType, Plugin plugin )
132     {
133         DiggSubmitType diggSubmitType = null;
134         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin );
135         daoUtil.setInt( 1, nIdDiggSubmitType );
136         daoUtil.executeQuery(  );
137 
138         if ( daoUtil.next(  ) )
139         {
140             diggSubmitType = getRow( daoUtil );
141         }
142 
143         daoUtil.free(  );
144 
145         return diggSubmitType;
146     }
147 
148     /**
149      * Load the list of the diggSubmitType from the table
150      *The images are not loaded in the DiggSubmitType Objects
151      * @param plugin the plugin
152      * @return the instance of the diggSubmitType
153      */
154     public List<DiggSubmitType> selectList( Plugin plugin )
155     {
156         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_ALL, plugin );
157         daoUtil.executeQuery(  );
158 
159         List<DiggSubmitType> list = new ArrayList<DiggSubmitType>(  );
160 
161         while ( daoUtil.next(  ) )
162         {
163             list.add( getRow( daoUtil ) );
164         }
165 
166         daoUtil.free(  );
167 
168         return list;
169     }
170 
171     /**
172      * Delete   the digg submit type whose identifier is specified in parameter
173      *
174      * @param nIdDiggSubmitType The identifier of the digg submit
175      * @param plugin the plugin
176      */
177     public void delete( int nIdDiggSubmitType, Plugin plugin )
178     {
179         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
180         daoUtil.setInt( 1, nIdDiggSubmitType );
181         daoUtil.executeUpdate(  );
182         daoUtil.free(  );
183     }
184 
185     /**
186      * Update the the diggSubmitType in the table
187      *
188      * @param diggSubmitType instance of the diggSubmit object to update
189      * @param plugin the plugin
190      */
191     public void store( DiggSubmitType diggSubmitType, Plugin plugin )
192     {
193         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
194 
195         daoUtil.setString( 1, diggSubmitType.getName(  ) );
196         daoUtil.setString( 2, diggSubmitType.getColor(  ) );
197         daoUtil.setBoolean( 3, diggSubmitType.getParameterizableInFO(  ) );
198         daoUtil.setInt( 4, diggSubmitType.getIdXSLStyleSheet(  ) );
199 
200         if ( diggSubmitType.getIdImageResource(  ) != null )
201         {
202             daoUtil.setInt( 5, diggSubmitType.getIdImageResource(  ) );
203         }
204         else
205         {
206             daoUtil.setIntNull( 5 );
207         }
208 
209         daoUtil.setInt( 6, diggSubmitType.getIdType(  ) );
210 
211         daoUtil.executeUpdate(  );
212         daoUtil.free(  );
213     }
214 
215     /**
216      * return DiggSubmitType
217      * @param daoUtil daoUtil
218      * @return DiggSubmitType
219      */
220     private DiggSubmitType getRow( DAOUtil daoUtil )
221     {
222         DiggSubmitType diggSubmitType = null;
223         diggSubmitType = new DiggSubmitType(  );
224         diggSubmitType.setIdType( daoUtil.getInt( 1 ) );
225         diggSubmitType.setName( daoUtil.getString( 2 ) );
226         diggSubmitType.setColor( daoUtil.getString( 3 ) );
227         diggSubmitType.setParameterizableInFO( daoUtil.getBoolean( 4 ) );
228         diggSubmitType.setIdXSLStyleSheet( daoUtil.getInt( 5 ) );
229 
230         if ( daoUtil.getObject( 6 ) != null )
231         {
232             diggSubmitType.setIdImageResource( daoUtil.getInt( 6 ) );
233         }
234 
235         return diggSubmitType;
236     }
237 
238     /**
239      * Load the list of the diggSubmitType from the table
240      *The images are not loaded in the DiggSubmitType Objects
241      * @param plugin the plugin
242      * @return the instance of the diggSubmitType
243      */
244     public List<DiggSubmitType> selectListByIdDigg( int nIdDigg, Plugin plugin )
245     {
246         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_ID_DIGG, plugin );
247         daoUtil.setInt( 1, nIdDigg );
248         daoUtil.executeQuery(  );
249 
250         List<DiggSubmitType> list = new ArrayList<DiggSubmitType>(  );
251 
252         while ( daoUtil.next(  ) )
253         {
254             list.add( getRow( daoUtil ) );
255         }
256 
257         daoUtil.free(  );
258 
259         return list;
260     }
261 
262     /**
263      * true if there is a  digg associate to the digg submit type
264      * @param nIdType the key of the type
265      * @param plugin the plugin
266      * @return true if there is a digg associate to the type
267      */
268     public boolean isAssociateToDigg( int nIdType, Plugin plugin )
269     {
270         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_COUNT_NUMBER_OF_DIGG_ASSOCIATE_TO_THE_DIGG_SUBMIT_TYPE, plugin );
271         daoUtil.setInt( 1, nIdType );
272         daoUtil.executeQuery(  );
273 
274         if ( daoUtil.next(  ) )
275         {
276             if ( daoUtil.getInt( 1 ) != 0 )
277             {
278                 daoUtil.free(  );
279 
280                 return true;
281             }
282         }
283 
284         daoUtil.free(  );
285 
286         return false;
287     }
288 
289     /**
290     * {@inheritDoc}
291     */
292     @Override
293     public void deleteDiggAssociation( int nIdDigg, int nIdDiggSubmitType, Plugin plugin )
294     {
295         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_ASSOCIATION_DIGG, plugin );
296         daoUtil.setInt( 1, nIdDigg );
297         daoUtil.setInt( 2, nIdDiggSubmitType );
298 
299         daoUtil.executeUpdate(  );
300         daoUtil.free(  );
301     }
302 
303     /**
304     * {@inheritDoc}
305     */
306     @Override
307     public void insertDiggAssociation( int nIdDigg, int nIdDiggSubmitType, Plugin plugin )
308     {
309         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_ASSOCIATION_DIGG, plugin );
310         daoUtil.setInt( 1, nIdDigg );
311         daoUtil.setInt( 2, nIdDiggSubmitType );
312         daoUtil.executeUpdate(  );
313         daoUtil.free(  );
314     }
315 }