View Javadoc
1   package org.apache.fulcrum.security.torque.basic;
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.Group;
25  import org.apache.fulcrum.security.model.basic.entity.BasicUser;
26  import org.apache.fulcrum.security.torque.om.TorqueBasicUserGroup;
27  import org.apache.fulcrum.security.torque.om.TorqueBasicUserGroupPeer;
28  import org.apache.fulcrum.security.torque.om.TorqueBasicUserPeer;
29  import org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity;
30  import org.apache.fulcrum.security.util.DataBackendException;
31  import org.apache.fulcrum.security.util.GroupSet;
32  import org.apache.torque.TorqueException;
33  import org.apache.torque.criteria.Criteria;
34  import org.apache.torque.om.SimpleKey;
35  
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 TorqueAbstractBasicUser extends TorqueAbstractSecurityEntity
43      implements BasicUser
44  {
45      /** Serial version */
46  	private static final long serialVersionUID = 7669398253522416329L;
47  	/** a cache of group objects */
48      private Set<Group> groupSet = null;
49  
50      /**
51       * Forward reference to generated code
52       *
53       * Get a list of association objects, pre-populated with their TorqueBasicGroup
54       * objects.
55       *
56       * @param criteria Criteria to define the selection of records
57       * @param con a database connection
58       * @throws TorqueException  if any database error occurs
59       *
60       * @return a list of User/Group relations
61       */
62      protected List<TorqueBasicUserGroup> getTorqueBasicUserGroupsJoinTorqueBasicGroup(Criteria criteria, Connection con)
63          throws TorqueException
64      {
65          criteria.and(TorqueBasicUserGroupPeer.USER_ID, getEntityId() );
66          return TorqueBasicUserGroupPeer.doSelectJoinTorqueBasicGroup(criteria, con);
67      }
68  
69      /**
70       * @see org.apache.fulcrum.security.model.basic.entity.BasicUser#addGroup(org.apache.fulcrum.security.entity.Group)
71       */
72      public void addGroup(Group group)
73      {
74          getGroups().add(group);
75      }
76  
77      /**
78       * @see org.apache.fulcrum.security.model.basic.entity.BasicUser#getGroups()
79       */
80      public GroupSet getGroups()
81      {
82          if (groupSet == null)
83          {
84              groupSet = new GroupSet();
85          }
86          else if(!(groupSet instanceof GroupSet))
87          {
88              groupSet = new GroupSet(groupSet);
89          }
90  
91          return (GroupSet)groupSet;
92      }
93  
94      /**
95       * @see org.apache.fulcrum.security.model.basic.entity.BasicUser#getGroupsAsSet()
96       */
97      @SuppressWarnings("unchecked")
98  	public <T extends Group> Set<T> getGroupsAsSet()
99      {
100         return (Set<T>)groupSet;
101     }
102 
103     /**
104      * @see org.apache.fulcrum.security.model.basic.entity.BasicUser#removeGroup(org.apache.fulcrum.security.entity.Group)
105      */
106     public void removeGroup(Group group)
107     {
108         getGroups().remove(group);
109     }
110 
111     /**
112      * @see org.apache.fulcrum.security.model.basic.entity.BasicUser#setGroups(org.apache.fulcrum.security.util.GroupSet)
113      */
114     public void setGroups(GroupSet groups)
115     {
116         if(groups != null)
117         {
118             this.groupSet = groups;
119         }
120         else
121         {
122             this.groupSet = new GroupSet();
123         }
124     }
125 
126     /**
127      * @see org.apache.fulcrum.security.model.basic.entity.BasicUser#setGroupsAsSet(java.util.Set)
128      */
129     public <T extends Group> void setGroupsAsSet(Set<T> groups)
130     {
131         setGroups(new GroupSet(groups));
132     }
133     /**
134      * Retrieve attached objects such as users, permissions,....
135      */
136     @Override
137     public void retrieveAttachedObjects( Connection con )
138         throws DataBackendException
139     {
140         retrieveAttachedObjects( con, false );
141     }
142 
143     /**
144      * Retrieve attached objects such as users, permissions,....
145      */
146     @Override
147     public void retrieveAttachedObjects( Connection con, Boolean lazy )
148         throws DataBackendException
149     {
150         this.groupSet = new GroupSet();
151 
152         try {
153             List<TorqueBasicUserGroup> usergroups = getTorqueBasicUserGroupsJoinTorqueBasicGroup(new Criteria(), con);
154     
155             for (TorqueBasicUserGroup tbug : usergroups)
156             {
157                 groupSet.add(tbug.getTorqueBasicGroup());
158             }
159         } catch (TorqueException e ) {
160             throw new DataBackendException( e.getMessage(),e );
161         }
162     }
163 
164     /**
165      * Update this instance to the database with all dependent objects
166      *
167      * @param con A database connection
168      */
169     public void update(Connection con) throws TorqueException
170     {
171         if (groupSet != null)
172         {
173             Criteria criteria = new Criteria();
174 
175             /* remove old entries */
176             criteria.where(TorqueBasicUserGroupPeer.USER_ID, getEntityId());
177             TorqueBasicUserGroupPeer.doDelete(criteria, con);
178 
179             for (Group g : groupSet)
180             {
181                 TorqueBasicUserGroupue/om/TorqueBasicUserGroup.html#TorqueBasicUserGroup">TorqueBasicUserGroup ug = new TorqueBasicUserGroup();
182                 ug.setUserId(getEntityId());
183                 ug.setGroupId((Integer)g.getId());
184                 ug.save(con);
185             }
186         }
187 
188         try
189         {
190             save(con);
191         }
192         catch (Exception e)
193         {
194             throw new TorqueException(e);
195         }
196     }
197 
198     /**
199      * Get the name of the connnection pool associated to this object
200      *
201      * @return the logical Torque database name
202      */
203     public String getDatabaseName()
204     {
205         return TorqueBasicUserPeer.DATABASE_NAME;
206     }
207 
208     /**
209      * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#delete()
210      */
211     public void delete() throws TorqueException
212     {
213         TorqueBasicUserPeer.doDelete(SimpleKey.keyFor(getEntityId()));
214     }
215 }