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.TorqueTurbineGroupPeer; 028import org.apache.fulcrum.security.torque.om.TorqueTurbineUserGroupRole; 029import org.apache.fulcrum.security.torque.om.TorqueTurbineUserGroupRolePeer; 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 FulcrumAbstractTurbineGroup 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 TorqueTurbineRole 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 List<TorqueTurbineUserGroupRole> getTorqueTurbineUserGroupRolesJoinTorqueTurbineRole(Criteria criteria, Connection con) 061 throws TorqueException 062 { 063 criteria.and(TorqueTurbineUserGroupRolePeer.GROUP_ID, getEntityId() ); 064 return TorqueTurbineUserGroupRolePeer.doSelectJoinTorqueTurbineRole(criteria, con); 065 } 066 067 @Override 068 public void retrieveAttachedObjects( Connection con ) 069 throws DataBackendException 070 { 071 retrieveAttachedObjects( con, false ); 072 } 073 074 /** 075 * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#retrieveAttachedObjects(Connection, 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 List<TorqueTurbineUserGroupRole> ugrs = getTorqueTurbineUserGroupRolesJoinTorqueTurbineRole(new Criteria(), con); 084 085 for (TorqueTurbineUserGroupRole ttugr : ugrs) 086 { 087 TurbineUserGroupRole ugr = new TurbineUserGroupRole(); 088 ugr.setGroup(this); 089 ugr.setRole(ttugr.getTorqueTurbineRole()); 090 ugr.setUser(ttugr.getTorqueTurbineUser(con)); 091 userGroupRoleSet.add(ugr); 092 } 093 094 setUserGroupRoleSet(userGroupRoleSet); 095 } 096 } catch (TorqueException e ) { 097 throw new DataBackendException( e.getMessage(),e ); 098 } 099 } 100 101 @Override 102 public <T extends TurbineUserGroupRole> Set<T> getUserGroupRoleSet() throws DataBackendException 103 { 104 if (super.getUserGroupRoleSet() == null || super.getUserGroupRoleSet().isEmpty()) { 105 Connection con = null; 106 try 107 { 108 con = Transaction.begin(); 109 110 retrieveAttachedObjects( con, false ); // not configurable, we set it 111 112 Transaction.commit(con); 113 con = null; 114 } 115 catch (TorqueException e) 116 { 117 throw new DataBackendException("Error retrieving group information", e); 118 } 119 finally 120 { 121 if (con != null) 122 { 123 Transaction.safeRollback(con); 124 } 125 } 126 } 127 128 return super.getUserGroupRoleSet(); 129 } 130 131 /** 132 * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#update(java.sql.Connection) 133 */ 134 @Override 135 public void update(Connection con) throws TorqueException 136 { 137 try 138 { 139 Set<TurbineUserGroupRole> userGroupRoleSet = getUserGroupRoleSet(); 140 if (userGroupRoleSet != null) 141 { 142 Criteria criteria = new Criteria(); 143 144 /* remove old entries */ 145 criteria.where(TorqueTurbineUserGroupRolePeer.GROUP_ID, getEntityId()); 146 TorqueTurbineUserGroupRolePeer.doDelete(criteria, con); 147 148 for (TurbineUserGroupRole ugr : userGroupRoleSet) 149 { 150 TorqueTurbineUserGroupRole ttugr = new TorqueTurbineUserGroupRole(); 151 ttugr.setGroupId((Integer)ugr.getGroup().getId()); 152 ttugr.setUserId((Integer)ugr.getUser().getId()); 153 ttugr.setRoleId((Integer)ugr.getRole().getId()); 154 ttugr.save(con); 155 } 156 } 157 save(con); 158 } 159 catch (Exception e) 160 { 161 throw new TorqueException(e); 162 } 163 } 164 165 /** 166 * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#delete() 167 */ 168 @Override 169 public void delete() throws TorqueException 170 { 171 TorqueTurbineGroupPeer.doDelete(SimpleKey.keyFor(getEntityId())); 172 } 173}