1 16 package org.outerj.daisy.frontend.editor; 17 18 import org.apache.cocoon.forms.formmodel.Form; 19 import org.apache.cocoon.forms.formmodel.Repeater; 20 import org.apache.cocoon.forms.FormContext; 21 import org.apache.cocoon.environment.Request; 22 import org.outerj.daisy.repository.schema.DocumentType; 23 import org.outerj.daisy.repository.Repository; 24 25 import java.util.*; 26 27 31 public class DocumentEditorForm { 32 35 private List partFormInfos = new ArrayList(); 36 private Map partForms = new HashMap(); 37 private Map partFormTemplates = new HashMap(); 38 39 private Form linksForm; 40 private Form fieldsForm; 41 private Form miscForm; 42 private Form additionalPartsAndFieldsForm; 43 private String documentName; 44 private boolean validateOnSave = true; 45 private boolean publishImmediately = false; 46 private String activeFormName; 47 48 private Map forms = new HashMap(); 49 54 private boolean hasBeenTriedToSave = false; 55 59 private Map formValidState = new HashMap(); 60 private final DocumentType documentType; 61 private final Repository repository; 62 private final long documentBranchId; 63 private final long documentLanguageId; 64 65 protected DocumentEditorForm(DocumentType documentType, long documentBranchId, long documentLanguageId, 66 Repository repository) { 67 this.documentType = documentType; 70 this.repository = repository; 71 this.documentBranchId = documentBranchId; 72 this.documentLanguageId = documentLanguageId; 73 74 this.activeFormName = "links"; 77 } 78 79 public DocumentType getDocumentType() { 80 return documentType; 81 } 82 83 public Repository getRepository() { 84 return repository; 85 } 86 87 public long getDocumentBranchId() { 88 return documentBranchId; 89 } 90 91 public long getDocumentLanguageId() { 92 return documentLanguageId; 93 } 94 95 public boolean process(Request request, Locale locale, String formName) throws Exception { 96 String activeForm = request.getParameter("activeForm"); 100 if (activeForm == null) { 101 throw new Exception ("Missing request parameter: activeForm"); 102 } 103 if (!forms.containsKey(activeForm)) { 104 throw new Exception ("Invalid value for activeForm request parameter: " + activeForm); 105 } 106 this.activeFormName = activeForm; 107 108 Form form = getForm(formName); 109 form.process(new FormContext(request, locale)); 110 formValidState.put(formName, FormValidState.NOT_VALIDATED); 111 boolean isSave = form.getSubmitWidget() == null; 112 113 if (!hasBeenTriedToSave && isSave) { 114 hasBeenTriedToSave = true; 115 } 116 117 this.documentName = request.getParameter("name") != null ? request.getParameter("name").trim() : null; 119 this.validateOnSave = request.getParameter("validateOnSave") != null; 120 this.publishImmediately = request.getParameter("publishImmediately") != null; 121 122 if (isSave) { 123 boolean allFormsValid = true; 125 Iterator formsIt = forms.keySet().iterator(); 126 while (formsIt.hasNext()) { 127 String currentFormName = (String )formsIt.next(); 128 if (!validateForm(currentFormName)) 129 allFormsValid = false; 130 } 131 if (!documentNameValid()) 132 allFormsValid = false; 133 return allFormsValid; 134 } else { 135 return false; 136 } 137 } 138 139 140 public boolean documentNameValid() { 141 return !hasBeenTriedToSave || !(documentName == null || documentName.length() == 0); 142 } 143 144 private Form getForm(String formName) throws Exception { 145 Form form = (Form)forms.get(formName); 146 if (form == null) 147 throw new Exception ("Invalid form name: \"" + formName + "\"."); 148 return form; 149 } 150 151 public Form getPartForm(String partName) { 152 return (Form)partForms.get("part-" + partName); 153 } 154 155 public Form[] getPartForms() { 156 return (Form[])partForms.values().toArray(new Form[0]); 157 } 158 159 public Form getMiscForm() { 160 return miscForm; 161 } 162 163 public Form getFieldsForm() { 164 return fieldsForm; 165 } 166 167 public Form getLinksForm() { 168 return linksForm; 169 } 170 171 public Form getAdditionalPartsAndFieldsForm() { 172 return additionalPartsAndFieldsForm; 173 } 174 175 public void setActiveForm(String formName) throws Exception { 176 if (!forms.containsKey(formName)) { 177 throw new Exception ("Invalid form name: \"" + formName + "\"."); 178 } 179 this.activeFormName = formName; 180 } 181 182 public Form getActiveForm() { 183 return (Form)forms.get(activeFormName); 184 } 185 186 public String getActiveFormName() { 187 return activeFormName; 188 } 189 190 public String getActiveFormTemplate() { 191 if (activeFormName.equals("fields")) { 192 return "cocoon:/FieldEditorFormTemplate"; 193 } else if (activeFormName.startsWith("part-")) { 194 return (String )partFormTemplates.get(activeFormName); 195 } else { 196 return "resources/form/doceditor_" + activeFormName + "_template.xml"; 197 } 198 } 199 200 protected void addPartForm(String partTypeName, Form form, String formTemplate, String partLabel, String partDescription, boolean isRequired) { 201 String formName = "part-" + partTypeName; 202 partForms.put(formName, form); 203 partFormTemplates.put(formName, formTemplate); 204 partFormInfos.add(new PartFormInfo(formName, partLabel, partDescription, isRequired)); 205 forms.put(formName, form); 206 207 if (partForms.size() == 1) { 208 activeFormName = formName; 210 } 211 } 212 213 protected void setLinksForm(Form form) { 214 this.linksForm = form; 215 forms.put("links", form); 216 } 217 218 protected void setFieldsForm(Form form) { 219 this.fieldsForm = form; 220 forms.put("fields", form); 221 222 if (partForms.size() == 0) 223 activeFormName = "fields"; 224 } 225 226 protected void setMiscForm(Form form) { 227 this.miscForm = form; 228 forms.put("misc", form); 229 } 230 231 protected void setAdditionalPartsAndFieldsForm(Form form) { 232 this.additionalPartsAndFieldsForm = form; 233 forms.put("additionalPartsAndFields", form); 234 } 235 236 public boolean hasPartForms() { 237 return partForms.size() > 0; 238 } 239 240 public boolean hasFieldsForm() { 241 return fieldsForm != null; 242 } 243 244 public boolean hasAdditionalPartsOrFieldsForm() { 245 boolean hasAdditionalParts = ((Repeater)additionalPartsAndFieldsForm.getChild("additionalParts")).getSize() > 0; 246 boolean hasAdditionalFields = ((Repeater)additionalPartsAndFieldsForm.getChild("additionalFields")).getSize() > 0; 247 248 return hasAdditionalParts || hasAdditionalFields; 249 } 250 251 public List getPartFormInfos() { 252 return partFormInfos; 253 } 254 255 public static class PartFormInfo { 256 private String partFormName; 257 private String partLabel; 258 private String partDescription; 259 private boolean isRequired; 260 261 public PartFormInfo(String partFormName, String partLabel, String partDescription, boolean isRequired) { 262 this.partFormName = partFormName; 263 this.partLabel = partLabel; 264 this.partDescription = partDescription; 265 this.isRequired = isRequired; 266 } 267 268 public String getFormName() { 269 return partFormName; 270 } 271 272 public String getLabel() { 273 return partLabel; 274 } 275 276 public String getPartDescription() { 277 return partDescription; 278 } 279 280 public boolean isRequired() { 281 return isRequired; 282 } 283 } 284 285 public PartFormInfo getCurrentPartFormInfo() { 286 for (int i = 0; i < partFormInfos.size(); i++) { 287 PartFormInfo partFormInfo = (PartFormInfo)partFormInfos.get(i); 288 if (partFormInfo.getFormName().equals(activeFormName)) 289 return partFormInfo; 290 } 291 return null; 292 } 293 294 public String getDocumentName() { 295 return documentName; 296 } 297 298 public void setDocumentName(String name) { 299 this.documentName = name; 300 } 301 302 public boolean getValidateOnSave() { 303 return validateOnSave; 304 } 305 306 public void setValidateOnSave(boolean validateOnSave) { 307 this.validateOnSave = validateOnSave; 308 } 309 310 public boolean getPublishImmediately() { 311 return publishImmediately; 312 } 313 314 public void setPublishImmediately(boolean publishImmediately) { 315 this.publishImmediately = publishImmediately; 316 } 317 318 private boolean validateForm(String formName) throws Exception { 319 FormValidState state = (FormValidState)formValidState.get(formName); 320 if (state == null) 321 state = FormValidState.NOT_VALIDATED; 322 323 if (state == FormValidState.NOT_VALIDATED) { 324 Form form = getForm(formName); 325 form.endProcessing(false); 326 form.validate(); 327 formValidState.put(formName, form.isValid() ? FormValidState.VALID : FormValidState.NOT_VALID); 328 return form.isValid(); 329 } else { 330 return state == FormValidState.VALID; 331 } 332 } 333 334 public boolean isValid(String formName) throws Exception { 335 FormValidState state = (FormValidState)formValidState.get(formName); 336 if (state == null || state == FormValidState.NOT_VALIDATED) { 337 if (!hasBeenTriedToSave) { 338 return true; 339 } else if (activeFormName.equals(formName) && getActiveForm().getSubmitWidget() != null) { 340 return true; 341 } else { 342 return validateForm(formName); 343 } 344 } else if (state == FormValidState.VALID) { 345 return true; 346 } else if (state == FormValidState.NOT_VALID) { 347 return false; 348 } else { 349 throw new RuntimeException ("Unexpected situation, FormValidState == " + state); 350 } 351 } 352 353 356 public boolean arePartFormsValid() throws Exception { 357 Iterator partFormsIt = partForms.keySet().iterator(); 358 while (partFormsIt.hasNext()) { 359 String formName = (String )partFormsIt.next(); 360 if (!isValid(formName)) 361 return false; 362 } 363 return true; 364 } 365 366 static class FormValidState { 367 private final String name; 368 369 private FormValidState(String name) { 370 this.name = name; 371 } 372 373 public String toString() { 374 return name; 375 } 376 377 public static final FormValidState VALID = new FormValidState("valid"); 378 public static final FormValidState NOT_VALID = new FormValidState("not_valid"); 379 public static final FormValidState NOT_VALIDATED = new FormValidState("not_validated"); 380 } 381 } 382 | Popular Tags |