001package org.apache.fulcrum.security.torque.dynamic;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021import java.sql.Connection;
022import java.util.List;
023import java.util.Set;
024
025import org.apache.fulcrum.security.entity.Group;
026import org.apache.fulcrum.security.entity.User;
027import org.apache.fulcrum.security.model.dynamic.entity.DynamicUser;
028import org.apache.fulcrum.security.torque.om.TorqueDynamicUserDelegates;
029import org.apache.fulcrum.security.torque.om.TorqueDynamicUserDelegatesPeer;
030import org.apache.fulcrum.security.torque.om.TorqueDynamicUserGroup;
031import org.apache.fulcrum.security.torque.om.TorqueDynamicUserGroupPeer;
032import org.apache.fulcrum.security.torque.om.TorqueDynamicUserPeer;
033import org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity;
034import org.apache.fulcrum.security.util.DataBackendException;
035import org.apache.fulcrum.security.util.GroupSet;
036import org.apache.fulcrum.security.util.UserSet;
037import org.apache.torque.TorqueException;
038import org.apache.torque.criteria.Criteria;
039import org.apache.torque.om.SimpleKey;
040
041/**
042 * This abstract class provides the SecurityInterface to the managers.
043 *
044 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
045 * @version $Id:$
046 */
047public abstract class TorqueAbstractDynamicUser extends TorqueAbstractSecurityEntity implements DynamicUser {
048    /** Serial version */
049    private static final long serialVersionUID = -7307211992287876455L;
050
051    /** a cache of group objects */
052    private Set<Group> groupSet = null;
053
054    /** a cache of delegator (user) objects */
055    private Set<User> delegators = null;
056
057    /** a cache of delegatee(user) objects */
058    private Set<User> delegatees = null;
059
060    /**
061     * Forward reference to generated code
062     *
063     * Get a list of association objects, pre-populated with their
064     * TorqueDynamicGroup objects.
065     *
066     * @param criteria Criteria to define the selection of records
067     * @param con      a database connection
068     * @throws TorqueException if any database error occurs
069     *
070     * @return a list of User/Group relations
071     */
072    protected List<TorqueDynamicUserGroup> getTorqueDynamicUserGroupsJoinTorqueDynamicGroup(Criteria criteria,
073            Connection con) throws TorqueException {
074        criteria.and(TorqueDynamicUserGroupPeer.USER_ID, getEntityId());
075        return TorqueDynamicUserGroupPeer.doSelectJoinTorqueDynamicGroup(criteria, con);
076    }
077
078    /**
079     * Forward reference to generated code
080     *
081     * Get a list of delegator association objects, pre-populated with their
082     * TorqueDynamicUserDelegates objects.
083     *
084     * @param criteria Criteria to define the selection of records
085     * @param con      a database connection
086     * @throws TorqueException if any database error occurs
087     *
088     * @return a list of User/Delegator relations
089     */
090    protected List<TorqueDynamicUserDelegates> getTorqueDynamicUserDelegatessRelatedByDelegateeUserIdJoinTorqueDynamicUserRelatedByDelegatorUserId(
091            Criteria criteria, Connection con) throws TorqueException {
092        criteria.and(TorqueDynamicUserDelegatesPeer.DELEGATEE_USER_ID, getEntityId());
093        return TorqueDynamicUserDelegatesPeer.doSelectJoinTorqueDynamicUserRelatedByDelegatorUserId(criteria, con);
094    }
095
096    /**
097     * Forward reference to generated code
098     *
099     * 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 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                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                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}