1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
46
47
48 public final class MyTaskDAO implements IMyTaskDAO
49 {
50
51 private static final String COMMA = ",";
52
53
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
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
84 nKey = 1;
85 }
86
87 nKey = daoUtil.getInt( 1 ) + 1;
88 daoUtil.free( );
89
90 return nKey;
91 }
92
93
94
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
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
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
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
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
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
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
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
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 }