KickJava   Java API By Example, From Geeks To Geeks.

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


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.Upload;
20 import org.apache.cocoon.forms.formmodel.Field;
21 import org.apache.cocoon.forms.formmodel.Widget;
22 import org.apache.cocoon.forms.validation.WidgetValidator;
23 import org.apache.cocoon.forms.validation.ValidationError;
24 import org.apache.cocoon.forms.util.I18nMessage;
25 import org.apache.cocoon.forms.FormsConstants;
26 import org.apache.cocoon.forms.event.*;
27 import org.apache.avalon.framework.service.ServiceManager;
28 import org.apache.avalon.framework.context.Context;
29 import org.outerj.daisy.frontend.util.FormHelper;
30 import org.outerj.daisy.frontend.DaisyException;
31 import org.outerj.daisy.frontend.RequestUtil;
32 import org.outerj.daisy.repository.Part;
33 import org.outerj.daisy.repository.Document;
34 import org.outerj.daisy.repository.Repository;
35 import org.outerj.daisy.repository.schema.PartType;
36 import org.outerj.daisy.repository.schema.PartTypeUse;
37
38 import java.util.Map JavaDoc;
39 import java.util.HashMap JavaDoc;
40
41 public class UploadPartEditor implements PartEditor {
42     private final ServiceManager serviceManager;
43
44     private UploadPartEditor(ServiceManager serviceManager) {
45         this.serviceManager = serviceManager;
46     }
47
48     public static class Factory implements PartEditorFactory {
49         public PartEditor getPartEditor(Map JavaDoc properties, ServiceManager serviceManager, Context context) {
50             return new UploadPartEditor(serviceManager);
51         }
52     }
53
54     public Form getForm(PartTypeUse partTypeUse, DocumentEditorForm documentEditorForm, Repository repository) throws Exception JavaDoc {
55         final Form form = FormHelper.createForm(serviceManager, "resources/form/parteditor_upload_definition.xml");
56         form.addValidator(new UploadValidator(documentEditorForm, partTypeUse.isRequired()));
57
58         Upload upload = (Upload)form.getChild("upload-part");
59         upload.addValueChangedListener(new ValueChangedListener() {
60             public void valueChanged(ValueChangedEvent valueChangedEvent) {
61                 org.apache.cocoon.servlet.multipart.Part part = (org.apache.cocoon.servlet.multipart.Part)valueChangedEvent.getSourceWidget().getValue();
62                 if (part != null) {
63                     form.getChild("upload-part-mimetype").setValue(part.getMimeType());
64                     form.getChild("upload-part-filename").setValue(RequestUtil.removePathFromUploadFileName(part.getUploadName()));
65                 }
66             }
67         });
68
69         return form;
70     }
71
72     public String JavaDoc getFormTemplate() {
73         return "resources/form/parteditor_upload_template.xml";
74     }
75
76     public void load(Form form, Document document, Part part, Repository repository) throws Exception JavaDoc {
77         Upload upload = (Upload)form.getChild("upload-part");
78
79         Map JavaDoc headers = new HashMap JavaDoc();
80         headers.put("filename", "existing data");
81         upload.setValue(new ExistingPart(headers));
82         form.getChild("upload-part-filename").setValue(part.getFileName());
83         form.getChild("upload-part-mimetype").setValue(part.getMimeType());
84     }
85
86     public void save(Form form, Document document) throws Exception JavaDoc {
87         Upload upload = (Upload)form.getChild("upload-part");
88         long partTypeId = ((PartType)form.getAttribute("partType")).getId();
89         org.apache.cocoon.servlet.multipart.Part part = (org.apache.cocoon.servlet.multipart.Part)upload.getValue();
90         if (part != null) {
91             if (!(part instanceof ExistingPart)) {
92                 if (part.getSize() < 0)
93                     throw new DaisyException("Uploaded part has a negative size: " + part.getSize());
94                 document.setPart(partTypeId, part.getMimeType(), new UploadPartDataSource(part));
95             }
96             Field mimeTypeField = (Field)form.getChild("upload-part-mimetype");
97             Field fileNameField = (Field)form.getChild("upload-part-filename");
98             document.setPartFileName(partTypeId, (String JavaDoc)fileNameField.getValue());
99             document.setPartMimeType(partTypeId, (String JavaDoc)mimeTypeField.getValue());
100         } else {
101             document.deletePart(partTypeId);
102         }
103     }
104
105     class UploadValidator implements WidgetValidator {
106         private DocumentEditorForm documentEditorForm;
107         private boolean isRequired;
108
109         public UploadValidator(DocumentEditorForm documentEditorForm, boolean isRequired) {
110             this.documentEditorForm = documentEditorForm;
111             this.isRequired = isRequired;
112         }
113
114         public boolean validate(Widget widget) {
115             boolean success = true;
116             if (documentEditorForm.getValidateOnSave() && isRequired) {
117                 Form form = widget.getForm();
118                 Upload upload = (Upload)form.getChild("upload-part");
119                 Field partMimeType = (Field)form.getChild("upload-part-mimetype");
120                 if (upload.getValue() == null) {
121                     upload.setValidationError(new ValidationError("editdoc.part-required", true));
122                     success = false;
123                 } else if (upload.getValue() != null && partMimeType.getValue() == null) {
124                     partMimeType.setValidationError(new ValidationError(new I18nMessage("general.field-required", FormsConstants.I18N_CATALOGUE)));
125                     success = false;
126                 }
127             }
128             return success;
129         }
130     }
131
132 }
133
Popular Tags