KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > ejb > approval > ApprovalDataBean


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.approval;
15
16 import java.io.ByteArrayOutputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.ObjectOutputStream JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import javax.ejb.CreateException JavaDoc;
25 import javax.ejb.EJBException JavaDoc;
26
27 import org.apache.log4j.Logger;
28 import org.ejbca.core.ejb.BaseEntityBean;
29 import org.ejbca.core.model.approval.Approval;
30 import org.ejbca.core.model.approval.ApprovalDataUtil;
31 import org.ejbca.core.model.approval.ApprovalDataVO;
32 import org.ejbca.core.model.approval.ApprovalException;
33 import org.ejbca.core.model.approval.ApprovalRequest;
34 import org.ejbca.core.model.approval.ApprovalRequestExecutionException;
35 import org.ejbca.core.model.approval.ApprovalRequestExpiredException;
36 import org.ejbca.util.Base64;
37 import org.ejbca.util.CertTools;
38
39 /**
40  * Entity bean should not be used directly, use though Session beans.
41  *
42  * Entity Bean representing approval data used to control request and their approvals.
43  * Information stored:
44  * <pre>
45  * id (Primary key), unique row id
46  * approvalid Constructed from action data as actiontype, admin, username etc. It should
47  * result in the same approvalid if the admin tries to request the same action twice.
48  * approvaltype type of action that should be approved, should be one of ApprovalDataVO.APPROVALTYPE_
49  * constants ex: ApprovalDataVO.APPROVALTYPE_VIEWHARDTOKENDATA
50  * endentityprofileid For RA specific approval requests should the related end entity profile id be specified
51  * for non ra request should this field be set to ApprovalDataVO.ANY_ENDENTITYPROFILE
52  * caid For CA specific approval requests should the related ca id be specified
53  * for non ca request should this field be set to ApprovalDataVO.ANY_CA
54  * reqadmincertissuerdn The issuerdn of the administrator certificate that generated the request.
55  * reqadmincertsn The serialnumber of the administrator certificate that generated the request. String in Hex
56  * status Should be one of ApprovalDataVO.STATUS_WAITINGFORAPPROVAL, STATUS_APPROVED, STATUS_REJECTED, STATUS_EXPIRED, STATUS_EXPIREDANDNOTIFIED, STATUS_EXECUTED
57  * approvaldata Stringrepresentation of data of approvals made by one or more administrators
58  * requestdata Data containing information about the request displayed for the approval administrator.
59  * requestdate Date the request for approval were added
60  * expiredate Date the request for action or the approvel action will expire, Long.MAX_VALUE
61  * means that the request/approval never expires
62  * remainingapprovals Indicates the number of approvals that remains in order to execute the action
63  * </pre>
64  *
65  * @ejb.bean
66  * description="This enterprise bean entity represents an approval request or approved request"
67  * display-name="ApprovalDataEB"
68  * name="ApprovalData"
69  * jndi-name="ApprovalData"
70  * local-jndi-name="ApprovalDataLocal"
71  * view-type="local"
72  * type="CMP"
73  * reentrant="True"
74  * cmp-version="2.x"
75  * transaction-type="Container"
76  * schema="ApprovalDataBean"
77  * primkey-field="id"
78  *
79  * @ejb.pk generate="false"
80  * class="java.lang.Integer"
81  *
82  * @ejb.persistence table-name = "ApprovalData"
83  *
84  * @ejb.home
85  * generate="local"
86  * local-extends="javax.ejb.EJBLocalHome"
87  * local-class="org.ejbca.core.ejb.approval.ApprovalDataLocalHome"
88  *
89  * @ejb.interface
90  * generate="local"
91  * local-extends="javax.ejb.EJBLocalObject"
92  * local-class="org.ejbca.core.ejb.approval.ApprovalDataLocal"
93  *
94  * @ejb.finder
95  * description="findByApprovalId"
96  * signature="Collection findByApprovalId(int approvalid)"
97  * query="SELECT OBJECT(a) from ApprovalDataBean a WHERE a.approvalid=?1"
98  *
99  * @ejb.finder
100  * description="findByApprovalIdNonExpired"
101  * signature="Collection findByApprovalIdNonExpired(int approvalid)"
102  * query="SELECT OBJECT(a) from ApprovalDataBean a WHERE a.approvalid=?1 and (a.status=-1 or a.status=0 or a.status=-3)"
103  *
104  * @ejb.finder
105  * description="findAll"
106  * signature="Collection findAll()"
107  * query="SELECT OBJECT(a) from ApprovalDataBean a"
108  *
109  * @ejb.transaction type="Required"
110  *
111  * @jonas.jdbc-mapping
112  * jndi-name="${datasource.jndi-name}"
113  *
114  * @author Philip Vendil
115  * @version $Id: ApprovalDataBean.java,v 1.7 2006/10/07 14:10:31 anatom Exp $
116  */

117 public abstract class ApprovalDataBean extends BaseEntityBean {
118
119     private static final Logger log = Logger.getLogger(ApprovalDataBean.class);
120     
121
122     /**
123      * unique row id
124      *
125      * @ejb.pk-field
126      * @ejb.persistence column-name="id"
127      * @ejb.interface-method view-type="local"
128      */

129     public abstract Integer JavaDoc getId();
130
131     /**
132      * unique row id
133      *
134      */

135     public abstract void setId(Integer JavaDoc id);
136     
137     /**
138      * Constructed from action data as actiontype, admin, username etc. It should
139      * result in the same approvalid if the admin tries to request the same action twice.
140      *
141      * @ejb.pk-field
142      * @ejb.persistence column-name="approvalid"
143      */

144     public abstract int getApprovalid();
145
146     /**
147      * Constructed from action data as actiontype, admin, username etc. It should
148      * result in the same approvalid if the admin tries to request the same action twice.
149      *
150      */

151     public abstract void setApprovalid(int approvalid);
152
153     
154     /**
155      * Type of action that should be approved, should be one of ApprovalDataVO.APPROVALTYPE_
156      * constants ex: ApprovalDataVO.APPROVALTYPE_ADDUSER
157      *
158      * @ejb.persistence column-name="approvaltype"
159      */

160     public abstract int getApprovaltype();
161
162     /**
163      * Type of action that should be approved, should be one of ApprovalDataVO.APPROVALTYPE_
164      * constants ex: ApprovalDataVO.APPROVALTYPE_ADDUSER
165      *
166      */

167     public abstract void setApprovaltype(int approvaltype);
168
169     
170     /**
171      * For RA specific approval requests should the related end entity profile id be specified
172      * for non ra request should this field be set to ApprovalDataVO.ANY_ENDENTITYPROFILE
173      *
174      * @ejb.persistence column-name="endentityprofileid"
175      * @ejb.interface-method view-type="local"
176      */

177     public abstract int getEndentityprofileid();
178
179     
180     /**
181      * For RA specific approval requests should the related end entity profile id be specified
182      * for non ra request should this field be set to ApprovalDataVO.ANY_ENDENTITYPROFILE
183      *
184      */

185     public abstract void setEndentityprofileid(int endentityprofileid);
186     
187     /**
188      * For CA specific approval requests should the related ca id be specified
189      * for non ca request should this field be set to ApprovalDataVO.ANY_CA
190      *
191      * @ejb.persistence column-name="caid"
192      * @ejb.interface-method view-type="local"
193      */

194     public abstract int getCaid();
195
196     
197     /**
198      * For CA specific approval requests should the related ca id be specified
199      * for non ca request should this field be set to ApprovalDataVO.ANY_CA
200      *
201      */

202     public abstract void setCaid(int caid);
203     
204     /**
205      * The issuerdn of the administrator certificate that generated the request.
206      *
207      * @ejb.persistence column-name="reqadmincertissuerdn"
208      */

209     public abstract String JavaDoc getReqadmincertissuerdn();
210
211     /**
212      * The issuerdn of the administrator certificate that generated the request.
213      *
214      */

215     public abstract void setReqadmincertissuerdn(String JavaDoc reqadmincertissuerdn);
216     
217     /**
218      * The serialnumber of the administrator certificate that generated the request. String in Hex.
219      *
220      * @ejb.persistence column-name="reqadmincertsn"
221      */

222     public abstract String JavaDoc getReqadmincertsn();
223
224     /**
225      * The serialnumber of the administrator certificate that generated the request. String in Hex.
226      *
227      */

228     public abstract void setReqadmincertsn(String JavaDoc reqadmincertsn);
229     
230     /**
231      * Should be one of ApprovalDataVO.STATUS_WAITINGFORAPPROVAL, STATUS_APPROVED,
232      * STATUS_REJECTED, STATUS_EXPIRED
233      *
234      * @ejb.persistence column-name="status"
235      * @ejb.interface-method view-type="local"
236      */

237     public abstract int getStatus();
238
239     /**
240      * Should be one of ApprovalDataVO.STATUS_WAITINGFORAPPROVAL, STATUS_APPROVED,
241      * STATUS_REJECTED, STATUS_EXPIRED
242      *
243      */

244     public abstract void setStatus(int status);
245
246     /**
247      * Stringrepresentation of data of approvals made by one or more administrators
248      *
249      * @ejb.persistence jdbc-type="LONGVARCHAR" column-name="approvaldata"
250      */

251     public abstract String JavaDoc getApprovaldata();
252
253     /**
254      * Stringrepresentation of data of approvals made by one or more administrators
255      */

256     public abstract void setApprovaldata(String JavaDoc approvaldata);
257
258     /**
259      * Data containing information about the request displayed for the approval administrator.
260      *
261      * @ejb.persistence jdbc-type="LONGVARCHAR" column-name="requestdata"
262      */

263     public abstract String JavaDoc getRequestdata();
264
265     /**
266      * Data containing information about the request displayed for the approval administrator.
267      */

268     public abstract void setRequestdata(String JavaDoc requestdata);
269     
270     /**
271      * Date the request for approval were added
272      *
273      * @ejb.persistence column-name="requestdate"
274      */

275     public abstract long getRequestdate();
276
277     /**
278      * Date the request for approval were added
279      *
280      */

281     public abstract void setRequestdate(long requestdate);
282     
283     /**
284      * Date the request for action or the approvel action will expire, Long.MAX_VALUE
285      * means that the request/approval never expires
286      *
287      * @ejb.persistence column-name="expiredate"
288      */

289     public abstract long getExpiredate();
290
291     /**
292      * Date the request for action or the approvel action will expire, Long.MAX_VALUE
293      * means that the request/approval never expires
294      *
295      */

296     public abstract void setExpiredate(long expiredate);
297       
298     /**
299      * Indicates the number of approvals that remains in order to execute the action
300      *
301      * @ejb.persistence column-name="remainingapprovals"
302      */

303     public abstract int getRemainingapprovals();
304
305     
306     /**
307      * Indicates the number of approvals that remains in order to execute the action
308      *
309      */

310     public abstract void setRemainingapprovals(int remainingapprovals);
311     
312     
313     
314     private Collection JavaDoc getApprovals() {
315         return ApprovalDataUtil.getApprovals(getApprovaldata());
316     }
317     
318     /**
319      * Collection of Approval
320      * @param approvals cannot be null.
321      * @throws IOException
322      */

323     private void setApprovals(Collection JavaDoc approvals){
324         try{
325             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
326             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
327             
328             int size = approvals.size();
329             oos.writeInt(size);
330             Iterator JavaDoc iter = approvals.iterator();
331             while(iter.hasNext()){
332                 Approval next = (Approval) iter.next();
333                 oos.writeObject(next);
334             }
335             oos.flush();
336             
337             setApprovaldata(new String JavaDoc(Base64.encode(baos.toByteArray(),false)));
338         } catch (IOException JavaDoc e) {
339             log.error("Error building approvals.",e);
340             throw new EJBException JavaDoc(e);
341         }
342     }
343     
344     private ApprovalRequest getApprovalRequest() {
345         return ApprovalDataUtil.getApprovalRequest(getRequestdata());
346     }
347     
348     private void setApprovalRequest(ApprovalRequest approvalRequest){
349         try{
350         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
351         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
352         oos.writeObject(approvalRequest);
353         oos.flush();
354         setRequestdata(new String JavaDoc(Base64.encode(baos.toByteArray(),false)));
355         }catch(IOException JavaDoc e){
356             log.error("Error building approval request.",e);
357             throw new EJBException JavaDoc(e);
358         }
359     }
360     
361     private Date JavaDoc getRequestDate(){
362         return new Date JavaDoc(getRequestdate());
363     }
364     
365     private void setRequestDate(Date JavaDoc requestDate){
366         setRequestdate(requestDate.getTime());
367     }
368     
369     private Date JavaDoc getExpireDate(){
370         return new Date JavaDoc(getExpiredate());
371     }
372
373     /**
374      * Method used to set the expire date of the request
375      *
376      */

377     public void setExpireDate(Date JavaDoc expireDate){
378         setExpiredate(expireDate.getTime());
379     }
380      
381     /**
382      * Method that checks if the request or approval have expired
383      * The status is set to expired of it as
384      * @return true of the request or approval have expired
385      */

386     private boolean haveRequestOrApprovalExpired(){
387         Date JavaDoc currentDate = new Date JavaDoc();
388         boolean retval = false;
389         if(currentDate.after(getExpireDate())){
390             if(getStatus() == ApprovalDataVO.STATUS_WAITINGFORAPPROVAL ||
391                getStatus() == ApprovalDataVO.STATUS_APPROVED ||
392                getStatus() == ApprovalDataVO.STATUS_REJECTED){
393               setStatus(ApprovalDataVO.STATUS_EXPIRED);
394             }
395             retval=true;
396         }
397         
398         return retval;
399         
400     }
401     
402     /**
403      * Method that returns the approval data.
404      *
405      * @ejb.interface-method view-type="local"
406      */

407     public ApprovalDataVO getApprovalDataVO() {
408         haveRequestOrApprovalExpired();
409
410         return new ApprovalDataVO(getId().intValue(),getApprovalid(),getApprovaltype(),
411                                   getEndentityprofileid(),getCaid(),getReqadmincertissuerdn(),
412                                   getReqadmincertsn(), getStatus(),getApprovals(), getApprovalRequest(),
413                                   getRequestDate(),getExpireDate(),getRemainingapprovals());
414                                                            
415     }
416     
417     /**
418      * Method adds an approval to the approval data.
419      * If the number of required approvals have been reached will
420      * the request be executed and expiredate set.
421      * @throws ApprovalRequestExpiredException
422      * @throws ApprovalRequestExecutionException
423      * @throws ApprovalException
424      *
425      * @ejb.interface-method view-type="local"
426      */

427     public void approve(Approval approval) throws ApprovalRequestExpiredException, ApprovalRequestExecutionException, ApprovalException {
428         if(haveRequestOrApprovalExpired()){
429             throw new ApprovalRequestExpiredException();
430         }
431         
432         if(getStatus() != ApprovalDataVO.STATUS_WAITINGFORAPPROVAL){
433             throw new ApprovalException("Wrong status of approval request.");
434         }
435         
436         int numberofapprovalsleft = getRemainingapprovals() -1;
437         if(numberofapprovalsleft < 0){
438             throw new ApprovalException("Error already enough approvals have been done on this request.");
439         }
440             
441         setRemainingapprovals(numberofapprovalsleft);
442         Collection JavaDoc approvals = getApprovals();
443         approvals.add(approval);
444         setApprovals(approvals);
445         
446         if(numberofapprovalsleft == 0){
447             ApprovalRequest approvalRequest = getApprovalRequest();
448             if(approvalRequest.isExecutable()){
449                 try{
450                   approvalRequest.execute();
451                   setStatus(ApprovalDataVO.STATUS_EXECUTED);
452                 } catch(ApprovalRequestExecutionException e){
453                   setStatus(ApprovalDataVO.STATUS_EXECUTIONFAILED);
454                   throw e;
455                 }
456                 setStatus(ApprovalDataVO.STATUS_EXECUTED);
457                 setExpireDate(new Date JavaDoc());
458             }else{
459                 setStatus(ApprovalDataVO.STATUS_APPROVED);
460                 setExpiredate((new Date JavaDoc()).getTime() + approvalRequest.getApprovalValidity());
461             }
462         }
463                                                            
464     }
465
466     /**
467      * Method that rejects an apporval.
468      * After someone have rejected the request noone else can approve it
469      *
470      * @throws ApprovalRequestExpiredException
471      * @throws ApprovalRequestExecutionException
472      * @throws ApprovalException
473      *
474      * @ejb.interface-method view-type="local"
475      */

476     public void reject(Approval approval) throws ApprovalRequestExpiredException, ApprovalException {
477         if(haveRequestOrApprovalExpired()){
478             throw new ApprovalRequestExpiredException();
479         }
480         
481         if(getStatus() != ApprovalDataVO.STATUS_WAITINGFORAPPROVAL){
482             throw new ApprovalException("Wrong status of approval request.");
483         }
484         
485         int numberofapprovalsleft = getRemainingapprovals() -1;
486         if(numberofapprovalsleft < 0){
487             throw new ApprovalException("Error already enough approvals have been done on this request.");
488         }
489             
490         setRemainingapprovals(0);
491         Collection JavaDoc approvals = getApprovals();
492         approvals.add(approval);
493         setApprovals(approvals);
494         
495         if(getApprovalRequest().isExecutable()){
496             setStatus(ApprovalDataVO.STATUS_EXECUTIONDENIED);
497             setExpireDate(new Date JavaDoc());
498         }else{
499             setStatus(ApprovalDataVO.STATUS_REJECTED);
500             setExpiredate((new Date JavaDoc()).getTime() + getApprovalRequest().getApprovalValidity());
501         }
502
503                                                                    
504     }
505     
506     /**
507      * Method used by the requestadmin to check if an approval request have been approved
508      *
509      * @return the number of approvals left, 0 if approved othervis is the ApprovalDataVO.STATUS constants returned indicating the statys.
510      * @throws ApprovalRequestExpiredException if the request or approval have expired, the status will be EXPIREDANDNOTIFIED in this case.
511      *
512      * @ejb.interface-method view-type="local"
513      */

514     public int isApproved() throws ApprovalRequestExpiredException {
515         if(haveRequestOrApprovalExpired()){
516             if(getStatus() != ApprovalDataVO.STATUS_EXPIREDANDNOTIFIED &&
517                getStatus() != ApprovalDataVO.STATUS_EXECUTED &&
518                getStatus() != ApprovalDataVO.STATUS_EXECUTIONDENIED &&
519                getStatus() != ApprovalDataVO.STATUS_EXECUTIONFAILED){
520                 setStatus(ApprovalDataVO.STATUS_EXPIREDANDNOTIFIED);
521                 throw new ApprovalRequestExpiredException();
522             }
523             return ApprovalDataVO.STATUS_EXPIREDANDNOTIFIED;
524             
525         }
526         
527         if(getStatus() == ApprovalDataVO.STATUS_WAITINGFORAPPROVAL){
528             return getRemainingapprovals();
529         }
530         
531         return getStatus();
532                                                                    
533     }
534
535
536     //
537
// Fields required by Container
538
//
539
/**
540      * Passivates bean
541      */

542     public void ejbPassivate() {
543     }
544
545
546     /**
547      * Entity Bean holding data of a approval data
548      *
549      * @return id
550      * @ejb.create-method view-type="local"
551      */

552     
553     
554     public Integer JavaDoc ejbCreate(Integer JavaDoc id, ApprovalRequest approvalRequest) throws CreateException JavaDoc {
555         setId(id);
556         setApprovalid(approvalRequest.generateApprovalId());
557         setApprovaltype(approvalRequest.getApprovalType());
558         setEndentityprofileid(approvalRequest.getEndEntityProfileId());
559         setCaid(approvalRequest.getCAId());
560         
561         if(approvalRequest.getRequestAdminCert() != null){
562           setReqadmincertissuerdn(CertTools.getIssuerDN(approvalRequest.getRequestAdminCert()));
563           setReqadmincertsn(approvalRequest.getRequestAdminCert().getSerialNumber().toString(16));
564         }
565         setStatus(ApprovalDataVO.STATUS_WAITINGFORAPPROVAL);
566         setApprovals(new ArrayList JavaDoc());
567         setApprovalRequest(approvalRequest);
568         setRequestDate(new Date JavaDoc());
569         setExpiredate((new Date JavaDoc()).getTime() + approvalRequest.getRequestValidity());
570         setRemainingapprovals(approvalRequest.getNumOfRequiredApprovals());
571
572
573         log.debug("Created approval with id " + id);
574         return id;
575     }
576
577     public void ejbPostCreate(Integer JavaDoc id, ApprovalRequest approvalRequest) {
578         // Do nothing. Required.
579
}
580 }
581
Popular Tags