KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > ejb > authorization > AdminGroupDataBean


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package org.ejbca.core.ejb.authorization;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 import javax.ejb.CreateException JavaDoc;
21 import javax.ejb.EJBException JavaDoc;
22 import javax.ejb.RemoveException JavaDoc;
23
24 import org.ejbca.core.ejb.BaseEntityBean;
25 import org.ejbca.core.ejb.ServiceLocator;
26 import org.ejbca.core.model.authorization.AccessRule;
27 import org.ejbca.core.model.authorization.AdminEntity;
28 import org.ejbca.core.model.authorization.AdminGroup;
29
30
31 /** Entity bean should not be used directly, use though Session beans.
32  *
33  * Entity Bean representing authorization admingroup.
34  * Information stored:
35  * <pre>
36  * admingroupname
37  * caid
38  *
39  * AccessRules
40  * Admin entities
41  * </pre>
42  *
43  * @version $Id: AdminGroupDataBean.java,v 1.6 2006/11/10 11:22:18 anatom Exp $
44  *
45  * @ejb.bean
46  * description="This enterprise bean entity represents an authorization usergroup"
47  * display-name="AdminGroupDataEB"
48  * name="AdminGroupData"
49  * jndi-name="AdminGroupData"
50  * view-type="local"
51  * type="CMP"
52  * reentrant="False"
53  * cmp-version="2.x"
54  * transaction-type="Container"
55  * schema="AdminGroupDataBean"
56  * primkey-field="primKey"
57  *
58  * @ejb.home
59  * generate="local"
60  * local-extends="javax.ejb.EJBLocalHome"
61  * local-class="org.ejbca.core.ejb.authorization.AdminGroupDataLocalHome"
62  *
63  * @ejb.persistence table-name = "AdminGroupData"
64  *
65  * Vi use EJB relationships here and it often requires a transaction so it's easiest to set Required on all.
66  * @ejb.transaction type="Required"
67  *
68  * @ejb.interface
69  * generate="local"
70  * local-extends="javax.ejb.EJBLocalObject"
71  * local-class="org.ejbca.core.ejb.authorization.AdminGroupDataLocal"
72  *
73  * @ejb.finder
74  * description="findByGroupNameAndCAId"
75  * signature="org.ejbca.core.ejb.authorization.AdminGroupDataLocal findByGroupNameAndCAId(java.lang.String name, int id)"
76  * query="SELECT OBJECT(a) from AdminGroupDataBean a WHERE a.adminGroupName=?1 AND a.caId=?2"
77  *
78  * @ejb.finder
79  * description="findAll"
80  * signature="java.util.Collection findAll()"
81  * query="SELECT OBJECT(a) from AdminGroupDataBean a"
82  *
83  * @ejb.ejb-external-ref
84  * description=""
85  * view-type="local"
86  * ref-name="ejb/AdminEntityDataLocal"
87  * type="Entity"
88  * home="org.ejbca.core.ejb.authorization.AdminEntityDataLocalHome"
89  * business="org.ejbca.core.ejb.authorization.AdminEntityDataLocal"
90  * link="AdminEntityData"
91  *
92  * @ejb.ejb-external-ref
93  * description=""
94  * view-type="local"
95  * ref-name="ejb/AccessRulesDataLocal"
96  * type="Entity"
97  * home="org.ejbca.core.ejb.authorization.AccessRulesDataLocalHome"
98  * business="org.ejbca.core.ejb.authorization.AccessRulesDataLocal"
99  * link="AccessRulesData"
100  *
101  */

102 public abstract class AdminGroupDataBean extends BaseEntityBean {
103
104     /**
105      * @ejb.persistence column-name="pK"
106      * @ejb.pk-field
107      */

108     public abstract Integer JavaDoc getPrimKey();
109     public abstract void setPrimKey(Integer JavaDoc primKey);
110
111     /**
112      * @ejb.persistence column-name="adminGroupName"
113      * @ejb.interface-method view-type="local"
114      */

115     public abstract String JavaDoc getAdminGroupName();
116
117     /**
118      * @ejb.interface-method view-type="local"
119      */

120     public abstract void setAdminGroupName(String JavaDoc admingroupname);
121
122     /**
123      * @ejb.persistence column-name="cAId"
124      * @ejb.interface-method view-type="local"
125      */

126     public abstract int getCaId();
127
128     /**
129      * @ejb.interface-method view-type="local"
130      */

131     public abstract void setCaId(int caid);
132
133     /**
134      * @ejb.relation name="AdminGroupDataToAdminEntities" role-name="AdminGroupData"
135      * target-role-name="AdminEntityData" target-ejb="AdminEntityData"
136      *
137      * @jboss.target-relation
138      * related-pk-field="primKey"
139      * fk-column="AdminGroupData_adminEntities"
140      *
141      * @weblogic.target-column-map
142      * key-column="pK"
143      * foreign-key-column="AdminGroupData_adminEntities"
144      *
145      * @sunone.relation
146      * column="AdminGroupData.pK"
147      * target="AdminEntityData.AdminGroupData_adminEntities"
148      */

149     public abstract Collection JavaDoc getAdminEntities();
150     public abstract void setAdminEntities(Collection JavaDoc adminentities);
151
152     /**
153      * @ejb.relation
154      * name="AdminGroupDataToAccessRules" role-name="AdminGroupData"
155      * target-role-name="AccessRulesData" target-ejb="AccessRulesData"
156      *
157      * @jboss.target-relation
158      * related-pk-field="primKey"
159      * fk-column="AdminGroupData_accessRules"
160      *
161      * @weblogic.target-column-map
162      * key-column="pK"
163      * foreign-key-column="AdminGroupData_accessRules"
164      *
165      * @sunone.relation
166      * column="AdminGroupData.pK"
167      * target="AccessRulesData.AdminGroupData_accessRules"
168      */

169     public abstract Collection JavaDoc getAccessRules();
170     public abstract void setAccessRules(Collection JavaDoc accessrules);
171
172     /**
173      * Adds a Collection of AccessRule to the database. Changing their values if they already exists
174      * @ejb.interface-method view-type="local"
175      */

176     public void addAccessRules(Collection JavaDoc accessrules) {
177         Iterator JavaDoc iter = accessrules.iterator();
178         while (iter.hasNext()) {
179             AccessRule accessrule = (AccessRule) iter.next();
180             try {
181                 AccessRulesDataLocal data = createAccessRule(accessrule);
182                 Iterator JavaDoc i = getAccessRules().iterator();
183                 while (i.hasNext()) {
184                     AccessRulesDataLocal ar = (AccessRulesDataLocal) i.next();
185                     if (ar.getAccessRuleObject().getAccessRule().equals(accessrule.getAccessRule())) {
186                         getAccessRules().remove(ar);
187                         try {
188                             ar.remove();
189                         } catch (RemoveException JavaDoc e) {
190                             error("Error adding AccessRules: ", e);
191                             throw new EJBException JavaDoc(e);
192                         }
193                         break;
194                     }
195                 }
196                 getAccessRules().add(data);
197             } catch (Exception JavaDoc e) {
198                 error("Error adding AccessRules: ", e);
199             }
200         }
201     } // addAccessRules
202

203     /**
204      * Removes a Collection of (String) accessrules from the database.
205      * @ejb.interface-method view-type="local"
206      */

207     public void removeAccessRules(Collection JavaDoc accessrules) {
208         Iterator JavaDoc iter = accessrules.iterator();
209         while (iter.hasNext()) {
210             String JavaDoc accessrule = (String JavaDoc) iter.next();
211
212             Iterator JavaDoc i = getAccessRules().iterator();
213             while (i.hasNext()) {
214                 AccessRulesDataLocal ar = (AccessRulesDataLocal) i.next();
215                 if (ar.getAccessRuleObject().getAccessRule().equals(accessrule)) {
216                     getAccessRules().remove(ar);
217                     try {
218                         ar.remove();
219                     } catch (RemoveException JavaDoc e) {
220                         error("Error removing AccessRules: ", e);
221                         throw new EJBException JavaDoc(e);
222                     }
223                     break;
224                 }
225             }
226         }
227     } // removeAccessRules
228

229     /**
230      * Returns the number of access rules in admingroup
231      *
232      * @return the number of accessrules in the database
233      * @ejb.interface-method view-type="local"
234      */

235     public int getNumberOfAccessRules() {
236         return getAccessRules().size();
237     } // getNumberOfAccessRules
238

239     /**
240      * Returns all the accessrules as a Collection of AccessRules
241      * @ejb.interface-method view-type="local"
242      */

243     public Collection JavaDoc getAccessRuleObjects() {
244         final Collection JavaDoc rules = getAccessRules();
245         ArrayList JavaDoc objects = new ArrayList JavaDoc(rules.size());
246         Iterator JavaDoc i = rules.iterator();
247         while (i.hasNext()) {
248             AccessRulesDataLocal ar = (AccessRulesDataLocal) i.next();
249             objects.add(ar.getAccessRuleObject());
250         }
251         return objects;
252     }
253
254     /**
255      * Adds a Collection of AdminEntity to the database. Changing their values if they already exists
256      * @ejb.interface-method view-type="local"
257      */

258     public void addAdminEntities(Collection JavaDoc adminentities) {
259         Iterator JavaDoc iter = adminentities.iterator();
260         while (iter.hasNext()) {
261             AdminEntity adminentity = (AdminEntity) iter.next();
262             try {
263                 AdminEntityDataLocal data = createAdminEntity(adminentity);
264                 AdminEntityPK datapk = createAdminEntityPK(getAdminGroupName(), getCaId(), adminentity.getMatchWith(), adminentity.getMatchType(), adminentity.getMatchValue());
265
266                 Iterator JavaDoc i = getAdminEntities().iterator();
267                 while (i.hasNext()) {
268                     AdminEntityDataLocal ue = (AdminEntityDataLocal) i.next();
269                     // TODO use ue.getPrimaryKey() ?
270
AdminEntityPK uepk = createAdminEntityPK(getAdminGroupName(), getCaId(), ue.getMatchWith()
271                             , ue.getMatchType(), ue.getMatchValue());
272                     if (uepk.equals(datapk)) {
273                         getAdminEntities().remove(ue);
274                         try {
275                             ue.remove();
276                         } catch (RemoveException JavaDoc e) {
277                             error("Error adding AdminEntities: ", e);
278                             throw new EJBException JavaDoc(e);
279                         }
280                         break;
281                     }
282                 }
283                 getAdminEntities().add(data);
284             } catch (Exception JavaDoc e) {
285                 error("Error adding AdminEntities: ", e);
286             }
287         }
288     } // addAdminEntities
289

290
291     /**
292      * Removes a Collection if AdminEntity from the database.
293      * @ejb.interface-method view-type="local"
294      */

295     public void removeAdminEntities(Collection JavaDoc adminentities) {
296         Iterator JavaDoc iter = adminentities.iterator();
297
298         while (iter.hasNext()) {
299             AdminEntity adminentity = (AdminEntity) iter.next();
300             AdminEntityPK datapk = createAdminEntityPK(getAdminGroupName(), getCaId(), adminentity.getMatchWith(), adminentity.getMatchType(), adminentity.getMatchValue());
301
302             Iterator JavaDoc i = getAdminEntities().iterator();
303             while (i.hasNext()) {
304                 AdminEntityDataLocal ue = (AdminEntityDataLocal) i.next();
305                 // TODO use ue.getPrimaryKey() ?
306
AdminEntityPK uepk = createAdminEntityPK(getAdminGroupName(), getCaId(), ue.getMatchWith(), ue.getMatchType(), ue.getMatchValue());
307                 if (uepk.equals(datapk)) {
308                     getAdminEntities().remove(ue);
309                     try {
310                         ue.remove();
311                     } catch (RemoveException JavaDoc e) {
312                         error("Error removing AdminEntities: ", e);
313                         throw new EJBException JavaDoc(e);
314                     }
315                     break;
316                 }
317             }
318         }
319     } // removeAdminEntities
320

321     // this method is to avoid matching arguments errors while generating the class
322
private AdminEntityPK createAdminEntityPK(String JavaDoc name, int id, int with, int type, String JavaDoc value){
323         AdminEntityPK pk = new AdminEntityPK(name, id, with, type, value);
324         return pk;
325     }
326
327
328     /**
329      * Returns the number of user entities in admingroup
330      *
331      * @return the number of user entities in the database
332      * @ejb.interface-method view-type="local"
333      */

334     public int getNumberOfAdminEntities() {
335         return getAdminEntities().size();
336     } // getNumberOfAdminEntities
337

338     /**
339      * Returns all the adminentities as Collection of AdminEntity.
340      * @ejb.interface-method view-type="local"
341      */

342     public Collection JavaDoc getAdminEntityObjects() {
343         ArrayList JavaDoc returnval = new ArrayList JavaDoc();
344         Iterator JavaDoc i = getAdminEntities().iterator();
345         while (i.hasNext()) {
346             AdminEntityDataLocal ae = (AdminEntityDataLocal) i.next();
347             returnval.add(ae.getAdminEntity(getCaId()));
348         }
349         return returnval;
350     } // getAdminEntityObjects
351

352     /**
353      * Returns the data in admingroup representation.
354      * @ejb.interface-method view-type="local"
355      */

356     public AdminGroup getAdminGroup() {
357         ArrayList JavaDoc accessrules = new ArrayList JavaDoc();
358         ArrayList JavaDoc adminentities = new ArrayList JavaDoc();
359
360         Iterator JavaDoc i = null;
361         i = getAdminEntities().iterator();
362         while (i.hasNext()) {
363             AdminEntityDataLocal ae = (AdminEntityDataLocal) i.next();
364             adminentities.add(ae.getAdminEntity(getCaId()));
365         }
366
367         i = getAccessRules().iterator();
368         while (i.hasNext()) {
369             AccessRulesDataLocal ar = (AccessRulesDataLocal) i.next();
370             accessrules.add(ar.getAccessRuleObject());
371         }
372
373         return new AdminGroup(getPrimKey().intValue(), getAdminGroupName(), getCaId(), accessrules, adminentities);
374     } // getAdminGroup
375

376     /**
377      * Returns an AdminGroup object only containing name and caid and no access data.
378      * @ejb.interface-method view-type="local"
379      */

380     public AdminGroup getAdminGroupNames() {
381         return new AdminGroup(getPrimKey().intValue(), getAdminGroupName(), getCaId(), null, null);
382     } // getAdminGroupNames
383
//
384
// Fields required by Container
385
//
386

387     /**
388      * Entity Bean holding data of raadmin profilegroups.
389      * @param admingroupname
390      *
391      * @ejb.create-method view-type="local"
392      */

393     public Integer JavaDoc ejbCreate(Integer JavaDoc pk, String JavaDoc admingroupname, int caid) throws CreateException JavaDoc {
394         setPrimKey(pk);
395         setAdminGroupName(admingroupname);
396         setCaId(caid);
397         debug("Created admingroup : " + admingroupname);
398         return pk;
399     }
400
401     public void ejbPostCreate(Integer JavaDoc pk, String JavaDoc admingroupname, int caid) {
402         // Do nothing. Required.
403
}
404
405     // Private Methods.
406
private AdminEntityDataLocal createAdminEntity(AdminEntity adminentity) throws CreateException JavaDoc {
407         AdminEntityDataLocalHome home = (AdminEntityDataLocalHome) ServiceLocator.getInstance().getLocalHome(AdminEntityDataLocalHome.COMP_NAME);
408         AdminEntityDataLocal returnval = home.create(getAdminGroupName(), getCaId(), adminentity.getMatchWith(),
409                 adminentity.getMatchType(), adminentity.getMatchValue());
410         return returnval;
411     }
412
413     private AccessRulesDataLocal createAccessRule(AccessRule accessrule) throws CreateException JavaDoc {
414         AccessRulesDataLocalHome home = (AccessRulesDataLocalHome) ServiceLocator.getInstance().getLocalHome(AccessRulesDataLocalHome.COMP_NAME);
415         AccessRulesDataLocal returnval = home.create(getAdminGroupName(), getCaId(), accessrule.getAccessRule(),
416                 accessrule.getRule(), accessrule.isRecursive());
417         return returnval;
418     }
419 }
420
Popular Tags