| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
package org.itracker.services.util; |
| 20 |
|
|
| 21 |
|
import java.lang.reflect.Field; |
| 22 |
|
import java.util.ArrayList; |
| 23 |
|
import java.util.Collection; |
| 24 |
|
import java.util.HashMap; |
| 25 |
|
import java.util.LinkedList; |
| 26 |
|
import java.util.List; |
| 27 |
|
import java.util.Locale; |
| 28 |
|
import java.util.Map; |
| 29 |
|
|
| 30 |
|
import org.apache.log4j.Logger; |
| 31 |
|
import org.apache.struts.action.ActionErrors; |
| 32 |
|
import org.apache.struts.action.ActionMessage; |
| 33 |
|
import org.apache.struts.action.ActionMessages; |
| 34 |
|
import org.apache.struts.validator.ValidatorForm; |
| 35 |
|
import org.itracker.core.resources.ITrackerResources; |
| 36 |
|
import org.itracker.model.NameValuePair; |
| 37 |
|
import org.itracker.model.ProjectScript; |
| 38 |
|
import org.itracker.services.exceptions.WorkflowException; |
| 39 |
|
|
| 40 |
|
import bsh.Interpreter; |
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
|
|
|
| 0% |
Uncovered Elements: 189 (189) |
Complexity: 55 |
Complexity Density: 0.47 |
|
| 45 |
|
public class WorkflowUtilities { |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
public static final int EVENT_FIELD_ONPOPULATE = 1; |
| 49 |
|
|
| 50 |
|
public static final int EVENT_FIELD_ONSORT = 2; |
| 51 |
|
|
| 52 |
|
public static final int EVENT_FIELD_ONSETDEFAULT = 3; |
| 53 |
|
|
| 54 |
|
public static final int EVENT_FIELD_ONVALIDATE = 4; |
| 55 |
|
|
| 56 |
|
public static final int EVENT_FIELD_ONPRESUBMIT = 5; |
| 57 |
|
|
| 58 |
|
public static final int EVENT_FIELD_ONPOSTSUBMIT = 6; |
| 59 |
|
|
| 60 |
|
private static final Logger logger = Logger.getLogger(WorkflowUtilities.class); |
| 61 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 62 |
0
|
public WorkflowUtilities() {... |
| 63 |
|
} |
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
@param |
| 68 |
|
@param |
| 69 |
|
@return |
| 70 |
|
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 71 |
0
|
public static String getEventName(int value, Locale locale) {... |
| 72 |
0
|
final String eventName = getEventName(Integer.toString(value), locale); |
| 73 |
|
|
| 74 |
0
|
return eventName; |
| 75 |
|
} |
| 76 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 77 |
0
|
public static String getEventName(String value, Locale locale) {... |
| 78 |
0
|
return ITrackerResources.getString(ITrackerResources.KEY_BASE_WORKFLOW_EVENT + value, locale); |
| 79 |
|
} |
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
@param |
| 85 |
|
@return |
| 86 |
|
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
| 87 |
0
|
public static NameValuePair[] getEvents(Locale locale) {... |
| 88 |
0
|
NameValuePair[] eventNames = new NameValuePair[6]; |
| 89 |
0
|
eventNames[0] = new NameValuePair(getEventName(EVENT_FIELD_ONPOPULATE, locale), Integer.toString(EVENT_FIELD_ONPOPULATE)); |
| 90 |
0
|
eventNames[1] = new NameValuePair(getEventName(EVENT_FIELD_ONSORT, locale), Integer.toString(EVENT_FIELD_ONSORT)); |
| 91 |
0
|
eventNames[2] = new NameValuePair(getEventName(EVENT_FIELD_ONSETDEFAULT, locale), Integer.toString(EVENT_FIELD_ONSETDEFAULT)); |
| 92 |
0
|
eventNames[3] = new NameValuePair(getEventName(EVENT_FIELD_ONVALIDATE, locale), Integer.toString(EVENT_FIELD_ONVALIDATE)); |
| 93 |
0
|
eventNames[4] = new NameValuePair(getEventName(EVENT_FIELD_ONPRESUBMIT, locale), Integer.toString(EVENT_FIELD_ONPRESUBMIT)); |
| 94 |
0
|
eventNames[5] = new NameValuePair(getEventName(EVENT_FIELD_ONPOSTSUBMIT, locale), Integer.toString(EVENT_FIELD_ONPOSTSUBMIT)); |
| 95 |
0
|
return eventNames; |
| 96 |
|
} |
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
@param |
| 102 |
|
|
| 103 |
|
@param |
| 104 |
|
@return |
| 105 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 106 |
0
|
public static List<NameValuePair> getListOptions(Map<Integer, List<NameValuePair>> listOptions, int fieldId) {... |
| 107 |
0
|
return getListOptions(listOptions, Integer.valueOf(fieldId)); |
| 108 |
|
} |
| 109 |
|
|
| 110 |
|
|
| 111 |
|
|
| 112 |
|
|
| 113 |
|
@param |
| 114 |
|
|
| 115 |
|
@param |
| 116 |
|
@return |
| 117 |
|
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 5 |
Complexity Density: 0.83 |
|
| 118 |
0
|
@SuppressWarnings("unchecked")... |
| 119 |
|
public static List<NameValuePair> getListOptions(Map listOptions, Integer fieldId) { |
| 120 |
0
|
List<NameValuePair> options = new ArrayList<NameValuePair>(); |
| 121 |
|
|
| 122 |
0
|
if(listOptions != null && listOptions.size() != 0 && fieldId != null) { |
| 123 |
0
|
Object mapOptions = listOptions.get(fieldId); |
| 124 |
0
|
if(mapOptions != null) { |
| 125 |
0
|
options = (List<NameValuePair>) mapOptions; |
| 126 |
|
} |
| 127 |
|
} |
| 128 |
|
|
| 129 |
0
|
return options; |
| 130 |
|
} |
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| 134 |
|
|
| 135 |
|
|
| 136 |
|
|
| 137 |
|
@param |
| 138 |
|
@param |
| 139 |
|
@param |
| 140 |
|
@param |
| 141 |
|
@param |
| 142 |
|
@throws |
| 143 |
|
|
|
|
|
| 0% |
Uncovered Elements: 42 (42) |
Complexity: 10 |
Complexity Density: 0.42 |
|
| 144 |
0
|
public static void processFieldScripts(List<ProjectScript> projectScriptModels, int event, Map<Integer, List<NameValuePair>> currentValues, ActionMessages currentErrors, ValidatorForm form) throws WorkflowException {... |
| 145 |
0
|
if(projectScriptModels == null || projectScriptModels.size() == 0) { |
| 146 |
0
|
return; |
| 147 |
|
} |
| 148 |
0
|
logger.debug("Processing " + projectScriptModels.size() + " field scripts for project " + projectScriptModels.get(0).getProject().getId()); |
| 149 |
|
|
| 150 |
0
|
List<ProjectScript> scriptsToRun = new ArrayList<ProjectScript>(); |
| 151 |
0
|
for(int i = 0; i < projectScriptModels.size(); i++) { |
| 152 |
0
|
if(projectScriptModels.get(i).getScript().getEvent() == event) { |
| 153 |
0
|
int insertIndex = 0; |
| 154 |
0
|
for(insertIndex = 0; insertIndex < scriptsToRun.size(); insertIndex++) { |
| 155 |
0
|
if(projectScriptModels.get(i).getPriority() < ((ProjectScript) scriptsToRun.get(insertIndex)).getPriority()) { |
| 156 |
0
|
break; |
| 157 |
|
} |
| 158 |
|
} |
| 159 |
0
|
scriptsToRun.add(insertIndex,projectScriptModels.get(i)); |
| 160 |
|
} |
| 161 |
|
} |
| 162 |
0
|
logger.debug(scriptsToRun.size() + " eligible scripts found for event " + event); |
| 163 |
|
|
| 164 |
0
|
if (currentValues == null) { |
| 165 |
0
|
currentValues = new HashMap<Integer, List<NameValuePair>>(); |
| 166 |
|
} |
| 167 |
|
|
| 168 |
0
|
for (int i = 0; i < scriptsToRun.size(); i++) { |
| 169 |
0
|
ProjectScript currentScript = (ProjectScript) scriptsToRun.get(i); |
| 170 |
0
|
try { |
| 171 |
0
|
logger.debug("Running script " + currentScript.getScript().getId() + " with priority " + currentScript.getPriority()); |
| 172 |
0
|
List<NameValuePair> currentValue = currentValues.get(currentScript.getFieldId()); |
| 173 |
|
|
| 174 |
0
|
logger.debug("Before script current value for field " + IssueUtilities.getFieldName(currentScript.getFieldId()) + " (" + currentScript.getFieldId() + ") is " + (currentValue == null ? "NULL" : "'" + currentValue.toString() + "' (" + currentValue.getClass().getName() + "'")); |
| 175 |
0
|
currentValue = processFieldScript(currentScript, event, currentScript.getFieldId(), currentValue, currentErrors, form); |
| 176 |
0
|
logger.debug("After script current value for field " + IssueUtilities.getFieldName(currentScript.getFieldId()) + " (" + currentScript.getFieldId() + ") is " + (currentValue == null ? "NULL" : "'" + currentValue.toString() + "' (" + currentValue.getClass().getName() + "'")); |
| 177 |
|
|
| 178 |
0
|
currentValues.put(currentScript.getFieldId(), currentValue); |
| 179 |
|
} catch(WorkflowException we) { |
| 180 |
0
|
logger.error("Error processing script " + currentScript.getScript().getId() + ": " + we.getMessage()); |
| 181 |
|
} |
| 182 |
|
} |
| 183 |
|
} |
| 184 |
|
|
| 185 |
|
|
| 186 |
|
|
| 187 |
|
|
| 188 |
|
@param |
| 189 |
|
@param |
| 190 |
|
@param |
| 191 |
|
@param |
| 192 |
|
@param |
| 193 |
|
@param |
| 194 |
|
@return |
| 195 |
|
@throws |
| 196 |
|
|
|
|
|
| 0% |
Uncovered Elements: 31 (31) |
Complexity: 11 |
Complexity Density: 0.58 |
|
| 197 |
0
|
public static List<NameValuePair> processFieldScripts(List<ProjectScript> projectScripts, int event, Integer fieldId, List<NameValuePair> currentValue, ActionErrors currentErrors, ValidatorForm form) throws WorkflowException {... |
| 198 |
0
|
if(projectScripts == null || projectScripts.size() == 0 || fieldId == null) { |
| 199 |
0
|
return null; |
| 200 |
|
} |
| 201 |
0
|
logger.debug("Processing " + projectScripts.size() + " field scripts for project " + projectScripts.get(0).getProject().getId()); |
| 202 |
|
|
| 203 |
0
|
List<ProjectScript> scriptsToRun = new LinkedList<ProjectScript>(); |
| 204 |
0
|
for(int i = 0; i < projectScripts.size(); i++) { |
| 205 |
0
|
if(projectScripts.get(i).getScript().getEvent() == event && fieldId.equals(projectScripts.get(i).getFieldId())) { |
| 206 |
0
|
int insertIndex = 0; |
| 207 |
0
|
for(insertIndex = 0; insertIndex < scriptsToRun.size(); insertIndex++) { |
| 208 |
0
|
if(projectScripts.get(i).getPriority() < ((ProjectScript) scriptsToRun.get(insertIndex)).getPriority()) { |
| 209 |
0
|
break; |
| 210 |
|
} |
| 211 |
|
} |
| 212 |
0
|
scriptsToRun.add(insertIndex,projectScripts.get(i)); |
| 213 |
|
} |
| 214 |
|
} |
| 215 |
0
|
logger.debug(scriptsToRun.size() + " eligible scripts found for event " + event + " on field " + fieldId); |
| 216 |
|
|
| 217 |
0
|
for(int i = 0; i < scriptsToRun.size(); i++) { |
| 218 |
0
|
ProjectScript currentScript = (ProjectScript) scriptsToRun.get(i); |
| 219 |
0
|
try { |
| 220 |
0
|
logger.debug("Running script " + currentScript.getScript().getId() + " with priority " + currentScript.getPriority()); |
| 221 |
0
|
currentValue = processFieldScript(currentScript, event, fieldId, currentValue, currentErrors, form); |
| 222 |
|
} catch(WorkflowException we) { |
| 223 |
0
|
logger.error("Error processing script " + currentScript.getScript().getId() + ": " + we.getMessage()); |
| 224 |
|
} |
| 225 |
|
} |
| 226 |
|
|
| 227 |
0
|
return currentValue; |
| 228 |
|
} |
| 229 |
|
|
| 230 |
|
|
| 231 |
|
|
| 232 |
|
|
| 233 |
|
|
| 234 |
|
|
| 235 |
|
|
| 236 |
|
|
| 237 |
|
@param |
| 238 |
|
@param |
| 239 |
|
@param |
| 240 |
|
@param |
| 241 |
|
@param |
| 242 |
|
@param |
| 243 |
|
@return |
| 244 |
|
@throws |
| 245 |
|
|
|
|
|
| 0% |
Uncovered Elements: 38 (38) |
Complexity: 10 |
Complexity Density: 0.42 |
|
| 246 |
0
|
public static List<NameValuePair> processFieldScript(ProjectScript projectScript, int event, Integer fieldId, List<NameValuePair> currentValues, ActionMessages currentErrors, ValidatorForm form) throws WorkflowException {... |
| 247 |
0
|
if (projectScript == null) { |
| 248 |
0
|
throw new WorkflowException("ProjectScript was null.", WorkflowException.INVALID_ARGS); |
| 249 |
|
} |
| 250 |
0
|
if (currentErrors == null) { |
| 251 |
0
|
throw new WorkflowException("Errors was null.", WorkflowException.INVALID_ARGS); |
| 252 |
|
} |
| 253 |
|
|
| 254 |
0
|
try { |
| 255 |
0
|
Interpreter bshInterpreter = new Interpreter(); |
| 256 |
0
|
bshInterpreter.set("event", event); |
| 257 |
0
|
bshInterpreter.set("fieldId", fieldId); |
| 258 |
|
|
| 259 |
0
|
bshInterpreter.set("currentValue", currentValues); |
| 260 |
0
|
bshInterpreter.set("currentValues", currentValues); |
| 261 |
0
|
bshInterpreter.set("currentErrors", currentErrors); |
| 262 |
0
|
bshInterpreter.set("currentForm", form); |
| 263 |
0
|
bshInterpreter.eval(projectScript.getScript().getScript()); |
| 264 |
|
|
| 265 |
|
|
| 266 |
0
|
if (logger.isDebugEnabled()) { |
| 267 |
0
|
logger.debug("processFieldScript: Script returned current value of '" + currentValues + "' (" + (currentValues != null ? currentValues.getClass().getName() : "NULL") + ")"); |
| 268 |
|
} |
| 269 |
0
|
if (event == EVENT_FIELD_ONSETDEFAULT && form != null && currentValues != null) { |
| 270 |
0
|
if (logger.isDebugEnabled()) { |
| 271 |
0
|
logger.debug("processFieldScript: Setting current form field value for field " + IssueUtilities.getFieldName(projectScript.getFieldId()) + " to '" + currentValues + "'"); |
| 272 |
|
} |
| 273 |
0
|
setFormProperty(form, projectScript.getFieldId(), currentValues); |
| 274 |
|
} |
| 275 |
|
} catch(Exception e) { |
| 276 |
0
|
logger.error("processFieldScript: Error processing field script.", e); |
| 277 |
|
|
| 278 |
0
|
currentErrors.add(ActionMessages.GLOBAL_MESSAGE, |
| 279 |
|
new ActionMessage("itracker.web.error.system.message", |
| 280 |
|
new Object[]{ITrackerResources.getString("itracker.web.attr.script"), |
| 281 |
|
e.getMessage() |
| 282 |
|
})); |
| 283 |
|
} |
| 284 |
0
|
if (logger.isDebugEnabled()) { |
| 285 |
0
|
logger.debug("processFieldScript: returning " + currentValues + ", errors: " + currentErrors); |
| 286 |
|
} |
| 287 |
0
|
return currentValues; |
| 288 |
|
} |
| 289 |
|
|
|
|
|
| 0% |
Uncovered Elements: 46 (46) |
Complexity: 14 |
Complexity Density: 0.44 |
|
| 290 |
0
|
@SuppressWarnings("unchecked")... |
| 291 |
|
private static void setFormProperty(ValidatorForm form, Integer fieldId, Object currentValue) { |
| 292 |
0
|
String fieldName = IssueUtilities.getFieldName(fieldId); |
| 293 |
0
|
int fieldType = IssueUtilities.getFieldType(fieldId); |
| 294 |
0
|
if(fieldType == IssueUtilities.FIELD_TYPE_SINGLE) { |
| 295 |
0
|
try { |
| 296 |
0
|
Field formField = form.getClass().getField( fieldName ); |
| 297 |
0
|
if ( formField != null ) { |
| 298 |
0
|
formField.set( form , currentValue ); |
| 299 |
|
} else { |
| 300 |
0
|
throw new IllegalArgumentException( "no field with name " |
| 301 |
|
+ fieldName + " found in form " + form ); |
| 302 |
|
} |
| 303 |
|
} catch ( NoSuchFieldException e ) { |
| 304 |
0
|
e.printStackTrace(); |
| 305 |
|
} catch ( IllegalAccessException e ) { |
| 306 |
0
|
e.printStackTrace(); |
| 307 |
|
} |
| 308 |
0
|
} else if(fieldType == IssueUtilities.FIELD_TYPE_INDEXED) { |
| 309 |
0
|
try { |
| 310 |
0
|
Object indexedField = null; |
| 311 |
0
|
Field formField = form.getClass().getField(fieldName); |
| 312 |
0
|
indexedField = formField.get( form ); |
| 313 |
0
|
if ( indexedField instanceof List ) { |
| 314 |
0
|
((List)indexedField).set( 0 , currentValue ); |
| 315 |
0
|
} else if ( indexedField instanceof Collection ) { |
| 316 |
0
|
((Collection)indexedField).add( currentValue ); |
| 317 |
|
} else { |
| 318 |
0
|
throw new IllegalArgumentException( "field with name " |
| 319 |
|
+ fieldName + " found in form " + form + " is of unknown type" ); |
| 320 |
|
} |
| 321 |
|
} catch ( NoSuchFieldException e ) { |
| 322 |
0
|
e.printStackTrace(); |
| 323 |
|
} catch ( IllegalAccessException e ) { |
| 324 |
0
|
e.printStackTrace(); |
| 325 |
|
} |
| 326 |
0
|
} else if(fieldType == IssueUtilities.FIELD_TYPE_MAP) { |
| 327 |
0
|
try { |
| 328 |
0
|
Object indexedField = null; |
| 329 |
0
|
Field formField = form.getClass().getField( fieldName ); |
| 330 |
0
|
indexedField = formField.get( form ); |
| 331 |
0
|
if ( indexedField instanceof Map ) { |
| 332 |
0
|
((Map)indexedField).put( fieldId.toString(), currentValue ); |
| 333 |
|
} else { |
| 334 |
0
|
throw new IllegalArgumentException( "field with name " |
| 335 |
|
+ fieldName + " found in form " + form + " is of unknown type" ); |
| 336 |
|
} |
| 337 |
|
} catch ( NoSuchFieldException e ) { |
| 338 |
0
|
e.printStackTrace(); |
| 339 |
|
} catch ( IllegalAccessException e ) { |
| 340 |
0
|
e.printStackTrace(); |
| 341 |
|
} |
| 342 |
|
} |
| 343 |
|
} |
| 344 |
|
|
| 345 |
|
} |