1 16 17 package info.jtrac.webflow; 18 19 import info.jtrac.domain.Field; 20 import info.jtrac.domain.Role; 21 import info.jtrac.domain.Space; 22 import info.jtrac.domain.User; 23 import info.jtrac.domain.UserSpaceRole; 24 import info.jtrac.domain.WorkflowRenderer; 25 import info.jtrac.util.SecurityUtils; 26 import info.jtrac.util.ValidationUtils; 27 import info.jtrac.webflow.FieldFormAction.FieldForm; 28 import java.util.Collections ; 29 import java.util.List ; 30 import org.springframework.beans.propertyeditors.StringTrimmerEditor; 31 import org.springframework.validation.DataBinder; 32 33 import org.springframework.validation.Errors; 34 import org.springframework.webflow.execution.Event; 35 import org.springframework.webflow.execution.RequestContext; 36 import org.springframework.webflow.execution.ScopeType; 37 38 41 public class SpaceFormAction extends AbstractFormAction { 42 43 public SpaceFormAction() { 44 setFormObjectClass(Space.class); 45 setFormObjectName("space"); 46 setFormObjectScope(ScopeType.FLOW); 47 } 48 49 @Override 50 protected void initBinder(RequestContext request, DataBinder binder) { 51 binder.registerCustomEditor(String .class, new StringTrimmerEditor(true)); 52 } 53 54 @Override 55 public Object createFormObject(RequestContext context) { 56 context.getFlowScope().put("_flowId", "space"); 57 String spaceId = ValidationUtils.getParameter(context, "spaceId"); 58 Space space = null; 59 if (spaceId != null) { 60 space = jtrac.loadSpace(Integer.parseInt(spaceId)); 61 space.getMetadata().getXmlString(); return space; 63 } else { 64 space = new Space(); 65 space.getMetadata().initRoles(); 66 context.getFlowScope().put("spaces", jtrac.findAllSpaces()); 67 return space; 68 } 69 } 70 71 public Event spaceFormHandler(RequestContext context) throws Exception { 72 Space space = (Space) getFormObject(context); 73 Errors errors = getFormErrors(context); 74 if (space.getPrefixCode() == null) { 75 errors.rejectValue("prefixCode", ValidationUtils.ERROR_EMPTY_CODE); 76 } else { 77 if (space.getPrefixCode().length() < 3) { 78 errors.rejectValue("prefixCode", "space_form.error.prefixCode.tooShort"); 79 } 80 if (space.getPrefixCode().length() > 10) { 81 errors.rejectValue("prefixCode", "space_form.error.prefixCode.tooLong"); 82 } 83 if (!ValidationUtils.isAllUpperCase(space.getPrefixCode())) { 84 errors.rejectValue("prefixCode", "space_form.error.prefixCode.invalid"); 85 } 86 } 87 if (space.getName() == null) { 88 errors.rejectValue("name", ValidationUtils.ERROR_EMPTY_CODE); 89 } 90 if (errors.hasErrors()) { 91 return error(); 92 } 93 Space temp = jtrac.loadSpace(space.getPrefixCode()); 94 if (temp != null && temp.getId() != space.getId()) { 95 errors.rejectValue("prefixCode", "space_form.error.prefixCode.exists"); 96 return error(); 97 } 98 String copyFrom = ValidationUtils.getParameter(context, "copyFrom"); 99 if (copyFrom != null) { 100 temp = jtrac.loadSpace(Long.parseLong(copyFrom)); 101 space.getMetadata().setXmlString(temp.getMetadata().getXmlString()); 102 } 103 return success(); 104 } 105 106 public Event fieldUpHandler(RequestContext context) { 107 Space space = (Space) context.getFlowScope().get("space"); 108 String fieldName = ValidationUtils.getParameter(context, "fieldName"); 109 List <Field.Name> fieldOrder = space.getMetadata().getFieldOrder(); 110 int index = fieldOrder.indexOf(Field.convertToName(fieldName)); 111 int swapIndex = index - 1; 112 if (swapIndex < 0) { 113 if (fieldOrder.size() > 1) { 114 swapIndex = fieldOrder.size() - 1; 115 } else { 116 swapIndex = 0; 117 } 118 } 119 if (index != swapIndex) { 120 Collections.swap(fieldOrder, index, swapIndex); 121 } 122 context.getRequestScope().put("selectedFieldName", fieldName); 123 return success(); 124 } 125 126 public Event fieldDownHandler(RequestContext context) { 127 Space space = (Space) context.getFlowScope().get("space"); 128 String fieldName = ValidationUtils.getParameter(context, "fieldName"); 129 List <Field.Name> fieldOrder = space.getMetadata().getFieldOrder(); 130 int index = fieldOrder.indexOf(Field.convertToName(fieldName)); 131 int swapIndex = index + 1; 132 if (swapIndex == fieldOrder.size() ) { 133 swapIndex = 0; 134 } 135 if (index != swapIndex) { 136 Collections.swap(fieldOrder, index, swapIndex); 137 } 138 context.getRequestScope().put("selectedFieldName", fieldName); 139 return success(); 140 } 141 142 public Event fieldAddHandler(RequestContext context) { 143 Space space = (Space) context.getFlowScope().get("space"); 144 String fieldType = ValidationUtils.getParameter(context, "fieldType"); 145 if (fieldType == null) { 146 return error(); 148 } 149 int type = Integer.parseInt(fieldType); 150 FieldForm fieldForm = new FieldForm(); 151 Field field = space.getMetadata().getNextAvailableField(type); 152 field.initOptions(); 154 fieldForm.setField(field); 155 space.getMetadata().add(field); 156 context.getFlowScope().put("fieldForm", fieldForm); 157 return success(); 158 } 159 160 public Event fieldEditHandler(RequestContext context) { 161 Space space = (Space) context.getFlowScope().get("space"); 162 String fieldName = ValidationUtils.getParameter(context, "fieldName"); 163 Field field = space.getMetadata().getField(fieldName); 164 FieldForm fieldForm = new FieldForm(); 165 fieldForm.setField(field); 166 context.getFlowScope().put("fieldForm", fieldForm); 167 return success(); 168 } 169 170 public Event fieldUpdateHandler(RequestContext context) { 171 FieldForm fieldForm = (FieldForm) context.getFlowScope().get("fieldForm"); 172 Field field = fieldForm.getField(); 173 context.getRequestScope().put("selectedFieldName", field.getNameText()); 174 return success(); 175 } 176 177 public Event fieldDeleteHandler(RequestContext context) { 178 Space space = (Space) context.getFlowScope().get("space"); 179 String fieldName = ValidationUtils.getParameter(context, "fieldName"); 180 Field field = space.getMetadata().getField(fieldName); 181 if (space.getId() > 0) { 182 int affectedCount = jtrac.loadCountOfRecordsHavingFieldNotNull(space, field); 183 if (affectedCount > 0) { 184 context.getRequestScope().put("affectedCount", affectedCount); 185 return new Event(this, "confirm"); 186 } 187 } 188 space.getMetadata().removeField(fieldName); 190 return success(); 191 } 192 193 public Event fieldDeleteConfirmHandler(RequestContext context) { 194 Space space = (Space) context.getFlowScope().get("space"); 195 String fieldName = ValidationUtils.getParameter(context, "fieldName"); 196 Field field = space.getMetadata().getField(fieldName); 197 logger.debug("saving space after field delete operation"); 200 jtrac.bulkUpdateFieldToNull(space, field); 201 space.getMetadata().removeField(fieldName); 202 jtrac.storeSpace(space); 203 space.setMetadata(jtrac.loadMetadata(space.getMetadata().getId())); 205 return success(); 206 } 207 208 210 public Event stateFormSetupHandler(RequestContext context) { 211 Space space = (Space) context.getFlowScope().get("space"); 212 String stateKey = ValidationUtils.getParameter(context, "stateKey"); 213 if (stateKey != null) { 214 context.getRequestScope().put("stateKey", stateKey); 215 context.getRequestScope().put("state", space.getMetadata().getStates().get(Integer.parseInt(stateKey))); 216 } 217 return success(); 218 } 219 220 public Event stateFormHandler(RequestContext context) throws Exception { 221 String state = ValidationUtils.getParameter(context, "state"); 222 String stateKey = ValidationUtils.getParameter(context, "stateKey"); 223 Errors errors = getFormErrors(context); 224 context.getRequestScope().put("state", state); 225 context.getRequestScope().put("stateKey", stateKey); 226 if (!ValidationUtils.isCamelDashCase(state)) { 227 errors.reject("space_state_form.error.state.invalid"); 228 return error(); 229 } 230 Space space = (Space) context.getFlowScope().get("space"); 231 if(space.getMetadata().getStates().containsValue(state)) { 232 errors.reject("space_state_form.error.state.exists"); 233 return error(); 234 } 235 if (stateKey == null) { 236 space.getMetadata().addState(state); 237 } else { 238 space.getMetadata().getStates().put(Integer.parseInt(stateKey), state); 239 } 240 return success(); 241 } 242 243 public Event stateDeleteHandler(RequestContext context) { 244 Space space = (Space) context.getFlowScope().get("space"); 245 String stateKey = ValidationUtils.getParameter(context, "stateKey"); 246 int status = Integer.parseInt(stateKey); 247 if (space.getId() > 0) { 248 int affectedCount = jtrac.loadCountOfRecordsHavingStatus(space, status); 249 if (affectedCount > 0) { 250 context.getRequestScope().put("affectedCount", affectedCount); 251 context.getRequestScope().put("stateKey", stateKey); 252 context.getRequestScope().put("state", ValidationUtils.getParameter(context, "state")); 253 return new Event(this, "confirm"); 254 } 255 } 256 space.getMetadata().removeState(status); 258 return success(); 259 } 260 261 public Event stateDeleteConfirmHandler(RequestContext context) { 262 Space space = (Space) context.getFlowScope().get("space"); 263 String stateKey = ValidationUtils.getParameter(context, "stateKey"); 264 int status = Integer.parseInt(stateKey); 265 logger.debug("saving space after field delete operation"); 268 jtrac.bulkUpdateStatusToOpen(space, status); 269 space.getMetadata().removeState(status); 270 jtrac.storeSpace(space); 271 space.setMetadata(jtrac.loadMetadata(space.getMetadata().getId())); 273 return success(); 274 } 275 276 278 public Event spaceRolesSetupHandler(RequestContext context) throws Exception { 279 Space space = (Space) context.getFlowScope().get("space"); 280 WorkflowRenderer workflow = new WorkflowRenderer(space.getMetadata().getRoles(), space.getMetadata().getStates()); 281 context.getRequestScope().put("workflow", workflow); 282 return success(); 283 } 284 285 286 public Event roleFormSetupHandler(RequestContext context) { 287 String roleKey = ValidationUtils.getParameter(context, "roleKey"); 288 if (roleKey != null) { 289 context.getRequestScope().put("oldRoleKey", roleKey); 290 context.getRequestScope().put("roleKey", roleKey); 291 } 292 return success(); 293 } 294 295 public Event roleFormHandler(RequestContext context) throws Exception { 296 Space space = (Space) context.getFlowScope().get("space"); 297 String roleKey = ValidationUtils.getParameter(context, "roleKey"); 298 String oldRoleKey = ValidationUtils.getParameter(context, "oldRoleKey"); 299 context.getRequestScope().put("oldRoleKey", oldRoleKey); 301 context.getRequestScope().put("roleKey", roleKey); 302 Errors errors = getFormErrors(context); 303 if (!ValidationUtils.isAllUpperCase(roleKey)) { 304 errors.reject("space_role_form.error.role.invalid"); 305 return error(); 306 } 307 if (space.getMetadata().getRoles().containsKey(roleKey)) { 308 errors.reject("space_role_form.error.role.exists"); 309 return error(); 310 } 311 if (oldRoleKey == null) { 312 space.getMetadata().addRole(roleKey); 313 } else if (!oldRoleKey.equals(roleKey)) { 314 if (space.getId() > 0) { 315 return new Event(this, "confirm"); 316 } else { 317 space.getMetadata().renameRole(oldRoleKey, roleKey); 318 jtrac.bulkUpdateRenameSpaceRole(space, oldRoleKey, roleKey); 319 } 320 } 321 return success(); 322 } 323 324 public Event roleFormConfirmHandler(RequestContext context) { 325 Space space = (Space) context.getFlowScope().get("space"); 326 String roleKey = ValidationUtils.getParameter(context, "roleKey"); 327 String oldRoleKey = ValidationUtils.getParameter(context, "oldRoleKey"); 328 jtrac.bulkUpdateRenameSpaceRole(space, oldRoleKey, roleKey); 330 space.getMetadata().renameRole(oldRoleKey, roleKey); 331 jtrac.storeSpace(space); 332 space.setMetadata(jtrac.loadMetadata(space.getMetadata().getId())); 334 SecurityUtils.refreshSecurityContext(); 336 return success(); 337 } 338 339 public Event roleDeleteHandler(RequestContext context) { 340 Space space = (Space) context.getFlowScope().get("space"); 341 String roleKey = ValidationUtils.getParameter(context, "roleKey"); 342 if (space.getId() > 0) { 343 List <User> users = jtrac.findUsersWithRoleForSpace(space.getId(), roleKey); 344 if (users.size() > 0) { 345 String oldRoleKey = ValidationUtils.getParameter(context, "oldRoleKey"); 346 context.getRequestScope().put("oldRoleKey", oldRoleKey); 347 context.getRequestScope().put("roleKey", roleKey); 348 return new Event(this, "confirm"); 349 } 350 } 351 space.getMetadata().removeRole(roleKey); 353 return success(); 354 } 355 356 public Event roleDeleteConfirmHandler(RequestContext context) { 357 Space space = (Space) context.getFlowScope().get("space"); 358 String roleKey = ValidationUtils.getParameter(context, "roleKey"); 359 logger.debug("saving space after role delete operation"); 362 jtrac.bulkUpdateDeleteSpaceRole(space, roleKey); 363 space.getMetadata().removeRole(roleKey); 364 jtrac.storeSpace(space); 365 space.setMetadata(jtrac.loadMetadata(space.getMetadata().getId())); 367 SecurityUtils.refreshSecurityContext(); 369 return success(); 370 } 371 372 374 public Event editTransitionHandler(RequestContext context) { 375 Space space = (Space) context.getFlowScope().get("space"); 376 String stateKey = ValidationUtils.getParameter(context, "stateKey"); 377 String roleKey = ValidationUtils.getParameter(context, "roleKey"); 378 String transitionKey = ValidationUtils.getParameter(context, "transitionKey"); 379 space.getMetadata().toggleTransition(roleKey, Integer.parseInt(stateKey), Integer.parseInt(transitionKey)); 380 return success(); 381 } 382 383 public Event editMaskHandler(RequestContext context) { 384 Space space = (Space) context.getFlowScope().get("space"); 385 String stateKey = ValidationUtils.getParameter(context, "stateKey"); 386 String roleKey = ValidationUtils.getParameter(context, "roleKey"); 387 String fieldKey = ValidationUtils.getParameter(context, "fieldKey"); 388 space.getMetadata().switchMask(Integer.parseInt(stateKey), roleKey, fieldKey); 389 return success(); 390 } 391 392 394 public Event spaceSaveHandler(RequestContext context) { 395 Space space = (Space) context.getFlowScope().get("space"); 396 jtrac.storeSpace(space); 397 SecurityUtils.refreshSecurityContext(); 399 return success(); 400 } 401 402 public Event spaceDeleteHandler(RequestContext context) { 403 Space space = (Space) context.getFlowScope().get("space"); 404 jtrac.removeSpace(space); 405 SecurityUtils.refreshSecurityContext(); 407 return success(); 408 } 409 410 412 public Event spaceAllocateSetup(RequestContext context) { 413 context.getFlowScope().put("_flowId", "space"); 414 Space space = (Space) context.getFlowScope().get("space"); 415 if (space == null) { 416 String spaceId = ValidationUtils.getParameter(context, "spaceId"); 417 int id = Integer.parseInt(spaceId); 418 space = jtrac.loadSpace(id); 419 context.getFlowScope().put("space", space); 420 } 421 context.getRequestScope().put("userSpaceRoles", jtrac.findUserRolesForSpace(space.getId())); 422 context.getRequestScope().put("unallocatedUsers", jtrac.findUnallocatedUsersForSpace(space.getId())); 423 return success(); 424 } 425 426 public Event spaceAllocateHandler(RequestContext context) { 427 String userId = ValidationUtils.getParameter(context, "userId"); 428 if (userId == null) { 429 return error(); 431 } 432 int id = Integer.parseInt(userId); 433 User user = jtrac.loadUser(id); 434 Space space = (Space) context.getFlowScope().get("space"); 435 String roleKey = ValidationUtils.getParameter(context, "roleKey"); 436 String admin = ValidationUtils.getParameter(context, "admin"); 437 jtrac.storeUserSpaceRole(user, space, roleKey); 438 if (admin != null) { 439 jtrac.storeUserSpaceRole(user, space, "ROLE_ADMIN"); 440 } 441 SecurityUtils.refreshSecurityContextIfPrincipal(user); 442 return success(); 443 } 444 445 public Event spaceDeallocateHandler(RequestContext context) { 446 String userSpaceRoleId = ValidationUtils.getParameter(context, "deallocate"); 447 long id = Long.parseLong(userSpaceRoleId); 448 UserSpaceRole userSpaceRole = jtrac.loadUserSpaceRole(id); 449 jtrac.removeUserSpaceRole(userSpaceRole); 450 SecurityUtils.refreshSecurityContextIfPrincipal(userSpaceRole.getUser()); 451 return success(); 452 } 453 454 } 455 | Popular Tags |