001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.frontend.editor;
017:
018: import org.apache.cocoon.forms.formmodel.Form;
019: import org.apache.cocoon.forms.formmodel.Upload;
020: import org.apache.cocoon.forms.formmodel.Field;
021: import org.apache.cocoon.forms.formmodel.Widget;
022: import org.apache.cocoon.forms.validation.WidgetValidator;
023: import org.apache.cocoon.forms.validation.ValidationError;
024: import org.apache.cocoon.forms.util.I18nMessage;
025: import org.apache.cocoon.forms.FormsConstants;
026: import org.apache.cocoon.forms.event.*;
027: import org.apache.avalon.framework.service.ServiceManager;
028: import org.apache.avalon.framework.context.Context;
029: import org.outerj.daisy.frontend.util.FormHelper;
030: import org.outerj.daisy.frontend.DaisyException;
031: import org.outerj.daisy.frontend.RequestUtil;
032: import org.outerj.daisy.repository.Part;
033: import org.outerj.daisy.repository.Document;
034: import org.outerj.daisy.repository.Repository;
035: import org.outerj.daisy.repository.schema.PartType;
036: import org.outerj.daisy.repository.schema.PartTypeUse;
037:
038: import java.util.Map;
039: import java.util.HashMap;
040:
041: public class UploadPartEditor implements PartEditor {
042: private final ServiceManager serviceManager;
043:
044: private UploadPartEditor(ServiceManager serviceManager) {
045: this .serviceManager = serviceManager;
046: }
047:
048: public static class Factory implements PartEditorFactory {
049: public PartEditor getPartEditor(Map properties,
050: ServiceManager serviceManager, Context context) {
051: return new UploadPartEditor(serviceManager);
052: }
053: }
054:
055: public Form getForm(PartTypeUse partTypeUse,
056: DocumentEditorForm documentEditorForm, Repository repository)
057: throws Exception {
058: final Form form = FormHelper.createForm(serviceManager,
059: "resources/form/parteditor_upload_definition.xml");
060: form.addValidator(new UploadValidator(documentEditorForm,
061: partTypeUse.isRequired()));
062:
063: Upload upload = (Upload) form.getChild("upload-part");
064: upload.addValueChangedListener(new ValueChangedListener() {
065: public void valueChanged(ValueChangedEvent valueChangedEvent) {
066: org.apache.cocoon.servlet.multipart.Part part = (org.apache.cocoon.servlet.multipart.Part) valueChangedEvent
067: .getSourceWidget().getValue();
068: if (part != null) {
069: form.getChild("upload-part-mimetype").setValue(
070: part.getMimeType());
071: form.getChild("upload-part-filename").setValue(
072: RequestUtil
073: .removePathFromUploadFileName(part
074: .getUploadName()));
075: }
076: }
077: });
078:
079: return form;
080: }
081:
082: public String getFormTemplate() {
083: return "resources/form/parteditor_upload_template.xml";
084: }
085:
086: public void load(Form form, Document document, Part part,
087: Repository repository) throws Exception {
088: Upload upload = (Upload) form.getChild("upload-part");
089:
090: Map headers = new HashMap();
091: headers.put("filename", "existing data");
092: upload.setValue(new ExistingPart(headers));
093: form.getChild("upload-part-filename").setValue(
094: part.getFileName());
095: form.getChild("upload-part-mimetype").setValue(
096: part.getMimeType());
097: }
098:
099: public void save(Form form, Document document) throws Exception {
100: Upload upload = (Upload) form.getChild("upload-part");
101: long partTypeId = ((PartType) form.getAttribute("partType"))
102: .getId();
103: org.apache.cocoon.servlet.multipart.Part part = (org.apache.cocoon.servlet.multipart.Part) upload
104: .getValue();
105: if (part != null) {
106: if (!(part instanceof ExistingPart)) {
107: if (part.getSize() < 0)
108: throw new DaisyException(
109: "Uploaded part has a negative size: "
110: + part.getSize());
111: document.setPart(partTypeId, part.getMimeType(),
112: new UploadPartDataSource(part));
113: }
114: Field mimeTypeField = (Field) form
115: .getChild("upload-part-mimetype");
116: Field fileNameField = (Field) form
117: .getChild("upload-part-filename");
118: document.setPartFileName(partTypeId, (String) fileNameField
119: .getValue());
120: document.setPartMimeType(partTypeId, (String) mimeTypeField
121: .getValue());
122: } else {
123: document.deletePart(partTypeId);
124: }
125: }
126:
127: class UploadValidator implements WidgetValidator {
128: private DocumentEditorForm documentEditorForm;
129: private boolean isRequired;
130:
131: public UploadValidator(DocumentEditorForm documentEditorForm,
132: boolean isRequired) {
133: this .documentEditorForm = documentEditorForm;
134: this .isRequired = isRequired;
135: }
136:
137: public boolean validate(Widget widget) {
138: boolean success = true;
139: if (documentEditorForm.getValidateOnSave() && isRequired) {
140: Form form = widget.getForm();
141: Upload upload = (Upload) form.getChild("upload-part");
142: Field partMimeType = (Field) form
143: .getChild("upload-part-mimetype");
144: if (upload.getValue() == null) {
145: upload.setValidationError(new ValidationError(
146: "editdoc.part-required", true));
147: success = false;
148: } else if (upload.getValue() != null
149: && partMimeType.getValue() == null) {
150: partMimeType
151: .setValidationError(new ValidationError(
152: new I18nMessage(
153: "general.field-required",
154: FormsConstants.I18N_CATALOGUE)));
155: success = false;
156: }
157: }
158: return success;
159: }
160: }
161:
162: }
|