001package org.apache.fulcrum.security.torque.turbine; 002/* 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 */ 020import java.sql.Connection; 021import java.util.HashSet; 022import java.util.List; 023import java.util.Set; 024 025import org.apache.fulcrum.security.model.turbine.entity.TurbineGroup; 026import org.apache.fulcrum.security.model.turbine.entity.TurbineUserGroupRole; 027import org.apache.fulcrum.security.torque.om.TurbineGroupPeer; 028import org.apache.fulcrum.security.torque.om.TurbineUserGroupRolePeer; 029import org.apache.fulcrum.security.torque.peer.TurbineUserGroupRoleModelPeerMapper; 030import org.apache.fulcrum.security.torque.security.turbine.TorqueAbstractTurbineTurbineSecurityEntity; 031import org.apache.fulcrum.security.util.DataBackendException; 032import org.apache.torque.TorqueException; 033import org.apache.torque.criteria.Criteria; 034import org.apache.torque.om.SimpleKey; 035import org.apache.torque.util.Transaction; 036/** 037 * This abstract class provides the SecurityInterface to the managers. 038 * 039 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a> 040 * @version $Id:$ 041 */ 042public abstract class DefaultAbstractTurbineGroup extends TorqueAbstractTurbineTurbineSecurityEntity 043 implements TurbineGroup 044{ 045 /** Serial version */ 046 private static final long serialVersionUID = -6230312046016785990L; 047 048 /** 049 * Forward reference to generated code 050 * 051 * Get a list of association objects, pre-populated with their TurbineRole 052 * objects. 053 * 054 * @param criteria Criteria to define the selection of records 055 * @param con a database connection 056 * @throws TorqueException if any database error occurs 057 * 058 * @return a list of User/Group/Role relations 059 */ 060 protected <T extends TurbineUserGroupRoleModelPeerMapper> List<T> getTurbineUserGroupRolesJoinTurbineRole(Criteria criteria, Connection con) 061 throws TorqueException, DataBackendException 062 { 063 criteria.and(TurbineUserGroupRolePeer.GROUP_ID, getEntityId() ); 064 return (List<T>) TurbineUserGroupRolePeer.doSelectJoinTurbineRole(criteria, con); 065 } 066 067 @Override 068 public void retrieveAttachedObjects( Connection con ) 069 throws DataBackendException 070 { 071 retrieveAttachedObjects( con, false ); 072 } 073 074 /* (non-Javadoc) 075 * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#retrieveAttachedObjects(java.sql.Connection, java.lang.Boolean) 076 */ 077 @Override 078 public void retrieveAttachedObjects( Connection con, Boolean lazy ) throws DataBackendException 079 { 080 try { 081 if (!lazy) { 082 Set<TurbineUserGroupRole> userGroupRoleSet = new HashSet<TurbineUserGroupRole>(); 083 084 List<TurbineUserGroupRoleModelPeerMapper> ugrs = getTurbineUserGroupRolesJoinTurbineRole(new Criteria(), con); 085 086 for (TurbineUserGroupRoleModelPeerMapper ttugr : ugrs) 087 { 088 TurbineUserGroupRole ugr = new TurbineUserGroupRole(); 089 ugr.setGroup(this); 090 ugr.setRole(ttugr.getTurbineRole()); 091 ugr.setUser(ttugr.getTurbineUser(con)); 092 userGroupRoleSet.add(ugr); 093 } 094 setUserGroupRoleSet(userGroupRoleSet); 095 } 096 } catch (TorqueException e ) { 097 throw new DataBackendException( e.getMessage(),e ); 098 } 099 } 100 101 102 /* (non-Javadoc) 103 * @see org.apache.fulcrum.security.torque.security.turbine.TorqueAbstractTurbineTurbineSecurityEntity#getUserGroupRoleSet() 104 */ 105 @Override 106 public <T extends TurbineUserGroupRole> Set<T> getUserGroupRoleSet() throws DataBackendException 107 { 108 if (super.getUserGroupRoleSet() == null || super.getUserGroupRoleSet().isEmpty()) { 109 Connection con = null; 110 try 111 { 112 con = Transaction.begin(); 113 114 retrieveAttachedObjects( con, false ); // not configurable, we set it 115 116 Transaction.commit(con); 117 con = null; 118 } 119 catch (TorqueException e) 120 { 121 throw new DataBackendException("Error retrieving group information", e); 122 } 123 finally 124 { 125 if (con != null) 126 { 127 Transaction.safeRollback(con); 128 } 129 } 130 } 131 132 return super.getUserGroupRoleSet(); 133 134 } 135 136 /* (non-Javadoc) 137 * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#update(java.sql.Connection) 138 */ 139 @Override 140 public void update(Connection con) throws TorqueException 141 { 142 try 143 { 144 Set<TurbineUserGroupRole> userGroupRoleSet = getUserGroupRoleSet(); 145 if (userGroupRoleSet != null && !userGroupRoleSet.isEmpty()) 146 { 147 Criteria criteria = new Criteria(); 148 149 /* remove old entries */ 150 criteria.where(TurbineUserGroupRolePeer.GROUP_ID, getEntityId()); 151 TurbineUserGroupRolePeer.doDelete(criteria, con); 152 153 for (TurbineUserGroupRole ugr : userGroupRoleSet) 154 { 155 org.apache.fulcrum.security.torque.om.TurbineUserGroupRole ttugr = new org.apache.fulcrum.security.torque.om.TurbineUserGroupRole(); 156 ttugr.setGroupId((Integer)ugr.getGroup().getId()); 157 ttugr.setUserId((Integer)ugr.getUser().getId()); 158 ttugr.setRoleId((Integer)ugr.getRole().getId()); 159 ttugr.save(con); 160 } 161 } 162 save(con); 163 } 164 catch (Exception e) 165 { 166 throw new TorqueException(e); 167 } 168 } 169 170 /* (non-Javadoc) 171 * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#delete() 172 */ 173 @Override 174 public void delete() throws TorqueException 175 { 176 TurbineGroupPeer.doDelete(SimpleKey.keyFor(getEntityId())); 177 } 178}