0001: /* Copyright 2001, 2002 The JA-SIG Collaborative. All rights reserved.
0002: * See license distributed with this file and
0003: * available online at http://www.uportal.org/license.html
0004: */
0005:
0006: package org.jasig.portal.groups;
0007:
0008: import java.sql.Connection;
0009: import java.sql.PreparedStatement;
0010: import java.sql.ResultSet;
0011: import java.sql.SQLException;
0012: import java.sql.Statement;
0013: import java.util.ArrayList;
0014: import java.util.Collection;
0015: import java.util.Iterator;
0016: import java.util.List;
0017:
0018: import org.apache.commons.logging.Log;
0019: import org.apache.commons.logging.LogFactory;
0020: import org.jasig.portal.EntityIdentifier;
0021: import org.jasig.portal.EntityTypes;
0022: import org.jasig.portal.RDBMServices;
0023: import org.jasig.portal.services.GroupService;
0024: import org.jasig.portal.services.SequenceGenerator;
0025: import org.jasig.portal.utils.SqlTransaction;
0026:
0027: /**
0028: * Store for <code>EntityGroupImpl</code>.
0029: * @author Dan Ellentuck
0030: * @version $Revision: 36810 $
0031: */
0032: public class RDBMEntityGroupStore implements IEntityGroupStore,
0033: IGroupConstants {
0034: private static final Log log = LogFactory
0035: .getLog(RDBMEntityGroupStore.class);
0036: private static RDBMEntityGroupStore singleton;
0037:
0038: // Constant SQL strings:
0039: private static String EQ = " = ";
0040: private static String QUOTE = "'";
0041: private static String EQUALS_PARAM = EQ + "?";
0042:
0043: // Constant strings for GROUP table:
0044: private static String GROUP_TABLE = "UP_GROUP";
0045: private static String GROUP_TABLE_ALIAS = "T1";
0046: private static String GROUP_TABLE_WITH_ALIAS = GROUP_TABLE + " "
0047: + GROUP_TABLE_ALIAS;
0048: private static String GROUP_ID_COLUMN = "GROUP_ID";
0049: private static String GROUP_CREATOR_COLUMN = "CREATOR_ID";
0050: private static String GROUP_TYPE_COLUMN = "ENTITY_TYPE_ID";
0051: private static String GROUP_NAME_COLUMN = "GROUP_NAME";
0052: private static String GROUP_DESCRIPTION_COLUMN = "DESCRIPTION";
0053:
0054: // SQL strings for GROUP crud:
0055: private static String allGroupColumns;
0056: private static String allGroupColumnsWithTableAlias;
0057: private static String countAMemberGroupSql;
0058: private static String countMemberGroupsNamedSql;
0059: private static String countAMemberEntitySql;
0060: private static String findContainingGroupsForEntitySql;
0061: private static String findContainingGroupsForGroupSql;
0062: private static String findGroupSql;
0063: private static String findGroupsByCreatorSql;
0064: private static String findMemberGroupKeysSql;
0065: private static String findMemberGroupsSql;
0066: private static String insertGroupSql;
0067: private static String updateGroupSql;
0068:
0069: // Constant strings for MEMBERS table:
0070: private static String MEMBER_TABLE = "UP_GROUP_MEMBERSHIP";
0071: private static String MEMBER_TABLE_ALIAS = "T2";
0072: private static String MEMBER_TABLE_WITH_ALIAS = MEMBER_TABLE + " "
0073: + MEMBER_TABLE_ALIAS;
0074: private static String MEMBER_GROUP_ID_COLUMN = "GROUP_ID";
0075: private static String MEMBER_MEMBER_SERVICE_COLUMN = "MEMBER_SERVICE";
0076: private static String MEMBER_MEMBER_KEY_COLUMN = "MEMBER_KEY";
0077: private static String MEMBER_IS_GROUP_COLUMN = "MEMBER_IS_GROUP";
0078: private static String MEMBER_IS_ENTITY = "F";
0079: private static String MEMBER_IS_GROUP = "T";
0080: private static String GROUP_NODE_SEPARATOR;
0081:
0082: // SQL strings for group MEMBERS crud:
0083: private static String allMemberColumns;
0084: private static String deleteMembersInGroupSql;
0085: private static String deleteMemberGroupSql;
0086: private static String deleteMemberEntitySql;
0087: private static String insertMemberSql;
0088:
0089: // SQL group search string
0090: private static String searchGroupsPartial = "SELECT "
0091: + GROUP_ID_COLUMN + " FROM " + GROUP_TABLE + " WHERE "
0092: + GROUP_TYPE_COLUMN + "=? AND UPPER(" + GROUP_NAME_COLUMN
0093: + ") LIKE UPPER(?)";
0094: private static String searchGroups = "SELECT " + GROUP_ID_COLUMN
0095: + " FROM " + GROUP_TABLE + " WHERE " + GROUP_TYPE_COLUMN
0096: + "=? AND UPPER(" + GROUP_NAME_COLUMN + ") = UPPER(?)";
0097:
0098: /**
0099: * RDBMEntityGroupStore constructor.
0100: */
0101: public RDBMEntityGroupStore() {
0102: super ();
0103: initialize();
0104: }
0105:
0106: /**
0107: * Get the node separator character from the GroupServiceConfiguration.
0108: * Default it to IGroupConstants.NODE_SEPARATOR.
0109: */
0110: private void initialize() {
0111: String sep;
0112: try {
0113: sep = GroupServiceConfiguration.getConfiguration()
0114: .getNodeSeparator();
0115: } catch (Exception ex) {
0116: sep = NODE_SEPARATOR;
0117: }
0118: GROUP_NODE_SEPARATOR = sep;
0119: if (log.isInfoEnabled()) {
0120: String msg = "RDBMEntityGroupStore.initialize(): Node separator set to "
0121: + sep;
0122: log.info(msg);
0123: }
0124: }
0125:
0126: /**
0127: * @param conn java.sql.Connection
0128: * @exception java.sql.SQLException
0129: */
0130: protected static void commit(Connection conn)
0131: throws java.sql.SQLException {
0132: SqlTransaction.commit(conn);
0133: }
0134:
0135: /**
0136: * Answers if <code>IGroupMember</code> member is a member of <code>group</code>.
0137: * @return boolean
0138: * @param group org.jasig.portal.groups.IEntityGroup
0139: * @param member org.jasig.portal.groups.IGroupMember
0140: */
0141: public boolean contains(IEntityGroup group, IGroupMember member)
0142: throws GroupsException {
0143: return (member.isGroup()) ? containsGroup(group,
0144: (IEntityGroup) member) : containsEntity(group, member);
0145: }
0146:
0147: private boolean containsEntity(IEntityGroup group,
0148: IGroupMember member) throws GroupsException {
0149: String groupKey = group.getLocalKey();
0150: String memberKey = member.getKey();
0151: Connection conn = RDBMServices.getConnection();
0152: try {
0153: String sql = getCountAMemberEntitySql();
0154: PreparedStatement ps = conn.prepareStatement(sql);
0155: try {
0156: ps.clearParameters();
0157: ps.setString(1, groupKey);
0158: ps.setString(2, memberKey);
0159: if (log.isDebugEnabled())
0160: log.debug("RDBMEntityGroupStore.containsEntity(): "
0161: + ps + " (" + groupKey + ", " + memberKey
0162: + ")");
0163: ResultSet rs = ps.executeQuery();
0164: try {
0165: return (rs.next()) && (rs.getInt(1) > 0);
0166: } finally {
0167: rs.close();
0168: }
0169: } finally {
0170: ps.close();
0171: }
0172: } catch (Exception e) {
0173: log.error("RDBMEntityGroupStore.containsEntity(): " + e);
0174: throw new GroupsException(
0175: "Problem retrieving data from store: " + e);
0176: } finally {
0177: RDBMServices.releaseConnection(conn);
0178: }
0179: }
0180:
0181: private boolean containsGroup(IEntityGroup group,
0182: IEntityGroup member) throws GroupsException {
0183: String memberService = member.getServiceName().toString();
0184: String groupKey = group.getLocalKey();
0185: String memberKey = member.getLocalKey();
0186: Connection conn = RDBMServices.getConnection();
0187: try {
0188: String sql = getCountAMemberGroupSql();
0189: PreparedStatement ps = conn.prepareStatement(sql);
0190: try {
0191: ps.clearParameters();
0192: ps.setString(1, groupKey);
0193: ps.setString(2, memberKey);
0194: ps.setString(3, memberService);
0195: if (log.isDebugEnabled())
0196: log.debug("RDBMEntityGroupStore.containsGroup(): "
0197: + ps + " (" + groupKey + ", " + memberKey
0198: + ", " + memberService + ")");
0199: ResultSet rs = ps.executeQuery();
0200: try {
0201: return (rs.next()) && (rs.getInt(1) > 0);
0202: } finally {
0203: rs.close();
0204: }
0205: } finally {
0206: ps.close();
0207: }
0208: } catch (Exception e) {
0209: log.error("RDBMEntityGroupStore.containsGroup(): " + e);
0210: throw new GroupsException(
0211: "Problem retrieving data from store: " + e);
0212: } finally {
0213: RDBMServices.releaseConnection(conn);
0214: }
0215: }
0216:
0217: public boolean containsGroupNamed(IEntityGroup containingGroup,
0218: String memberName) throws GroupsException {
0219: String groupKey = containingGroup.getLocalKey();
0220: String service = containingGroup.getServiceName().toString();
0221:
0222: Connection conn = RDBMServices.getConnection();
0223: try {
0224: String sql = getCountMemberGroupsNamedSql();
0225: PreparedStatement ps = conn.prepareStatement(sql);
0226: try {
0227: ps.clearParameters();
0228: ps.setString(1, groupKey);
0229: ps.setString(2, memberName);
0230: ps.setString(3, service);
0231: if (log.isDebugEnabled())
0232: log
0233: .debug("RDBMEntityGroupStore.containsGroupNamed(): "
0234: + ps
0235: + " ("
0236: + groupKey
0237: + ", "
0238: + memberName + ", " + service + ")");
0239: ResultSet rs = ps.executeQuery();
0240: try {
0241: return (rs.next()) && (rs.getInt(1) > 0);
0242: } finally {
0243: rs.close();
0244: }
0245: } finally {
0246: ps.close();
0247: }
0248: } catch (Exception e) {
0249: log.error("RDBMEntityGroupStore.containsGroup(): " + e);
0250: throw new GroupsException(
0251: "Problem retrieving data from store: " + e);
0252: } finally {
0253: RDBMServices.releaseConnection(conn);
0254: }
0255: }
0256:
0257: /**
0258: * If this entity exists, delete it.
0259: * @param group org.jasig.portal.groups.IEntityGroup
0260: */
0261: public void delete(IEntityGroup group) throws GroupsException {
0262: if (existsInDatabase(group)) {
0263: try {
0264: primDelete(group);
0265: } catch (SQLException sqle) {
0266: throw new GroupsException("Problem deleting " + group,
0267: sqle);
0268: }
0269: }
0270: }
0271:
0272: /**
0273: * Answer if the IEntityGroup entity exists in the database.
0274: * @return boolean
0275: * @param group IEntityGroup
0276: */
0277: private boolean existsInDatabase(IEntityGroup group)
0278: throws GroupsException {
0279: IEntityGroup ug = this .find(group.getLocalKey());
0280: return ug != null;
0281: }
0282:
0283: /**
0284: * Find and return an instance of the group.
0285: * @param groupID the group ID
0286: * @return org.jasig.portal.groups.IEntityGroup
0287: */
0288: public IEntityGroup find(String groupID) throws GroupsException {
0289: return primFind(groupID, false);
0290: }
0291:
0292: /**
0293: * Find the groups that this entity belongs to.
0294: * @param ent the entity in question
0295: * @return java.util.Iterator
0296: */
0297: public java.util.Iterator findContainingGroups(IEntity ent)
0298: throws GroupsException {
0299: String memberKey = ent.getKey();
0300: Integer type = EntityTypes.getEntityTypeID(ent.getLeafType());
0301: return findContainingGroupsForEntity(memberKey, type.intValue());
0302: }
0303:
0304: /**
0305: * Find the groups that this group belongs to.
0306: * @param group org.jasig.portal.groups.IEntityGroup
0307: * @return java.util.Iterator
0308: */
0309: public java.util.Iterator findContainingGroups(IEntityGroup group)
0310: throws GroupsException {
0311: String memberKey = group.getLocalKey();
0312: String serviceName = group.getServiceName().toString();
0313: Integer type = EntityTypes.getEntityTypeID(group.getLeafType());
0314: return findContainingGroupsForGroup(serviceName, memberKey,
0315: type.intValue());
0316: }
0317:
0318: /**
0319: * Find the groups that this group member belongs to.
0320: * @param gm the group member in question
0321: * @return java.util.Iterator
0322: */
0323: public Iterator findContainingGroups(IGroupMember gm)
0324: throws GroupsException {
0325: if (gm.isGroup()) {
0326: IEntityGroup group = (IEntityGroup) gm;
0327: return findContainingGroups(group);
0328: } else {
0329: IEntity ent = (IEntity) gm;
0330: return findContainingGroups(ent);
0331: }
0332: }
0333:
0334: /**
0335: * Find the groups associated with this member key.
0336: * @param memberKey
0337: * @param type
0338: * @return java.util.Iterator
0339: */
0340: private java.util.Iterator findContainingGroupsForEntity(
0341: String memberKey, int type) throws GroupsException {
0342: java.sql.Connection conn = null;
0343: Collection groups = new ArrayList();
0344: IEntityGroup eg = null;
0345:
0346: try {
0347: conn = RDBMServices.getConnection();
0348: String sql = getFindContainingGroupsForEntitySql();
0349: PreparedStatement ps = conn.prepareStatement(sql);
0350: try {
0351: ps.setString(1, memberKey);
0352: ps.setInt(2, type);
0353: if (log.isDebugEnabled())
0354: log
0355: .debug("RDBMEntityGroupStore.findContainingGroupsForEntity(): "
0356: + ps
0357: + " ("
0358: + memberKey
0359: + ", "
0360: + type + ", memberIsGroup = F)");
0361: java.sql.ResultSet rs = ps.executeQuery();
0362: try {
0363: while (rs.next()) {
0364: eg = instanceFromResultSet(rs);
0365: groups.add(eg);
0366: }
0367: } finally {
0368: rs.close();
0369: }
0370: } finally {
0371: ps.close();
0372: }
0373: } catch (Exception e) {
0374: log
0375: .error("RDBMEntityGroupStore.findContainingGroupsForEntity(): "
0376: + e);
0377: throw new GroupsException(
0378: "Problem retrieving containing groups: " + e);
0379: }
0380:
0381: finally {
0382: RDBMServices.releaseConnection(conn);
0383: }
0384:
0385: return groups.iterator();
0386: }
0387:
0388: /**
0389: * Find the groups associated with this member key.
0390: * @param serviceName
0391: * @param memberKey
0392: * @param type
0393: * @return java.util.Iterator
0394: */
0395: private java.util.Iterator findContainingGroupsForGroup(
0396: String serviceName, String memberKey, int type)
0397: throws GroupsException {
0398: java.sql.Connection conn = null;
0399: Collection groups = new ArrayList();
0400: IEntityGroup eg = null;
0401:
0402: try {
0403: conn = RDBMServices.getConnection();
0404: String sql = getFindContainingGroupsForGroupSql();
0405: PreparedStatement ps = conn.prepareStatement(sql);
0406: try {
0407: ps.setString(1, serviceName);
0408: ps.setString(2, memberKey);
0409: ps.setInt(3, type);
0410: if (log.isDebugEnabled())
0411: log
0412: .debug("RDBMEntityGroupStore.findContainingGroupsForGroup(): "
0413: + ps
0414: + " ("
0415: + serviceName
0416: + ", "
0417: + memberKey
0418: + ", "
0419: + type
0420: + ", memberIsGroup = T)");
0421: java.sql.ResultSet rs = ps.executeQuery();
0422: try {
0423: while (rs.next()) {
0424: eg = instanceFromResultSet(rs);
0425: groups.add(eg);
0426: }
0427: } finally {
0428: rs.close();
0429: }
0430: } finally {
0431: ps.close();
0432: }
0433: } catch (Exception e) {
0434: log
0435: .error("RDBMEntityGroupStore.findContainingGroupsForGroup(): "
0436: + e);
0437: throw new GroupsException(
0438: "Problem retrieving containing groups: " + e);
0439: }
0440:
0441: finally {
0442: RDBMServices.releaseConnection(conn);
0443: }
0444:
0445: return groups.iterator();
0446: }
0447:
0448: /**
0449: * Find the <code>IEntities</code> that are members of the <code>IEntityGroup</code>.
0450: * @param group the entity group in question
0451: * @return java.util.Iterator
0452: */
0453: public Iterator findEntitiesForGroup(IEntityGroup group)
0454: throws GroupsException {
0455: Collection entities = new ArrayList();
0456: Connection conn = null;
0457: String groupID = group.getLocalKey();
0458: Class cls = group.getLeafType();
0459:
0460: try {
0461: conn = RDBMServices.getConnection();
0462: Statement stmnt = conn.createStatement();
0463: try {
0464:
0465: String query = "SELECT " + MEMBER_MEMBER_KEY_COLUMN
0466: + " FROM " + MEMBER_TABLE + " WHERE "
0467: + MEMBER_GROUP_ID_COLUMN + " = '" + groupID
0468: + "' AND " + MEMBER_IS_GROUP_COLUMN + " = '"
0469: + MEMBER_IS_ENTITY + "'";
0470:
0471: ResultSet rs = stmnt.executeQuery(query);
0472: try {
0473: while (rs.next()) {
0474: String key = rs.getString(1);
0475: IEntity e = newEntity(cls, key);
0476: entities.add(e);
0477: }
0478: } finally {
0479: rs.close();
0480: }
0481: } finally {
0482: stmnt.close();
0483: }
0484: } catch (SQLException sqle) {
0485: log.error(
0486: "Problem retrieving Entities for Group: " + group,
0487: sqle);
0488: throw new GroupsException(
0489: "Problem retrieving Entities for Group", sqle);
0490: } finally {
0491: RDBMServices.releaseConnection(conn);
0492: }
0493:
0494: return entities.iterator();
0495: }
0496:
0497: /**
0498: * Find the groups with this creatorID.
0499: * @param creatorID
0500: * @return java.util.Iterator
0501: */
0502: public java.util.Iterator findGroupsByCreator(String creatorID)
0503: throws GroupsException {
0504: java.sql.Connection conn = null;
0505: Collection groups = new ArrayList();
0506: IEntityGroup eg = null;
0507:
0508: try {
0509: conn = RDBMServices.getConnection();
0510: String sql = getFindGroupsByCreatorSql();
0511: PreparedStatement ps = conn.prepareStatement(sql);
0512: try {
0513: ps.setString(1, creatorID);
0514: if (log.isDebugEnabled())
0515: log
0516: .debug("RDBMEntityGroupStore.findGroupsByCreator(): "
0517: + ps);
0518: ResultSet rs = ps.executeQuery();
0519: try {
0520: while (rs.next()) {
0521: eg = instanceFromResultSet(rs);
0522: groups.add(eg);
0523: }
0524: } finally {
0525: rs.close();
0526: }
0527: } finally {
0528: ps.close();
0529: }
0530: } catch (Exception e) {
0531: log.error("RDBMEntityGroupStore.findGroupsByCreator(): "
0532: + e);
0533: throw new GroupsException("Problem retrieving groups: " + e);
0534: }
0535:
0536: finally {
0537: RDBMServices.releaseConnection(conn);
0538: }
0539:
0540: return groups.iterator();
0541: }
0542:
0543: /**
0544: * Find and return an instance of the group.
0545: * @param groupID the group ID
0546: * @return org.jasig.portal.groups.ILockableEntityGroup
0547: */
0548: public ILockableEntityGroup findLockable(String groupID)
0549: throws GroupsException {
0550: return (ILockableEntityGroup) primFind(groupID, true);
0551: }
0552:
0553: /**
0554: * Find the keys of groups that are members of group.
0555: * @param group the org.jasig.portal.groups.IEntityGroup
0556: * @return String[]
0557: */
0558: public String[] findMemberGroupKeys(IEntityGroup group)
0559: throws GroupsException {
0560: java.sql.Connection conn = null;
0561: Collection groupKeys = new ArrayList();
0562: String groupKey = null;
0563:
0564: try {
0565: conn = RDBMServices.getConnection();
0566: String sql = getFindMemberGroupKeysSql();
0567: PreparedStatement ps = conn.prepareStatement(sql);
0568: try {
0569: ps.setString(1, group.getLocalKey());
0570: if (log.isDebugEnabled())
0571: log
0572: .debug("RDBMEntityGroupStore.findMemberGroupKeys(): "
0573: + ps
0574: + " ("
0575: + group.getLocalKey()
0576: + ")");
0577: java.sql.ResultSet rs = ps.executeQuery();
0578: try {
0579: while (rs.next()) {
0580: groupKey = rs.getString(1)
0581: + GROUP_NODE_SEPARATOR
0582: + rs.getString(2);
0583: groupKeys.add(groupKey);
0584: }
0585: } finally {
0586: rs.close();
0587: }
0588: } finally {
0589: ps.close();
0590: }
0591: } catch (Exception sqle) {
0592: log.error("RDBMEntityGroupStore.findMemberGroupKeys(): "
0593: + sqle);
0594: throw new GroupsException(
0595: "Problem retrieving member group keys: " + sqle);
0596: } finally {
0597: RDBMServices.releaseConnection(conn);
0598: }
0599:
0600: return (String[]) groupKeys
0601: .toArray(new String[groupKeys.size()]);
0602: }
0603:
0604: /**
0605: * Find the IUserGroups that are members of the group.
0606: * @param group org.jasig.portal.groups.IEntityGroup
0607: * @return java.util.Iterator
0608: */
0609: public Iterator findMemberGroups(IEntityGroup group)
0610: throws GroupsException {
0611: java.sql.Connection conn = null;
0612: Collection groups = new ArrayList();
0613: IEntityGroup eg = null;
0614: String serviceName = group.getServiceName().toString();
0615: String localKey = group.getLocalKey();
0616:
0617: try {
0618: conn = RDBMServices.getConnection();
0619: String sql = getFindMemberGroupsSql();
0620: PreparedStatement ps = conn.prepareStatement(sql);
0621: try {
0622: ps.setString(1, localKey);
0623: ps.setString(2, serviceName);
0624: if (log.isDebugEnabled())
0625: log
0626: .debug("RDBMEntityGroupStore.findMemberGroups(): "
0627: + ps
0628: + " ("
0629: + localKey
0630: + ", "
0631: + serviceName + ")");
0632: java.sql.ResultSet rs = ps.executeQuery();
0633: try {
0634: while (rs.next()) {
0635: eg = instanceFromResultSet(rs);
0636: groups.add(eg);
0637: }
0638: } finally {
0639: rs.close();
0640: }
0641: } finally {
0642: ps.close();
0643: }
0644: } catch (Exception sqle) {
0645: log.error("RDBMEntityGroupStore.findMemberGroups(): "
0646: + sqle);
0647: throw new GroupsException(
0648: "Problem retrieving member groups: " + sqle);
0649: } finally {
0650: RDBMServices.releaseConnection(conn);
0651: }
0652:
0653: return groups.iterator();
0654: }
0655:
0656: /**
0657: * @return java.lang.String
0658: */
0659: private static java.lang.String getAllGroupColumns() {
0660:
0661: if (allGroupColumns == null) {
0662: StringBuffer buff = new StringBuffer(100);
0663: buff.append(GROUP_ID_COLUMN);
0664: buff.append(", ");
0665: buff.append(GROUP_CREATOR_COLUMN);
0666: buff.append(", ");
0667: buff.append(GROUP_TYPE_COLUMN);
0668: buff.append(", ");
0669: buff.append(GROUP_NAME_COLUMN);
0670: buff.append(", ");
0671: buff.append(GROUP_DESCRIPTION_COLUMN);
0672:
0673: allGroupColumns = buff.toString();
0674: }
0675: return allGroupColumns;
0676: }
0677:
0678: /**
0679: * @return java.lang.String
0680: */
0681: private static java.lang.String getAllGroupColumnsWithTableAlias() {
0682:
0683: if (allGroupColumnsWithTableAlias == null) {
0684: StringBuffer buff = new StringBuffer(100);
0685: buff.append(groupAlias(GROUP_ID_COLUMN));
0686: buff.append(", ");
0687: buff.append(groupAlias(GROUP_CREATOR_COLUMN));
0688: buff.append(", ");
0689: buff.append(groupAlias(GROUP_TYPE_COLUMN));
0690: buff.append(", ");
0691: buff.append(groupAlias(GROUP_NAME_COLUMN));
0692: buff.append(", ");
0693: buff.append(groupAlias(GROUP_DESCRIPTION_COLUMN));
0694:
0695: allGroupColumnsWithTableAlias = buff.toString();
0696: }
0697: return allGroupColumnsWithTableAlias;
0698: }
0699:
0700: /**
0701: * @return java.lang.String
0702: */
0703: private static java.lang.String getAllMemberColumns() {
0704: if (allMemberColumns == null) {
0705: StringBuffer buff = new StringBuffer(100);
0706:
0707: buff.append(MEMBER_GROUP_ID_COLUMN);
0708: buff.append(", ");
0709: buff.append(MEMBER_MEMBER_SERVICE_COLUMN);
0710: buff.append(", ");
0711: buff.append(MEMBER_MEMBER_KEY_COLUMN);
0712: buff.append(", ");
0713: buff.append(MEMBER_IS_GROUP_COLUMN);
0714:
0715: allMemberColumns = buff.toString();
0716: }
0717: return allMemberColumns;
0718: }
0719:
0720: /**
0721: * @return java.lang.String
0722: */
0723: private static java.lang.String getCountAMemberEntitySql() {
0724: if (countAMemberEntitySql == null) {
0725: StringBuffer buff = new StringBuffer(100);
0726: buff.append("SELECT COUNT(*) FROM " + MEMBER_TABLE);
0727: buff.append(" WHERE " + MEMBER_GROUP_ID_COLUMN
0728: + EQUALS_PARAM);
0729: buff.append(" AND " + MEMBER_MEMBER_KEY_COLUMN
0730: + EQUALS_PARAM);
0731: buff.append(" AND " + MEMBER_IS_GROUP_COLUMN + EQ
0732: + sqlQuote(MEMBER_IS_ENTITY));
0733: countAMemberEntitySql = buff.toString();
0734: }
0735: return countAMemberEntitySql;
0736: }
0737:
0738: /**
0739: * @return java.lang.String
0740: */
0741: private static java.lang.String getCountAMemberGroupSql() {
0742: if (countAMemberGroupSql == null) {
0743: StringBuffer buff = new StringBuffer(100);
0744: buff.append("SELECT COUNT(*) FROM " + MEMBER_TABLE);
0745: buff.append(" WHERE " + MEMBER_GROUP_ID_COLUMN
0746: + EQUALS_PARAM);
0747: buff.append(" AND " + MEMBER_MEMBER_KEY_COLUMN
0748: + EQUALS_PARAM);
0749: buff.append(" AND " + MEMBER_MEMBER_SERVICE_COLUMN
0750: + EQUALS_PARAM);
0751: buff.append(" AND " + MEMBER_IS_GROUP_COLUMN + EQ
0752: + sqlQuote(MEMBER_IS_GROUP));
0753: countAMemberGroupSql = buff.toString();
0754: }
0755: return countAMemberGroupSql;
0756: }
0757:
0758: /**
0759: * @return java.lang.String
0760: */
0761: private static java.lang.String getCountMemberGroupsNamedSql() {
0762: if (countMemberGroupsNamedSql == null) {
0763: StringBuffer buff = new StringBuffer(100);
0764: buff.append("SELECT COUNT (*) FROM ");
0765: buff.append(GROUP_TABLE_WITH_ALIAS + ", "
0766: + MEMBER_TABLE_WITH_ALIAS);
0767: buff.append(" WHERE " + groupAlias(GROUP_ID_COLUMN) + EQ);
0768: buff.append(memberAlias(MEMBER_MEMBER_KEY_COLUMN));
0769: buff.append(" AND " + memberAlias(MEMBER_GROUP_ID_COLUMN)
0770: + EQUALS_PARAM);
0771: buff.append(" AND " + groupAlias(GROUP_NAME_COLUMN)
0772: + EQUALS_PARAM);
0773: buff.append(" AND "
0774: + memberAlias(MEMBER_MEMBER_SERVICE_COLUMN)
0775: + EQUALS_PARAM);
0776: countMemberGroupsNamedSql = buff.toString();
0777: }
0778: return countMemberGroupsNamedSql;
0779: }
0780:
0781: /**
0782: * @return java.lang.String
0783: */
0784: private static java.lang.String getDeleteGroupSql(IEntityGroup group) {
0785: StringBuffer buff = new StringBuffer(100);
0786: buff.append("DELETE FROM ");
0787: buff.append(GROUP_TABLE);
0788: buff.append(" WHERE ");
0789: buff.append(GROUP_ID_COLUMN + EQ
0790: + sqlQuote(group.getLocalKey()));
0791: return buff.toString();
0792: }
0793:
0794: /**
0795: * @return java.lang.String
0796: */
0797: private static java.lang.String getDeleteMemberEntitySql() {
0798: if (deleteMemberEntitySql == null) {
0799: StringBuffer buff = new StringBuffer(100);
0800: buff.append("DELETE FROM ");
0801: buff.append(MEMBER_TABLE);
0802: buff.append(" WHERE ");
0803: buff.append(MEMBER_GROUP_ID_COLUMN + EQUALS_PARAM);
0804: buff.append(" AND ");
0805: buff.append(MEMBER_MEMBER_KEY_COLUMN + EQUALS_PARAM);
0806: buff.append(" AND ");
0807: buff.append(MEMBER_IS_GROUP_COLUMN + EQ
0808: + sqlQuote(MEMBER_IS_ENTITY));
0809:
0810: deleteMemberEntitySql = buff.toString();
0811: }
0812: return deleteMemberEntitySql;
0813: }
0814:
0815: /**
0816: * @return java.lang.String
0817: */
0818: private static java.lang.String getDeleteMemberGroupSql() {
0819: if (deleteMemberGroupSql == null) {
0820: StringBuffer buff = new StringBuffer(100);
0821: buff.append("DELETE FROM ");
0822: buff.append(MEMBER_TABLE);
0823: buff.append(" WHERE ");
0824: buff.append(MEMBER_GROUP_ID_COLUMN + EQUALS_PARAM);
0825: buff.append(" AND ");
0826: buff.append(MEMBER_MEMBER_SERVICE_COLUMN + EQUALS_PARAM);
0827: buff.append(" AND ");
0828: buff.append(MEMBER_MEMBER_KEY_COLUMN + EQUALS_PARAM);
0829: buff.append(" AND ");
0830: buff.append(MEMBER_IS_GROUP_COLUMN + EQ
0831: + sqlQuote(MEMBER_IS_GROUP));
0832: deleteMemberGroupSql = buff.toString();
0833: }
0834: return deleteMemberGroupSql;
0835: }
0836:
0837: /**
0838: * @return java.lang.String
0839: */
0840: private static java.lang.String getDeleteMembersInGroupSql() {
0841: if (deleteMembersInGroupSql == null) {
0842: StringBuffer buff = new StringBuffer(100);
0843: buff.append("DELETE FROM ");
0844: buff.append(MEMBER_TABLE);
0845: buff.append(" WHERE ");
0846: buff.append(GROUP_ID_COLUMN + EQ);
0847:
0848: deleteMembersInGroupSql = buff.toString();
0849: }
0850: return deleteMembersInGroupSql;
0851: }
0852:
0853: /**
0854: * @return java.lang.String
0855: */
0856: private static java.lang.String getDeleteMembersInGroupSql(
0857: IEntityGroup group) {
0858: return getDeleteMembersInGroupSql()
0859: + sqlQuote(group.getLocalKey());
0860: }
0861:
0862: /**
0863: * @return java.lang.String
0864: */
0865: private static java.lang.String getFindContainingGroupsForEntitySql() {
0866: if (findContainingGroupsForEntitySql == null) {
0867: StringBuffer buff = new StringBuffer(500);
0868: buff.append("SELECT ");
0869: buff.append(getAllGroupColumnsWithTableAlias());
0870: buff.append(" FROM " + GROUP_TABLE_WITH_ALIAS + ", "
0871: + MEMBER_TABLE_WITH_ALIAS);
0872: buff.append(" WHERE ");
0873: buff.append(groupAlias(GROUP_ID_COLUMN) + EQ);
0874: buff.append(memberAlias(MEMBER_GROUP_ID_COLUMN));
0875: buff.append(" AND ");
0876: buff.append(memberAlias(MEMBER_MEMBER_KEY_COLUMN)
0877: + EQUALS_PARAM);
0878: buff.append(" AND ");
0879: buff.append(groupAlias(GROUP_TYPE_COLUMN) + EQUALS_PARAM);
0880: buff.append(" AND ");
0881: buff.append(memberAlias(MEMBER_IS_GROUP_COLUMN) + EQ
0882: + sqlQuote(MEMBER_IS_ENTITY));
0883:
0884: findContainingGroupsForEntitySql = buff.toString();
0885: }
0886: return findContainingGroupsForEntitySql;
0887: }
0888:
0889: /**
0890: * @return java.lang.String
0891: */
0892: private static java.lang.String getFindContainingGroupsForGroupSql() {
0893: if (findContainingGroupsForGroupSql == null) {
0894: StringBuffer buff = new StringBuffer(500);
0895: buff.append("SELECT ");
0896: buff.append(getAllGroupColumnsWithTableAlias());
0897: buff.append(" FROM ");
0898: buff.append(GROUP_TABLE_WITH_ALIAS);
0899: buff.append(", ");
0900: buff.append(MEMBER_TABLE_WITH_ALIAS);
0901: buff.append(" WHERE ");
0902: buff.append(groupAlias(GROUP_ID_COLUMN) + EQ);
0903: buff.append(memberAlias(MEMBER_GROUP_ID_COLUMN));
0904: buff.append(" AND ");
0905: buff.append(memberAlias(MEMBER_MEMBER_SERVICE_COLUMN)
0906: + EQUALS_PARAM);
0907: buff.append(" AND ");
0908: buff.append(memberAlias(MEMBER_MEMBER_KEY_COLUMN)
0909: + EQUALS_PARAM);
0910: buff.append(" AND ");
0911: buff.append(groupAlias(GROUP_TYPE_COLUMN) + EQUALS_PARAM);
0912: buff.append(" AND ");
0913: buff.append(memberAlias(MEMBER_IS_GROUP_COLUMN) + EQ
0914: + sqlQuote(MEMBER_IS_GROUP));
0915:
0916: findContainingGroupsForGroupSql = buff.toString();
0917: }
0918: return findContainingGroupsForGroupSql;
0919: }
0920:
0921: /**
0922: * @return java.lang.String
0923: */
0924: private static java.lang.String getFindGroupsByCreatorSql() {
0925: if (findGroupsByCreatorSql == null) {
0926: StringBuffer buff = new StringBuffer(200);
0927: buff.append("SELECT ");
0928: buff.append(getAllGroupColumns());
0929: buff.append(" FROM ");
0930: buff.append(GROUP_TABLE);
0931: buff.append(" WHERE ");
0932: buff.append(GROUP_CREATOR_COLUMN + EQUALS_PARAM);
0933:
0934: findGroupsByCreatorSql = buff.toString();
0935: }
0936: return findGroupsByCreatorSql;
0937: }
0938:
0939: /**
0940: * @return java.lang.String
0941: */
0942: private static java.lang.String getFindGroupSql() {
0943:
0944: if (findGroupSql == null) {
0945: StringBuffer buff = new StringBuffer(200);
0946: buff.append("SELECT ");
0947: buff.append(getAllGroupColumns());
0948: buff.append(" FROM ");
0949: buff.append(GROUP_TABLE);
0950: buff.append(" WHERE ");
0951: buff.append(GROUP_ID_COLUMN + EQUALS_PARAM);
0952:
0953: findGroupSql = buff.toString();
0954: }
0955: return findGroupSql;
0956: }
0957:
0958: /**
0959: * @return java.lang.String
0960: */
0961: private static java.lang.String getFindMemberGroupKeysSql() {
0962: if (findMemberGroupKeysSql == null) {
0963: StringBuffer buff = new StringBuffer(200);
0964: buff.append("SELECT ");
0965: buff.append(MEMBER_MEMBER_SERVICE_COLUMN + ", "
0966: + MEMBER_MEMBER_KEY_COLUMN);
0967: buff.append(" FROM ");
0968: buff.append(MEMBER_TABLE);
0969: buff.append(" WHERE ");
0970: buff.append(MEMBER_GROUP_ID_COLUMN + EQUALS_PARAM);
0971: buff.append(" AND ");
0972: buff.append(MEMBER_IS_GROUP_COLUMN + EQ);
0973: buff.append(sqlQuote(MEMBER_IS_GROUP));
0974:
0975: findMemberGroupKeysSql = buff.toString();
0976: }
0977:
0978: return findMemberGroupKeysSql;
0979: }
0980:
0981: /**
0982: * @return java.lang.String
0983: */
0984: private static java.lang.String getFindMemberGroupsSql() {
0985: if (findMemberGroupsSql == null) {
0986: StringBuffer buff = new StringBuffer(500);
0987: buff.append("SELECT ");
0988: buff.append(getAllGroupColumnsWithTableAlias());
0989: buff.append(" FROM ");
0990: buff.append(GROUP_TABLE + " " + GROUP_TABLE_ALIAS);
0991: buff.append(", ");
0992: buff.append(MEMBER_TABLE + " " + MEMBER_TABLE_ALIAS);
0993: buff.append(" WHERE ");
0994: buff.append(groupAlias(GROUP_ID_COLUMN) + EQ);
0995: buff.append(memberAlias(MEMBER_MEMBER_KEY_COLUMN));
0996: buff.append(" AND ");
0997: buff.append(memberAlias(MEMBER_IS_GROUP_COLUMN) + EQ);
0998: buff.append(sqlQuote(MEMBER_IS_GROUP));
0999: buff.append(" AND ");
1000: buff.append(memberAlias(MEMBER_GROUP_ID_COLUMN)
1001: + EQUALS_PARAM);
1002: buff.append(" AND ");
1003: buff.append(memberAlias(MEMBER_MEMBER_SERVICE_COLUMN)
1004: + EQUALS_PARAM);
1005:
1006: findMemberGroupsSql = buff.toString();
1007: }
1008:
1009: return findMemberGroupsSql;
1010: }
1011:
1012: /**
1013: * @return java.lang.String
1014: */
1015: private static java.lang.String getInsertGroupSql() {
1016: if (insertGroupSql == null) {
1017: StringBuffer buff = new StringBuffer(200);
1018: buff.append("INSERT INTO ");
1019: buff.append(GROUP_TABLE);
1020: buff.append(" (");
1021: buff.append(getAllGroupColumns());
1022: buff.append(") VALUES (?, ?, ?, ?, ?)");
1023:
1024: insertGroupSql = buff.toString();
1025: }
1026: return insertGroupSql;
1027: }
1028:
1029: /**
1030: * @return java.lang.String
1031: */
1032: private static java.lang.String getInsertMemberSql() {
1033: if (insertMemberSql == null) {
1034: StringBuffer buff = new StringBuffer(200);
1035: buff.append("INSERT INTO ");
1036: buff.append(MEMBER_TABLE);
1037: buff.append(" (");
1038: buff.append(getAllMemberColumns());
1039: buff.append(") VALUES (?, ?, ?, ? )");
1040:
1041: insertMemberSql = buff.toString();
1042: }
1043: return insertMemberSql;
1044: }
1045:
1046: /**
1047: * @return java.lang.String
1048: * @exception java.lang.Exception
1049: */
1050: private String getNextKey() throws java.lang.Exception {
1051: return SequenceGenerator.instance().getNext(GROUP_TABLE);
1052: }
1053:
1054: /**
1055: * @return java.lang.String
1056: */
1057: private static java.lang.String getUpdateGroupSql() {
1058: if (updateGroupSql == null) {
1059: StringBuffer buff = new StringBuffer(200);
1060: buff.append("UPDATE ");
1061: buff.append(GROUP_TABLE);
1062: buff.append(" SET ");
1063: buff.append(GROUP_CREATOR_COLUMN + EQUALS_PARAM);
1064: buff.append(", ");
1065: buff.append(GROUP_TYPE_COLUMN + EQUALS_PARAM);
1066: buff.append(", ");
1067: buff.append(GROUP_NAME_COLUMN + EQUALS_PARAM);
1068: buff.append(", ");
1069: buff.append(GROUP_DESCRIPTION_COLUMN + EQUALS_PARAM);
1070: buff.append(" WHERE ");
1071: buff.append(GROUP_ID_COLUMN + EQUALS_PARAM);
1072:
1073: updateGroupSql = buff.toString();
1074: }
1075: return updateGroupSql;
1076: }
1077:
1078: /**
1079: * Find and return an instance of the group.
1080: * @param rs the SQL result set
1081: * @return org.jasig.portal.groups.IEntityGroup
1082: */
1083: private IEntityGroup instanceFromResultSet(java.sql.ResultSet rs)
1084: throws SQLException, GroupsException {
1085: IEntityGroup eg = null;
1086:
1087: String key = rs.getString(1);
1088: String creatorID = rs.getString(2);
1089: Integer entityTypeID = new Integer(rs.getInt(3));
1090: Class entityType = EntityTypes.getEntityType(entityTypeID);
1091: String groupName = rs.getString(4);
1092: String description = rs.getString(5);
1093:
1094: if (key != null) {
1095: eg = newInstance(key, entityType, creatorID, groupName,
1096: description);
1097: }
1098:
1099: return eg;
1100: }
1101:
1102: /**
1103: * Find and return an instance of the group.
1104: * @param rs the SQL result set
1105: * @return org.jasig.portal.groups.ILockableEntityGroup
1106: */
1107: private ILockableEntityGroup lockableInstanceFromResultSet(
1108: java.sql.ResultSet rs) throws SQLException, GroupsException {
1109: ILockableEntityGroup eg = null;
1110:
1111: String key = rs.getString(1);
1112: String creatorID = rs.getString(2);
1113: Integer entityTypeID = new Integer(rs.getInt(3));
1114: Class entityType = EntityTypes.getEntityType(entityTypeID);
1115: String groupName = rs.getString(4);
1116: String description = rs.getString(5);
1117:
1118: if (key != null) {
1119: eg = newLockableInstance(key, entityType, creatorID,
1120: groupName, description);
1121: }
1122:
1123: return eg;
1124: }
1125:
1126: /**
1127: *
1128: */
1129: protected static void logNoTransactionWarning() {
1130: if (log.isWarnEnabled()) {
1131: String msg = "You are running the portal on a database that does not support transactions. "
1132: + "This is not a supported production environment for uPortal. "
1133: + "Sooner or later, your database will become corrupt.";
1134:
1135: log.warn(msg);
1136: }
1137: }
1138:
1139: /**
1140: * @return org.jasig.portal.groups.IEntity
1141: */
1142: public IEntity newEntity(Class type, String key)
1143: throws GroupsException {
1144: if (EntityTypes.getEntityTypeID(type) == null) {
1145: throw new GroupsException("Invalid group type: " + type);
1146: }
1147: return GroupService.getEntity(key, type);
1148: }
1149:
1150: /**
1151: * @return org.jasig.portal.groups.IEntityGroup
1152: */
1153: public IEntityGroup newInstance(Class type) throws GroupsException {
1154: if (EntityTypes.getEntityTypeID(type) == null) {
1155: throw new GroupsException("Invalid group type: " + type);
1156: }
1157: try {
1158: return new EntityGroupImpl(getNextKey(), type);
1159: } catch (Exception ex) {
1160: throw new GroupsException("Could not create new group", ex);
1161: }
1162: }
1163:
1164: /**
1165: * @return org.jasig.portal.groups.IEntityGroup
1166: */
1167: private IEntityGroup newInstance(String newKey, Class newType,
1168: String newCreatorID, String newName, String newDescription)
1169: throws GroupsException {
1170: EntityGroupImpl egi = new EntityGroupImpl(newKey, newType);
1171: egi.setCreatorID(newCreatorID);
1172: egi.primSetName(newName);
1173: egi.setDescription(newDescription);
1174: return egi;
1175: }
1176:
1177: /**
1178: * @return org.jasig.portal.groups.ILockableEntityGroup
1179: */
1180: private ILockableEntityGroup newLockableInstance(String newKey,
1181: Class newType, String newCreatorID, String newName,
1182: String newDescription) throws GroupsException {
1183: LockableEntityGroupImpl group = new LockableEntityGroupImpl(
1184: newKey, newType);
1185: group.setCreatorID(newCreatorID);
1186: group.primSetName(newName);
1187: group.setDescription(newDescription);
1188: return group;
1189: }
1190:
1191: /**
1192: * @return java.lang.String
1193: */
1194: private static java.lang.String groupAlias(String column) {
1195: return GROUP_TABLE_ALIAS + "." + column;
1196: }
1197:
1198: /**
1199: * @return java.lang.String
1200: */
1201: private static java.lang.String memberAlias(String column) {
1202: return MEMBER_TABLE_ALIAS + "." + column;
1203: }
1204:
1205: /**
1206: * Insert the entity into the database.
1207: * @param group org.jasig.portal.groups.IEntityGroup
1208: * @param conn the database connection
1209: */
1210: private void primAdd(IEntityGroup group, Connection conn)
1211: throws SQLException, GroupsException {
1212: try {
1213: PreparedStatement ps = conn
1214: .prepareStatement(getInsertGroupSql());
1215: try {
1216: Integer typeID = EntityTypes.getEntityTypeID(group
1217: .getLeafType());
1218: ps.setString(1, group.getLocalKey());
1219: ps.setString(2, group.getCreatorID());
1220: ps.setInt(3, typeID.intValue());
1221: ps.setString(4, group.getName());
1222: ps.setString(5, group.getDescription());
1223:
1224: if (log.isDebugEnabled())
1225: log.debug("RDBMEntityGroupStore.primAdd(): " + ps
1226: + "(" + group.getLocalKey() + ", "
1227: + group.getCreatorID() + ", " + typeID
1228: + ", " + group.getName() + ", "
1229: + group.getDescription() + ")");
1230:
1231: int rc = ps.executeUpdate();
1232:
1233: if (rc != 1) {
1234: String errString = "Problem adding " + group;
1235: log.error(errString);
1236: throw new GroupsException(errString);
1237: }
1238: } finally {
1239: ps.close();
1240: }
1241: } catch (java.sql.SQLException sqle) {
1242: log.error(
1243: "Error inserting an entity into the database. Group:"
1244: + group, sqle);
1245: throw sqle;
1246: }
1247: }
1248:
1249: /**
1250: * Delete this entity from the database after first deleting
1251: * its memberships.
1252: * Exception java.sql.SQLException - if we catch a SQLException,
1253: * we rollback and re-throw it.
1254: * @param group org.jasig.portal.groups.IEntityGroup
1255: */
1256: private void primDelete(IEntityGroup group) throws SQLException {
1257: java.sql.Connection conn = null;
1258: String deleteGroupSql = getDeleteGroupSql(group);
1259: String deleteMembershipSql = getDeleteMembersInGroupSql(group);
1260:
1261: try {
1262: conn = RDBMServices.getConnection();
1263: Statement stmnt = conn.createStatement();
1264: setAutoCommit(conn, false);
1265:
1266: try {
1267: if (log.isDebugEnabled())
1268: log.debug("RDBMEntityGroupStore.primDelete(): "
1269: + deleteMembershipSql);
1270:
1271: stmnt.executeUpdate(deleteMembershipSql);
1272:
1273: if (log.isDebugEnabled())
1274: log.debug("RDBMEntityGroupStore.primDelete(): "
1275: + deleteGroupSql);
1276: stmnt.executeUpdate(deleteGroupSql);
1277: } finally {
1278: stmnt.close();
1279: }
1280: commit(conn);
1281:
1282: } catch (SQLException sqle) {
1283: rollback(conn);
1284: throw sqle;
1285: } finally {
1286: try {
1287: setAutoCommit(conn, true);
1288: } finally {
1289: RDBMServices.releaseConnection(conn);
1290: }
1291: }
1292: }
1293:
1294: /**
1295: * Find and return an instance of the group.
1296: * @param groupID the group ID
1297: * @param lockable boolean
1298: * @return org.jasig.portal.groups.IEntityGroup
1299: */
1300: private IEntityGroup primFind(String groupID, boolean lockable)
1301: throws GroupsException {
1302: IEntityGroup eg = null;
1303: java.sql.Connection conn = null;
1304: try {
1305: conn = RDBMServices.getConnection();
1306: String sql = getFindGroupSql();
1307: PreparedStatement ps = conn.prepareStatement(sql);
1308: try {
1309: ps.setString(1, groupID);
1310: if (log.isDebugEnabled())
1311: log.debug("RDBMEntityGroupStore.find(): " + ps
1312: + " (" + groupID + ")");
1313: java.sql.ResultSet rs = ps.executeQuery();
1314: try {
1315: while (rs.next()) {
1316: eg = (lockable) ? lockableInstanceFromResultSet(rs)
1317: : instanceFromResultSet(rs);
1318: }
1319: } finally {
1320: rs.close();
1321: }
1322: } finally {
1323: ps.close();
1324: }
1325: } catch (Exception e) {
1326: log.error("RDBMEntityGroupStore.find(): " + e);
1327: throw new GroupsException("Error retrieving " + groupID
1328: + ": " + e);
1329: } finally {
1330: RDBMServices.releaseConnection(conn);
1331: }
1332:
1333: return eg;
1334: }
1335:
1336: /**
1337: * Update the entity in the database.
1338: * @param group org.jasig.portal.groups.IEntityGroup
1339: * @param conn the database connection
1340: */
1341: private void primUpdate(IEntityGroup group, Connection conn)
1342: throws SQLException, GroupsException {
1343: try {
1344: PreparedStatement ps = conn
1345: .prepareStatement(getUpdateGroupSql());
1346:
1347: try {
1348: Integer typeID = EntityTypes.getEntityTypeID(group
1349: .getLeafType());
1350:
1351: ps.setString(1, group.getCreatorID());
1352: ps.setInt(2, typeID.intValue());
1353: ps.setString(3, group.getName());
1354: ps.setString(4, group.getDescription());
1355: ps.setString(5, group.getLocalKey());
1356:
1357: if (log.isDebugEnabled())
1358: log.debug("RDBMEntityGroupStore.primUpdate(): "
1359: + ps + "(" + group.getCreatorID() + ", "
1360: + typeID + ", " + group.getName() + ", "
1361: + group.getDescription() + ", "
1362: + group.getLocalKey() + ")");
1363:
1364: int rc = ps.executeUpdate();
1365:
1366: if (rc != 1) {
1367: String errString = "Problem updating " + group;
1368: log.error(errString);
1369: throw new GroupsException(errString);
1370: }
1371: } finally {
1372: ps.close();
1373: }
1374: } catch (java.sql.SQLException sqle) {
1375: log.error("Error updating entity in database. Group: "
1376: + group, sqle);
1377: throw sqle;
1378: }
1379: }
1380:
1381: /**
1382: * Insert and delete group membership rows. The transaction is maintained by
1383: * the caller.
1384: * @param egi org.jasig.portal.groups.EntityGroupImpl
1385: * @param conn the database connection
1386: */
1387: private void primUpdateMembers(EntityGroupImpl egi, Connection conn)
1388: throws java.sql.SQLException {
1389: String groupKey = egi.getLocalKey();
1390: String memberKey, isGroup, serviceName = null;
1391: try {
1392: if (egi.hasDeletes()) {
1393: List deletedGroups = new ArrayList();
1394: List deletedEntities = new ArrayList();
1395: Iterator deletes = egi.getRemovedMembers().values()
1396: .iterator();
1397: while (deletes.hasNext()) {
1398: IGroupMember gm = (IGroupMember) deletes.next();
1399: if (gm.isGroup()) {
1400: deletedGroups.add(gm);
1401: } else {
1402: deletedEntities.add(gm);
1403: }
1404: }
1405:
1406: if (!deletedGroups.isEmpty()) {
1407: PreparedStatement psDeleteMemberGroup = conn
1408: .prepareStatement(getDeleteMemberGroupSql());
1409:
1410: try {
1411: for (Iterator groups = deletedGroups.iterator(); groups
1412: .hasNext();) {
1413: IEntityGroup removedGroup = (IEntityGroup) groups
1414: .next();
1415: memberKey = removedGroup.getLocalKey();
1416: isGroup = MEMBER_IS_GROUP;
1417: serviceName = removedGroup.getServiceName()
1418: .toString();
1419:
1420: psDeleteMemberGroup.setString(1, groupKey);
1421: psDeleteMemberGroup.setString(2,
1422: serviceName);
1423: psDeleteMemberGroup.setString(3, memberKey);
1424:
1425: if (log.isDebugEnabled())
1426: log
1427: .debug("RDBMEntityGroupStore.primUpdateMembers(): "
1428: + psDeleteMemberGroup
1429: + "("
1430: + groupKey
1431: + ", "
1432: + serviceName
1433: + ", "
1434: + memberKey
1435: + ", isGroup = T)");
1436:
1437: psDeleteMemberGroup.executeUpdate();
1438: } // for
1439: } // try
1440: finally {
1441: psDeleteMemberGroup.close();
1442: }
1443: } // if ( ! deletedGroups.isEmpty() )
1444:
1445: if (!deletedEntities.isEmpty()) {
1446: PreparedStatement psDeleteMemberEntity = conn
1447: .prepareStatement(getDeleteMemberEntitySql());
1448:
1449: try {
1450: for (Iterator entities = deletedEntities
1451: .iterator(); entities.hasNext();) {
1452: IGroupMember removedEntity = (IGroupMember) entities
1453: .next();
1454: memberKey = removedEntity
1455: .getUnderlyingEntityIdentifier()
1456: .getKey();
1457: isGroup = MEMBER_IS_ENTITY;
1458:
1459: psDeleteMemberEntity.setString(1, groupKey);
1460: psDeleteMemberEntity
1461: .setString(2, memberKey);
1462:
1463: if (log.isDebugEnabled())
1464: log
1465: .debug("RDBMEntityGroupStore.primUpdateMembers(): "
1466: + psDeleteMemberEntity
1467: + "("
1468: + groupKey
1469: + ", "
1470: + memberKey
1471: + ", "
1472: + "isGroup = F)");
1473:
1474: psDeleteMemberEntity.executeUpdate();
1475: } // for
1476: } // try
1477: finally {
1478: psDeleteMemberEntity.close();
1479: }
1480: } // if ( ! deletedEntities.isEmpty() )
1481:
1482: }
1483:
1484: if (egi.hasAdds()) {
1485: PreparedStatement psAdd = conn
1486: .prepareStatement(getInsertMemberSql());
1487:
1488: try {
1489: Iterator adds = egi.getAddedMembers().values()
1490: .iterator();
1491: while (adds.hasNext()) {
1492: IGroupMember addedGM = (IGroupMember) adds
1493: .next();
1494: memberKey = addedGM.getKey();
1495: if (addedGM.isGroup()) {
1496: IEntityGroup addedGroup = (IEntityGroup) addedGM;
1497: isGroup = MEMBER_IS_GROUP;
1498: serviceName = addedGroup.getServiceName()
1499: .toString();
1500: memberKey = addedGroup.getLocalKey();
1501: } else {
1502: isGroup = MEMBER_IS_ENTITY;
1503: serviceName = egi.getServiceName()
1504: .toString();
1505: memberKey = addedGM
1506: .getUnderlyingEntityIdentifier()
1507: .getKey();
1508: }
1509:
1510: psAdd.setString(1, groupKey);
1511: psAdd.setString(2, serviceName);
1512: psAdd.setString(3, memberKey);
1513: psAdd.setString(4, isGroup);
1514:
1515: if (log.isDebugEnabled())
1516: log
1517: .debug("RDBMEntityGroupStore.primUpdateMembers(): "
1518: + psAdd
1519: + "("
1520: + groupKey
1521: + ", "
1522: + memberKey
1523: + ", "
1524: + isGroup + ")");
1525:
1526: psAdd.executeUpdate();
1527: }
1528: } finally {
1529: psAdd.close();
1530: }
1531: }
1532:
1533: } catch (SQLException sqle) {
1534: log
1535: .error("Error inserting/deleting membership rows.",
1536: sqle);
1537: throw sqle;
1538: }
1539: }
1540:
1541: /**
1542: * @param conn java.sql.Connection
1543: * @exception java.sql.SQLException
1544: */
1545: protected static void rollback(Connection conn)
1546: throws java.sql.SQLException {
1547: SqlTransaction.rollback(conn);
1548: }
1549:
1550: public EntityIdentifier[] searchForGroups(String query, int method,
1551: Class leaftype) throws GroupsException {
1552: EntityIdentifier[] r = new EntityIdentifier[0];
1553: ArrayList ar = new ArrayList();
1554: Connection conn = null;
1555: PreparedStatement ps = null;
1556: int type = EntityTypes.getEntityTypeID(leaftype).intValue();
1557: //System.out.println("Checking out groups of leaftype "+leaftype.getName()+" or "+type);
1558:
1559: try {
1560: conn = RDBMServices.getConnection();
1561: switch (method) {
1562: case IS:
1563: ps = conn
1564: .prepareStatement(RDBMEntityGroupStore.searchGroups);
1565: break;
1566: case STARTS_WITH:
1567: query = query + "%";
1568: ps = conn
1569: .prepareStatement(RDBMEntityGroupStore.searchGroupsPartial);
1570: break;
1571: case ENDS_WITH:
1572: query = "%" + query;
1573: ps = conn
1574: .prepareStatement(RDBMEntityGroupStore.searchGroupsPartial);
1575: break;
1576: case CONTAINS:
1577: query = "%" + query + "%";
1578: ps = conn
1579: .prepareStatement(RDBMEntityGroupStore.searchGroupsPartial);
1580: break;
1581: default:
1582: throw new GroupsException("Unknown search type");
1583: }
1584: ps.clearParameters();
1585: ps.setInt(1, type);
1586: ps.setString(2, query);
1587: ResultSet rs = ps.executeQuery();
1588: //System.out.println(ps.toString());
1589: while (rs.next()) {
1590: //System.out.println("result");
1591: ar
1592: .add(new EntityIdentifier(
1593: rs.getString(1),
1594: org.jasig.portal.EntityTypes.GROUP_ENTITY_TYPE));
1595: }
1596: ps.close();
1597: } catch (Exception e) {
1598: log.error("RDBMChannelDefSearcher.searchForEntities(): "
1599: + ps, e);
1600: } finally {
1601: RDBMServices.releaseConnection(conn);
1602: }
1603: return (EntityIdentifier[]) ar.toArray(r);
1604: }
1605:
1606: /**
1607: * @param conn java.sql.Connection
1608: * @param newValue boolean
1609: * @exception java.sql.SQLException The exception description.
1610: */
1611: protected static void setAutoCommit(Connection conn,
1612: boolean newValue) throws java.sql.SQLException {
1613: SqlTransaction.setAutoCommit(conn, newValue);
1614: }
1615:
1616: /**
1617: * @return org.jasig.portal.groups.RDBMEntityGroupStore
1618: */
1619: public static synchronized RDBMEntityGroupStore singleton()
1620: throws GroupsException {
1621: if (singleton == null) {
1622: singleton = new RDBMEntityGroupStore();
1623: }
1624: return singleton;
1625: }
1626:
1627: /**
1628: * @return java.lang.String
1629: */
1630: private static java.lang.String sqlQuote(Object o) {
1631: return QUOTE + o + QUOTE;
1632: }
1633:
1634: /**
1635: * Commit this entity AND ITS MEMBERSHIPS to the underlying store.
1636: * @param group org.jasig.portal.groups.IEntityGroup
1637: */
1638: public void update(IEntityGroup group) throws GroupsException {
1639: Connection conn = null;
1640: boolean exists = existsInDatabase(group);
1641: try {
1642: conn = RDBMServices.getConnection();
1643: setAutoCommit(conn, false);
1644:
1645: try {
1646: if (exists) {
1647: primUpdate(group, conn);
1648: } else {
1649: primAdd(group, conn);
1650: }
1651: primUpdateMembers((EntityGroupImpl) group, conn);
1652: commit(conn);
1653: }
1654:
1655: catch (Exception ex) {
1656: rollback(conn);
1657: throw new GroupsException("Problem updating " + this
1658: + ex);
1659: }
1660: }
1661:
1662: catch (SQLException sqlex) {
1663: throw new GroupsException(sqlex);
1664: }
1665:
1666: finally {
1667: try {
1668: setAutoCommit(conn, true);
1669: } catch (SQLException sqle) {
1670: throw new GroupsException(sqle);
1671: } finally {
1672: RDBMServices.releaseConnection(conn);
1673: }
1674: }
1675: }
1676:
1677: /**
1678: * Insert and delete group membership rows inside a transaction.
1679: * @param eg org.jasig.portal.groups.IEntityGroup
1680: */
1681: public void updateMembers(IEntityGroup eg) throws GroupsException {
1682: Connection conn = null;
1683: EntityGroupImpl egi = (EntityGroupImpl) eg;
1684: if (egi.isDirty())
1685: try {
1686: conn = RDBMServices.getConnection();
1687: setAutoCommit(conn, false);
1688:
1689: try {
1690: primUpdateMembers(egi, conn);
1691: commit(conn);
1692: } catch (SQLException sqle) {
1693: rollback(conn);
1694: throw new GroupsException(
1695: "Problem updating memberships for " + egi,
1696: sqle);
1697: }
1698: }
1699:
1700: catch (SQLException sqlex) {
1701: throw new GroupsException(sqlex);
1702: }
1703:
1704: finally {
1705: try {
1706: setAutoCommit(conn, true);
1707: } catch (SQLException sqle) {
1708: throw new GroupsException(sqle);
1709: } finally {
1710: RDBMServices.releaseConnection(conn);
1711: }
1712: }
1713: }
1714: }
|