1 13 package org.ejbca.core.model.approval.approvalrequests; 14 15 import java.io.IOException ; 16 import java.io.ObjectInput ; 17 import java.io.ObjectOutput ; 18 import java.util.ArrayList ; 19 import java.util.List ; 20 21 import javax.ejb.CreateException ; 22 import javax.ejb.EJBException ; 23 import javax.ejb.FinderException ; 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 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 username; 59 private int oldstatus; 60 private int newstatus; 61 62 65 public ChangeStatusEndEntityApprovalRequest() {} 66 67 68 public ChangeStatusEndEntityApprovalRequest( String username, int oldstatus, int newstatus, Admin requestAdmin, String 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 80 public boolean isAllowedTransition() { 81 boolean ret = false; 82 83 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 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 e) { 180 throw new ApprovalRequestExecutionException("User with username + " + username + " doesn't exist.", e); 181 } catch (ApprovalException e) { 182 throw new EJBException ("This should never happen",e); 183 } catch (WaitingForApprovalException e) { 184 throw new EJBException ("This should never happen",e); 185 } 186 187 } 188 189 192 public int generateApprovalId() { 193 return new String (getApprovalType() + ";" + username).hashCode(); 194 } 195 196 197 public int getApprovalType() { 198 return ApprovalDataVO.APPROVALTYPE_CHANGESTATUSENDENTITY; 199 } 200 201 202 public List getNewRequestDataAsText(Admin admin) { 203 ArrayList retval = new ArrayList (); 204 retval.add(new ApprovalDataText("USERNAME",username,true,false)); 205 retval.add(new ApprovalDataText("STATUS",(String ) UserDataConstants.STATUS_TEXT.get(new Integer (newstatus)),true,true)); 206 return retval; 207 } 208 209 public List getOldRequestDataAsText(Admin admin) { 210 ArrayList retval = new ArrayList (); 211 retval.add(new ApprovalDataText("USERNAME",username,true,false)); 212 retval.add(new ApprovalDataText("STATUS",(String ) UserDataConstants.STATUS_TEXT.get(new Integer (oldstatus)),true,true)); 213 return retval; 214 } 215 216 217 public boolean isExecutable() { 218 return true; 219 } 220 221 public void writeExternal(ObjectOutput out) throws IOException { 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 in) throws IOException , ClassNotFoundException { 231 super.readExternal(in); 232 int version = in.readInt(); 233 if(version == 1){ 234 username = (String ) in.readObject(); 235 newstatus = in.readInt(); 236 oldstatus = in.readInt(); 237 } 238 239 } 240 241 } 242 | Popular Tags |