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.TurbineGroupPeer;
28  import org.apache.fulcrum.security.torque.om.TurbineUserGroupRolePeer;
29  import org.apache.fulcrum.security.torque.peer.TurbineUserGroupRoleModelPeerMapper;
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 DefaultAbstractTurbineGroup 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 TurbineRole
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 <T extends TurbineUserGroupRoleModelPeerMapper> List<T> getTurbineUserGroupRolesJoinTurbineRole(Criteria criteria, Connection con)
61              throws TorqueException, DataBackendException
62      {
63          criteria.and(TurbineUserGroupRolePeer.GROUP_ID, getEntityId() );
64          return (List<T>) TurbineUserGroupRolePeer.doSelectJoinTurbineRole(criteria, con);
65      }
66      
67      @Override
68      public void retrieveAttachedObjects( Connection con )
69          throws DataBackendException
70      {
71          retrieveAttachedObjects( con, false );
72      }    
73      
74      /* (non-Javadoc)
75       * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#retrieveAttachedObjects(java.sql.Connection, java.lang.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                                 
84                  List<TurbineUserGroupRoleModelPeerMapper> ugrs = getTurbineUserGroupRolesJoinTurbineRole(new Criteria(), con);
85          
86                  for (TurbineUserGroupRoleModelPeerMapper ttugr : ugrs)
87                  {
88                      TurbineUserGroupRolee/om/TurbineUserGroupRole.html#TurbineUserGroupRole">TurbineUserGroupRole ugr = new TurbineUserGroupRole();
89                      ugr.setGroup(this);
90                      ugr.setRole(ttugr.getTurbineRole());
91                      ugr.setUser(ttugr.getTurbineUser(con));
92                      userGroupRoleSet.add(ugr);
93                  }
94                  setUserGroupRoleSet(userGroupRoleSet);
95              }
96          } catch (TorqueException e ) {
97              throw new DataBackendException( e.getMessage(),e );
98          }
99      }
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 }