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.quiz.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.ReferenceItem;
38  import fr.paris.lutece.util.ReferenceList;
39  import fr.paris.lutece.util.sql.DAOUtil;
40  
41  import java.util.ArrayList;
42  import java.util.Collection;
43  
44  import org.apache.commons.lang.StringUtils;
45  
46  
47  /**
48   * Quiz profile DAO
49   */
50  public class QuizProfileDAO implements IQuizProfileDAO
51  {
52      private static final String SQL_QUERY_NEW_PK = " SELECT max( id_profil ) FROM quiz_profil ";
53      private static final String SQL_QUERY_INSERT_PROFIL = "INSERT INTO quiz_profil ( id_profil, name, description, id_quiz ) VALUES ( ?, ?, ?, ? )";
54      private static final String SQL_QUERY_SELECT_ALL = "SELECT id_profil, name, description FROM quiz_profil WHERE id_quiz = ? ORDER BY id_profil";
55      private static final String SQL_QUERY_LOAD_PROFIL_BY_ID = "SELECT name FROM quiz_profil WHERE id_profil = ?";
56      private static final String SQL_QUERY_SELECT_PROFIL = "SELECT id_profil, name, description FROM quiz_profil WHERE id_profil = ?";
57      private static final String SQL_QUERY_DELETE = "DELETE FROM quiz_profil WHERE id_profil = ? ";
58      private static final String SQL_QUERY_DELETE_BY_QUIZ = "DELETE FROM quiz_profil WHERE id_quiz = ? ";
59      private static final String SQL_QUERY_UPDATE = " UPDATE quiz_profil SET name = ?, description = ? WHERE id_profil = ?  ";
60  
61      /**
62       * Find the new primary key in the table.
63       * 
64       * @param plugin the plugin
65       * @return the new primary key
66       */
67      private int newPrimaryKey( Plugin plugin )
68      {
69          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
70          daoUtil.executeQuery( );
71  
72          int nKey;
73  
74          if ( !daoUtil.next( ) )
75          {
76              // if the table is empty
77              nKey = 1;
78          }
79  
80          nKey = daoUtil.getInt( 1 ) + 1;
81          daoUtil.free( );
82  
83          return nKey;
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public void insert( QuizProfile profil, Plugin plugin )
91      {
92          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_PROFIL, plugin );
93          profil.setIdProfile( newPrimaryKey( plugin ) );
94  
95          daoUtil.setInt( 1, profil.getIdProfile( ) );
96          daoUtil.setString( 2, profil.getName( ) );
97          daoUtil.setString( 3, profil.getDescription( ) );
98          daoUtil.setInt( 4, profil.getIdQuiz( ) );
99  
100         daoUtil.executeUpdate( );
101         daoUtil.free( );
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public ReferenceList selectQuizProfilsReferenceList( int nIdQuiz, Plugin plugin )
109     {
110         ReferenceList profilsReferenceList = new ReferenceList( );
111 
112         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL, plugin );
113         daoUtil.setInt( 1, nIdQuiz );
114 
115         daoUtil.executeQuery( );
116 
117         while ( daoUtil.next( ) )
118         {
119             ReferenceItem item = new ReferenceItem( );
120             item.setCode( String.valueOf( daoUtil.getInt( 1 ) ) );
121             item.setName( daoUtil.getString( 2 ) );
122             profilsReferenceList.add( item );
123         }
124 
125         daoUtil.free( );
126 
127         return profilsReferenceList;
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public String getName( int nIdProfil, Plugin plugin )
135     {
136         String name = StringUtils.EMPTY;
137 
138         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_LOAD_PROFIL_BY_ID, plugin );
139         daoUtil.setInt( 1, nIdProfil );
140 
141         daoUtil.executeQuery( );
142 
143         if ( daoUtil.next( ) )
144         {
145             name = daoUtil.getString( 1 );
146         }
147 
148         daoUtil.free( );
149 
150         return name;
151     }
152 
153     /**
154      * {@inheritDoc}
155      */
156     @Override
157     public QuizProfile load( int nKey, Plugin plugin )
158     {
159         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_PROFIL, plugin );
160         daoUtil.setInt( 1, nKey );
161         daoUtil.executeQuery( );
162 
163         QuizProfile quizProfil = null;
164 
165         if ( daoUtil.next( ) )
166         {
167             quizProfil = new QuizProfile( );
168             quizProfil.setIdProfile( daoUtil.getInt( 1 ) );
169             quizProfil.setName( daoUtil.getString( 2 ) );
170             quizProfil.setDescription( daoUtil.getString( 3 ) );
171         }
172 
173         daoUtil.free( );
174 
175         return quizProfil;
176     }
177 
178     /**
179      * {@inheritDoc}
180      */
181     @Override
182     public Collection<QuizProfile> findAll( int nIdQuiz, Plugin plugin )
183     {
184         Collection<QuizProfile> result = new ArrayList<QuizProfile>( );
185 
186         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL, plugin );
187         daoUtil.setInt( 1, nIdQuiz );
188 
189         daoUtil.executeQuery( );
190 
191         while ( daoUtil.next( ) )
192         {
193             QuizProfile profil = new QuizProfile( );
194             profil.setIdProfile( daoUtil.getInt( 1 ) );
195             profil.setName( daoUtil.getString( 2 ) );
196             profil.setDescription( daoUtil.getString( 3 ) );
197             result.add( profil );
198         }
199 
200         daoUtil.free( );
201 
202         return result;
203     }
204 
205     /**
206      * {@inheritDoc}
207      */
208     @Override
209     public void delete( int nIdProfil, Plugin plugin )
210     {
211         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
212         daoUtil.setInt( 1, nIdProfil );
213         daoUtil.executeUpdate( );
214         daoUtil.free( );
215     }
216 
217     /**
218      * {@inheritDoc}
219      */
220     @Override
221     public void store( QuizProfile quizProfil, Plugin plugin )
222     {
223         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
224         daoUtil.setString( 1, quizProfil.getName( ) );
225         daoUtil.setString( 2, quizProfil.getDescription( ) );
226         daoUtil.setInt( 3, quizProfil.getIdProfile( ) );
227 
228         daoUtil.executeUpdate( );
229 
230         daoUtil.free( );
231     }
232 
233     /**
234      * {@inheritDoc}
235      */
236     @Override
237     public void deleteByQuiz( int nIdQuiz, Plugin plugin )
238     {
239         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_BY_QUIZ, plugin );
240         daoUtil.setInt( 1, nIdQuiz );
241         daoUtil.executeUpdate( );
242         daoUtil.free( );
243     }
244 }