1 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 ; 39 import java.util.HashMap ; 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 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 { 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 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 { 77 Upload upload = (Upload)form.getChild("upload-part"); 78 79 Map headers = new HashMap (); 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 { 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 )fileNameField.getValue()); 99 document.setPartMimeType(partTypeId, (String )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 |