1 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 ; 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.HashSet ; 27 import java.util.LinkedHashMap ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Set ; 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 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 53 public static class FieldForm implements Serializable { 54 55 private transient Field field; 56 private String 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 getOption() { 67 return option; 68 } 69 70 public void setOption(String option) { 71 this.option = option; 72 } 73 74 } 75 76 79 public static class FieldFormValidator implements Validator { 80 81 public boolean supports(Class clazz) { 82 return FieldForm.class.isAssignableFrom(clazz); 83 } 84 85 public void validate(Object o, Errors errors) { 86 FieldForm fieldForm = (FieldForm) o; 87 ValidationUtils.rejectIfEmpty(errors, "field.label"); 88 String 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 { 97 FieldForm fieldForm = (FieldForm) getFormObject(context); 98 String 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 { 107 FieldForm fieldForm = (FieldForm) getFormObject(context); 108 String optionKey = ValidationUtils.getParameter(context, "optionKey"); 109 String 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 { 116 FieldForm fieldForm = (FieldForm) getFormObject(context); 117 String optionKey = ValidationUtils.getParameter(context, "optionKey"); 118 String 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); return success(); 132 } 133 134 public Event fieldOptionDeleteHandler(RequestContext context) throws Exception { 135 Space space = (Space) context.getFlowScope().get("space"); 136 FieldForm fieldForm = (FieldForm) getFormObject(context); 137 String optionKey = ValidationUtils.getParameter(context, "optionKey"); 138 String 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 { 153 Space space = (Space) context.getFlowScope().get("space"); 154 FieldForm fieldForm = (FieldForm) getFormObject(context); 155 String optionKey = ValidationUtils.getParameter(context, "optionKey"); 156 fieldForm.field.getOptions().remove(optionKey); 157 jtrac.bulkUpdateFieldToNullForValue(space, fieldForm.field, Integer.parseInt(optionKey)); 158 jtrac.storeSpace(space); 161 space.setMetadata(jtrac.loadMetadata(space.getMetadata().getId())); 163 return success(); 164 } 165 166 public Event fieldOptionUpHandler(RequestContext context) throws Exception { 167 FieldForm fieldForm = (FieldForm) getFormObject(context); 168 String optionKey = ValidationUtils.getParameter(context, "optionKey"); 169 Map <String , String > options = fieldForm.field.getOptions(); 170 List <String > keys = new ArrayList <String >(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 <String , String > updated = new LinkedHashMap <String , String >(keys.size()); 184 for (String 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 { 192 FieldForm fieldForm = (FieldForm) getFormObject(context); 193 String optionKey = ValidationUtils.getParameter(context, "optionKey"); 194 Map <String , String > options = fieldForm.field.getOptions(); 195 List <String > keys = new ArrayList <String >(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 <String , String > updated = new LinkedHashMap <String , String >(keys.size()); 205 for (String s : keys) { 206 updated.put(s, options.get(s)); 207 } 208 fieldForm.field.setOptions(updated); 209 return success(); 210 } 211 212 } 213 | Popular Tags |