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.User;
25  import org.apache.fulcrum.security.model.basic.entity.BasicGroup;
26  import org.apache.fulcrum.security.torque.om.TorqueBasicGroupPeer;
27  import org.apache.fulcrum.security.torque.om.TorqueBasicUserGroup;
28  import org.apache.fulcrum.security.torque.om.TorqueBasicUserGroupPeer;
29  import org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity;
30  import org.apache.fulcrum.security.util.DataBackendException;
31  import org.apache.fulcrum.security.util.UserSet;
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 TorqueAbstractBasicGroup extends TorqueAbstractSecurityEntity
43      implements BasicGroup
44  {
45      /** Serial version */
46  	private static final long serialVersionUID = -3639383219058996135L;
47  
48  	/** a cache of user objects */
49      private Set<User> userSet = null;
50  
51      /**
52       * Forward reference to generated code
53       *
54       * Get a list of association objects, pre-populated with their TorqueBasicUser
55       * objects.
56       *
57       * @param criteria Criteria to define the selection of records
58       * @param con a database connection
59       * @throws TorqueException  if any database error occurs
60       *
61       * @return a list of User/Group relations
62       */
63      protected List<TorqueBasicUserGroup> getTorqueBasicUserGroupsJoinTorqueBasicUser(Criteria criteria, Connection con)
64          throws TorqueException
65      {
66          criteria.and(TorqueBasicUserGroupPeer.GROUP_ID, getEntityId() );
67          return TorqueBasicUserGroupPeer.doSelectJoinTorqueBasicUser(criteria, con);
68      }
69  
70      /**
71       * @see org.apache.fulcrum.security.model.basic.entity.BasicGroup#addUser(org.apache.fulcrum.security.entity.User)
72       */
73      public void addUser(User user)
74      {
75          getUsers().add(user);
76      }
77  
78      /**
79       * @see org.apache.fulcrum.security.model.basic.entity.BasicGroup#getUsers()
80       */
81      public UserSet getUsers()
82      {
83          if (userSet == null)
84          {
85              userSet = new UserSet();
86          }
87          else if(!(userSet instanceof UserSet))
88          {
89              userSet = new UserSet(userSet);
90          }
91  
92          return (UserSet)userSet;
93      }
94  
95      /**
96       * @see org.apache.fulcrum.security.model.basic.entity.BasicGroup#getUsersAsSet()
97       */
98      @SuppressWarnings("unchecked")
99  	public <T extends User> Set<T> getUsersAsSet()
100     {
101         return (Set<T>)userSet;
102     }
103 
104     /**
105      * @see org.apache.fulcrum.security.model.basic.entity.BasicGroup#removeUser(org.apache.fulcrum.security.entity.User)
106      */
107     public void removeUser(User user)
108     {
109         getUsers().remove(user);
110     }
111 
112     /**
113      * @see org.apache.fulcrum.security.model.basic.entity.BasicGroup#setUsers(org.apache.fulcrum.security.util.UserSet)
114      */
115     public void setUsers(UserSet userSet)
116     {
117         if(userSet != null)
118         {
119             this.userSet = userSet;
120         }
121         else
122         {
123             this.userSet = new UserSet();
124         }
125     }
126 
127     /**
128      * @see org.apache.fulcrum.security.model.basic.entity.BasicGroup#setUsersAsSet(java.util.Set)
129      */
130     public <T extends User> void setUsersAsSet(Set<T> users)
131     {
132         setUsers(new UserSet(users));
133     }
134     
135     @Override
136     public void retrieveAttachedObjects( Connection con )
137         throws DataBackendException
138     {
139         retrieveAttachedObjects( con, false );
140     }
141 
142     /**
143      * Retrieve attached objects such as users, permissions,....
144      */
145     @Override
146     public void retrieveAttachedObjects( Connection con, Boolean lazy )
147         throws DataBackendException
148     {
149         this.userSet = new UserSet();
150 
151         try {
152             List<TorqueBasicUserGroup> usergroups =
153             	getTorqueBasicUserGroupsJoinTorqueBasicUser(new Criteria(), con);
154     
155             for (TorqueBasicUserGroup tbug : usergroups)
156             {
157                 userSet.add(tbug.getTorqueBasicUser());
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 (userSet != null)
172         {
173             Criteria criteria = new Criteria();
174 
175             /* remove old entries */
176             criteria.where(TorqueBasicUserGroupPeer.GROUP_ID, getEntityId());
177             TorqueBasicUserGroupPeer.doDelete(criteria, con);
178 
179             for (User u : userSet)
180             {
181                 TorqueBasicUserGroupue/om/TorqueBasicUserGroup.html#TorqueBasicUserGroup">TorqueBasicUserGroup ug = new TorqueBasicUserGroup();
182                 ug.setUserId((Integer)u.getId());
183                 ug.setGroupId(getEntityId());
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 connection pool associated to this object
200      *
201      * @return the logical Torque database name
202      */
203     public String getDatabaseName()
204     {
205         return TorqueBasicGroupPeer.DATABASE_NAME;
206     }
207 
208     /**
209      * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#delete()
210      */
211     public void delete() throws TorqueException
212     {
213         TorqueBasicGroupPeer.doDelete(SimpleKey.keyFor(getEntityId()));
214     }
215 }