1 19 20 package za.org.coefficient.invokers.base; 21 22 import org.apache.commons.beanutils.MethodUtils; 23 import org.apache.commons.beanutils.PropertyUtils; 24 import org.apache.commons.lang.StringUtils; 25 26 import za.co.csir.icomtek.workflow.interfaces.WorkflowContext; 27 import za.co.csir.icomtek.workflow.interfaces.WorkflowModuleInvoker; 28 import za.org.coefficient.interfaces.Invoker; 29 import za.org.coefficient.core.Constants; 30 31 import za.org.coefficient.authentication.Role; 32 import za.org.coefficient.exception.ConfigurationException; 33 import za.org.coefficient.interfaces.CoefficientContext; 34 import za.org.coefficient.interfaces.ModuleLocal; 35 import za.org.coefficient.interfaces.ThemeLocalIf; 36 import za.org.coefficient.util.ejb.SecurityUtil; 37 38 39 49 public abstract class BaseInvoker implements Invoker, WorkflowModuleInvoker { 50 52 private static final String CREATE = "create"; 53 private static final String LOCAL = "Local"; 54 55 57 61 public Object invoke(CoefficientContext ctx) throws Exception { 62 String module = ctx.getParameter("module"); 64 String operation = ctx.getParameter("op"); 65 if (module == null || module.trim().equals("")) { 66 if(ctx.getCurrentUser() == null) { 67 module = Constants.WELCOME_MODULE; 68 } else { 69 module = "MyData"; 71 } 72 } 73 74 return invokeOpOnModule(module, operation, ctx); 75 } 76 77 public Object invokeGetterOnModule(String module, String method) 78 throws Exception { 79 if ((method == null) || (module == null)) { 80 throw new ConfigurationException( 81 "module and method must not be null"); 82 } 83 Object retVal = null; 84 85 ModuleLocal mod = getModule(module); 87 try { 88 if (mod != null) { 89 retVal = PropertyUtils.getProperty(mod, method); 91 } 92 } catch (NoSuchMethodException nsme) { 93 nsme.printStackTrace(); 94 } catch (IllegalAccessException iae) { 95 iae.printStackTrace(); 96 } 97 98 return retVal; 99 } 100 101 public Object invokeMethodOnModule(String module, String method, 102 Object [] paramVals) throws Exception { 103 if ((method == null) || (module == null)) { 104 throw new ConfigurationException( 105 "module and method must not be null"); 106 } 107 Object retVal = null; 108 109 ModuleLocal mod = getModule(module); 111 try { 112 if (mod != null) { 113 retVal = MethodUtils.invokeMethod(mod, method, paramVals); 115 } 116 } catch (NoSuchMethodException nsme) { 117 nsme.printStackTrace(); 118 } catch (IllegalAccessException iae) { 119 iae.printStackTrace(); 120 } 121 122 return retVal; 123 } 124 125 public Object invokeMethodOnService(String serviceName, String method, 126 Object [] paramVals) throws Exception { 127 if ((method == null) || (serviceName == null)) { 128 throw new ConfigurationException( 129 "serviceName and method must not be null"); 130 } 131 Object retVal = null; 132 133 Object service = getService(serviceName); 135 try { 136 if (service != null) { 137 retVal = MethodUtils.invokeMethod(service, method, paramVals); 139 } 140 } catch (NoSuchMethodException nsme) { 141 nsme.printStackTrace(); 142 } catch (IllegalAccessException iae) { 143 iae.printStackTrace(); 144 } 145 146 return retVal; 147 } 148 149 153 public Object invokeOpOnModule(String module, String operation, 154 CoefficientContext ctx) throws Exception { 155 Object retVal = null; 156 157 ModuleLocal mod = getModule(module); 159 try { 160 if (mod != null) { 161 162 if(mod.isProjectRequired() && ctx.getProject() == null) { 163 ctx.setError("You must select a project to perform this operation"); 164 } else { 165 PropertyUtils.setProperty(mod, "coefficientContext", ctx); 167 if (operation == null) { 168 operation = 170 (String ) PropertyUtils.getProperty(mod, "mainMethod"); 171 } 172 173 Role role = 175 SecurityUtil.getHighestRoleForUser(ctx.getCurrentUser(), 176 ctx.getProject()); 177 String err = mod.canExecuteForRole(ctx, operation, role); 178 if (err == null) { 179 retVal = MethodUtils.invokeMethod(mod, operation, ctx); 181 } else { 182 ctx.setError(err); 183 } 184 } 185 } else { 186 ctx.setError("<b>No such module exists!</b>"); 187 } 188 } catch (NoSuchMethodException nsme) { 189 nsme.printStackTrace(); 190 ctx.setError("<b>No such operation exists on module " + module 191 + "!</b>"); 192 } catch (IllegalAccessException iae) { 193 iae.printStackTrace(); 194 ctx.setError("<b>No such operation exists on module " + module 195 + "!</b>"); 196 } 197 198 if(ctx.isError()) { 199 retVal = ctx; 200 } 201 return retVal; 202 } 203 204 210 public Object invokeOpOnModule(String module, String operation, 211 WorkflowContext ctx) throws Exception { 212 if (ctx instanceof CoefficientContext) { 213 CoefficientContext pctx = (CoefficientContext) ctx; 214 215 return invokeOpOnModule(module, operation, pctx); 216 } else { 217 return null; 218 } 219 } 220 221 public Object invokeMethodOnTheme(String theme, String method, 222 Object [] paramVals) throws Exception { 223 if ((theme == null) || (method == null)) { 224 throw new ConfigurationException("theme and method must not be null"); 225 } 226 Object retVal = null; 227 228 ThemeLocalIf themeIf = getTheme(theme); 230 try { 231 if (themeIf != null) { 232 retVal = MethodUtils.invokeMethod(themeIf, method, paramVals); 234 } 235 } catch (NoSuchMethodException nsme) { 236 nsme.printStackTrace(); 237 } catch (IllegalAccessException iae) { 238 iae.printStackTrace(); 239 } 240 return retVal; 241 } 242 243 public abstract Object getService(String serviceName) throws Exception ; 244 245 protected abstract ThemeLocalIf getTheme(String theme) throws Exception ; 246 247 protected abstract ModuleLocal getModule(String module) throws Exception ; 248 249 } 250 | Popular Tags |