001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Kernel/Sources/es/udc/mypersonalizer/kernel/model/repository/sql/storers/UserGroupStorer.java,v 1.1.1.1 2004/03/25 12:08:36 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:36 $
005: *
006: * =============================================================================
007: *
008: * Copyright (c) 2003, The MyPersonalizer Development Group
009: * (http://www.tic.udc.es/~fbellas/mypersonalizer/index.html) at
010: * University Of A Coruna
011: * All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions are met:
015: *
016: * - Redistributions of source code must retain the above copyright notice,
017: * this list of conditions and the following disclaimer.
018: *
019: * - Redistributions in binary form must reproduce the above copyright notice,
020: * this list of conditions and the following disclaimer in the documentation
021: * and/or other materials provided with the distribution.
022: *
023: * - Neither the name of the University Of A Coruna nor the names of its
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: */
040:
041: package es.udc.mypersonalizer.kernel.model.repository.sql.storers;
042:
043: import java.util.Collection;
044: import java.util.ArrayList;
045: import java.sql.Connection;
046: import java.sql.SQLException;
047: import java.sql.Statement;
048: import java.sql.ResultSet;
049: import java.sql.PreparedStatement;
050:
051: import es.udc.mypersonalizer.kernel.log.Log;
052: import es.udc.mypersonalizer.kernel.log.LogManager;
053: import es.udc.mypersonalizer.kernel.log.LogNamingConventions;
054: import es.udc.mypersonalizer.kernel.model.repository.sql.config.DatabaseConventionsConfig;
055: import es.udc.mypersonalizer.kernel.model.repository.sql.config.DatabaseConventionsConfigManager;
056: import es.udc.mypersonalizer.kernel.model.repository.interfaces.UserGroup;
057: import es.udc.mypersonalizer.kernel.model.repository.interfaces.UserAlreadyExistsInGroupException;
058: import es.udc.mypersonalizer.kernel.model.repository.interfaces.UserDoesNotExistInGroupException;
059: import es.udc.mypersonalizer.kernel.model.repository.sql.util.SQLOperations;
060: import es.udc.mypersonalizer.kernel.util.exceptions.InstanceNotFoundException;
061: import es.udc.mypersonalizer.kernel.util.exceptions.DuplicateInstanceException;
062: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
063:
064: /**
065: * This "storer" class stores and removes <code>UserGroup</code>s in the
066: * database.
067: *
068: * @author Abel Iago Toral Quiroga
069: * @author Abel Muinho
070: * @since 1.0
071: */
072: public class UserGroupStorer {
073:
074: /** Constant for the user-groups table name*/
075: private static final String USER_GROUPS_TABLE_NAME;
076:
077: /** Constant for the user-groups <-> users assigments table name*/
078: private static final String USER_GROUPS_ASSIGNMENTS_TABLE_NAME;
079:
080: /** Constant for the user group identifier field. */
081: private static final String USER_GROUP_IDENTIFIER_COLUMN_NAME;
082:
083: /**
084: * Constant for the user group name field in table
085: * <code>USER_GROUPS_TABLE_NAME</code>
086: */
087: private static final String USER_GROUP_COLUMN_NAME;
088:
089: /**
090: * Constant for the login name field in
091: * <code>USER_GROUPS_ASSIGNMENTS_TABLE_NAME</code> table
092: */
093: private static final String LOGIN_COLUMN_NAME;
094:
095: static {
096: String userGroupsTableName = null;
097: String userGroupsAssignmentsTableName = null;
098: String userGroupIdentifier = null;
099: String userGroupColumnName = null;
100: String loginColumnName = null;
101: try {
102: DatabaseConventionsConfig config = DatabaseConventionsConfigManager
103: .getConfig();
104: userGroupsTableName = config.getUserGroupsTable();
105: userGroupsAssignmentsTableName = config
106: .getUserGroupsAssignmentsTable();
107: userGroupIdentifier = config.getUserGroupIdentifierColumn();
108: userGroupColumnName = config.getUserGroupNameColumn();
109: loginColumnName = config.getLoginColumn();
110: } catch (Exception e) {
111: Log mypersonalizerLog = LogManager
112: .getLog(LogNamingConventions.MYPERSONALIZER);
113: mypersonalizerLog.write(
114: "Could not initialize configuration for "
115: + "UserRegistrationInformationStorer", e,
116: UserRegistrationInformationStorer.class);
117: }
118: USER_GROUPS_TABLE_NAME = userGroupsTableName;
119: USER_GROUPS_ASSIGNMENTS_TABLE_NAME = userGroupsAssignmentsTableName;
120: USER_GROUP_IDENTIFIER_COLUMN_NAME = userGroupIdentifier;
121: USER_GROUP_COLUMN_NAME = userGroupColumnName;
122: LOGIN_COLUMN_NAME = loginColumnName;
123: }
124:
125: /**
126: * Checks if a user is assigned to a group.
127: *
128: * @param connection the connection to the database
129: * @param loginName the login name of the user
130: * @param userGroupIdentifier the user group identifier
131: * @return <code>true</code> if the user is assigned to the group,
132: * <code>false</code> otherwise
133: * @throws InternalErrorException if a failure is detected.
134: */
135: public boolean userIsAssignedToGroup(Connection connection,
136: String loginName, Long userGroupIdentifier)
137: throws InternalErrorException {
138:
139: PreparedStatement statement = null;
140: ResultSet resultSet = null;
141: String query = "SELECT *" + " FROM "
142: + USER_GROUPS_ASSIGNMENTS_TABLE_NAME + " WHERE "
143: + LOGIN_COLUMN_NAME + " = ?" + " AND "
144: + USER_GROUP_IDENTIFIER_COLUMN_NAME + " = ?";
145:
146: try {
147: statement = connection.prepareStatement(query);
148: int col = 1;
149: statement.setString(col++, loginName);
150: statement.setLong(col++, userGroupIdentifier.longValue());
151: resultSet = statement.executeQuery();
152:
153: return resultSet.next();
154: } catch (SQLException e) {
155: throw new InternalErrorException(e);
156: } finally {
157: SQLOperations.closeStatement(statement);
158: SQLOperations.closeResultSet(resultSet);
159: }
160: }
161:
162: /**
163: * Checks if a group exists.
164: *
165: * @param connection the connection to the database
166: * @param userGroupIdentifier the user group identifier
167: * @return <code>true</code> if the group exists,
168: * <code>false</code> otherwise
169: * @throws InternalErrorException if a failure is detected.
170: */
171: public boolean groupExists(Connection connection,
172: Long userGroupIdentifier) throws InternalErrorException {
173:
174: Statement statement = null;
175: ResultSet resultSet = null;
176: String query = "SELECT *" + " FROM " + USER_GROUPS_TABLE_NAME
177: + " WHERE " + USER_GROUP_IDENTIFIER_COLUMN_NAME + " = "
178: + userGroupIdentifier;
179:
180: try {
181: statement = connection.createStatement();
182: resultSet = statement.executeQuery(query);
183:
184: return resultSet.next();
185: } catch (SQLException e) {
186: throw new InternalErrorException(e);
187: } finally {
188: SQLOperations.closeStatement(statement);
189: SQLOperations.closeResultSet(resultSet);
190: }
191: }
192:
193: /**
194: * Checks if a group exists.
195: *
196: * @param connection the connection to the database
197: * @param userGroupName the user group name
198: * @return <code>true</code> if the group exists,
199: * <code>false</code> otherwise
200: * @throws InternalErrorException if a failure is detected.
201: */
202: public boolean groupExists(Connection connection,
203: String userGroupName) throws InternalErrorException {
204:
205: PreparedStatement statement = null;
206: ResultSet resultSet = null;
207: String query = "SELECT *" + " FROM " + USER_GROUPS_TABLE_NAME
208: + " WHERE " + USER_GROUP_COLUMN_NAME + " = ?";
209:
210: try {
211: statement = connection.prepareStatement(query);
212: int col = 1;
213: statement.setString(col++, userGroupName);
214: resultSet = statement.executeQuery();
215:
216: return resultSet.next();
217: } catch (SQLException e) {
218: throw new InternalErrorException(e);
219: } finally {
220: SQLOperations.closeStatement(statement);
221: SQLOperations.closeResultSet(resultSet);
222: }
223: }
224:
225: /**
226: * Finds all groups assigned to a user. Implements Page-by-Page Iterator
227: * design pattern.
228: * @param connection the connection to the database
229: * @param loginName the user's login name.
230: * @param startIndex the index of the group assigned to this user from
231: * which start to retrive groups.
232: * @param count the number of groups to retrive begining in the
233: * <code>startIndex</code> group. If count is < 0, then will return
234: * all the remaining user groups from startIndex.
235: * @throws InternalErrorException if a failure is detected.
236: * @return a <code>Collection</code> of <code>UserGroup</code> objects
237: * representing groups assigned to this user.
238: */
239: public Collection findAllGroupsAssignedToUser(
240: Connection connection, String loginName, int startIndex,
241: int count) throws InternalErrorException {
242:
243: PreparedStatement statement = null;
244: ResultSet resultSet = null;
245: String query = "SELECT ugt."
246: + USER_GROUP_IDENTIFIER_COLUMN_NAME + ", ugt."
247: + USER_GROUP_COLUMN_NAME + " FROM "
248: + USER_GROUPS_TABLE_NAME + " ugt, "
249: + USER_GROUPS_ASSIGNMENTS_TABLE_NAME + " ugat"
250: + " WHERE (ugt." + USER_GROUP_IDENTIFIER_COLUMN_NAME
251: + " =" + " ugat." + USER_GROUP_IDENTIFIER_COLUMN_NAME
252: + ") AND" + " (ugat." + LOGIN_COLUMN_NAME + " = ?)"
253: + " ORDER BY ugt." + USER_GROUP_COLUMN_NAME;
254:
255: try {
256: statement = connection.prepareStatement(query);
257: int col = 1;
258: statement.setString(col++, loginName);
259: resultSet = statement.executeQuery();
260: Collection assignedGroups = new ArrayList();
261:
262: /* Go to startIndex register */
263: int i = 0;
264: while (i < startIndex && resultSet.next())
265: i++;
266:
267: /* Get desired registers */
268: while (resultSet.next()
269: && ((assignedGroups.size() < count) || (count < 0))) {
270: assignedGroups
271: .add(getUserGroupFromResultSet(resultSet));
272: }
273:
274: return assignedGroups;
275: } catch (SQLException e) {
276: throw new InternalErrorException(e);
277: } finally {
278: SQLOperations.closeStatement(statement);
279: SQLOperations.closeResultSet(resultSet);
280: }
281: }
282:
283: /**
284: * Finds all users assigned to a group. Implements Page-by-Page Iterator
285: * design pattern.
286: * @param connection the connection to the database
287: * @param groupId the groupIdentifier.
288: * @param startIndex the index of the user assigned to this user group from
289: * which start to retrive users.
290: * @param count the number of users to retrive begining in the
291: * <code>startIndex</code> user.
292: * @throws InternalErrorException if a failure is detected.
293: * @return a <code>Collection</code> with the login names of the
294: * users assigned to the group.
295: */
296: public Collection findAllUsersAssignedToGroup(
297: Connection connection, Long groupId, int startIndex,
298: int count) throws InternalErrorException {
299:
300: Statement statement = null;
301: ResultSet resultSet = null;
302: String query = "SELECT ugat." + LOGIN_COLUMN_NAME + " FROM "
303: + USER_GROUPS_TABLE_NAME + " ugt, "
304: + USER_GROUPS_ASSIGNMENTS_TABLE_NAME + " ugat"
305: + " WHERE (ugt." + USER_GROUP_IDENTIFIER_COLUMN_NAME
306: + " =" + " ugat." + USER_GROUP_IDENTIFIER_COLUMN_NAME
307: + ") AND" + " (ugt."
308: + USER_GROUP_IDENTIFIER_COLUMN_NAME + " = " + groupId
309: + ")" + " ORDER BY ugat." + LOGIN_COLUMN_NAME;
310:
311: try {
312: statement = connection.createStatement();
313: resultSet = statement.executeQuery(query);
314: Collection assignedUsers = new ArrayList();
315:
316: /* Go to startIndex register */
317: int i = 0;
318: while (i < startIndex && resultSet.next())
319: i++;
320:
321: /* Get desired registers */
322: while (resultSet.next() && assignedUsers.size() < count) {
323: assignedUsers.add(resultSet.getString(1));
324: }
325:
326: return assignedUsers;
327: } catch (SQLException e) {
328: throw new InternalErrorException(e);
329: } finally {
330: SQLOperations.closeStatement(statement);
331: SQLOperations.closeResultSet(resultSet);
332: }
333: }
334:
335: /**
336: * Finds all groups. Implements Page-by-Page Iterator design pattern.
337: * @param connection the connection to the database
338: * @param startIndex the index of the group from
339: * which start to retrive groups.
340: * @param count the number of groups to retrive begining in the
341: * <code>startIndex</code> group.
342: * @throws InternalErrorException if a failure is detected.
343: * @return a <code>Collection</code> of <code>UserGroup</code> objects.
344: */
345: public Collection findAllGroups(Connection connection,
346: int startIndex, int count) throws InternalErrorException {
347:
348: Statement statement = null;
349: ResultSet resultSet = null;
350: String query = "SELECT *" + " FROM " + USER_GROUPS_TABLE_NAME
351: + " ORDER BY " + USER_GROUP_COLUMN_NAME;
352:
353: try {
354: statement = connection.createStatement();
355: resultSet = statement.executeQuery(query);
356: Collection groups = new ArrayList();
357:
358: /* Go to startIndex register */
359: int i = 0;
360: while (i < startIndex && resultSet.next())
361: i++;
362:
363: /* Get desired registers */
364: while (resultSet.next()
365: && ((groups.size() < count) || (count == -1))) {
366: groups.add(getUserGroupFromResultSet(resultSet));
367: }
368:
369: return groups;
370: } catch (SQLException e) {
371: throw new InternalErrorException(e);
372: } finally {
373: SQLOperations.closeStatement(statement);
374: SQLOperations.closeResultSet(resultSet);
375: }
376: }
377:
378: /**
379: * Finds a specific group by its identifier.
380: * @param connection the connection to the database
381: * @param userGroupIdentifier the user group identifier
382: * @throws InternalErrorException if a failure is detected.
383: * @return a <code>Collection</code> of <code>UserGroup</code> objects.
384: */
385: public UserGroup findGroup(Connection connection,
386: Long userGroupIdentifier) throws InternalErrorException,
387: InstanceNotFoundException {
388:
389: Statement statement = null;
390: ResultSet resultSet = null;
391: String query = "SELECT *" + " FROM " + USER_GROUPS_TABLE_NAME
392: + " WHERE " + USER_GROUP_IDENTIFIER_COLUMN_NAME + " = "
393: + userGroupIdentifier;
394:
395: try {
396: statement = connection.createStatement();
397: resultSet = statement.executeQuery(query);
398:
399: /* Get info of the user group */
400: UserGroup userGroup = null;
401: if (resultSet.next()) {
402: int col = 2;
403: String groupName = resultSet.getString(col++);
404: userGroup = new UserGroup(userGroupIdentifier,
405: groupName);
406: } else {
407: throw new InstanceNotFoundException(
408: userGroupIdentifier, UserGroup.class.getName());
409: }
410:
411: return userGroup;
412: } catch (SQLException e) {
413: throw new InternalErrorException(e);
414: } finally {
415: SQLOperations.closeStatement(statement);
416: SQLOperations.closeResultSet(resultSet);
417: }
418: }
419:
420: /**
421: * Finds a specific group by its name.
422: * @param connection the connection to the database
423: * @param userGroupName the user group name.
424: * @throws InternalErrorException if a failure is detected.
425: * @return a <code>Collection</code> of <code>UserGroup</code> objects.
426: */
427: public UserGroup findGroupByName(Connection connection,
428: String userGroupName) throws InternalErrorException,
429: InstanceNotFoundException {
430:
431: PreparedStatement statement = null;
432: ResultSet resultSet = null;
433: String query = "SELECT *" + " FROM " + USER_GROUPS_TABLE_NAME
434: + " WHERE " + USER_GROUP_COLUMN_NAME + " = ?";
435:
436: try {
437: statement = connection.prepareStatement(query);
438: int col = 1;
439: statement.setString(col++, userGroupName);
440: resultSet = statement.executeQuery();
441:
442: /* Get info of the user group */
443: UserGroup userGroup = null;
444: if (resultSet.next()) {
445: col = 1;
446: Long userGroupIdentifier = new Long(resultSet
447: .getLong(col++));
448: userGroup = new UserGroup(userGroupIdentifier,
449: userGroupName);
450: } else {
451: throw new InstanceNotFoundException(userGroupName,
452: UserGroup.class.getName());
453: }
454:
455: return userGroup;
456: } catch (SQLException e) {
457: throw new InternalErrorException(e);
458: } finally {
459: SQLOperations.closeStatement(statement);
460: SQLOperations.closeResultSet(resultSet);
461: }
462: }
463:
464: /**
465: * Adds a user to a group.
466: * @param connection the connection to the database
467: * @param loginName the user's login name.
468: * @param userGroupIdentifier the group where to add the user.
469: * @throws InternalErrorException if a failure is detected.
470: * @throws <code>InstanceNotFoundException</code> if the user group
471: * does not exist.
472: * @throws <code>UserAlreadyExistsInGroupException</code> if the user
473: * was previously assigned to this group.
474: */
475: public void addUserToGroup(Connection connection, String loginName,
476: Long userGroupIdentifier) throws InternalErrorException,
477: InstanceNotFoundException,
478: UserAlreadyExistsInGroupException {
479:
480: PreparedStatement statement = null;
481: String query = "INSERT INTO "
482: + USER_GROUPS_ASSIGNMENTS_TABLE_NAME + " ("
483: + LOGIN_COLUMN_NAME + ", "
484: + USER_GROUP_IDENTIFIER_COLUMN_NAME + ")"
485: + " VALUES (?, ?)";
486:
487: try {
488: /* Check that the group exists and user is not assigned to it */
489: if (!groupExists(connection, userGroupIdentifier)) {
490: throw new InstanceNotFoundException(
491: userGroupIdentifier, UserGroup.class.getName());
492: }
493: if (userIsAssignedToGroup(connection, loginName,
494: userGroupIdentifier)) {
495: throw new UserAlreadyExistsInGroupException(loginName,
496: userGroupIdentifier);
497: }
498:
499: /* Try to add the user to group */
500: statement = connection.prepareStatement(query);
501: int col = 1;
502: statement.setString(col++, loginName);
503: statement.setLong(col++, userGroupIdentifier.longValue());
504: statement.executeUpdate();
505: } catch (SQLException e) {
506: throw new InternalErrorException(e);
507: } finally {
508: SQLOperations.closeStatement(statement);
509: }
510: }
511:
512: /**
513: * Adds a new group of users.
514: * @param connection the connection to the database
515: * @param userGroup a <code>UserGroup</code> representing the new group
516: * of users
517: * @throws InternalErrorException if a failure is detected.
518: * @throws <code>DuplicateInstanceException</code> if the group
519: * already exists.
520: * @return a <code>UserGroup</code> with the added group, including its
521: * identifier.
522: */
523: public UserGroup addGroup(Connection connection, UserGroup userGroup)
524: throws InternalErrorException, DuplicateInstanceException {
525: PreparedStatement statement = null;
526: String query = "INSERT INTO " + USER_GROUPS_TABLE_NAME + " ("
527: + USER_GROUP_IDENTIFIER_COLUMN_NAME + ", "
528: + USER_GROUP_COLUMN_NAME + ")" + " VALUES (?, ?)";
529:
530: try {
531: /* Check user group does not exist */
532: String userGroupName = userGroup.getName();
533: if (groupExists(connection, userGroupName)) {
534: throw new DuplicateInstanceException(userGroupName,
535: UserGroup.class.getName());
536: }
537:
538: /* Try add group */
539: /* First, get a generated identifier */
540: EntityIdentifierGeneratorSingleton identifierGenerator = EntityIdentifierGeneratorSingleton
541: .getInstance();
542: Long userGroupIdentifier = identifierGenerator
543: .nextIdentifier(connection, USER_GROUPS_TABLE_NAME);
544:
545: statement = connection.prepareStatement(query);
546: int col = 1;
547: statement.setLong(col++, userGroupIdentifier.longValue());
548: statement.setString(col++, userGroupName);
549:
550: statement.executeUpdate();
551:
552: userGroup.setUserGroupIdentifier(userGroupIdentifier);
553: return userGroup;
554: } catch (SQLException e) {
555: throw new InternalErrorException(e);
556: } finally {
557: SQLOperations.closeStatement(statement);
558: }
559: }
560:
561: /**
562: * Removes a user from a group
563: * @param connection the connection to the database
564: * @param loginName the user's login name.
565: * @param userGroupIdentifier the group where to remove the user
566: * @throws InternalErrorException if a failure is detected.
567: * @throws <code>InstanceNotFoundException</code> if the user group
568: * does not exist.
569: * @throws <code>UserDoesNotExistInGroupException</code> if the user
570: * is not assigned to this group.
571: */
572: public void removeUserFromGroup(Connection connection,
573: String loginName, Long userGroupIdentifier)
574: throws InternalErrorException, InstanceNotFoundException,
575: UserDoesNotExistInGroupException {
576:
577: PreparedStatement statement = null;
578: String query = "DELETE FROM "
579: + USER_GROUPS_ASSIGNMENTS_TABLE_NAME + " WHERE ("
580: + LOGIN_COLUMN_NAME + " = ?) AND ("
581: + USER_GROUP_IDENTIFIER_COLUMN_NAME + " = ?)";
582:
583: try {
584: /* Check user group exists */
585: if (!groupExists(connection, userGroupIdentifier)) {
586: throw new InstanceNotFoundException(
587: userGroupIdentifier, UserGroup.class.getName());
588: }
589: /* Check user exists in group */
590: if (!userIsAssignedToGroup(connection, loginName,
591: userGroupIdentifier)) {
592: throw new UserDoesNotExistInGroupException(loginName,
593: userGroupIdentifier);
594: }
595:
596: /* Try remove user from group */
597: statement = connection.prepareStatement(query);
598: int col = 1;
599: statement.setString(col++, loginName);
600: statement.setLong(col++, userGroupIdentifier.longValue());
601: statement.executeUpdate();
602: } catch (SQLException e) {
603: throw new InternalErrorException(e);
604: } finally {
605: SQLOperations.closeStatement(statement);
606: }
607: }
608:
609: /**
610: * Removes a user group and the user assignments to it.
611: * @param connection the connection to the database
612: * @param userGroupIdentifier the group to remove
613: * @throws InternalErrorException if a failure is detected.
614: * @throws <code>InstanceNotFoundException</code> if the user group
615: * does not exist.
616: */
617: public void removeGroup(Connection connection,
618: Long userGroupIdentifier) throws InternalErrorException,
619: InstanceNotFoundException {
620:
621: Statement statement = null;
622: String query = "DELETE FROM " + USER_GROUPS_TABLE_NAME
623: + " WHERE " + USER_GROUP_IDENTIFIER_COLUMN_NAME + " = "
624: + userGroupIdentifier;
625:
626: try {
627: /* Check user group exists */
628: if (!groupExists(connection, userGroupIdentifier)) {
629: throw new InstanceNotFoundException(
630: userGroupIdentifier, UserGroup.class.getName());
631: }
632:
633: /* Try remove group */
634: statement = connection.createStatement();
635: statement.executeUpdate(query);
636: } catch (SQLException e) {
637: throw new InternalErrorException(e);
638: } finally {
639: SQLOperations.closeStatement(statement);
640: }
641: }
642:
643: /**
644: * Removes all groups assignments for a given user.
645: * @param connection the connection to the database.
646: * @param loginName the user login name.
647: * @throws InternalErrorException if a failure is detected.
648: */
649: public void removeAllGroupAssignments(Connection connection,
650: String loginName) throws InternalErrorException {
651:
652: PreparedStatement statement = null;
653: String query = "DELETE FROM "
654: + USER_GROUPS_ASSIGNMENTS_TABLE_NAME + " WHERE "
655: + LOGIN_COLUMN_NAME + " = ?";
656:
657: try {
658:
659: /* Try remove all group assignments for the given user */
660: statement = connection.prepareStatement(query);
661: int col = 1;
662: statement.setString(col++, loginName);
663: statement.executeUpdate();
664:
665: } catch (SQLException e) {
666: throw new InternalErrorException(e);
667: } finally {
668: SQLOperations.closeStatement(statement);
669: }
670: }
671:
672: /**
673: * Updates data of a user group
674: * @param connection the connection to the database
675: * @param userGroup the updated user group information
676: * @throws InternalErrorException if a failure is detected.
677: * @throws <code>InstanceNotFoundException</code> if the user group
678: * does not exist.
679: */
680: public void updateGroup(Connection connection, UserGroup userGroup)
681: throws InternalErrorException, DuplicateInstanceException,
682: InstanceNotFoundException {
683:
684: PreparedStatement statement = null;
685: String query = "UPDATE " + USER_GROUPS_TABLE_NAME + " SET "
686: + USER_GROUP_COLUMN_NAME + " = ?" + " WHERE "
687: + USER_GROUP_IDENTIFIER_COLUMN_NAME + " = ?";
688:
689: try {
690: /* Check user group exists */
691: Long userGroupIdentifier = userGroup
692: .getUserGroupIdentifier();
693: if (!groupExists(connection, userGroupIdentifier)) {
694: throw new InstanceNotFoundException(
695: userGroupIdentifier, UserGroup.class.getName());
696: }
697: /* Check there isn't another group with the new name */
698: if (groupExists(connection, userGroup.getName())) {
699: throw new DuplicateInstanceException(userGroup
700: .getName(), UserGroup.class.getName());
701: }
702:
703: /* Try update group */
704: statement = connection.prepareStatement(query);
705: int col = 1;
706: statement.setString(col++, userGroup.getName());
707: statement.setLong(col++, userGroupIdentifier.longValue());
708: statement.executeUpdate();
709: } catch (SQLException e) {
710: throw new InternalErrorException(e);
711: } finally {
712: SQLOperations.closeStatement(statement);
713: }
714: }
715:
716: /**
717: * Gets a <code>UserGroup</code> object from a <code>ResultSet</code>.
718: * @param resultSet the <code>ResultSet</code> that has user groups
719: * information
720: * @throws <code>SQLException</code> if an error eccured while accessing
721: * the <code>ResultSet</code>
722: * @return A <code>UserGroup</code> object with the current
723: * user groups table row in the <code>ResultSet</code>
724: */
725: private UserGroup getUserGroupFromResultSet(ResultSet resultSet)
726: throws SQLException {
727: int col = 1;
728: Long userGroupIdentifier = new Long(resultSet.getLong(col++));
729: String userGroupName = resultSet.getString(col++);
730: return (new UserGroup(userGroupIdentifier, userGroupName));
731: }
732: }
|