View Javadoc
1   package org.apache.fulcrum.security.torque.turbine;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  import java.sql.Connection;
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Set;
24  
25  import org.apache.fulcrum.security.model.turbine.entity.TurbineGroup;
26  import org.apache.fulcrum.security.model.turbine.entity.TurbineUserGroupRole;
27  import org.apache.fulcrum.security.torque.om.TorqueTurbineGroupPeer;
28  import org.apache.fulcrum.security.torque.om.TorqueTurbineUserGroupRole;
29  import org.apache.fulcrum.security.torque.om.TorqueTurbineUserGroupRolePeer;
30  import org.apache.fulcrum.security.torque.security.turbine.TorqueAbstractTurbineTurbineSecurityEntity;
31  import org.apache.fulcrum.security.util.DataBackendException;
32  import org.apache.torque.TorqueException;
33  import org.apache.torque.criteria.Criteria;
34  import org.apache.torque.om.SimpleKey;
35  import org.apache.torque.util.Transaction;
36  /**
37   * This abstract class provides the SecurityInterface to the managers.
38   *
39   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
40   * @version $Id:$
41   */
42  public abstract class FulcrumAbstractTurbineGroup extends TorqueAbstractTurbineTurbineSecurityEntity
43      implements TurbineGroup
44  {
45      /** Serial version */
46  	private static final long serialVersionUID = -6230312046016785990L;
47  
48      /**
49       * Forward reference to generated code
50       *
51       * Get a list of association objects, pre-populated with their TorqueTurbineRole
52       * objects.
53       *
54       * @param criteria Criteria to define the selection of records
55       * @param con a database connection
56       * @throws TorqueException  if any database error occurs
57       *
58       * @return a list of User/Group/Role relations
59       */
60      protected List<TorqueTurbineUserGroupRole> getTorqueTurbineUserGroupRolesJoinTorqueTurbineRole(Criteria criteria, Connection con)
61          throws TorqueException
62      {
63          criteria.and(TorqueTurbineUserGroupRolePeer.GROUP_ID, getEntityId() );
64          return TorqueTurbineUserGroupRolePeer.doSelectJoinTorqueTurbineRole(criteria, con);
65      }
66      
67      @Override
68      public void retrieveAttachedObjects( Connection con )
69          throws DataBackendException
70      {
71          retrieveAttachedObjects( con, false );
72      }
73  
74      /**
75       * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#retrieveAttachedObjects(Connection, Boolean)
76       */
77      @Override
78      public void retrieveAttachedObjects( Connection con, Boolean lazy ) throws DataBackendException
79      {
80          try {
81              if (!lazy) {
82                  Set<TurbineUserGroupRole> userGroupRoleSet = new HashSet<TurbineUserGroupRole>();
83                  List<TorqueTurbineUserGroupRole> ugrs = getTorqueTurbineUserGroupRolesJoinTorqueTurbineRole(new Criteria(), con);
84          
85                  for (TorqueTurbineUserGroupRole ttugr : ugrs)
86                  {
87                      TurbineUserGroupRolee/om/TurbineUserGroupRole.html#TurbineUserGroupRole">TurbineUserGroupRole ugr = new TurbineUserGroupRole();
88                      ugr.setGroup(this);
89                      ugr.setRole(ttugr.getTorqueTurbineRole());
90                      ugr.setUser(ttugr.getTorqueTurbineUser(con));
91                      userGroupRoleSet.add(ugr);
92                  }
93          
94                  setUserGroupRoleSet(userGroupRoleSet);
95              }
96          } catch (TorqueException e ) {
97              throw new DataBackendException( e.getMessage(),e );
98          }
99      }
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                     TorqueTurbineUserGroupRolequeTurbineUserGroupRole.html#TorqueTurbineUserGroupRole">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 }