KickJava   Java API By Example, From Geeks To Geeks.

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


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.Space;
21 import info.jtrac.util.ValidationUtils;
22
23 import java.io.Serializable JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.LinkedHashMap JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31 import org.springframework.validation.Errors;
32 import org.springframework.validation.Validator;
33 import org.springframework.webflow.execution.Event;
34 import org.springframework.webflow.execution.RequestContext;
35 import org.springframework.webflow.execution.ScopeType;
36
37 /**
38  * Multiaction that participates in the "Space Create / Edit" flow
39  * for editing individual Fields
40  */

41 public class FieldFormAction extends AbstractFormAction {
42     
43     public FieldFormAction() {
44         setFormObjectClass(FieldForm.class);
45         setFormObjectName("fieldForm");
46         setFormObjectScope(ScopeType.FLOW);
47         setValidator(new FieldFormValidator());
48     }
49     
50     /**
51      * Form backing object
52      */

53     public static class FieldForm implements Serializable JavaDoc {
54         
55         private transient Field field;
56         private String JavaDoc option;
57         
58         public Field getField() {
59             return field;
60         }
61         
62         public void setField(Field field) {
63             this.field = field;
64         }
65         
66         public String JavaDoc getOption() {
67             return option;
68         }
69         
70         public void setOption(String JavaDoc option) {
71             this.option = option;
72         }
73         
74     }
75     
76     /**
77      * A validator for our form backing object.
78      */

79     public static class FieldFormValidator implements Validator {
80         
81         public boolean supports(Class JavaDoc clazz) {
82             return FieldForm.class.isAssignableFrom(clazz);
83         }
84         
85         public void validate(Object JavaDoc o, Errors errors) {
86             FieldForm fieldForm = (FieldForm) o;
87             ValidationUtils.rejectIfEmpty(errors, "field.label");
88             String JavaDoc option = fieldForm.option;
89             if (fieldForm.field.hasOption(option)) {
90                 errors.rejectValue("option", "space_field_form.error.optionExists");
91             }
92         }
93         
94     }
95     
96     public Event fieldUpdateHandler(RequestContext context) throws Exception JavaDoc {
97         FieldForm fieldForm = (FieldForm) getFormObject(context);
98         String JavaDoc option = fieldForm.option;
99         if (option != null && !option.equals("")) {
100             fieldForm.field.addOption(option);
101             fieldForm.setOption(null);
102         }
103         return success();
104     }
105     
106     public Event fieldOptionEditSetupHandler(RequestContext context) throws Exception JavaDoc {
107         FieldForm fieldForm = (FieldForm) getFormObject(context);
108         String JavaDoc optionKey = ValidationUtils.getParameter(context, "optionKey");
109         String JavaDoc option = fieldForm.field.getCustomValue(optionKey);
110         context.getRequestScope().put("option", option);
111         context.getRequestScope().put("optionKey", optionKey);
112         return success();
113     }
114     
115     public Event fieldOptionEditHandler(RequestContext context) throws Exception JavaDoc {
116         FieldForm fieldForm = (FieldForm) getFormObject(context);
117         String JavaDoc optionKey = ValidationUtils.getParameter(context, "optionKey");
118         String JavaDoc option = ValidationUtils.getParameter(context, "option");
119         Errors errors = getFormErrors(context);
120         context.getRequestScope().put("option", option);
121         context.getRequestScope().put("optionKey", optionKey);
122         if (option == null) {
123             errors.reject("space_field_option_edit.error.optionEmpty");
124             return error();
125         }
126         if (fieldForm.field.hasOption(option)) {
127             errors.reject("space_field_option_edit.error.exists");
128             return error();
129         }
130         fieldForm.field.addOption(optionKey, option); // will overwrite
131
return success();
132     }
133     
134     public Event fieldOptionDeleteHandler(RequestContext context) throws Exception JavaDoc {
135         Space space = (Space) context.getFlowScope().get("space");
136         FieldForm fieldForm = (FieldForm) getFormObject(context);
137         String JavaDoc optionKey = ValidationUtils.getParameter(context, "optionKey");
138         String JavaDoc option = fieldForm.field.getCustomValue(optionKey);
139         context.getRequestScope().put("optionKey", optionKey);
140         context.getRequestScope().put("option", option);
141         if (space.getId() > 0) {
142             int affectedCount = jtrac.loadCountOfRecordsHavingFieldWithValue(space, fieldForm.field, Integer.parseInt(optionKey));
143             if (affectedCount > 0) {
144                 context.getRequestScope().put("affectedCount", affectedCount);
145                 return new Event(this, "confirm");
146             }
147         }
148         fieldForm.field.getOptions().remove(optionKey);
149         return success();
150     }
151     
152     public Event fieldOptionDeleteConfirmHandler(RequestContext context) throws Exception JavaDoc {
153         Space space = (Space) context.getFlowScope().get("space");
154         FieldForm fieldForm = (FieldForm) getFormObject(context);
155         String JavaDoc optionKey = ValidationUtils.getParameter(context, "optionKey");
156         fieldForm.field.getOptions().remove(optionKey);
157         jtrac.bulkUpdateFieldToNullForValue(space, fieldForm.field, Integer.parseInt(optionKey));
158         // database has been updated, if we don't do this
159
// user may leave without committing metadata change
160
jtrac.storeSpace(space);
161         // horrible hack, but otherwise if we save again we get the dreaded Stale Object Exception
162
space.setMetadata(jtrac.loadMetadata(space.getMetadata().getId()));
163         return success();
164     }
165     
166     public Event fieldOptionUpHandler(RequestContext context) throws Exception JavaDoc {
167         FieldForm fieldForm = (FieldForm) getFormObject(context);
168         String JavaDoc optionKey = ValidationUtils.getParameter(context, "optionKey");
169         Map JavaDoc<String JavaDoc, String JavaDoc> options = fieldForm.field.getOptions();
170         List JavaDoc<String JavaDoc> keys = new ArrayList JavaDoc<String JavaDoc>(options.keySet());
171         int index = keys.indexOf(optionKey);
172         int swapIndex = index - 1;
173         if (swapIndex < 0) {
174             if (keys.size() > 1) {
175                 swapIndex = keys.size() - 1;
176             } else {
177                 swapIndex = 0;
178             }
179         }
180         if (index != swapIndex) {
181             Collections.swap(keys, index, swapIndex);
182         }
183         Map JavaDoc<String JavaDoc, String JavaDoc> updated = new LinkedHashMap JavaDoc<String JavaDoc, String JavaDoc>(keys.size());
184         for (String JavaDoc s : keys) {
185             updated.put(s, options.get(s));
186         }
187         fieldForm.field.setOptions(updated);
188         return success();
189     }
190     
191     public Event fieldOptionDownHandler(RequestContext context) throws Exception JavaDoc {
192         FieldForm fieldForm = (FieldForm) getFormObject(context);
193         String JavaDoc optionKey = ValidationUtils.getParameter(context, "optionKey");
194         Map JavaDoc<String JavaDoc, String JavaDoc> options = fieldForm.field.getOptions();
195         List JavaDoc<String JavaDoc> keys = new ArrayList JavaDoc<String JavaDoc>(options.keySet());
196         int index = keys.indexOf(optionKey);
197         int swapIndex = index + 1;
198         if (swapIndex == keys.size() ) {
199             swapIndex = 0;
200         }
201         if (index != swapIndex) {
202             Collections.swap(keys, index, swapIndex);
203         }
204         Map JavaDoc<String JavaDoc, String JavaDoc> updated = new LinkedHashMap JavaDoc<String JavaDoc, String JavaDoc>(keys.size());
205         for (String JavaDoc s : keys) {
206             updated.put(s, options.get(s));
207         }
208         fieldForm.field.setOptions(updated);
209         return success();
210     }
211     
212 }
213
Popular Tags