📄 approvaldatabean.java
字号:
* Date the request for action or the approvel action will expire, Long.MAX_VALUE * means that the request/approval never expires * */ public abstract void setExpiredate(long expiredate); /** * Indicates the number of approvals that remains in order to execute the action * * @ejb.persistence column-name="remainingapprovals" */ public abstract int getRemainingapprovals(); /** * Indicates the number of approvals that remains in order to execute the action * */ public abstract void setRemainingapprovals(int remainingapprovals); private Collection getApprovals() { return ApprovalDataUtil.getApprovals(getApprovaldata()); } /** * Collection of Approval * @param approvals cannot be null. * @throws IOException */ private void setApprovals(Collection approvals){ try{ ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); int size = approvals.size(); oos.writeInt(size); Iterator iter = approvals.iterator(); while(iter.hasNext()){ Approval next = (Approval) iter.next(); oos.writeObject(next); } oos.flush(); setApprovaldata(new String(Base64.encode(baos.toByteArray(),false))); } catch (IOException e) { log.error("Error building approvals.",e); throw new EJBException(e); } } private ApprovalRequest getApprovalRequest() { return ApprovalDataUtil.getApprovalRequest(getRequestdata()); } private void setApprovalRequest(ApprovalRequest approvalRequest){ try{ ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(approvalRequest); oos.flush(); setRequestdata(new String(Base64.encode(baos.toByteArray(),false))); }catch(IOException e){ log.error("Error building approval request.",e); throw new EJBException(e); } } private Date getRequestDate(){ return new Date(getRequestdate()); } private void setRequestDate(Date requestDate){ setRequestdate(requestDate.getTime()); } private Date getExpireDate(){ return new Date(getExpiredate()); } /** * Method used to set the expire date of the request * */ public void setExpireDate(Date expireDate){ setExpiredate(expireDate.getTime()); } /** * Method that checks if the request or approval have expired * The status is set to expired of it as * @return true of the request or approval have expired */ private boolean haveRequestOrApprovalExpired(){ Date currentDate = new Date(); boolean retval = false; if(currentDate.after(getExpireDate())){ if(getStatus() == ApprovalDataVO.STATUS_WAITINGFORAPPROVAL || getStatus() == ApprovalDataVO.STATUS_APPROVED || getStatus() == ApprovalDataVO.STATUS_REJECTED){ setStatus(ApprovalDataVO.STATUS_EXPIRED); } retval=true; } return retval; } /** * Method that returns the approval data. * * @ejb.interface-method view-type="local" */ public ApprovalDataVO getApprovalDataVO() { haveRequestOrApprovalExpired(); return new ApprovalDataVO(getId().intValue(),getApprovalid(),getApprovaltype(), getEndentityprofileid(),getCaid(),getReqadmincertissuerdn(), getReqadmincertsn(), getStatus(),getApprovals(), getApprovalRequest(), getRequestDate(),getExpireDate(),getRemainingapprovals()); } /** * Method adds an approval to the approval data. * If the number of required approvals have been reached will * the request be executed and expiredate set. * @throws ApprovalRequestExpiredException * @throws ApprovalRequestExecutionException * @throws ApprovalException * * @ejb.interface-method view-type="local" */ public void approve(Approval approval) throws ApprovalRequestExpiredException, ApprovalRequestExecutionException, ApprovalException { if(haveRequestOrApprovalExpired()){ throw new ApprovalRequestExpiredException(); } if(getStatus() != ApprovalDataVO.STATUS_WAITINGFORAPPROVAL){ throw new ApprovalException("Wrong status of approval request."); } int numberofapprovalsleft = getRemainingapprovals() -1; if(numberofapprovalsleft < 0){ throw new ApprovalException("Error already enough approvals have been done on this request."); } setRemainingapprovals(numberofapprovalsleft); Collection approvals = getApprovals(); approvals.add(approval); setApprovals(approvals); if(numberofapprovalsleft == 0){ ApprovalRequest approvalRequest = getApprovalRequest(); if(approvalRequest.isExecutable()){ try{ approvalRequest.execute(); setStatus(ApprovalDataVO.STATUS_EXECUTED); } catch(ApprovalRequestExecutionException e){ setStatus(ApprovalDataVO.STATUS_EXECUTIONFAILED); throw e; } setStatus(ApprovalDataVO.STATUS_EXECUTED); setExpireDate(new Date()); }else{ setStatus(ApprovalDataVO.STATUS_APPROVED); setExpiredate((new Date()).getTime() + approvalRequest.getApprovalValidity()); } } } /** * Method that rejects an apporval. * After someone have rejected the request noone else can approve it * * @throws ApprovalRequestExpiredException * @throws ApprovalRequestExecutionException * @throws ApprovalException * * @ejb.interface-method view-type="local" */ public void reject(Approval approval) throws ApprovalRequestExpiredException, ApprovalException { if(haveRequestOrApprovalExpired()){ throw new ApprovalRequestExpiredException(); } if(getStatus() != ApprovalDataVO.STATUS_WAITINGFORAPPROVAL){ throw new ApprovalException("Wrong status of approval request."); } int numberofapprovalsleft = getRemainingapprovals() -1; if(numberofapprovalsleft < 0){ throw new ApprovalException("Error already enough approvals have been done on this request."); } setRemainingapprovals(0); Collection approvals = getApprovals(); approvals.add(approval); setApprovals(approvals); if(getApprovalRequest().isExecutable()){ setStatus(ApprovalDataVO.STATUS_EXECUTIONDENIED); setExpireDate(new Date()); }else{ setStatus(ApprovalDataVO.STATUS_REJECTED); setExpiredate((new Date()).getTime() + getApprovalRequest().getApprovalValidity()); } } /** * Method used by the requestadmin to check if an approval request have been approved * * @return the number of approvals left, 0 if approved othervis is the ApprovalDataVO.STATUS constants returned indicating the statys. * @throws ApprovalRequestExpiredException if the request or approval have expired, the status will be EXPIREDANDNOTIFIED in this case. * * @ejb.interface-method view-type="local" */ public int isApproved() throws ApprovalRequestExpiredException { if(haveRequestOrApprovalExpired()){ if(getStatus() != ApprovalDataVO.STATUS_EXPIREDANDNOTIFIED && getStatus() != ApprovalDataVO.STATUS_EXECUTED && getStatus() != ApprovalDataVO.STATUS_EXECUTIONDENIED && getStatus() != ApprovalDataVO.STATUS_EXECUTIONFAILED){ setStatus(ApprovalDataVO.STATUS_EXPIREDANDNOTIFIED); throw new ApprovalRequestExpiredException(); } return ApprovalDataVO.STATUS_EXPIREDANDNOTIFIED; } if(getStatus() == ApprovalDataVO.STATUS_WAITINGFORAPPROVAL){ return getRemainingapprovals(); } return getStatus(); } // // Fields required by Container // /** * Passivates bean */ public void ejbPassivate() { } /** * Entity Bean holding data of a approval data * * @return id * @ejb.create-method view-type="local" */ public Integer ejbCreate(Integer id, ApprovalRequest approvalRequest) throws CreateException { setId(id); setApprovalid(approvalRequest.generateApprovalId()); setApprovaltype(approvalRequest.getApprovalType()); setEndentityprofileid(approvalRequest.getEndEntityProfileId()); setCaid(approvalRequest.getCAId()); if(approvalRequest.getRequestAdminCert() != null){ setReqadmincertissuerdn(CertTools.getIssuerDN(approvalRequest.getRequestAdminCert())); setReqadmincertsn(approvalRequest.getRequestAdminCert().getSerialNumber().toString(16)); } setStatus(ApprovalDataVO.STATUS_WAITINGFORAPPROVAL); setApprovals(new ArrayList()); setApprovalRequest(approvalRequest); setRequestDate(new Date()); setExpiredate((new Date()).getTime() + approvalRequest.getRequestValidity()); setRemainingapprovals(approvalRequest.getNumOfRequiredApprovals()); log.debug("Created approval with id " + id); return id; } public void ejbPostCreate(Integer id, ApprovalRequest approvalRequest) { // Do nothing. Required. }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -