1 package org.apache.fulcrum.security.torque.dynamic;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
37
38
39
40
41 public abstract class TorqueAbstractDynamicPermission extends TorqueAbstractSecurityEntity
42 implements DynamicPermission
43 {
44
45 private static final long serialVersionUID = -6857144824327889029L;
46
47 private Set<Role> roleSet = null;
48
49
50
51
52
53
54
55
56
57
58
59
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
70
71 public void addRole(Role role)
72 {
73 getRoles().add(role);
74 }
75
76
77
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
95
96 @SuppressWarnings("unchecked")
97 public <T extends Role> Set<T> getRolesAsSet()
98 {
99 return (Set<T>)roleSet;
100 }
101
102
103
104
105 public void removeRole(Role role)
106 {
107 getRoles().remove(role);
108 }
109
110
111
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
127
128 public <T extends Role> void setRolesAsSet(Set<T> roles)
129 {
130 setRoles(new RoleSet(roles));
131 }
132
133
134
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
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
171
172 public void update(Connection con) throws TorqueException
173 {
174 if (roleSet != null)
175 {
176 Criteria criteria = new Criteria();
177
178
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
203
204 public void delete() throws TorqueException
205 {
206 TorqueDynamicPermissionPeer.doDelete(SimpleKey.keyFor(getEntityId()));
207 }
208 }