View Javadoc
1   package org.apache.fulcrum.security.torque.turbine;
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.HashSet;
22  import java.util.List;
23  import java.util.Set;
24  
25  import org.apache.fulcrum.security.entity.User;
26  import org.apache.fulcrum.security.model.turbine.entity.TurbineUser;
27  import org.apache.fulcrum.security.model.turbine.entity.TurbineUserGroupRole;
28  import org.apache.fulcrum.security.torque.om.TurbineUserGroupRolePeer;
29  import org.apache.fulcrum.security.torque.om.TurbineUserPeer;
30  import org.apache.fulcrum.security.torque.peer.TurbineUserGroupRoleModelPeerMapper;
31  import org.apache.fulcrum.security.torque.security.turbine.TorqueAbstractTurbineTurbineSecurityEntityDefault;
32  import org.apache.fulcrum.security.util.DataBackendException;
33  import org.apache.torque.TorqueException;
34  import org.apache.torque.criteria.Criteria;
35  import org.apache.torque.om.SimpleKey;
36  
37  /**
38   * This abstract class provides the SecurityInterface to the managers.
39   * 
40   * An implementing class is required to implement {@link User} at least. Most probably the OM classes allow to implement {@link TurbineUser}, which includes User.
41   *
42   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
43   * @version $Id:$
44   */
45  public abstract class DefaultAbstractTurbineUser extends TorqueAbstractTurbineTurbineSecurityEntityDefault
46  {
47      /** Serial version */
48  	private static final long serialVersionUID = -7255623655281852566L;
49      
50      /**
51       * Forward reference to module generated code
52       *
53       * Get a list of association objects, pre-populated with their TurbineGroup
54       * objects.
55       * 
56       * Does intentionally not initialize the cache collTurbineUserGroupRoles for referenced objects. 
57       * 
58       * Be careful not to call any of the generated getTurbineUserGroupRoles methods in derived classes,
59       * the link {@link #save()} method otherwise might not update the right relationships.
60       *
61       * @param criteria Criteria to define the selection of records
62       * @param con a database connection
63       * @throws DataBackendException  
64       *
65       * @return a list of User/Group/Role relations
66       * @throws TorqueException if any database error occurs
67       */
68      protected <T extends TurbineUserGroupRoleModelPeerMapper> List<T> getTurbineUserGroupRolesJoinTurbineGroup(Criteria criteria, Connection con)
69          throws TorqueException, DataBackendException
70      {
71          criteria.and(TurbineUserGroupRolePeer.USER_ID, getEntityId() );
72          try {
73              return (List<T>) TurbineUserGroupRolePeer.doSelectJoinTurbineGroup(criteria, con);
74          } catch (  TorqueException e) {
75              throw new DataBackendException( e.getMessage(), e );
76          }
77      }
78      
79      /** 
80       * @see org.apache.fulcrum.security.torque.security.turbine.TorqueAbstractTurbineTurbineSecurityEntityDefault#retrieveAttachedObjects(java.sql.Connection, java.lang.Boolean, java.util.List)
81       */
82      @Override
83      public <T extends TurbineUserGroupRoleModelPeerMapper> void retrieveAttachedObjects( Connection con, Boolean lazy, List<T> ugrs ) throws DataBackendException, TorqueException
84      {
85          if (!lazy ) { 
86              Set<TurbineUserGroupRole> userGroupRoleSet = new HashSet<TurbineUserGroupRole>();
87      
88              if (ugrs == null) { // default 
89                  // the groups are the keys in roleset, roles are not expected to be used as keys
90                  ugrs = getTurbineUserGroupRolesJoinTurbineGroup(new Criteria(), con);
91              } 
92      
93              maptoModel( con, userGroupRoleSet, ugrs );
94              
95              setUserGroupRoleSet(userGroupRoleSet);
96          }
97      }
98      
99      /* (non-Javadoc)
100      * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#retrieveAttachedObjects(java.sql.Connection, java.lang.Boolean)
101      */
102     @Override
103     public void retrieveAttachedObjects( Connection con, Boolean lazy ) throws TorqueException, DataBackendException
104     {
105         if (!lazy) {
106             Set<TurbineUserGroupRole> userGroupRoleSet = new HashSet<TurbineUserGroupRole>();
107             
108             List<TurbineUserGroupRoleModelPeerMapper> ugrs = getTurbineUserGroupRolesJoinTurbineGroup(new Criteria(), con);
109     
110             // TODO: Need to call this too to fetch right role object? getTurbineUserGroupRolesJoinTurbineRole
111             // org.apache.fulcrum.security.torque.om.TurbineUserGroupRole
112             maptoModel( con, userGroupRoleSet, ugrs );
113     
114             setUserGroupRoleSet(userGroupRoleSet);
115         }
116     }
117 
118     /* (non-Javadoc)
119      * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#retrieveAttachedObjects(java.sql.Connection)
120      */
121     @Override
122     public void retrieveAttachedObjects( Connection con )
123         throws DataBackendException, TorqueException
124     {
125         retrieveAttachedObjects( con, false );
126     }
127 
128     /* (non-Javadoc)
129      * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#update(java.sql.Connection)
130      */
131     @Override
132 	public void update(Connection con) throws TorqueException
133     {
134         try
135         {
136             Set<TurbineUserGroupRole> userGroupRoleSet = getUserGroupRoleSet();
137             if ( userGroupRoleSet != null ) //&& !userGroupRoleSet.isEmpty() commented allow delete/empty 
138             {
139                 Criteria criteria = new Criteria();
140 
141                 /* remove old entries */
142                 criteria.where(TurbineUserGroupRolePeer.USER_ID, getEntityId());
143                 TurbineUserGroupRolePeer.doDelete(criteria, con);
144 
145                 for (TurbineUserGroupRole ugr : userGroupRoleSet)
146                 {
147                     org.apache.fulcrum.security.torque.om.TurbineUserGroupRole ttugr = new org.apache.fulcrum.security.torque.om.TurbineUserGroupRole();
148                     ttugr.setGroupId((Integer)ugr.getGroup().getId());
149                     ttugr.setUserId((Integer)ugr.getUser().getId());
150                     ttugr.setRoleId((Integer)ugr.getRole().getId());
151                     ttugr.save(con);
152                 }
153             }
154             save(con);
155         }
156         catch (Exception e)
157         {
158             throw new TorqueException(e);
159         }
160     }
161 
162     /* (non-Javadoc)
163      * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#delete()
164      */
165     @Override
166 	public void delete() throws TorqueException
167     {
168         TurbineUserPeer.doDelete(SimpleKey.keyFor(getEntityId()));
169     }
170     
171     /**
172      * @param con data connection
173      * @param userGroupRoleSet U/G/R to be set / target object
174      * @param ugrs list of all ugrs
175      * @throws TorqueException if data connection could not be found
176      */
177     private <T extends TurbineUserGroupRoleModelPeerMapper> void maptoModel( Connection con, 
178             Set<TurbineUserGroupRole> userGroupRoleSet,
179                              List<T> ugrs )
180         throws DataBackendException
181     {
182         try {
183             for (TurbineUserGroupRoleModelPeerMapper ttugr : ugrs)
184             {
185                 TurbineUserGroupRolee/om/TurbineUserGroupRole.html#TurbineUserGroupRole">TurbineUserGroupRole ugr = new TurbineUserGroupRole();
186                 ugr.setUser((User) this);
187                 ugr.setRole(ttugr.getTurbineRole(con));
188                 
189                 // org.apache.fulcrum.security.torque.om.TurbineGroup implements 
190                 // org.apache.fulcrum.security.model.turbine.entity.TurbineGroup
191                 // but may be hides it? 
192                 ugr.setGroup(ttugr.getTurbineGroup(con));
193                 userGroupRoleSet.add(ugr);
194             }
195         } catch (TorqueException e ) {
196             throw new DataBackendException( e.getMessage(),e );
197         }
198     }
199 }