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.mytasks.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   * MyTaskDAO
46   *
47   */
48  public final class MyTaskDAO implements IMyTaskDAO
49  {
50      // CONSTANTS
51      private static final String COMMA = ",";
52  
53      // SQL
54      private static final String SQL_QUERY_NEW_PK = " SELECT max( id_mytask ) FROM mytasks_mytask ";
55      private static final String SQL_QUERY_SELECT = " SELECT id_mytask, name, date_mytask, is_done FROM mytasks_mytask WHERE id_mytask = ? ";
56      private static final String SQL_QUERY_INSERT = " INSERT INTO mytasks_mytask ( id_mytask, name, date_mytask, is_done ) VALUES ( ?, ?, ?, ? ) ";
57      private static final String SQL_QUERY_DELETE = " DELETE FROM mytasks_mytask WHERE id_mytask = ? ";
58      private static final String SQL_QUERY_UPDATE = " UPDATE mytasks_mytask SET name = ?, date_mytask = ?, is_done = ? WHERE id_mytask = ? ";
59      private static final String SQL_QUERY_UNDONE_MYTASKS = " UPDATE mytasks_mytask SET is_done = 0 WHERE id_mytask IN " +
60          " ( SELECT id_mytask FROM mytasks_user_mytask WHERE user_guid = ? ) ";
61      private static final String SQL_ORDER_BY = " ORDER BY ";
62      private static final String SQL_ASC = " ASC ";
63      private static final String SQL_IS_DONE = " is_done ";
64      private static final String SQL_DATE_MYTASK = " date_mytask ";
65      private static final String SQL_QUERY_SELECT_MYTASKS_FROM_USER = " SELECT a.id_mytask, a.name, a.date_mytask, a.is_done " +
66          " FROM mytasks_mytask AS a INNER JOIN mytasks_user_mytask b ON a.id_mytask = b.id_mytask WHERE b.user_guid = ? ";
67      private static final String SQL_QUERY_INSERT_USER_MYTASK = " INSERT INTO mytasks_user_mytask ( user_guid, id_mytask ) VALUES ( ?, ? ) ";
68      private static final String SQL_QUERY_DELETE_MYTASK_FROM_USER = " DELETE FROM mytasks_user_mytask WHERE id_mytask = ? ";
69      private static final String QSL_QUERY_COUNT_MYTASK = "SELECT COUNT(*) FROM mytasks_user_mytask WHERE user_guid = ? ";
70  
71      /**
72       * {@inheritDoc}
73       */
74      public int newPrimaryKey( Plugin plugin )
75      {
76          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
77          daoUtil.executeQuery(  );
78  
79          int nKey;
80  
81          if ( !daoUtil.next(  ) )
82          {
83              // if the table is empty
84              nKey = 1;
85          }
86  
87          nKey = daoUtil.getInt( 1 ) + 1;
88          daoUtil.free(  );
89  
90          return nKey;
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      public void insert( MyTask myTask, Plugin plugin )
97      {
98          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
99  
100         int nIndex = 1;
101         myTask.setIdMyTask( newPrimaryKey( plugin ) );
102 
103         daoUtil.setInt( nIndex++, myTask.getIdMyTask(  ) );
104         daoUtil.setString( nIndex++, myTask.getName(  ) );
105         daoUtil.setDate( nIndex++, myTask.getDate(  ) );
106         daoUtil.setBoolean( nIndex++, myTask.isDone(  ) );
107 
108         daoUtil.executeUpdate(  );
109         daoUtil.free(  );
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     public MyTask load( int nIdMyTask, Plugin plugin )
116     {
117         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
118         daoUtil.setInt( 1, nIdMyTask );
119         daoUtil.executeQuery(  );
120 
121         MyTask myTask = null;
122 
123         if ( daoUtil.next(  ) )
124         {
125             int nIndex = 1;
126             myTask = new MyTask(  );
127 
128             myTask.setIdMyTask( daoUtil.getInt( nIndex++ ) );
129             myTask.setName( daoUtil.getString( nIndex++ ) );
130             myTask.setDate( daoUtil.getDate( nIndex++ ) );
131             myTask.setDone( daoUtil.getBoolean( nIndex++ ) );
132         }
133 
134         daoUtil.free(  );
135 
136         return myTask;
137     }
138 
139     /**
140      * {@inheritDoc}
141      */
142     public void delete( int nIdMyTask, Plugin plugin )
143     {
144         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
145         daoUtil.setInt( 1, nIdMyTask );
146         daoUtil.executeUpdate(  );
147         daoUtil.free(  );
148     }
149 
150     /**
151      * {@inheritDoc}
152      */
153     public void store( MyTask myTask, Plugin plugin )
154     {
155         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
156 
157         int nIndex = 1;
158         daoUtil.setString( nIndex++, myTask.getName(  ) );
159         daoUtil.setDate( nIndex++, myTask.getDate(  ) );
160         daoUtil.setBoolean( nIndex++, myTask.isDone(  ) );
161 
162         daoUtil.setInt( nIndex++, myTask.getIdMyTask(  ) );
163 
164         daoUtil.executeUpdate(  );
165         daoUtil.free(  );
166     }
167 
168     /**
169      * {@inheritDoc}
170      */
171     public List<MyTask> selectMyTasksListFromUser( String strUserGuid, Plugin plugin )
172     {
173         StringBuilder sbSQL = new StringBuilder( SQL_QUERY_SELECT_MYTASKS_FROM_USER );
174         sbSQL.append( SQL_ORDER_BY );
175         sbSQL.append( SQL_IS_DONE + SQL_ASC );
176         sbSQL.append( COMMA + SQL_DATE_MYTASK + SQL_ASC );
177 
178         DAOUtil daoUtil = new DAOUtil( sbSQL.toString(  ), plugin );
179         daoUtil.setString( 1, strUserGuid );
180         daoUtil.executeQuery(  );
181 
182         List<MyTask> myTasksList = new ArrayList<MyTask>(  );
183 
184         while ( daoUtil.next(  ) )
185         {
186             int nIndex = 1;
187             MyTask myTask = new MyTask(  );
188 
189             myTask.setIdMyTask( daoUtil.getInt( nIndex++ ) );
190             myTask.setName( daoUtil.getString( nIndex++ ) );
191             myTask.setDate( daoUtil.getDate( nIndex++ ) );
192             myTask.setDone( daoUtil.getBoolean( nIndex++ ) );
193             myTasksList.add( myTask );
194         }
195 
196         daoUtil.free(  );
197 
198         return myTasksList;
199     }
200 
201     /**
202      * {@inheritDoc}
203      */
204     public void insertUserMyTask( String strUserGuid, int nIdMyTask, Plugin plugin )
205     {
206         int nIndex = 1;
207         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_USER_MYTASK, plugin );
208         daoUtil.setString( nIndex++, strUserGuid );
209         daoUtil.setInt( nIndex++, nIdMyTask );
210 
211         daoUtil.executeUpdate(  );
212         daoUtil.free(  );
213     }
214 
215     /**
216      * {@inheritDoc}
217      */
218     public void deleteUserMyTask( int nIdMyTask, Plugin plugin )
219     {
220         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_MYTASK_FROM_USER, plugin );
221         daoUtil.setInt( 1, nIdMyTask );
222 
223         daoUtil.executeUpdate(  );
224         daoUtil.free(  );
225     }
226 
227     /**
228      * {@inheritDoc}
229      */
230     public void undoneMyTasks( String strUserGuid, Plugin plugin )
231     {
232         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UNDONE_MYTASKS, plugin );
233         daoUtil.setString( 1, strUserGuid );
234 
235         daoUtil.executeUpdate(  );
236         daoUtil.free(  );
237     }
238 
239     /**
240      * {@inheritDoc}
241      */
242     public int getNbMyTasks( String strUserGuid, Plugin plugin )
243     {
244         int nNbTasks = 0;
245         DAOUtil daoUtil = new DAOUtil( QSL_QUERY_COUNT_MYTASK, plugin );
246         daoUtil.setString( 1, strUserGuid );
247 
248         daoUtil.executeQuery(  );
249 
250         if ( daoUtil.next(  ) )
251         {
252             nNbTasks = daoUtil.getInt( 1 );
253         }
254 
255         daoUtil.free(  );
256 
257         return nNbTasks;
258     }
259 }