KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > model > approval > approvalrequests > ChangeStatusEndEntityApprovalRequest


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 package org.ejbca.core.model.approval.approvalrequests;
14
15 import java.io.IOException JavaDoc;
16 import java.io.ObjectInput JavaDoc;
17 import java.io.ObjectOutput JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 import javax.ejb.CreateException JavaDoc;
22 import javax.ejb.EJBException JavaDoc;
23 import javax.ejb.FinderException JavaDoc;
24
25 import org.apache.log4j.Logger;
26 import org.ejbca.core.ejb.ServiceLocator;
27 import org.ejbca.core.ejb.ra.IUserAdminSessionLocal;
28 import org.ejbca.core.ejb.ra.IUserAdminSessionLocalHome;
29 import org.ejbca.core.model.approval.ApprovalDataText;
30 import org.ejbca.core.model.approval.ApprovalDataVO;
31 import org.ejbca.core.model.approval.ApprovalException;
32 import org.ejbca.core.model.approval.ApprovalRequest;
33 import org.ejbca.core.model.approval.ApprovalRequestExecutionException;
34 import org.ejbca.core.model.approval.WaitingForApprovalException;
35 import org.ejbca.core.model.authorization.AuthorizationDeniedException;
36 import org.ejbca.core.model.log.Admin;
37 import org.ejbca.core.model.ra.UserDataConstants;
38
39 /**
40  * Approval Request created when trying to edit an end entity.
41  *
42  *
43  *
44  * @author Philip Vendil
45  * @version $Id: ChangeStatusEndEntityApprovalRequest.java,v 1.6 2006/08/13 14:56:36 herrvendil Exp $
46  */

47 public class ChangeStatusEndEntityApprovalRequest extends ApprovalRequest {
48
49     private static final long serialVersionUID = -1L;
50
51     private static final Logger log = Logger.getLogger(ChangeStatusEndEntityApprovalRequest.class);
52     
53     private static final int LATEST_VERSION = 1;
54     
55
56     
57     
58     private String JavaDoc username;
59     private int oldstatus;
60     private int newstatus;
61     
62     /**
63      * Constuctor used in externaliziation only
64      */

65     public ChangeStatusEndEntityApprovalRequest() {}
66
67
68     public ChangeStatusEndEntityApprovalRequest( String JavaDoc username, int oldstatus, int newstatus, Admin requestAdmin, String JavaDoc requestSignature, int numOfReqApprovals, int cAId, int endEntityProfileId) {
69         super(requestAdmin, requestSignature, REQUESTTYPE_COMPARING,
70                 numOfReqApprovals, cAId, endEntityProfileId);
71         this.username = username;
72         this.oldstatus = oldstatus;
73         this.newstatus = newstatus;
74     }
75
76
77     /** Overrides ApprovalRequest.isAllowedTransition()
78      * @see ApprovalRequest#isAllowedTransition()
79      */

80     public boolean isAllowedTransition() {
81         boolean ret = false;
82         
83         // Allow Keyrecovery transitions until keyrecovery approvals have been implemented.
84
if(newstatus == UserDataConstants.STATUS_KEYRECOVERY){
85             return true;
86         }
87         
88         
89         if (oldstatus == UserDataConstants.STATUS_GENERATED) {
90             switch (newstatus) {
91             case UserDataConstants.STATUS_GENERATED:
92                 ret = true;
93                 break;
94             default:
95                 break;
96             }
97         }
98         if (oldstatus == UserDataConstants.STATUS_NEW) {
99             switch (newstatus) {
100             case UserDataConstants.STATUS_NEW:
101                 ret = true;
102                 break;
103             case UserDataConstants.STATUS_INPROCESS:
104                 ret = true;
105                 break;
106             case UserDataConstants.STATUS_GENERATED:
107                 ret = true;
108                 break;
109             case UserDataConstants.STATUS_FAILED:
110                 ret = true;
111                 break;
112             default:
113                 break;
114             }
115         }
116         if (oldstatus == UserDataConstants.STATUS_FAILED) {
117             switch (newstatus) {
118             case UserDataConstants.STATUS_FAILED:
119                 ret = true;
120                 break;
121             case UserDataConstants.STATUS_INPROCESS:
122                 ret = true;
123                 break;
124             case UserDataConstants.STATUS_GENERATED:
125                 ret = true;
126                 break;
127             default:
128                 break;
129             }
130         }
131         if (oldstatus == UserDataConstants.STATUS_INPROCESS) {
132             switch (newstatus) {
133             case UserDataConstants.STATUS_INPROCESS:
134                 ret = true;
135                 break;
136             case UserDataConstants.STATUS_GENERATED:
137                 ret = true;
138                 break;
139             case UserDataConstants.STATUS_FAILED:
140                 ret = true;
141                 break;
142             default:
143                 break;
144             }
145         }
146         if (oldstatus == UserDataConstants.STATUS_KEYRECOVERY) {
147             switch (newstatus) {
148             case UserDataConstants.STATUS_KEYRECOVERY:
149                 ret = true;
150                 break;
151             case UserDataConstants.STATUS_INPROCESS:
152                 ret = true;
153                 break;
154             case UserDataConstants.STATUS_GENERATED:
155                 ret = true;
156                 break;
157             case UserDataConstants.STATUS_FAILED:
158                 ret = true;
159                 break;
160             default:
161                 break;
162             }
163         }
164         return ret;
165     }
166
167     public void execute() throws ApprovalRequestExecutionException {
168         log.debug("Executing Change Status for user:" + username);
169         try{
170             ServiceLocator locator = ServiceLocator.getInstance();
171             IUserAdminSessionLocalHome userdatahome = (IUserAdminSessionLocalHome) locator.getLocalHome(IUserAdminSessionLocalHome.COMP_NAME);
172             IUserAdminSessionLocal usersession = userdatahome.create();
173         
174             usersession.setUserStatus(getRequestAdmin(), username, newstatus);
175         }catch (CreateException JavaDoc e) {
176             throw new ApprovalRequestExecutionException("Error creating new userdata session", e);
177         } catch (AuthorizationDeniedException e) {
178             throw new ApprovalRequestExecutionException("Authorization Denied :" + e.getMessage(), e);
179         } catch (FinderException JavaDoc e) {
180             throw new ApprovalRequestExecutionException("User with username + " + username + " doesn't exist.", e);
181         } catch (ApprovalException e) {
182             throw new EJBException JavaDoc("This should never happen",e);
183         } catch (WaitingForApprovalException e) {
184             throw new EJBException JavaDoc("This should never happen",e);
185         }
186
187     }
188
189     /**
190      * Approval Id is genereated of This approval type (i.e AddEndEntityApprovalRequest) and UserName
191      */

192     public int generateApprovalId() {
193         return new String JavaDoc(getApprovalType() + ";" + username).hashCode();
194     }
195
196
197     public int getApprovalType() {
198         return ApprovalDataVO.APPROVALTYPE_CHANGESTATUSENDENTITY;
199     }
200
201
202     public List JavaDoc getNewRequestDataAsText(Admin admin) {
203         ArrayList JavaDoc retval = new ArrayList JavaDoc();
204         retval.add(new ApprovalDataText("USERNAME",username,true,false));
205         retval.add(new ApprovalDataText("STATUS",(String JavaDoc) UserDataConstants.STATUS_TEXT.get(new Integer JavaDoc(newstatus)),true,true));
206         return retval;
207     }
208     
209     public List JavaDoc getOldRequestDataAsText(Admin admin) {
210         ArrayList JavaDoc retval = new ArrayList JavaDoc();
211         retval.add(new ApprovalDataText("USERNAME",username,true,false));
212         retval.add(new ApprovalDataText("STATUS",(String JavaDoc) UserDataConstants.STATUS_TEXT.get(new Integer JavaDoc(oldstatus)),true,true));
213         return retval;
214     }
215
216
217     public boolean isExecutable() {
218         return true;
219     }
220     
221     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
222         super.writeExternal(out);
223         out.writeInt(LATEST_VERSION);
224         out.writeObject(username);
225         out.writeInt(newstatus);
226         out.writeInt(oldstatus);
227         
228     }
229
230     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
231         super.readExternal(in);
232         int version = in.readInt();
233         if(version == 1){
234             username = (String JavaDoc) in.readObject();
235             newstatus = in.readInt();
236             oldstatus = in.readInt();
237         }
238
239     }
240
241 }
242
Popular Tags