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.TorqueTurbineUserGroupRole;
29  import org.apache.fulcrum.security.torque.om.TorqueTurbineUserGroupRolePeer;
30  import org.apache.fulcrum.security.torque.om.TorqueTurbineUserPeer;
31  import org.apache.fulcrum.security.torque.security.turbine.TorqueAbstractTurbineTurbineSecurityEntity;
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   * This abstract class provides the SecurityInterface to the managers.
38   * 
39   * An implementing class is required to implement {@link User} at least. Most probably the OM classes allow to implement {@link TurbineUser}, which includes User.
40   *
41   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
42   * @version $Id:$
43   */
44  public abstract class FulcrumAbstractTurbineUser extends TorqueAbstractTurbineTurbineSecurityEntity
45  {
46      /** Serial version */
47  	private static final long serialVersionUID = -7255623655281852566L;
48  
49      /**
50       * Forward reference to generated code
51       *
52       * Get a list of association objects, pre-populated with their TorqueTurbineRole
53       * objects.
54       *
55       * @param criteria Criteria to define the selection of records
56       * @param con a database connection
57       * @throws TorqueException  if any database error occurs
58       *
59       * @return a list of User/Group/Role relations
60       */
61      protected List<TorqueTurbineUserGroupRole> getTorqueTurbineUserGroupRolesJoinTorqueTurbineGroup(Criteria criteria, Connection con)
62          throws TorqueException
63      {
64          criteria.and(TorqueTurbineUserGroupRolePeer.USER_ID, getEntityId() );
65          return TorqueTurbineUserGroupRolePeer.doSelectJoinTorqueTurbineGroup(criteria, con);
66      }
67      
68      /* (non-Javadoc)
69       * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#retrieveAttachedObjects(java.sql.Connection)
70       */
71      @Override
72      public void retrieveAttachedObjects( Connection con )
73          throws DataBackendException
74      {
75          retrieveAttachedObjects( con, false );
76      }
77  
78      /* (non-Javadoc)
79       * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#retrieveAttachedObjects(java.sql.Connection, java.lang.Boolean)
80       */
81      @Override
82      public void retrieveAttachedObjects(Connection con, Boolean lazy) throws DataBackendException
83      {
84          try {
85              if (!lazy) {
86                  Set<TurbineUserGroupRole> userGroupRoleSet = new HashSet<TurbineUserGroupRole>();
87                  List<TorqueTurbineUserGroupRole> ugrs = getTorqueTurbineUserGroupRolesJoinTorqueTurbineGroup(new Criteria(), con);
88          
89                  for (TorqueTurbineUserGroupRole ttugr : ugrs)
90                  {
91                      TurbineUserGroupRolee/om/TurbineUserGroupRole.html#TurbineUserGroupRole">TurbineUserGroupRole ugr = new TurbineUserGroupRole();
92                      ugr.setUser((User) this);
93                      ugr.setRole(ttugr.getTorqueTurbineRole());
94                      ugr.setGroup(ttugr.getTorqueTurbineGroup(con));
95                      userGroupRoleSet.add(ugr);
96                  }
97                  setUserGroupRoleSet(userGroupRoleSet);
98              }
99         } catch (TorqueException e ) {
100             throw new DataBackendException( e.getMessage(),e );
101        }
102     }
103 
104     /* (non-Javadoc)
105      * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#update(java.sql.Connection)
106      */
107     @Override
108 	public void update(Connection con) throws TorqueException
109     {
110         try
111         {
112         	Set<TurbineUserGroupRole> userGroupRoleSet = getUserGroupRoleSet();
113             if (userGroupRoleSet != null)
114             {
115                 Criteria criteria = new Criteria();
116     
117                 /* remove old entries */
118                 criteria.where(TorqueTurbineUserGroupRolePeer.USER_ID, getEntityId());
119                 TorqueTurbineUserGroupRolePeer.doDelete(criteria, con);
120     
121                 for (TurbineUserGroupRole ugr : userGroupRoleSet)
122                 {
123                     TorqueTurbineUserGroupRolequeTurbineUserGroupRole.html#TorqueTurbineUserGroupRole">TorqueTurbineUserGroupRole ttugr = new TorqueTurbineUserGroupRole();
124                     ttugr.setGroupId((Integer)ugr.getGroup().getId());
125                     ttugr.setUserId((Integer)ugr.getUser().getId());
126                     ttugr.setRoleId((Integer)ugr.getRole().getId());
127                     ttugr.save(con);
128                 }
129             }
130             save(con);
131         }
132         catch (Exception e)
133         {
134             throw new TorqueException(e);
135         }
136     }
137 
138     
139     /* (non-Javadoc)
140      * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#delete()
141      */
142     @Override
143 	public void delete() throws TorqueException
144     {
145         TorqueTurbineUserPeer.doDelete(SimpleKey.keyFor(getEntityId()));
146     }
147 }