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.portal.business.workgroup;
35  
36  import fr.paris.lutece.portal.business.user.AdminUser;
37  import fr.paris.lutece.portal.business.user.AdminUserHome;
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  
45  /**
46   * This class provides Data Access methods for AdminWorkgroup objects
47   */
48  public final class AdminWorkgroupDAO implements IAdminWorkgroupDAO
49  {
50      // Constants
51      private static final String CONSTANT_PERCENT = "%";
52      private static final String SQL_QUERY_SELECT = " SELECT workgroup_key, workgroup_description FROM core_admin_workgroup WHERE workgroup_key = ?  ";
53      private static final String SQL_QUERY_INSERT = " INSERT INTO core_admin_workgroup ( workgroup_key, workgroup_description ) VALUES ( ?, ? ) ";
54      private static final String SQL_QUERY_DELETE = " DELETE FROM core_admin_workgroup WHERE workgroup_key = ?  ";
55      private static final String SQL_QUERY_UPDATE = " UPDATE core_admin_workgroup SET workgroup_description = ? WHERE workgroup_key = ?  ";
56      private static final String SQL_QUERY_SELECTALL = " SELECT workgroup_key, workgroup_description FROM core_admin_workgroup ORDER BY workgroup_key";
57      private static final String SQL_QUERY_SELECT_USER_WORKGROUP = " SELECT workgroup_key FROM core_admin_workgroup_user WHERE id_user = ? AND workgroup_key = ? ";
58      private static final String SQL_QUERY_USER_IN_WORKGROUP = " SELECT id_user FROM core_admin_workgroup_user WHERE id_user = ? ";
59      private static final String SQL_QUERY_SELECT_USER_WORKGROUPS = " SELECT a.workgroup_key, a.workgroup_description " +
60          " FROM core_admin_workgroup a, core_admin_workgroup_user b " +
61          " WHERE a.workgroup_key = b.workgroup_key AND b.id_user = ?  ";
62      private static final String SQL_QUERY_SELECT_USERS_LIST_FOR_WORKGROUP = " SELECT b.id_user " +
63          " FROM core_admin_workgroup a, core_admin_workgroup_user b " +
64          " WHERE a.workgroup_key = b.workgroup_key AND a.workgroup_key = ?";
65      private static final String SQL_QUERY_DELETE_ALL_USERS_WORKGROUP = " DELETE FROM core_admin_workgroup_user WHERE workgroup_key = ?  ";
66      private static final String SQL_QUERY_INSERT_USER_WORKGROUP = " INSERT INTO core_admin_workgroup_user ( workgroup_key, id_user ) VALUES ( ?, ? ) ";
67      private static final String SQL_QUERY_DELETE_USER_FROM_WORKGROUP = " DELETE FROM core_admin_workgroup_user WHERE workgroup_key = ?  AND id_user = ?";
68      private static final String SQL_QUERY_SELECT_WORKGROUP_FROM_SEARCH = " SELECT workgroup_key, workgroup_description FROM core_admin_workgroup " +
69          " WHERE workgroup_key LIKE ? AND workgroup_description LIKE ? ORDER BY workgroup_key ";
70  
71      /**
72       * {@inheritDoc}
73       */
74      public void insert( AdminWorkgroup workgroup )
75      {
76          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
77          daoUtil.setString( 1, workgroup.getKey(  ) );
78          daoUtil.setString( 2, workgroup.getDescription(  ) );
79  
80          daoUtil.executeUpdate(  );
81          daoUtil.free(  );
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      public AdminWorkgroup load( String strWorkgroupKey )
88      {
89          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
90          daoUtil.setString( 1, strWorkgroupKey );
91          daoUtil.executeQuery(  );
92  
93          AdminWorkgroup workgroup = null;
94  
95          if ( daoUtil.next(  ) )
96          {
97              workgroup = new AdminWorkgroup(  );
98              workgroup.setKey( daoUtil.getString( 1 ) );
99              workgroup.setDescription( daoUtil.getString( 2 ) );
100         }
101 
102         daoUtil.free(  );
103 
104         return workgroup;
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     public void delete( String strWorkgroupKey )
111     {
112         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
113         daoUtil.setString( 1, strWorkgroupKey );
114 
115         daoUtil.executeUpdate(  );
116         daoUtil.free(  );
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     public void store( AdminWorkgroup workgroup )
123     {
124         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
125         daoUtil.setString( 1, workgroup.getDescription(  ) );
126         daoUtil.setString( 2, workgroup.getKey(  ) );
127 
128         daoUtil.executeUpdate(  );
129         daoUtil.free(  );
130     }
131 
132     /**
133      * {@inheritDoc}
134      */
135     public Collection<AdminWorkgroup> selectWorkgroupList(  )
136     {
137         Collection<AdminWorkgroup> listWorkgroups = new ArrayList<AdminWorkgroup>(  );
138         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL );
139         daoUtil.executeQuery(  );
140 
141         while ( daoUtil.next(  ) )
142         {
143             AdminWorkgroup workgroup = new AdminWorkgroup(  );
144             workgroup.setKey( daoUtil.getString( 1 ) );
145             workgroup.setDescription( daoUtil.getString( 2 ) );
146 
147             listWorkgroups.add( workgroup );
148         }
149 
150         daoUtil.free(  );
151 
152         return listWorkgroups;
153     }
154 
155     /**
156      * {@inheritDoc}
157      */
158     public boolean checkExistWorkgroup( String strWorkgroupKey )
159     {
160         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
161         daoUtil.setString( 1, strWorkgroupKey );
162         daoUtil.executeQuery(  );
163 
164         if ( daoUtil.next(  ) )
165         {
166             daoUtil.free(  );
167 
168             return true;
169         }
170         else
171         {
172             daoUtil.free(  );
173 
174             return false;
175         }
176     }
177 
178     /**
179      * {@inheritDoc}
180      */
181     public boolean isUserInWorkgroup( int nIdUser, String strWorkgroupKey )
182     {
183         boolean bInWorkgroup = false;
184         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_USER_WORKGROUP );
185         daoUtil.setInt( 1, nIdUser );
186         daoUtil.setString( 2, strWorkgroupKey );
187         daoUtil.executeQuery(  );
188 
189         if ( daoUtil.next(  ) )
190         {
191             bInWorkgroup = true;
192         }
193 
194         daoUtil.free(  );
195 
196         return bInWorkgroup;
197     }
198 
199     /**
200      * {@inheritDoc}
201      */
202     public boolean checkUserHasWorkgroup( int nIdUser )
203     {
204         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_USER_IN_WORKGROUP );
205         daoUtil.setInt( 1, nIdUser );
206         daoUtil.executeQuery(  );
207 
208         if ( daoUtil.next(  ) )
209         {
210             daoUtil.free(  );
211 
212             return true;
213         }
214         else
215         {
216             daoUtil.free(  );
217 
218             return false;
219         }
220     }
221 
222     /**
223      * {@inheritDoc}
224      */
225     public ReferenceList getUserWorkgroups( int nIdUser )
226     {
227         ReferenceList listWorkgroups = new ReferenceList(  );
228         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_USER_WORKGROUPS );
229         daoUtil.setInt( 1, nIdUser );
230         daoUtil.executeQuery(  );
231 
232         while ( daoUtil.next(  ) )
233         {
234             listWorkgroups.addItem( daoUtil.getString( 1 ), daoUtil.getString( 2 ) );
235         }
236 
237         daoUtil.free(  );
238 
239         return listWorkgroups;
240     }
241 
242     /**
243      * {@inheritDoc}
244      */
245     public Collection<AdminUser> getUsersListForWorkgroup( String strWorkgroupKey )
246     {
247         Collection<AdminUser> listUsers = new ArrayList<AdminUser>(  );
248         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_USERS_LIST_FOR_WORKGROUP );
249         daoUtil.setString( 1, strWorkgroupKey );
250         daoUtil.executeQuery(  );
251 
252         AdminUser adminUser = null;
253 
254         while ( daoUtil.next(  ) )
255         {
256             adminUser = AdminUserHome.findByPrimaryKey( daoUtil.getInt( 1 ) );
257 
258             if ( adminUser != null )
259             {
260                 listUsers.add( adminUser );
261             }
262         }
263 
264         daoUtil.free(  );
265 
266         return listUsers;
267     }
268 
269     /**
270      * {@inheritDoc}
271      */
272     public void deleteAllUsersForWorkgroup( String strWorkgroupKey )
273     {
274         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_ALL_USERS_WORKGROUP );
275         daoUtil.setString( 1, strWorkgroupKey );
276         daoUtil.executeUpdate(  );
277         daoUtil.free(  );
278     }
279 
280     /**
281      * {@inheritDoc}
282      */
283     public void insertUserForWorkgroup( AdminUser user, String strWorkgroupKey )
284     {
285         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_USER_WORKGROUP );
286         daoUtil.setString( 1, strWorkgroupKey );
287         daoUtil.setInt( 2, user.getUserId(  ) );
288         daoUtil.executeUpdate(  );
289         daoUtil.free(  );
290     }
291 
292     /**
293      * {@inheritDoc}
294      */
295     public void deleteUserFromWorkgroup( int nUserId, String strWorkgroupKey )
296     {
297         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_USER_FROM_WORKGROUP );
298         daoUtil.setString( 1, strWorkgroupKey );
299         daoUtil.setInt( 2, nUserId );
300         daoUtil.executeUpdate(  );
301         daoUtil.free(  );
302     }
303 
304     /**
305      * Find workgroups from a filter
306      * @param awFilter the filter
307      * @return the list of workgroups
308      */
309     public Collection<AdminWorkgroup> selectWorkgroupsByFilter( AdminWorkgroupFilter awFilter )
310     {
311         Collection<AdminWorkgroup> listFilteredWorkgroups = new ArrayList<AdminWorkgroup>(  );
312         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_WORKGROUP_FROM_SEARCH );
313 
314         daoUtil.setString( 1, CONSTANT_PERCENT + awFilter.getKey(  ) + CONSTANT_PERCENT );
315         daoUtil.setString( 2, CONSTANT_PERCENT + awFilter.getDescription(  ) + CONSTANT_PERCENT );
316 
317         daoUtil.executeQuery(  );
318 
319         while ( daoUtil.next(  ) )
320         {
321             AdminWorkgroup workgroup = new AdminWorkgroup(  );
322             workgroup.setKey( daoUtil.getString( 1 ) );
323             workgroup.setDescription( daoUtil.getString( 2 ) );
324 
325             listFilteredWorkgroups.add( workgroup );
326         }
327 
328         daoUtil.free(  );
329 
330         return listFilteredWorkgroups;
331     }
332 }