View Javadoc
1   package org.apache.fulcrum.security.torque.dynamic;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  import java.sql.Connection;
22  import java.util.List;
23  import java.util.Set;
24  
25  import org.apache.fulcrum.security.entity.Group;
26  import org.apache.fulcrum.security.entity.User;
27  import org.apache.fulcrum.security.model.dynamic.entity.DynamicUser;
28  import org.apache.fulcrum.security.torque.om.TorqueDynamicUserDelegates;
29  import org.apache.fulcrum.security.torque.om.TorqueDynamicUserDelegatesPeer;
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.om.TorqueDynamicUserPeer;
33  import org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity;
34  import org.apache.fulcrum.security.util.DataBackendException;
35  import org.apache.fulcrum.security.util.GroupSet;
36  import org.apache.fulcrum.security.util.UserSet;
37  import org.apache.torque.TorqueException;
38  import org.apache.torque.criteria.Criteria;
39  import org.apache.torque.om.SimpleKey;
40  
41  /**
42   * This abstract class provides the SecurityInterface to the managers.
43   *
44   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
45   * @version $Id:$
46   */
47  public abstract class TorqueAbstractDynamicUser extends TorqueAbstractSecurityEntity implements DynamicUser {
48      /** Serial version */
49      private static final long serialVersionUID = -7307211992287876455L;
50  
51      /** a cache of group objects */
52      private Set<Group> groupSet = null;
53  
54      /** a cache of delegator (user) objects */
55      private Set<User> delegators = null;
56  
57      /** a cache of delegatee(user) objects */
58      private Set<User> delegatees = null;
59  
60      /**
61       * Forward reference to generated code
62       *
63       * Get a list of association objects, pre-populated with their
64       * TorqueDynamicGroup objects.
65       *
66       * @param criteria Criteria to define the selection of records
67       * @param con      a database connection
68       * @throws TorqueException if any database error occurs
69       *
70       * @return a list of User/Group relations
71       */
72      protected List<TorqueDynamicUserGroup> getTorqueDynamicUserGroupsJoinTorqueDynamicGroup(Criteria criteria,
73              Connection con) throws TorqueException {
74          criteria.and(TorqueDynamicUserGroupPeer.USER_ID, getEntityId());
75          return TorqueDynamicUserGroupPeer.doSelectJoinTorqueDynamicGroup(criteria, con);
76      }
77  
78      /**
79       * Forward reference to generated code
80       *
81       * Get a list of delegator association objects, pre-populated with their
82       * TorqueDynamicUserDelegates objects.
83       *
84       * @param criteria Criteria to define the selection of records
85       * @param con      a database connection
86       * @throws TorqueException if any database error occurs
87       *
88       * @return a list of User/Delegator relations
89       */
90      protected List<TorqueDynamicUserDelegates> getTorqueDynamicUserDelegatessRelatedByDelegateeUserIdJoinTorqueDynamicUserRelatedByDelegatorUserId(
91              Criteria criteria, Connection con) throws TorqueException {
92          criteria.and(TorqueDynamicUserDelegatesPeer.DELEGATEE_USER_ID, getEntityId());
93          return TorqueDynamicUserDelegatesPeer.doSelectJoinTorqueDynamicUserRelatedByDelegatorUserId(criteria, con);
94      }
95  
96      /**
97       * Forward reference to generated code
98       *
99       * Get a list of delegatee association objects, pre-populated with their
100      * TorqueDynamicUserDelegates objects.
101      *
102      * @param criteria Criteria to define the selection of records
103      * @param con      a database connection
104      * @throws TorqueException if any database error occurs
105      *
106      * @return a list of User/Delegator relations
107      */
108     protected List<TorqueDynamicUserDelegates> getTorqueDynamicUserDelegatessRelatedByDelegatorUserIdJoinTorqueDynamicUserRelatedByDelegateeUserId(
109             Criteria criteria, Connection con) throws TorqueException {
110         criteria.and(TorqueDynamicUserDelegatesPeer.DELEGATOR_USER_ID, getEntityId());
111         return TorqueDynamicUserDelegatesPeer.doSelectJoinTorqueDynamicUserRelatedByDelegateeUserId(criteria, con);
112     }
113 
114     /*
115      * (non-Javadoc)
116      * 
117      * @see
118      * org.apache.fulcrum.security.model.basic.entity.BasicUser#addGroup(org.apache.
119      * fulcrum.security.entity.Group)
120      */
121     public void addGroup(Group group) {
122         getGroups().add(group);
123     }
124 
125     /*
126      * (non-Javadoc)
127      * 
128      * @see org.apache.fulcrum.security.model.basic.entity.BasicUser#getGroups()
129      */
130     public GroupSet getGroups() {
131         if (groupSet == null) {
132             groupSet = new GroupSet();
133         } else if (!(groupSet instanceof GroupSet)) {
134             groupSet = new GroupSet(groupSet);
135         }
136 
137         return (GroupSet) groupSet;
138     }
139 
140     /*
141      * (non-Javadoc)
142      * 
143      * @see
144      * org.apache.fulcrum.security.model.basic.entity.BasicUser#getGroupsAsSet()
145      */
146     @SuppressWarnings("unchecked")
147     public <T extends Group> Set<T> getGroupsAsSet() {
148         return (Set<T>) groupSet;
149     }
150 
151     /*
152      * (non-Javadoc)
153      * 
154      * @see
155      * org.apache.fulcrum.security.model.basic.entity.BasicUser#removeGroup(org.
156      * apache.fulcrum.security.entity.Group)
157      */
158     public void removeGroup(Group group) {
159         getGroups().remove(group);
160     }
161 
162     /*
163      * (non-Javadoc)
164      * 
165      * @see
166      * org.apache.fulcrum.security.model.basic.entity.BasicUser#setGroups(org.apache
167      * .fulcrum.security.util.GroupSet)
168      */
169     public void setGroups(GroupSet groups) {
170         if (groups != null) {
171             this.groupSet = groups;
172         } else {
173             this.groupSet = new GroupSet();
174         }
175     }
176 
177     /*
178      * (non-Javadoc)
179      * 
180      * @see
181      * org.apache.fulcrum.security.model.basic.entity.BasicUser#setGroupsAsSet(java.
182      * util.Set)
183      */
184     public <T extends Group> void setGroupsAsSet(Set<T> groups) {
185         setGroups(new GroupSet(groups));
186     }
187 
188     /*
189      * (non-Javadoc)
190      * 
191      * @see
192      * org.apache.fulcrum.security.model.dynamic.entity.DynamicUser#getDelegatees()
193      */
194     @SuppressWarnings("unchecked")
195     public <T extends User> Set<T> getDelegatees() {
196         if (delegatees == null) {
197             delegatees = new UserSet<T>();
198         }
199 
200         return (Set<T>) delegatees;
201     }
202 
203     /*
204      * (non-Javadoc)
205      * 
206      * @see
207      * org.apache.fulcrum.security.model.dynamic.entity.DynamicUser#getDelegators()
208      */
209     @SuppressWarnings("unchecked")
210     public <T extends User> Set<T> getDelegators() {
211         if (delegators == null) {
212             delegators = new UserSet<T>();
213         }
214 
215         return (Set<T>) delegators;
216     }
217 
218     /*
219      * (non-Javadoc)
220      * 
221      * @see
222      * org.apache.fulcrum.security.model.dynamic.entity.DynamicUser#setDelegatees(
223      * java.util.Set)
224      */
225     public <T extends User> void setDelegatees(Set<T> delegatees) {
226         if (delegatees != null) {
227             this.delegatees = new UserSet<T>(delegatees);
228         } else {
229             this.delegatees = new UserSet<T>();
230         }
231     }
232 
233     /*
234      * (non-Javadoc)
235      * 
236      * @see
237      * org.apache.fulcrum.security.model.dynamic.entity.DynamicUser#setDelegators(
238      * java.util.Set)
239      */
240     public <T extends User> void setDelegators(Set<T> delegates) {
241         if (delegators != null) {
242             this.delegators = new UserSet<T>(delegates);
243         } else {
244             this.delegators = new UserSet<T>();
245         }
246     }
247 
248     /**
249      * @return the database name
250      */
251     public String getDatabaseName() {
252         return TorqueDynamicUserPeer.DATABASE_NAME;
253     }
254 
255     /*
256      * (non-Javadoc)
257      * 
258      * @see
259      * org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#
260      * retrieveAttachedObjects(java.sql.Connection)
261      */
262     @Override
263     public void retrieveAttachedObjects(Connection con) throws DataBackendException {
264         retrieveAttachedObjects(con, false);
265     }
266 
267     /**
268      * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#retrieveAttachedObjects(Connection,
269      *      Boolean)
270      */
271     @Override
272     public void retrieveAttachedObjects(Connection con, Boolean lazy) throws DataBackendException {
273         
274         this.groupSet = new GroupSet();
275 
276         try {
277             
278             List<TorqueDynamicUserGroup> usergroups = getTorqueDynamicUserGroupsJoinTorqueDynamicGroup(new Criteria(), con);
279     
280             for (TorqueDynamicUserGroup tdug : usergroups) {
281                 groupSet.add(tdug.getTorqueDynamicGroup());
282             }
283     
284             this.delegators = new UserSet<User>();
285     
286             List<TorqueDynamicUserDelegates> delegatorlist = getTorqueDynamicUserDelegatessRelatedByDelegateeUserIdJoinTorqueDynamicUserRelatedByDelegatorUserId(
287                     new Criteria(), con);
288     
289             for (TorqueDynamicUserDelegates tdud : delegatorlist) {
290                 delegators.add(tdud.getTorqueDynamicUserRelatedByDelegatorUserId());
291             }
292     
293             this.delegatees = new UserSet<User>();
294     
295             List<TorqueDynamicUserDelegates> delegateelist = getTorqueDynamicUserDelegatessRelatedByDelegatorUserIdJoinTorqueDynamicUserRelatedByDelegateeUserId(
296                     new Criteria(), con);
297     
298             for (TorqueDynamicUserDelegates tdud : delegateelist) {
299                 delegatees.add(tdud.getTorqueDynamicUserRelatedByDelegateeUserId());
300             }
301             
302         } catch (TorqueException e ) {
303             throw new DataBackendException( e.getMessage(),e );
304         }
305     }
306 
307     /**
308      * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#update(java.sql.Connection)
309      */
310     public void update(Connection con) throws TorqueException {
311         if (groupSet != null) {
312             Criteria criteria = new Criteria();
313 
314             /* remove old entries */
315             criteria.where(TorqueDynamicUserGroupPeer.USER_ID, getEntityId());
316             TorqueDynamicUserGroupPeer.doDelete(criteria, con);
317 
318             for (Group g : groupSet) {
319                 TorqueDynamicUserGroup/om/TorqueDynamicUserGroup.html#TorqueDynamicUserGroup">TorqueDynamicUserGroup ug = new TorqueDynamicUserGroup();
320                 ug.setUserId(getEntityId());
321                 ug.setGroupId((Integer) g.getId());
322                 ug.save(con);
323             }
324         }
325 
326         if (delegators != null) {
327             Criteria criteria = new Criteria();
328 
329             /* remove old entries */
330             criteria.where(TorqueDynamicUserDelegatesPeer.DELEGATEE_USER_ID, getEntityId());
331             TorqueDynamicUserDelegatesPeer.doDelete(criteria, con);
332 
333             for (User u : delegators) {
334                 TorqueDynamicUserDelegatesTorqueDynamicUserDelegates.html#TorqueDynamicUserDelegates">TorqueDynamicUserDelegates ud = new TorqueDynamicUserDelegates();
335                 ud.setDelegateeUserId(getEntityId());
336                 ud.setDelegatorUserId((Integer) u.getId());
337                 ud.save(con);
338             }
339         }
340 
341         if (delegatees != null) {
342             Criteria criteria = new Criteria();
343 
344             /* remove old entries */
345             criteria.where(TorqueDynamicUserDelegatesPeer.DELEGATOR_USER_ID, getEntityId());
346             TorqueDynamicUserDelegatesPeer.doDelete(criteria, con);
347 
348             for (User u : delegatees) {
349                 TorqueDynamicUserDelegatesTorqueDynamicUserDelegates.html#TorqueDynamicUserDelegates">TorqueDynamicUserDelegates ud = new TorqueDynamicUserDelegates();
350                 ud.setDelegatorUserId(getEntityId());
351                 ud.setDelegateeUserId((Integer) u.getId());
352                 ud.save(con);
353             }
354         }
355 
356         try {
357             save(con);
358         } catch (Exception e) {
359             throw new TorqueException(e);
360         }
361     }
362 
363     /**
364      * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#delete()
365      */
366     public void delete() throws TorqueException {
367         TorqueDynamicUserPeer.doDelete(SimpleKey.keyFor(getEntityId()));
368     }
369 }