1 19 package org.openharmonise.rm.commands; 20 21 import java.util.*; 22 23 import org.openharmonise.commons.dsi.AbstractDataStoreInterface; 24 import org.openharmonise.rm.PopulateException; 25 import org.openharmonise.rm.logging.*; 26 import org.openharmonise.rm.publishing.State; 27 import org.openharmonise.rm.resources.AbstractEditableObject; 28 import org.openharmonise.rm.resources.users.User; 29 import org.openharmonise.rm.security.authorization.AuthorizationValidator; 30 import org.openharmonise.rm.workflow.WorkflowLogEvent; 31 32 33 43 public abstract class AbstractCmd { 44 45 private static final String PARAM_INPUT = "input"; 46 private static final String PARAM_RESULT = "result"; 47 50 51 private User m_commandUser = null; 52 53 56 private State m_state = null; 57 60 public static final String TAG_COMMAND = "Event"; 61 62 65 public static final String TAG_COMMAND_PARAM = "Parameter"; 66 67 70 public final static String PARAM_TEMPLATE_ID = "form_id"; 71 72 75 public final static String PARAM_OUT_TEXT = "out_text"; 76 77 80 protected Object m_commandObj = null; 81 82 85 protected Map m_cmd_params; 86 87 90 private AbstractDataStoreInterface m_dsi = null; 91 92 96 public AbstractCmd() { 97 } 98 99 108 public abstract Object execute(Context context) throws CommandException; 109 110 115 public abstract String getName(); 116 117 122 public void setCommandObject(Object obj) { 123 m_commandObj = obj; 124 } 125 126 132 public Object getCommandObject(Context context) { 133 134 Object cmdObj = m_commandObj; 135 136 String sInput = (String ) getParameter(PARAM_INPUT); 137 138 if(sInput != null && context != null) { 139 cmdObj = context.getContextParameter(sInput); 140 } 141 142 return cmdObj; 143 } 144 145 151 public User getExecutingUser() { 152 User usr = null; 153 154 if (m_state != null) { 155 usr = m_state.getLoggedInUser(); 156 } 157 158 return usr; 159 } 160 161 167 public void setState(State state) { 168 m_state = state; 169 } 170 171 176 public State getState() { 177 return m_state; 178 } 179 180 185 public void setDataStoreInteface(AbstractDataStoreInterface dsi) { 186 m_dsi = dsi; 187 } 188 189 194 public AbstractDataStoreInterface getDataStoreInteface() { 195 return m_dsi; 196 } 197 198 203 public void setParameters(Map cmd_params) { 204 m_cmd_params = cmd_params; 205 } 206 207 214 public List getParameters(String parameter_name) { 215 List param = null; 216 217 if(m_cmd_params != null) { 218 param = (List) m_cmd_params.get(parameter_name); 219 } 220 221 return param; 222 } 223 224 231 public String getParameter(String parameter_name) { 232 String sParam = null; 233 234 if(m_cmd_params != null) { 235 List parameter_values = (List) m_cmd_params.get(parameter_name); 236 237 if ((parameter_values != null) && (parameter_values.size() > 1)) { 238 throw new RuntimeException ( 239 "Call getParameters if parameter has multiple values" 240 + " parameter_name:" 241 + parameter_name 242 + ", returns :" 243 + parameter_values); 244 } 245 246 sParam = (parameter_values == null) ? null : (String ) parameter_values.get(0); 247 } 248 249 return sParam; 250 } 251 252 258 public abstract boolean isValidCommandObject(Object obj); 259 260 266 protected void logCommand(Context context) throws CommandException { 267 try { 268 EventLogController logger = EventLogController.getInstance(); 269 270 WorkflowLogEvent event = new WorkflowLogEvent(); 271 272 event.setEventObject(getCommandObject(context)); 273 event.setState(getState()); 274 event.setLabel(getName()); 275 276 logger.logEvent(event); 277 } catch (LogException e) { 278 throw new CommandException("Error occured logging event", e); 279 } catch (PopulateException e) { 280 throw new CommandException( 281 "Error occured setting state for logging event", 282 e); 283 } 284 } 285 286 295 public boolean isAvailable(Context context) throws InvalidCommandException { 296 User usr = getExecutingUser(); 297 298 return isAvailable(usr,(AbstractEditableObject) getCommandObject(context)); 299 } 300 301 310 public boolean isAvailable(User usr,AbstractEditableObject obj) throws InvalidCommandException { 311 boolean bIsAvailable = false; 312 313 if(usr == null) { 314 throw new InvalidCommandException("Command is not valid with out specifying a user"); 315 } 316 317 try { 318 bIsAvailable = AuthorizationValidator.isCommandAvailable(usr, (AbstractEditableObject) obj, getName()); 319 } catch (org.openharmonise.rm.security.authorization.AuthorizationException e1) { 320 throw new InvalidCommandException("Command is not available for this object:" + m_commandObj.getClass(), e1); 321 } 322 323 return bIsAvailable; 324 } 325 326 336 public boolean isAvailable(User usr,Class objClass) throws InvalidCommandException { 337 boolean bIsAvailable = false; 338 339 if(usr == null) { 340 throw new InvalidCommandException("Command is not valid with out specifying a user"); 341 } 342 343 try { 344 bIsAvailable = AuthorizationValidator.isCommandAvailable(usr, objClass, getName()); 345 } catch (org.openharmonise.rm.security.authorization.AuthorizationException e1) { 346 throw new InvalidCommandException("Command is not available for this object:" + m_commandObj.getClass(), e1); 347 } 348 349 return bIsAvailable; 350 } 351 352 359 protected void addResultContext(Object result,Context context) { 360 String sResult = getParameter(PARAM_RESULT); 361 362 if(sResult != null) { 363 context.addContextParameter(sResult, result); 364 } 365 } 366 } | Popular Tags |