View Javadoc
1   package org.apache.fulcrum.security.torque.dynamic;
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.List;
22  import java.util.Set;
23  
24  import org.apache.fulcrum.security.entity.Role;
25  import org.apache.fulcrum.security.entity.User;
26  import org.apache.fulcrum.security.model.dynamic.entity.DynamicGroup;
27  import org.apache.fulcrum.security.torque.om.TorqueDynamicGroupPeer;
28  import org.apache.fulcrum.security.torque.om.TorqueDynamicGroupRole;
29  import org.apache.fulcrum.security.torque.om.TorqueDynamicGroupRolePeer;
30  import org.apache.fulcrum.security.torque.om.TorqueDynamicUserGroup;
31  import org.apache.fulcrum.security.torque.om.TorqueDynamicUserGroupPeer;
32  import org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity;
33  import org.apache.fulcrum.security.util.DataBackendException;
34  import org.apache.fulcrum.security.util.RoleSet;
35  import org.apache.fulcrum.security.util.UserSet;
36  import org.apache.torque.TorqueException;
37  import org.apache.torque.criteria.Criteria;
38  import org.apache.torque.om.SimpleKey;
39  /**
40   * This abstract class provides the SecurityInterface to the managers.
41   *
42   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
43   * @version $Id:$
44   */
45  public abstract class TorqueAbstractDynamicGroup extends TorqueAbstractSecurityEntity
46      implements DynamicGroup
47  {
48      /** Serial version */
49  	private static final long serialVersionUID = -122088742532595477L;
50  
51  	/** a cache of user objects */
52      private Set<User> userSet = null;
53  
54      /** a cache of role objects */
55      private Set<Role> roleSet = null;
56  
57      /**
58       * Forward reference to generated code
59       *
60       * Get a list of association objects, pre-populated with their TorqueDynamicUser
61       * objects.
62       *
63       * @param criteria Criteria to define the selection of records
64       * @param con a database connection
65       * @throws TorqueException  if any database error occurs
66       *
67       * @return a list of User/Group relations
68       */
69      protected List<TorqueDynamicUserGroup> getTorqueDynamicUserGroupsJoinTorqueDynamicUser(Criteria criteria, Connection con)
70          throws TorqueException
71      {
72          criteria.and(TorqueDynamicUserGroupPeer.GROUP_ID, getEntityId() );
73          return TorqueDynamicUserGroupPeer.doSelectJoinTorqueDynamicUser(criteria, con);
74      }
75  
76      /**
77       * Forward reference to generated code
78       *
79       * Get a list of association objects, pre-populated with their TorqueDynamicRole
80       * objects.
81       *
82       * @param criteria Criteria to define the selection of records
83       * @param con a database connection
84       * @throws TorqueException  if any database error occurs
85       *
86       * @return a list of Role/Group relations
87       */
88      protected List<TorqueDynamicGroupRole> getTorqueDynamicGroupRolesJoinTorqueDynamicRole(Criteria criteria, Connection con)
89          throws TorqueException
90      {
91          criteria.and(TorqueDynamicGroupRolePeer.GROUP_ID, getEntityId() );
92          return TorqueDynamicGroupRolePeer.doSelectJoinTorqueDynamicRole(criteria, con);
93      }
94  
95      /* (non-Javadoc)
96       * @see org.apache.fulcrum.security.model.basic.entity.BasicGroup#addUser(org.apache.fulcrum.security.entity.User)
97       */
98      public void addUser(User user)
99      {
100         getUsers().add(user);
101     }
102 
103     /* (non-Javadoc)
104      * @see org.apache.fulcrum.security.model.basic.entity.BasicGroup#getUsers()
105      */
106     public UserSet getUsers()
107     {
108         if (userSet == null)
109         {
110             userSet = new UserSet<User>();
111         }
112         else if(!(userSet instanceof UserSet))
113         {
114             userSet = new UserSet<User>(userSet);
115         }
116 
117         return (UserSet)userSet;
118     }
119 
120     /* (non-Javadoc)
121      * @see org.apache.fulcrum.security.model.basic.entity.BasicGroup#getUsersAsSet()
122      */
123     @SuppressWarnings("unchecked")
124 	public <T extends User> Set<T> getUsersAsSet()
125     {
126         return (Set<T>)userSet;
127     }
128 
129     /* (non-Javadoc)
130      * @see org.apache.fulcrum.security.model.basic.entity.BasicGroup#removeUser(org.apache.fulcrum.security.entity.User)
131      */
132     public void removeUser(User user)
133     {
134         getUsers().remove(user);
135     }
136 
137     /* (non-Javadoc)
138      * @see org.apache.fulcrum.security.model.basic.entity.BasicGroup#setUsers(org.apache.fulcrum.security.util.UserSet)
139      */
140     public void setUsers(UserSet userSet)
141     {
142         if(userSet != null)
143         {
144             this.userSet = (UserSet<User>) userSet;
145         }
146         else
147         {
148             this.userSet = new UserSet<User>();
149         }
150     }
151 
152     /* (non-Javadoc)
153      * @see org.apache.fulcrum.security.model.basic.entity.BasicGroup#setUsersAsSet(java.util.Set)
154      */
155     public <T extends User> void setUsersAsSet(Set<T> users)
156     {
157         setUsers(new UserSet<User>(users));
158     }
159 
160     /* (non-Javadoc)
161      * @see org.apache.fulcrum.security.model.dynamic.entity.DynamicGroup#addRole(org.apache.fulcrum.security.entity.Role)
162      */
163     public void addRole(Role role)
164     {
165         getRoles().add(role);
166     }
167 
168     /* (non-Javadoc)
169      * @see org.apache.fulcrum.security.model.dynamic.entity.DynamicGroup#getRoles()
170      */
171     public RoleSet getRoles()
172     {
173         if (roleSet == null)
174         {
175             roleSet = new RoleSet();
176         }
177         else if(!(roleSet instanceof RoleSet))
178         {
179             roleSet = new RoleSet(roleSet);
180         }
181 
182         return (RoleSet)roleSet;
183     }
184 
185     /* (non-Javadoc)
186      * @see org.apache.fulcrum.security.model.dynamic.entity.DynamicGroup#getRolesAsSet()
187      */
188     @SuppressWarnings("unchecked")
189 	public <T extends Role> Set<T> getRolesAsSet()
190     {
191         return (Set<T>)roleSet;
192     }
193 
194     /* (non-Javadoc)
195      * @see org.apache.fulcrum.security.model.dynamic.entity.DynamicGroup#removeRole(org.apache.fulcrum.security.entity.Role)
196      */
197     public void removeRole(Role role)
198     {
199         getRoles().remove(role);
200     }
201 
202     /* (non-Javadoc)
203      * @see org.apache.fulcrum.security.model.dynamic.entity.DynamicGroup#setRoles(org.apache.fulcrum.security.util.RoleSet)
204      */
205     public void setRoles(RoleSet roleSet)
206     {
207         if(roleSet != null)
208         {
209             this.roleSet = roleSet;
210         }
211         else
212         {
213             this.roleSet = new RoleSet();
214         }
215     }
216 
217     /* (non-Javadoc)
218      * @see org.apache.fulcrum.security.model.dynamic.entity.DynamicGroup#setRolesAsSet(java.util.Set)
219      */
220     public <T extends Role> void setRolesAsSet(Set<T> roles)
221     {
222         setRoles(new RoleSet(roles));
223     }
224 
225     /**
226      * @return the database name
227      */
228     public String getDatabaseName()
229     {
230         return TorqueDynamicGroupPeer.DATABASE_NAME;
231     }
232     
233     /* (non-Javadoc)
234      * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#retrieveAttachedObjects(java.sql.Connection)
235      */
236     @Override
237     public void retrieveAttachedObjects( Connection con )
238         throws DataBackendException
239     {
240         retrieveAttachedObjects( con, false );
241     }
242 
243     /* (non-Javadoc)
244      * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#retrieveAttachedObjects(java.sql.Connection, java.lang.Boolean)
245      */
246     @Override
247     public void retrieveAttachedObjects( Connection con, Boolean lazy )
248         throws DataBackendException
249     {
250         this.userSet = new UserSet<User>();
251         
252         try {
253 
254             List<TorqueDynamicUserGroup> usergroups = getTorqueDynamicUserGroupsJoinTorqueDynamicUser(new Criteria(), con);
255     
256             for (TorqueDynamicUserGroup tdug : usergroups)
257             {
258                 userSet.add(tdug.getTorqueDynamicUser());
259             }
260     
261             this.roleSet = new RoleSet();
262     
263             List<TorqueDynamicGroupRole> grouproles = getTorqueDynamicGroupRolesJoinTorqueDynamicRole(new Criteria(), con);
264     
265             for (TorqueDynamicGroupRole tdgr : grouproles)
266             {
267                 roleSet.add(tdgr.getTorqueDynamicRole());
268             }
269         
270         } catch (TorqueException e ) {
271             throw new DataBackendException( e.getMessage(),e );
272         }
273     }
274 
275     /* (non-Javadoc)
276      * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#update(java.sql.Connection)
277      */
278     public void update(Connection con) throws TorqueException
279     {
280         if (userSet != null)
281         {
282             Criteria criteria = new Criteria();
283 
284             /* remove old entries */
285             criteria.where(TorqueDynamicUserGroupPeer.GROUP_ID, getEntityId());
286             TorqueDynamicUserGroupPeer.doDelete(criteria, con);
287 
288             for (User u : userSet)
289             {
290                 TorqueDynamicUserGroup/om/TorqueDynamicUserGroup.html#TorqueDynamicUserGroup">TorqueDynamicUserGroup ug = new TorqueDynamicUserGroup();
291                 ug.setUserId((Integer)u.getId());
292                 ug.setGroupId(getEntityId());
293                 ug.save(con);
294             }
295         }
296 
297         if (roleSet != null)
298         {
299             Criteria criteria = new Criteria();
300 
301             /* remove old entries */
302             criteria.where(TorqueDynamicGroupRolePeer.GROUP_ID, getEntityId());
303             TorqueDynamicGroupRolePeer.doDelete(criteria, con);
304 
305             for (Role r : roleSet)
306             {
307                 TorqueDynamicGroupRole/om/TorqueDynamicGroupRole.html#TorqueDynamicGroupRole">TorqueDynamicGroupRole gr = new TorqueDynamicGroupRole();
308                 gr.setRoleId((Integer)r.getId());
309                 gr.setGroupId(getEntityId());
310                 gr.save(con);
311             }
312         }
313 
314         try
315         {
316             save(con);
317         }
318         catch (Exception e)
319         {
320             throw new TorqueException(e);
321         }
322     }
323 
324     /* (non-Javadoc)
325      * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#delete()
326      */
327     public void delete() throws TorqueException
328     {
329         TorqueDynamicGroupPeer.doDelete(SimpleKey.keyFor(getEntityId()));
330     }
331 }