KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > jtrac > webflow > SpaceFormAction


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
29 import java.util.List JavaDoc;
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 /**
39  * Multiaction that backs the "Space Create" flow
40  */

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 JavaDoc
50     protected void initBinder(RequestContext request, DataBinder binder) {
51         binder.registerCustomEditor(String JavaDoc.class, new StringTrimmerEditor(true));
52     }
53     
54     @Override JavaDoc
55     public Object JavaDoc createFormObject(RequestContext context) {
56         context.getFlowScope().put("_flowId", "space");
57         String JavaDoc spaceId = ValidationUtils.getParameter(context, "spaceId");
58         Space space = null;
59         if (spaceId != null) {
60             space = jtrac.loadSpace(Integer.parseInt(spaceId));
61             space.getMetadata().getXmlString(); // hack: ensure nothing left to be lazy loaded!
62
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 JavaDoc {
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 JavaDoc 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 JavaDoc fieldName = ValidationUtils.getParameter(context, "fieldName");
109         List JavaDoc<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 JavaDoc fieldName = ValidationUtils.getParameter(context, "fieldName");
129         List JavaDoc<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 JavaDoc fieldType = ValidationUtils.getParameter(context, "fieldType");
145         if (fieldType == null) {
146             // no fields left, just return to the space details screen
147
return error();
148         }
149         int type = Integer.parseInt(fieldType);
150         FieldForm fieldForm = new FieldForm();
151         Field field = space.getMetadata().getNextAvailableField(type);
152         // set intelligent defaults to make adding new field to space easier
153
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 JavaDoc 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 JavaDoc 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         // this is an unsaved space or there are no impacted items
189
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 JavaDoc fieldName = ValidationUtils.getParameter(context, "fieldName");
196         Field field = space.getMetadata().getField(fieldName);
197         // database will be updated, if we don't do this
198
// user may leave without committing metadata change
199
logger.debug("saving space after field delete operation");
200         jtrac.bulkUpdateFieldToNull(space, field);
201         space.getMetadata().removeField(fieldName);
202         jtrac.storeSpace(space);
203         // horrible hack, but otherwise if we save again we get the dreaded Stale Object Exception
204
space.setMetadata(jtrac.loadMetadata(space.getMetadata().getId()));
205         return success();
206     }
207     
208     //=============================== STATES ===================================
209

210     public Event stateFormSetupHandler(RequestContext context) {
211         Space space = (Space) context.getFlowScope().get("space");
212         String JavaDoc 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 JavaDoc {
221         String JavaDoc state = ValidationUtils.getParameter(context, "state");
222         String JavaDoc 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 JavaDoc 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         // this is an unsaved space or there are no impacted items
257
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 JavaDoc stateKey = ValidationUtils.getParameter(context, "stateKey");
264         int status = Integer.parseInt(stateKey);
265         // database will be updated, if we don't do this
266
// user may leave without committing metadata change
267
logger.debug("saving space after field delete operation");
268         jtrac.bulkUpdateStatusToOpen(space, status);
269         space.getMetadata().removeState(status);
270         jtrac.storeSpace(space);
271         // horrible hack, but otherwise if we save again we get the dreaded Stale Object Exception
272
space.setMetadata(jtrac.loadMetadata(space.getMetadata().getId()));
273         return success();
274     }
275     
276     //================================= ROLES ==================================
277

278     public Event spaceRolesSetupHandler(RequestContext context) throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc {
296         Space space = (Space) context.getFlowScope().get("space");
297         String JavaDoc roleKey = ValidationUtils.getParameter(context, "roleKey");
298         String JavaDoc oldRoleKey = ValidationUtils.getParameter(context, "oldRoleKey");
299         // needed for errors or if confirm rename screen to be shown
300
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 JavaDoc roleKey = ValidationUtils.getParameter(context, "roleKey");
327         String JavaDoc oldRoleKey = ValidationUtils.getParameter(context, "oldRoleKey");
328         // TODO next 3 lines should ideally be in a transaction
329
jtrac.bulkUpdateRenameSpaceRole(space, oldRoleKey, roleKey);
330         space.getMetadata().renameRole(oldRoleKey, roleKey);
331         jtrac.storeSpace(space);
332         // horrible hack, but otherwise if we save again we get the dreaded Stale Object Exception
333
space.setMetadata(jtrac.loadMetadata(space.getMetadata().getId()));
334         // current user may be allocated to this space with this role - refresh
335
SecurityUtils.refreshSecurityContext();
336         return success();
337     }
338     
339     public Event roleDeleteHandler(RequestContext context) {
340         Space space = (Space) context.getFlowScope().get("space");
341         String JavaDoc roleKey = ValidationUtils.getParameter(context, "roleKey");
342         if (space.getId() > 0) {
343             List JavaDoc<User> users = jtrac.findUsersWithRoleForSpace(space.getId(), roleKey);
344             if (users.size() > 0) {
345                 String JavaDoc 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         // this is an unsaved space or there are no impacted users
352
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 JavaDoc roleKey = ValidationUtils.getParameter(context, "roleKey");
359         // database will be updated, if we don't do this
360
// user may leave without committing metadata change
361
logger.debug("saving space after role delete operation");
362         jtrac.bulkUpdateDeleteSpaceRole(space, roleKey);
363         space.getMetadata().removeRole(roleKey);
364         jtrac.storeSpace(space);
365         // horrible hack, but otherwise if we save again we get the dreaded Stale Object Exception
366
space.setMetadata(jtrac.loadMetadata(space.getMetadata().getId()));
367         // current user may be allocated to this space with this role - refresh
368
SecurityUtils.refreshSecurityContext();
369         return success();
370     }
371     
372     //======================== TRANSITION / MASK ===============================
373

374     public Event editTransitionHandler(RequestContext context) {
375         Space space = (Space) context.getFlowScope().get("space");
376         String JavaDoc stateKey = ValidationUtils.getParameter(context, "stateKey");
377         String JavaDoc roleKey = ValidationUtils.getParameter(context, "roleKey");
378         String JavaDoc 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 JavaDoc stateKey = ValidationUtils.getParameter(context, "stateKey");
386         String JavaDoc roleKey = ValidationUtils.getParameter(context, "roleKey");
387         String JavaDoc fieldKey = ValidationUtils.getParameter(context, "fieldKey");
388         space.getMetadata().switchMask(Integer.parseInt(stateKey), roleKey, fieldKey);
389         return success();
390     }
391     
392     //============================ SAVE ========================================
393

394     public Event spaceSaveHandler(RequestContext context) {
395         Space space = (Space) context.getFlowScope().get("space");
396         jtrac.storeSpace(space);
397         // current user may be allocated to this space, and e.g. name could have changed
398
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         // current user may be allocated to this space - refresh
406
SecurityUtils.refreshSecurityContext();
407         return success();
408     }
409     
410     //========================== ALLOCATE ======================================
411

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 JavaDoc 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 JavaDoc userId = ValidationUtils.getParameter(context, "userId");
428         if (userId == null) {
429             // no users left, no navigation
430
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 JavaDoc roleKey = ValidationUtils.getParameter(context, "roleKey");
436         String JavaDoc 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 JavaDoc 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