View Javadoc
1   package org.apache.fulcrum.security.torque.dynamic;
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.Role;
25  import org.apache.fulcrum.security.model.dynamic.entity.DynamicPermission;
26  import org.apache.fulcrum.security.torque.om.TorqueDynamicPermissionPeer;
27  import org.apache.fulcrum.security.torque.om.TorqueDynamicRolePermission;
28  import org.apache.fulcrum.security.torque.om.TorqueDynamicRolePermissionPeer;
29  import org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity;
30  import org.apache.fulcrum.security.util.DataBackendException;
31  import org.apache.fulcrum.security.util.RoleSet;
32  import org.apache.torque.TorqueException;
33  import org.apache.torque.criteria.Criteria;
34  import org.apache.torque.om.SimpleKey;
35  /**
36   * This abstract class provides the SecurityInterface to the managers.
37   *
38   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
39   * @version $Id:$
40   */
41  public abstract class TorqueAbstractDynamicPermission extends TorqueAbstractSecurityEntity
42      implements DynamicPermission
43  {
44      /** Serial version */
45  	private static final long serialVersionUID = -6857144824327889029L;
46  	/** a cache of role objects */
47      private Set<Role> roleSet = null;
48  
49      /**
50       * Forward reference to generated code
51       *
52       * Get a list of association objects, pre-populated with their TorqueDynamicRole
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 Role/Permission relations
60       */
61      protected List<TorqueDynamicRolePermission> getTorqueDynamicRolePermissionsJoinTorqueDynamicRole(Criteria criteria, Connection con)
62          throws TorqueException
63      {
64          criteria.and(TorqueDynamicRolePermissionPeer.PERMISSION_ID, getEntityId() );
65          return TorqueDynamicRolePermissionPeer.doSelectJoinTorqueDynamicRole(criteria, con);
66      }
67  
68      /**
69       * @see org.apache.fulcrum.security.model.dynamic.entity.DynamicPermission#addRole(org.apache.fulcrum.security.entity.Role)
70       */
71      public void addRole(Role role)
72      {
73          getRoles().add(role);
74      }
75  
76      /**
77       * @see org.apache.fulcrum.security.model.dynamic.entity.DynamicPermission#getRoles()
78       */
79      public RoleSet getRoles()
80      {
81          if (roleSet == null)
82          {
83              roleSet = new RoleSet();
84          }
85          else if(!(roleSet instanceof RoleSet))
86          {
87              roleSet = new RoleSet(roleSet);
88          }
89  
90          return (RoleSet)roleSet;
91      }
92  
93      /**
94       * @see org.apache.fulcrum.security.model.dynamic.entity.DynamicPermission#getRolesAsSet()
95       */
96      @SuppressWarnings("unchecked")
97  	public <T extends Role> Set<T> getRolesAsSet()
98      {
99          return (Set<T>)roleSet;
100     }
101 
102     /**
103      * @see org.apache.fulcrum.security.model.dynamic.entity.DynamicPermission#removeRole(org.apache.fulcrum.security.entity.Role)
104      */
105     public void removeRole(Role role)
106     {
107         getRoles().remove(role);
108     }
109 
110     /**
111      * @see org.apache.fulcrum.security.model.dynamic.entity.DynamicPermission#setRoles(org.apache.fulcrum.security.util.RoleSet)
112      */
113     public void setRoles(RoleSet roleSet)
114     {
115         if (roleSet != null)
116         {
117             this.roleSet = roleSet;
118         }
119         else
120         {
121             this.roleSet = new RoleSet();
122         }
123     }
124 
125     /**
126      * @see org.apache.fulcrum.security.model.dynamic.entity.DynamicPermission#setRolesAsSet(java.util.Set)
127      */
128     public <T extends Role> void setRolesAsSet(Set<T> roles)
129     {
130         setRoles(new RoleSet(roles));
131     }
132 
133     /**
134      * @return the database name
135      */
136     public String getDatabaseName()
137     {
138         return TorqueDynamicPermissionPeer.DATABASE_NAME;
139     }
140     
141     @Override
142     public void retrieveAttachedObjects( Connection con )
143         throws DataBackendException
144     {
145         retrieveAttachedObjects( con, false );
146     }
147 
148     /**
149      * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#retrieveAttachedObjects(Connection, Boolean)
150      */
151     @Override
152     public void retrieveAttachedObjects( Connection con, Boolean lazy )
153         throws DataBackendException
154     {
155         this.roleSet = new RoleSet();
156 
157         try {
158             List<TorqueDynamicRolePermission> rolepermissions = getTorqueDynamicRolePermissionsJoinTorqueDynamicRole(new Criteria(), con);
159     
160             for (TorqueDynamicRolePermission tdrp : rolepermissions)
161             {
162                 roleSet.add(tdrp.getTorqueDynamicRole());
163             }
164         } catch (TorqueException e ) {
165             throw new DataBackendException( e.getMessage(),e );
166         }
167     }
168 
169     /**
170      * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#update(java.sql.Connection)
171      */
172     public void update(Connection con) throws TorqueException
173     {
174         if (roleSet != null)
175         {
176             Criteria criteria = new Criteria();
177 
178             /* remove old entries */
179             criteria.where(TorqueDynamicRolePermissionPeer.PERMISSION_ID, getEntityId());
180             TorqueDynamicRolePermissionPeer.doDelete(criteria, con);
181 
182             for (Role r : roleSet)
183             {
184                 TorqueDynamicRolePermissionorqueDynamicRolePermission.html#TorqueDynamicRolePermission">TorqueDynamicRolePermission rp = new TorqueDynamicRolePermission();
185                 rp.setRoleId((Integer)r.getId());
186                 rp.setPermissionId(getEntityId());
187                 rp.save(con);
188             }
189         }
190 
191         try
192         {
193             save(con);
194         }
195         catch (Exception e)
196         {
197             throw new TorqueException(e);
198         }
199     }
200 
201     /**
202      * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#delete()
203      */
204     public void delete() throws TorqueException
205     {
206         TorqueDynamicPermissionPeer.doDelete(SimpleKey.keyFor(getEntityId()));
207     }
208 }