KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > editor > DocumentEditorForm


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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 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 /**
28  * Object representing the document editor form. It consists of multiple CForms.
29  * This object should be created by the {@link DocumentEditorFormBuilder}.
30  */

31 public class DocumentEditorForm {
32     /**
33      * Contains an instance of {@link PartFormInfo} for each part form.
34      */

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 JavaDoc documentName;
44     private boolean validateOnSave = true;
45     private boolean publishImmediately = false;
46     private String JavaDoc activeFormName;
47     /** Contains all forms hashed on name. */
48     private Map forms = new HashMap();
49     /**
50      * hasBeenTriedToSave is set to true after the first time the user pressed
51      * the 'save' button. It determines whether validation should be performed
52      * and whether validation error should be shown.
53      */

54     private boolean hasBeenTriedToSave = false;
55     /**
56      * A cache of what forms have already been validated. This is needed to avoid
57      * frequent re-validation, which can be expensive.
58      */

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         // document type and repository are only stored in this object so that others
68
// (eg form validators) have access to them
69
this.documentType = documentType;
70         this.repository = repository;
71         this.documentBranchId = documentBranchId;
72         this.documentLanguageId = documentLanguageId;
73
74         // Note: if there are parts of fields, then their forms will be set as
75
// initial form to display instead.
76
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 JavaDoc formName) throws Exception JavaDoc {
96         // Note: the activeForm request parameter contains the name of the form that
97
// should become active, while the formName argument contains the name
98
// of the form currently submitted.
99
String JavaDoc activeForm = request.getParameter("activeForm");
100         if (activeForm == null) {
101             throw new Exception JavaDoc("Missing request parameter: activeForm");
102         }
103         if (!forms.containsKey(activeForm)) {
104             throw new Exception JavaDoc("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         // Handle cross-editor fields (not managed by CForms)
118
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             // validate all forms
124
boolean allFormsValid = true;
125             Iterator formsIt = forms.keySet().iterator();
126             while (formsIt.hasNext()) {
127                 String JavaDoc currentFormName = (String JavaDoc)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 JavaDoc formName) throws Exception JavaDoc {
145         Form form = (Form)forms.get(formName);
146         if (form == null)
147             throw new Exception JavaDoc("Invalid form name: \"" + formName + "\".");
148         return form;
149     }
150
151     public Form getPartForm(String JavaDoc 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 JavaDoc formName) throws Exception JavaDoc {
176         if (!forms.containsKey(formName)) {
177             throw new Exception JavaDoc("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 JavaDoc getActiveFormName() {
187         return activeFormName;
188     }
189
190     public String JavaDoc getActiveFormTemplate() {
191         if (activeFormName.equals("fields")) {
192             return "cocoon:/FieldEditorFormTemplate";
193         } else if (activeFormName.startsWith("part-")) {
194             return (String JavaDoc)partFormTemplates.get(activeFormName);
195         } else {
196             return "resources/form/doceditor_" + activeFormName + "_template.xml";
197         }
198     }
199
200     protected void addPartForm(String JavaDoc partTypeName, Form form, String JavaDoc formTemplate, String JavaDoc partLabel, String JavaDoc partDescription, boolean isRequired) {
201         String JavaDoc 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             // this was the first part form, set it as initial form to show
209
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 JavaDoc partFormName;
257         private String JavaDoc partLabel;
258         private String JavaDoc partDescription;
259         private boolean isRequired;
260
261         public PartFormInfo(String JavaDoc partFormName, String JavaDoc partLabel, String JavaDoc partDescription, boolean isRequired) {
262             this.partFormName = partFormName;
263             this.partLabel = partLabel;
264             this.partDescription = partDescription;
265             this.isRequired = isRequired;
266         }
267
268         public String JavaDoc getFormName() {
269             return partFormName;
270         }
271
272         public String JavaDoc getLabel() {
273             return partLabel;
274         }
275
276         public String JavaDoc 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 JavaDoc getDocumentName() {
295         return documentName;
296     }
297
298     public void setDocumentName(String JavaDoc 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 JavaDoc formName) throws Exception JavaDoc {
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 JavaDoc formName) throws Exception JavaDoc {
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 JavaDoc("Unexpected situation, FormValidState == " + state);
350         }
351     }
352
353     /**
354      * Returns true if all part forms are valid. Useful to be called from the template.
355      */

356     public boolean arePartFormsValid() throws Exception JavaDoc {
357         Iterator partFormsIt = partForms.keySet().iterator();
358         while (partFormsIt.hasNext()) {
359             String JavaDoc formName = (String JavaDoc)partFormsIt.next();
360             if (!isValid(formName))
361                 return false;
362         }
363         return true;
364     }
365
366     static class FormValidState {
367         private final String JavaDoc name;
368
369         private FormValidState(String JavaDoc name) {
370             this.name = name;
371         }
372
373         public String JavaDoc 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