| 1 19 20 package za.org.coefficient.modules.workflow; 21 22 import net.sf.hibernate.HibernateException; 23 24 import za.co.csir.icomtek.workflow.WorkflowEvaluator; 25 import za.co.csir.icomtek.workflow.interfaces.WorkflowFinder; 26 import za.co.csir.icomtek.workflow.model.Workflow; 27 28 import za.org.coefficient.authentication.Role; 29 import za.org.coefficient.interfaces.CoefficientContext; 30 import za.org.coefficient.modules.BaseModule; 31 import net.sf.hibernate.util.HibernateUtil; 32 import za.org.coefficient.util.ejb.SecurityUtil; 33 import za.org.coefficient.util.ejb.VelocityScreenUtil; 34 35 import java.io.ByteArrayInputStream ; 36 import java.io.InputStreamReader ; 37 38 import java.util.ArrayList ; 39 import java.util.HashMap ; 40 import java.util.Iterator ; 41 import java.util.List ; 42 43 57 public class WorkflowAdmin extends BaseModule implements WorkflowFinder { 58 60 private static final String SELECT_ALL = 61 "from za.org.coefficient.modules.workflow.WorkflowXMLData as workflowXMLData"; 62 63 static { 64 try { 65 HibernateUtil.configureHibernateForClass(WorkflowXMLData.class); 66 } catch (Exception e) { 67 e.printStackTrace(); 68 } 69 } 70 71 73 public List getAllWorkflows() throws Exception { 74 ArrayList workflows = new ArrayList (); 75 76 List workflowDatas = HibernateUtil.findAll(WorkflowXMLData.class); 77 for (Iterator it = workflowDatas.iterator(); it.hasNext();) { 78 WorkflowXMLData xmlData = (WorkflowXMLData) it.next(); 79 workflows.add(getProjectFromBytes(xmlData.getData())); 80 } 81 82 return workflows; 83 } 84 85 public String getMainMethod() { 86 return "viewWorkflows"; 87 } 88 89 public String getModuleDescription() { 90 return "This module is used to upload and delete workflows in the system"; 91 } 92 93 public String getModuleDisplayName() { 94 return "Workflow Administration"; 95 } 96 97 public String canExecuteForRole(CoefficientContext ctx, String methodName, 98 Role usersHighestRole) { 99 if ((usersHighestRole.getRoleValue() != SecurityUtil.SITE_ADMIN_ROLE_VAL) 100 && (methodName.equals("viewWorkflows") 101 || methodName.equals("deleteWorkflows") 102 || methodName.equals("doUpload"))) { 103 return "Only the site administrator can upload workflows"; 104 } else { 105 return null; 106 } 107 } 108 109 public CoefficientContext deleteWorkflow(CoefficientContext ctx) { 110 long workflowId = ctx.getParameterAsLongPrimitive("workflowId", -1); 111 String workflowName = ctx.getParameter("workflowName"); 112 if (workflowId > 0) { 113 try { 114 WorkflowXMLData data = 115 (WorkflowXMLData)HibernateUtil.load(WorkflowXMLData.class, 116 new Long (workflowId)); 117 HibernateUtil.delete(data); 118 WorkflowEvaluator.deregisterWorkflow(workflowName); 119 } catch (HibernateException he) { 120 he.printStackTrace(); 121 ctx.setError("unable to delete the workflow"); 122 } 123 } 124 if (!ctx.isError()) { 125 ctx.setForward("workflowAdmin"); 126 } 127 return ctx; 128 } 129 130 public CoefficientContext doUpload(CoefficientContext ctx) { 131 Workflow pw = null; 132 byte[] bytes = null; 133 try { 134 bytes = ctx.getUploadedFile("workflow") 135 .getContent(); 136 pw = getProjectFromBytes(bytes); 137 } catch (Exception e) { 138 e.printStackTrace(); 139 ctx.setError( 140 "uploaded file does not conform to the workflow definition"); 141 } 142 if ((pw != null) && (bytes != null)) { 143 WorkflowXMLData xmlData = new WorkflowXMLData(pw.getName(), bytes); 144 try { 145 HibernateUtil.save(xmlData); 146 WorkflowEvaluator.registerWorkflow(pw); 147 } catch (HibernateException he) { 148 he.printStackTrace(); 149 ctx.setError("Could not save the specified file"); 150 } 151 } 152 if (!ctx.isError()) { 153 ctx.setForward("workflowAdmin"); 154 } 155 return ctx; 156 } 157 158 public void genWorkflow(StringBuffer sb) { 159 Workflow pw = null; 160 byte[] bytes = null; 161 try { 162 bytes = sb.toString() 163 .getBytes(); 164 pw = getProjectFromBytes(bytes); 165 } catch (Exception e) { 166 System.out.println( 167 "uploaded file does not conform to the workflow definition"); 168 } 169 if ((pw != null) && (bytes != null)) { 170 WorkflowXMLData xmlData = new WorkflowXMLData(pw.getName(), bytes); 171 try { 172 HibernateUtil.save(xmlData); 173 WorkflowEvaluator.registerWorkflow(pw); 174 } catch (HibernateException he) { 175 he.printStackTrace(); 176 System.out.println("Could not save the specified file"); 177 } 178 } 179 } 180 181 public CoefficientContext viewWorkflows(CoefficientContext ctx) { 182 List workflows = null; 183 184 try { 185 workflows = HibernateUtil.findAll(WorkflowXMLData.class); 186 } catch (HibernateException he) { 187 ctx.setError("Can not load the existing workflows"); 188 return ctx; 189 } 190 191 HashMap map = new HashMap (); 193 map.put("module", this); 194 map.put("workflows", workflows); 195 StringBuffer sb = VelocityScreenUtil.getProcessedScreen("view.vm", map); 196 197 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 199 return ctx; 200 } 201 202 private Workflow getProjectFromBytes(byte[] bytes) throws Exception { 203 ByteArrayInputStream is = new ByteArrayInputStream (bytes); 204 InputStreamReader isr = new InputStreamReader (is); 205 206 return Workflow.unmarshal(isr); 207 } 208 } 209 | Popular Tags |