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.portal.service.util.AppLogService;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  
40  import java.util.ArrayList;
41  import java.util.List;
42  
43  
44  /**
45   * This class provides Data Access methods for Response objects
46   */
47  public final class ResponseDAO implements IResponseDAO
48  {
49      // Constants
50      private static final String EMPTY_STRING = "";
51      private static final String SQL_QUERY_NEW_PK = "SELECT MAX( id_response ) FROM digglike_response";
52      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT " +
53          "resp.id_response,resp.id_digg_submit,resp.response_value,type.class_name,ent.id_entry,ent.title,ent.id_type,ent.show_in_digg_submit_lis,res.id_resource_image " +
54          "FROM digglike_response resp,digglike_entry ent,digglike_entry_type type  " +
55          "WHERE resp.id_response=? and resp.id_entry =ent.id_entry and ent.id_type=type.id_type ";
56      private static final String SQL_QUERY_INSERT = "INSERT INTO digglike_response ( " +
57          "id_response,id_digg_submit,response_value,id_entry,id_resource_image) VALUES(?,?,?,?,?)";
58      private static final String SQL_QUERY_DELETE = "DELETE FROM digglike_response WHERE id_response = ? ";
59      private static final String SQL_QUERY_UPDATE = "UPDATE  digglike_response SET " +
60          "id_response=?,id_digg_submit=?,response_value=?,id_entry=?,id_resource_image=? WHERE id_response=?";
61      private static final String SQL_QUERY_SELECT_RESPONSE_BY_FILTER = "SELECT " +
62          "resp.id_response,resp.id_digg_submit,resp.response_value,type.class_name,ent.id_entry,ent.title,ent.id_type,ent.show_in_digg_submit_list,resp.id_resource_image " +
63          "FROM digglike_response resp,digglike_entry ent,digglike_entry_type type  " +
64          "WHERE resp.id_entry =ent.id_entry and ent.id_type=type.id_type ";
65      private static final String SQL_FILTER_ID_DIGG_SUBMIT = " AND resp.id_digg_submit = ? ";
66      private static final String SQL_FILTER_ID_ENTRY = " AND resp.id_entry = ? ";
67      private static final String SQL_ORDER_BY_ID_RESPONSE = " ORDER BY id_response ";
68  
69      /**
70       * Generates a new primary key
71       *
72       * @param plugin the plugin
73       * @return The new primary key
74       */
75      private int newPrimaryKey( Plugin plugin )
76      {
77          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
78          daoUtil.executeQuery(  );
79  
80          int nKey;
81  
82          if ( !daoUtil.next(  ) )
83          {
84              // if the table is empty
85              nKey = 1;
86          }
87  
88          nKey = daoUtil.getInt( 1 ) + 1;
89          daoUtil.free(  );
90  
91          return nKey;
92      }
93  
94      /**
95       * Insert a new record in the table.
96       *
97       * @param response instance of the Response object to insert
98       * @param plugin the plugin
99       */
100     public void insert( Response response, Plugin plugin )
101     {
102         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
103         response.setIdResponse( newPrimaryKey( plugin ) );
104         daoUtil.setInt( 1, response.getIdResponse(  ) );
105         daoUtil.setInt( 2, response.getDiggSubmit(  ).getIdDiggSubmit(  ) );
106         daoUtil.setString( 3, response.getValueResponse(  ) );
107         daoUtil.setInt( 4, response.getEntry(  ).getIdEntry(  ) );
108 
109         if ( response.getIdImageResource(  ) != null )
110         {
111             daoUtil.setInt( 5, response.getIdImageResource(  ) );
112         }
113         else
114         {
115             daoUtil.setIntNull( 5 );
116         }
117 
118         daoUtil.executeUpdate(  );
119         daoUtil.free(  );
120     }
121 
122     /**
123      * Load the data of the response from the table
124      *
125      * @param nIdResponse The identifier of the response
126      * @param plugin the plugin
127      * @return the instance of the response
128      */
129     public Response load( int nIdResponse, Plugin plugin )
130     {
131         boolean bException = false;
132         Response response = null;
133         IEntry entry = null;
134         EntryType entryType = null;
135         DiggSubmit diggSubmit = null;
136         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin );
137         daoUtil.setInt( 1, nIdResponse );
138         daoUtil.executeQuery(  );
139 
140         if ( daoUtil.next(  ) )
141         {
142             response = new Response(  );
143             response.setIdResponse( daoUtil.getInt( 1 ) );
144 
145             diggSubmit = new DiggSubmit(  );
146             diggSubmit.setIdDiggSubmit( daoUtil.getInt( 2 ) );
147             response.setDiggSubmit( diggSubmit );
148 
149             response.setValueResponse( daoUtil.getString( 3 ) );
150             entryType = new EntryType(  );
151             entryType.setClassName( daoUtil.getString( 4 ) );
152             entryType.setIdType( daoUtil.getInt( 7 ) );
153 
154             try
155             {
156                 entry = (IEntry) Class.forName( entryType.getClassName(  ) ).newInstance(  );
157             }
158             catch ( ClassNotFoundException e )
159             {
160                 //  class doesn't exist
161                 AppLogService.error( e );
162                 bException = true;
163             }
164             catch ( InstantiationException e )
165             {
166                 // Class is abstract or is an  interface or haven't accessible builder
167                 AppLogService.error( e );
168                 bException = true;
169             }
170             catch ( IllegalAccessException e )
171             {
172                 // can't access to rhe class
173                 AppLogService.error( e );
174                 bException = true;
175             }
176 
177             if ( bException )
178             {
179                 return null;
180             }
181 
182             entry.setEntryType( entryType );
183             entry.setIdEntry( daoUtil.getInt( 5 ) );
184             entry.setTitle( daoUtil.getString( 6 ) );
185             entry.setShowInDiggSubmitList( daoUtil.getBoolean( 8 ) );
186             response.setEntry( entry );
187 
188             if ( daoUtil.getObject( 9 ) != null )
189             {
190                 response.setIdImageResource( daoUtil.getInt( 9 ) );
191             }
192         }
193 
194         daoUtil.free(  );
195 
196         return response;
197     }
198 
199     /**
200      * Delete  response   whose identifier is specified in parameter
201      *
202      * @param nIdResponse The identifier of the response
203      * @param plugin the plugin
204      */
205     public void delete( int nIdResponse, Plugin plugin )
206     {
207         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
208         daoUtil.setInt( 1, nIdResponse );
209         daoUtil.executeUpdate(  );
210         daoUtil.free(  );
211     }
212 
213     /**
214      * Update the the response in the table
215      *
216      * @param response instance of the response object to update
217      * @param plugin the plugin
218      */
219     public void store( Response response, Plugin plugin )
220     {
221         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
222 
223         daoUtil.setInt( 1, response.getIdResponse(  ) );
224         daoUtil.setInt( 2, response.getDiggSubmit(  ).getIdDiggSubmit(  ) );
225         daoUtil.setString( 3, response.getValueResponse(  ) );
226         daoUtil.setInt( 4, response.getEntry(  ).getIdEntry(  ) );
227 
228         if ( response.getIdImageResource(  ) != null )
229         {
230             daoUtil.setInt( 5, response.getIdImageResource(  ) );
231         }
232         else
233         {
234             daoUtil.setIntNull( 5 );
235         }
236 
237         daoUtil.setInt( 6, response.getIdResponse(  ) );
238         daoUtil.executeUpdate(  );
239         daoUtil.free(  );
240     }
241 
242     /**
243      * Load the data of all the response who verify the filter and returns them in a  list
244      * @param filter the filter
245      * @param plugin the plugin
246      * @return  the list of response
247      */
248     public List<Response> selectListByFilter( SubmitFilter filter, Plugin plugin )
249     {
250         boolean bException = false;
251         List<Response> responseList = new ArrayList<Response>(  );
252         Response response;
253         IEntry entry = null;
254         EntryType entryType = null;
255 
256         DiggSubmit diggSubmit = null;
257 
258         String strSQL = SQL_QUERY_SELECT_RESPONSE_BY_FILTER;
259         strSQL += ( ( filter.containsIdDiggSubmit(  ) ) ? SQL_FILTER_ID_DIGG_SUBMIT : EMPTY_STRING );
260         strSQL += ( ( filter.containsIdEntry(  ) ) ? SQL_FILTER_ID_ENTRY : EMPTY_STRING );
261         strSQL += SQL_ORDER_BY_ID_RESPONSE;
262 
263         DAOUtil daoUtil = new DAOUtil( strSQL, plugin );
264         int nIndex = 1;
265 
266         if ( filter.containsIdDiggSubmit(  ) )
267         {
268             daoUtil.setInt( nIndex, filter.getIdDiggSubmit(  ) );
269             nIndex++;
270         }
271 
272         if ( filter.containsIdEntry(  ) )
273         {
274             daoUtil.setInt( nIndex, filter.getIdEntry(  ) );
275             nIndex++;
276         }
277 
278         daoUtil.executeQuery(  );
279 
280         while ( daoUtil.next(  ) )
281         {
282             response = new Response(  );
283             response.setIdResponse( daoUtil.getInt( 1 ) );
284 
285             diggSubmit = new DiggSubmit(  );
286             diggSubmit.setIdDiggSubmit( daoUtil.getInt( 2 ) );
287             response.setDiggSubmit( diggSubmit );
288 
289             response.setValueResponse( daoUtil.getString( 3 ) );
290             entryType = new EntryType(  );
291             entryType.setClassName( daoUtil.getString( 4 ) );
292             entryType.setIdType( daoUtil.getInt( 7 ) );
293 
294             try
295             {
296                 entry = (IEntry) Class.forName( entryType.getClassName(  ) ).newInstance(  );
297             }
298             catch ( ClassNotFoundException e )
299             {
300                 //  class doesn't exist
301                 AppLogService.error( e );
302                 bException = true;
303             }
304             catch ( InstantiationException e )
305             {
306                 // Class is abstract or is an  interface or haven't accessible builder
307                 AppLogService.error( e );
308                 bException = true;
309             }
310             catch ( IllegalAccessException e )
311             {
312                 // can't access to rhe class
313                 AppLogService.error( e );
314                 bException = true;
315             }
316 
317             if ( bException )
318             {
319                 return null;
320             }
321 
322             entry.setEntryType( entryType );
323             entry.setIdEntry( daoUtil.getInt( 5 ) );
324             entry.setTitle( daoUtil.getString( 6 ) );
325 
326             entry.setShowInDiggSubmitList( daoUtil.getBoolean( 8 ) );
327 
328             response.setEntry( entry );
329 
330             if ( daoUtil.getObject( 9 ) != null )
331             {
332                 response.setIdImageResource( daoUtil.getInt( 9 ) );
333             }
334 
335             responseList.add( response );
336         }
337 
338         daoUtil.free(  );
339 
340         return responseList;
341     }
342 }