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 Entry objects
46   */
47  public final class EntryDAO implements IEntryDAO
48  {
49      // Constants
50      private static final String EMPTY_STRING = "";
51      private static final String SQL_QUERY_NEW_PK = "SELECT MAX( id_entry ) FROM digglike_entry";
52      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT ent.id_type,typ.title,typ.class_name," +
53          "ent.id_entry,ent.id_digg,digg.title,ent.title,ent.help_message," +
54          "ent.entry_comment,ent.mandatory,ent.pos,ent.default_value,ent.height,ent.width,ent.max_size_enter,ent.show_in_digg_submit_list " +
55          "FROM digglike_entry ent,digglike_entry_type typ,digglike_digg digg  WHERE ent.id_entry = ? and ent.id_type=typ.id_type and " +
56          "ent.id_digg=digg.id_digg";
57      private static final String SQL_QUERY_INSERT = "INSERT INTO digglike_entry ( " +
58          "id_entry,id_digg,id_type,title,help_message,entry_comment,mandatory," +
59          "pos,default_value,height,width,max_size_enter,show_in_digg_submit_list) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
60      private static final String SQL_QUERY_DELETE = "DELETE FROM digglike_entry WHERE id_entry = ? ";
61      private static final String SQL_QUERY_UPDATE = "UPDATE  digglike_entry SET " +
62          "id_entry=?,id_digg=?,id_type=?,title=?,help_message=?," +
63          "entry_comment=?,mandatory=?,pos=?,default_value=?,height=?,width=?,max_size_enter=?,show_in_digg_submit_list=? WHERE id_entry=?";
64      private static final String SQL_QUERY_SELECT_ENTRY_BY_FILTER = "SELECT ent.id_type,typ.title,typ.class_name," +
65          "ent.id_entry,ent.id_digg,ent.title,ent.help_message," +
66          "ent.entry_comment,ent.mandatory,ent.pos,ent.show_in_digg_submit_list " +
67          "FROM digglike_entry ent,digglike_entry_type typ WHERE ent.id_type=typ.id_type ";
68      private static final String SQL_QUERY_SELECT_NUMBER_ENTRY_BY_FILTER = "SELECT COUNT(ent.id_entry) " +
69          "FROM digglike_entry ent,digglike_entry_type typ WHERE ent.id_type=typ.id_type ";
70      private static final String SQL_QUERY_NEW_POSITION = "SELECT MAX(pos) " + "FROM digglike_entry ";
71      private static final String SQL_FILTER_ID_DIGG = " AND ent.id_digg = ? ";
72      private static final String SQL_ORDER_BY_POSITION = " ORDER BY ent.pos ";
73      private static final String SQL_QUERY_INSERT_VERIF_BY = "INSERT INTO digglike_entry_verify_by(id_entry,id_expression) VALUES(?,?) ";
74      private static final String SQL_QUERY_DELETE_VERIF_BY = "DELETE FROM digglike_entry_verify_by WHERE id_entry = ? and id_expression= ?";
75      private static final String SQL_QUERY_SELECT_REGULAR_EXPRESSION_BY_ID_ENTRY = "SELECT id_expression " +
76          " FROM digglike_entry_verify_by  where id_entry=?";
77      private static final String SQL_QUERY_COUNT_ENTRY_BY_ID_REGULAR_EXPRESSION = "SELECT COUNT(id_entry) " +
78          " FROM digglike_entry_verify_by where id_expression = ?";
79  
80      /**
81       * Generates a new primary key
82       *
83       * @param plugin the plugin
84       * @return The new primary key
85       */
86      private int newPrimaryKey( Plugin plugin )
87      {
88          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
89          daoUtil.executeQuery(  );
90  
91          int nKey;
92  
93          if ( !daoUtil.next(  ) )
94          {
95              // if the table is empty
96              nKey = 1;
97          }
98  
99          nKey = daoUtil.getInt( 1 ) + 1;
100         daoUtil.free(  );
101 
102         return nKey;
103     }
104 
105     /**
106      * Generates a new entry position
107      * @param plugin the plugin
108      * @return the new entry position
109      */
110     private int newPosition( Plugin plugin )
111     {
112         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_POSITION, plugin );
113         daoUtil.executeQuery(  );
114 
115         int nPos;
116 
117         if ( !daoUtil.next(  ) )
118         {
119             // if the table is empty
120             nPos = 1;
121         }
122 
123         nPos = daoUtil.getInt( 1 ) + 1;
124         daoUtil.free(  );
125 
126         return nPos;
127     }
128 
129     /**
130      * Insert a new record in the table.
131      *
132      * @param entry instance of the Entry object to insert
133      * @param plugin the plugin
134      * @return the id of the new entry
135      */
136     public int insert( IEntry entry, Plugin plugin )
137     {
138         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
139         entry.setIdEntry( newPrimaryKey( plugin ) );
140 
141         daoUtil.setInt( 1, entry.getIdEntry(  ) );
142         daoUtil.setInt( 2, entry.getDigg(  ).getIdDigg(  ) );
143         daoUtil.setInt( 3, entry.getEntryType(  ).getIdType(  ) );
144         daoUtil.setString( 4, entry.getTitle(  ) );
145         daoUtil.setString( 5, entry.getHelpMessage(  ) );
146         daoUtil.setString( 6, entry.getComment(  ) );
147         daoUtil.setBoolean( 7, entry.isMandatory(  ) );
148         daoUtil.setInt( 8, newPosition( plugin ) );
149         daoUtil.setString( 9, entry.getDefaultValue(  ) );
150         daoUtil.setInt( 10, entry.getHeight(  ) );
151         daoUtil.setInt( 11, entry.getWidth(  ) );
152         daoUtil.setInt( 12, entry.getMaxSizeEnter(  ) );
153         daoUtil.setBoolean( 13, entry.isShowInDiggSubmitList(  ) );
154 
155         daoUtil.executeUpdate(  );
156         daoUtil.free(  );
157 
158         return entry.getIdEntry(  );
159     }
160 
161     /**
162      * Load the data of the entry from the table
163      *
164      * @param nId The identifier of the entry
165      * @param plugin the plugin
166      * @return the instance of the Entry
167      */
168     public IEntry load( int nId, Plugin plugin )
169     {
170         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin );
171         daoUtil.setInt( 1, nId );
172         daoUtil.executeQuery(  );
173 
174         IEntry entry = null;
175         EntryType entryType = null;
176         Digg digg = null;
177 
178         if ( daoUtil.next(  ) )
179         {
180             entryType = new EntryType(  );
181             entryType.setIdType( daoUtil.getInt( 1 ) );
182             entryType.setTitle( daoUtil.getString( 2 ) );
183             entryType.setClassName( daoUtil.getString( 3 ) );
184 
185             try
186             {
187                 entry = (IEntry) Class.forName( entryType.getClassName(  ) ).newInstance(  );
188             }
189             catch ( ClassNotFoundException e )
190             {
191                 //  class doesn't exist
192                 AppLogService.error( e );
193 
194                 return null;
195             }
196             catch ( InstantiationException e )
197             {
198                 // Class is abstract or is an  interface or haven't accessible builder
199                 AppLogService.error( e );
200 
201                 return null;
202             }
203             catch ( IllegalAccessException e )
204             {
205                 // can't access to rhe class
206                 AppLogService.error( e );
207 
208                 return null;
209             }
210 
211             entry.setEntryType( entryType );
212             entry.setIdEntry( daoUtil.getInt( 4 ) );
213             // insert form
214             digg = new Digg(  );
215             digg.setIdDigg( daoUtil.getInt( 5 ) );
216             digg.setTitle( daoUtil.getString( 6 ) );
217             entry.setDigg( digg );
218 
219             entry.setTitle( daoUtil.getString( 7 ) );
220             entry.setHelpMessage( daoUtil.getString( 8 ) );
221             entry.setComment( daoUtil.getString( 9 ) );
222             entry.setMandatory( daoUtil.getBoolean( 10 ) );
223             entry.setPosition( daoUtil.getInt( 11 ) );
224             entry.setDefaultValue( daoUtil.getString( 12 ) );
225             entry.setHeight( daoUtil.getInt( 13 ) );
226             entry.setWidth( daoUtil.getInt( 14 ) );
227             entry.setMaxSizeEnter( daoUtil.getInt( 15 ) );
228             entry.setShowInDiggSubmitList( daoUtil.getBoolean( 16 ) );
229         }
230 
231         daoUtil.free(  );
232 
233         return entry;
234     }
235 
236     /**
237      * Delete a record from the table
238      *
239      * @param nIdEntry The identifier of the entry
240      * @param plugin the plugin
241      */
242     public void delete( int nIdEntry, Plugin plugin )
243     {
244         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
245         daoUtil.setInt( 1, nIdEntry );
246         daoUtil.executeUpdate(  );
247         daoUtil.free(  );
248     }
249 
250     /**
251      * Update the entry in the table
252      *
253      * @param entry instance of the Entry object to update
254      * @param plugin the plugin
255      */
256     public void store( IEntry entry, Plugin plugin )
257     {
258         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
259 
260         daoUtil.setInt( 1, entry.getIdEntry(  ) );
261         daoUtil.setInt( 2, entry.getDigg(  ).getIdDigg(  ) );
262         daoUtil.setInt( 3, entry.getEntryType(  ).getIdType(  ) );
263         daoUtil.setString( 4, entry.getTitle(  ) );
264         daoUtil.setString( 5, entry.getHelpMessage(  ) );
265         daoUtil.setString( 6, entry.getComment(  ) );
266         daoUtil.setBoolean( 7, entry.isMandatory(  ) );
267         daoUtil.setInt( 8, entry.getPosition(  ) );
268         daoUtil.setString( 9, entry.getDefaultValue(  ) );
269         daoUtil.setInt( 10, entry.getHeight(  ) );
270         daoUtil.setInt( 11, entry.getWidth(  ) );
271         daoUtil.setInt( 12, entry.getMaxSizeEnter(  ) );
272         daoUtil.setBoolean( 13, entry.isShowInDiggSubmitList(  ) );
273         daoUtil.setInt( 14, entry.getIdEntry(  ) );
274 
275         daoUtil.executeUpdate(  );
276         daoUtil.free(  );
277     }
278 
279     /**
280      * Load the data of all the entry who verify the filter and returns them in a  list
281      * @param filter the filter
282      * @param plugin the plugin
283      * @return  the list of entry
284      */
285     public List<IEntry> selectEntryListByFilter( EntryFilter filter, Plugin plugin )
286     {
287         List<IEntry> entryList = new ArrayList<IEntry>(  );
288         IEntry entry = null;
289         EntryType entryType = null;
290         Digg digg = null;
291 
292         String strSQL = SQL_QUERY_SELECT_ENTRY_BY_FILTER;
293         strSQL += ( ( filter.containsIdDigg(  ) ) ? SQL_FILTER_ID_DIGG : EMPTY_STRING );
294 
295         strSQL += SQL_ORDER_BY_POSITION;
296 
297         DAOUtil daoUtil = new DAOUtil( strSQL, plugin );
298         int nIndex = 1;
299 
300         if ( filter.containsIdDigg(  ) )
301         {
302             daoUtil.setInt( nIndex, filter.getIdDigg(  ) );
303             nIndex++;
304         }
305 
306         daoUtil.executeQuery(  );
307 
308         while ( daoUtil.next(  ) )
309         {
310             entryType = new EntryType(  );
311             entryType.setIdType( daoUtil.getInt( 1 ) );
312             entryType.setTitle( daoUtil.getString( 2 ) );
313             entryType.setClassName( daoUtil.getString( 3 ) );
314 
315             try
316             {
317                 entry = (IEntry) Class.forName( entryType.getClassName(  ) ).newInstance(  );
318             }
319             catch ( ClassNotFoundException e )
320             {
321                 //  class doesn't exist
322                 AppLogService.error( e );
323 
324                 return null;
325             }
326             catch ( InstantiationException e )
327             {
328                 // Class is abstract or is an  interface or haven't accessible builder
329                 AppLogService.error( e );
330 
331                 return null;
332             }
333             catch ( IllegalAccessException e )
334             {
335                 // can't access to rhe class
336                 AppLogService.error( e );
337 
338                 return null;
339             }
340 
341             entry.setEntryType( entryType );
342             entry.setIdEntry( daoUtil.getInt( 4 ) );
343             // insert form
344             digg = new Digg(  );
345             digg.setIdDigg( daoUtil.getInt( 5 ) );
346             entry.setDigg( digg );
347 
348             entry.setTitle( daoUtil.getString( 6 ) );
349             entry.setHelpMessage( daoUtil.getString( 7 ) );
350             entry.setComment( daoUtil.getString( 8 ) );
351             entry.setMandatory( daoUtil.getBoolean( 9 ) );
352             entry.setPosition( daoUtil.getInt( 10 ) );
353             entry.setShowInDiggSubmitList( daoUtil.getBoolean( 11 ) );
354 
355             entryList.add( entry );
356         }
357 
358         daoUtil.free(  );
359 
360         return entryList;
361     }
362 
363     /**
364      * Return  the number of entry who verify the filter
365      * @param filter the filter
366      * @param plugin the plugin
367      * @return   the number of entry who verify the filter
368      */
369     public int selectNumberEntryByFilter( EntryFilter filter, Plugin plugin )
370     {
371         int nNumberEntry = 0;
372         String strSQL = SQL_QUERY_SELECT_NUMBER_ENTRY_BY_FILTER;
373         strSQL += ( ( filter.containsIdDigg(  ) ) ? SQL_FILTER_ID_DIGG : EMPTY_STRING );
374 
375         strSQL += SQL_ORDER_BY_POSITION;
376 
377         DAOUtil daoUtil = new DAOUtil( strSQL, plugin );
378         int nIndex = 1;
379 
380         if ( filter.containsIdDigg(  ) )
381         {
382             daoUtil.setInt( nIndex, filter.getIdDigg(  ) );
383             nIndex++;
384         }
385 
386         daoUtil.executeQuery(  );
387 
388         if ( daoUtil.next(  ) )
389         {
390             nNumberEntry = daoUtil.getInt( 1 );
391         }
392 
393         daoUtil.free(  );
394 
395         return nNumberEntry;
396     }
397 
398     /**
399      * Delete an association between  entry and a regular expression
400      *
401      * @param nIdEntry The identifier of the field
402      *  @param nIdExpression The identifier of the regular expression
403      * @param plugin the plugin
404      */
405     public void deleteVerifyBy( int nIdEntry, int nIdExpression, Plugin plugin )
406     {
407         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_VERIF_BY, plugin );
408         daoUtil.setInt( 1, nIdEntry );
409         daoUtil.setInt( 2, nIdExpression );
410         daoUtil.executeUpdate(  );
411         daoUtil.free(  );
412     }
413 
414     /**
415      * insert an association between  entry and a regular expression
416      *
417      * @param nIdEntry The identifier of the entry
418      * @param nIdExpression The identifier of the regular expression
419      * @param plugin the plugin
420      */
421     public void insertVerifyBy( int nIdEntry, int nIdExpression, Plugin plugin )
422     {
423         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_VERIF_BY, plugin );
424         daoUtil.setInt( 1, nIdEntry );
425         daoUtil.setInt( 2, nIdExpression );
426         daoUtil.executeUpdate(  );
427         daoUtil.free(  );
428     }
429 
430     /**
431      * Load the key of all the regularExpression associate to the entry and returns them in a  list
432      * @param nIdEntry the id of entry
433      * @param plugin the plugin
434      * @return  the list of regular expression key
435      */
436     public List<Integer> selectListRegularExpressionKeyByIdEntry( int nIdEntry, Plugin plugin )
437     {
438         List<Integer> regularExpressionList = new ArrayList<Integer>(  );
439         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_REGULAR_EXPRESSION_BY_ID_ENTRY, plugin );
440         daoUtil.setInt( 1, nIdEntry );
441         daoUtil.executeQuery(  );
442 
443         while ( daoUtil.next(  ) )
444         {
445             regularExpressionList.add( daoUtil.getInt( 1 ) );
446         }
447 
448         daoUtil.free(  );
449 
450         return regularExpressionList;
451     }
452 
453     /**
454           * verify if the regular expresssion is use
455           *
456           * @param nIdEntry The identifier of the entry
457           * @param plugin the plugin
458           * @return true if the regular expression is use
459           */
460     public boolean isRegularExpressionIsUse( int nIdExpression, Plugin plugin )
461     {
462         int nNumberEntry = 0;
463 
464         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_COUNT_ENTRY_BY_ID_REGULAR_EXPRESSION, plugin );
465         daoUtil.setInt( 1, nIdExpression );
466         daoUtil.executeQuery(  );
467 
468         if ( daoUtil.next(  ) )
469         {
470             nNumberEntry = daoUtil.getInt( 1 );
471         }
472 
473         daoUtil.free(  );
474 
475         return nNumberEntry != 0;
476     }
477 }