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.List;
22  import java.util.Set;
23  
24  import org.apache.fulcrum.security.entity.Role;
25  import org.apache.fulcrum.security.model.turbine.entity.TurbinePermission;
26  import org.apache.fulcrum.security.torque.om.TurbinePermissionPeer;
27  import org.apache.fulcrum.security.torque.om.TurbineRolePermission;
28  import org.apache.fulcrum.security.torque.om.TurbineRolePermissionPeer;
29  import org.apache.fulcrum.security.torque.peer.TurbineRolePermissionPeerMapper;
30  import org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity;
31  import org.apache.fulcrum.security.util.DataBackendException;
32  import org.apache.fulcrum.security.util.RoleSet;
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   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
40   * @version $Id:$
41   */
42  public abstract class DefaultAbstractTurbinePermission extends TorqueAbstractSecurityEntity
43      implements TurbinePermission
44  {
45      /** Serial version */
46  	private static final long serialVersionUID = -5313324873688923207L;
47  
48  	/** a cache of role objects */
49      private Set<Role> roleSet = null;
50  
51      /**
52       * Forward reference to generated code
53       *
54       * Get a list of association objects, pre-populated with their TurbineRole
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 Role/Permission relations
62       */
63      protected <T extends TurbineRolePermissionPeerMapper> List<T> getTurbineRolePermissionsJoinTurbineRole(Criteria criteria, Connection con)
64          throws TorqueException
65      {
66          criteria.and(TurbineRolePermissionPeer.PERMISSION_ID, getEntityId() );
67          return (List<T>) TurbineRolePermissionPeer.doSelectJoinTurbineRole(criteria, con);
68      }
69  
70      /**
71       * @see org.apache.fulcrum.security.model.turbine.entity.TurbinePermission#addRole(org.apache.fulcrum.security.entity.Role)
72       */
73      @Override
74  	public void addRole(Role role)
75      {
76          getRoles().add(role);
77      }
78  
79      /**
80       * @see org.apache.fulcrum.security.model.turbine.entity.TurbinePermission#getRoles()
81       */
82      @Override
83  	public RoleSet getRoles()
84      {
85          if (roleSet == null)
86          {
87              roleSet = new RoleSet();
88          }
89          else if(!(roleSet instanceof RoleSet))
90          {
91              roleSet = new RoleSet(roleSet);
92          }
93  
94          return (RoleSet)roleSet;
95      }
96  
97      /**
98       * @see org.apache.fulcrum.security.model.turbine.entity.TurbinePermission#getRolesAsSet()
99       */
100     @Override
101 	@SuppressWarnings("unchecked")
102 	public <T extends Role> Set<T> getRolesAsSet()
103     {
104         return (Set<T>)roleSet;
105     }
106 
107     /**
108      * @see org.apache.fulcrum.security.model.turbine.entity.TurbinePermission#removeRole(org.apache.fulcrum.security.entity.Role)
109      */
110     @Override
111 	public void removeRole(Role role)
112     {
113         getRoles().remove(role);
114     }
115 
116     /**
117      * @see org.apache.fulcrum.security.model.turbine.entity.TurbinePermission#setRoles(org.apache.fulcrum.security.util.RoleSet)
118      */
119     @Override
120 	public void setRoles(RoleSet roleSet)
121     {
122         if (roleSet != null)
123         {
124             this.roleSet = roleSet;
125         }
126         else
127         {
128             this.roleSet = new RoleSet();
129         }
130     }
131 
132     /**
133      * @see org.apache.fulcrum.security.model.turbine.entity.TurbinePermission#setRolesAsSet(java.util.Set)
134      */
135     @Override
136 	public <T extends Role> void setRolesAsSet(Set<T> roles)
137     {
138         setRoles(new RoleSet(roles));
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         try {
156             
157             this.roleSet = new RoleSet();
158     
159             List<TurbineRolePermissionPeerMapper> rolepermissions = getTurbineRolePermissionsJoinTurbineRole(new Criteria(), con);
160     
161             for (TurbineRolePermissionPeerMapper ttrp : rolepermissions)
162             {
163                 roleSet.add(ttrp.getTurbineRole());
164             }
165             
166         } catch (TorqueException e ) {
167             throw new DataBackendException( e.getMessage(),e );
168         }
169     }
170 
171     /**
172      * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#update(java.sql.Connection)
173      */
174     @Override
175 	public void update(Connection con) throws TorqueException
176     {
177         if (roleSet != null && !roleSet.isEmpty())
178         {
179             Criteria criteria = new Criteria();
180 
181             /* remove old entries */
182             criteria.where(TurbineRolePermissionPeer.PERMISSION_ID, getEntityId());
183             TurbineRolePermissionPeer.doDelete(criteria, con);
184 
185             for (Role r : roleSet)
186             {
187                 TurbineRolePermissione/om/TurbineRolePermission.html#TurbineRolePermission">TurbineRolePermission rp = new TurbineRolePermission();
188                 rp.setRoleId((Integer)r.getId());
189                 rp.setPermissionId(getEntityId());
190                 rp.save(con);
191             }
192         }
193 
194         try
195         {
196             save(con);
197         }
198         catch (Exception e)
199         {
200             throw new TorqueException(e);
201         }
202     }
203 
204     /**
205      * @see org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity#delete()
206      */
207     @Override
208 	public void delete() throws TorqueException
209     {
210         TurbinePermissionPeer.doDelete(SimpleKey.keyFor(getEntityId()));
211     }
212 }