1 16 package org.outerj.daisy.frontend.editor; 17 18 import org.apache.cocoon.forms.formmodel.Form; 19 import org.apache.cocoon.forms.formmodel.Field; 20 import org.apache.cocoon.forms.formmodel.Repeater; 21 import org.apache.cocoon.forms.validation.ValidationErrorAware; 22 import org.apache.cocoon.forms.validation.ValidationError; 23 import org.apache.avalon.framework.service.ServiceManager; 24 import org.apache.avalon.framework.context.Context; 25 import org.apache.xmlbeans.XmlOptions; 26 import org.outerj.daisy.repository.schema.PartTypeUse; 27 import org.outerj.daisy.repository.Document; 28 import org.outerj.daisy.repository.Part; 29 import org.outerj.daisy.repository.Repository; 30 import org.outerj.daisy.frontend.util.FormHelper; 31 import org.outerj.daisy.books.frontend.BookAclEditorApple; 32 import org.outerj.daisy.books.store.BookAclBuilder; 33 import org.outerj.daisy.books.store.BookAcl; 34 35 import java.io.StringReader ; 36 import java.util.Map ; 37 38 public class BookAclPartEditor implements PartEditor { 39 private ServiceManager serviceManager; 40 41 private BookAclPartEditor(ServiceManager serviceManager) { 42 this.serviceManager = serviceManager; 43 } 44 45 public static class Factory implements PartEditorFactory { 46 public PartEditor getPartEditor(Map properties, ServiceManager serviceManager, Context context) { 47 return new BookAclPartEditor(serviceManager); 48 } 49 } 50 51 public Form getForm(PartTypeUse partTypeUse, DocumentEditorForm documentEditorForm, final Repository repository) throws Exception { 52 final Form form = FormHelper.createForm(serviceManager, "books/resources/form/bookacl_definition.xml"); 53 54 Field editMode = (Field)form.getChild("editmode"); 55 editMode.setValue("gui"); 56 editMode.addValueChangedListener(new EditModeListener(new EditModeListener.EditModeListenerConfig() { 57 public String getXmlFieldPath() { 58 return "editors/xml/xmlText"; 59 } 60 61 public String getXmlFromGuiEditor() { 62 return getBookAclXml(form); 63 } 64 65 public boolean loadGui(String xml) { 66 BookAcl bookAcl; 67 try { 68 bookAcl = BookAclBuilder.build(new StringReader (xml)); 69 } catch (Throwable e) { 70 ((ValidationErrorAware)form.lookupWidget(getXmlFieldPath())).setValidationError(new ValidationError(e.toString(), false)); 72 return false; 73 } 74 try { 75 BookAclEditorApple.load(form, bookAcl); 76 BookAclEditorApple.annotateAclSubjectValues(form, repository); 77 } catch (Throwable e) { 78 ((ValidationErrorAware)form.lookupWidget(getXmlFieldPath())).setValidationError(new ValidationError(e.toString(), false)); 80 return false; 81 } 82 return true; 83 } 84 85 public void clearGui() { 86 Repeater repeater = (Repeater)form.lookupWidget("editors/gui/entries"); 87 repeater.clear(); 88 } 89 })); 90 91 return form; 92 } 93 94 public String getFormTemplate() { 95 return "resources/form/parteditor_bookacl_template.xml"; 96 } 97 98 public void load(Form form, Document document, Part part, Repository repository) throws Exception { 99 byte[] data = part.getData(); 100 101 BookAcl bookAcl = null; 102 try { 103 bookAcl = BookAclBuilder.build(part.getDataStream()); 105 } catch (Throwable e) { 106 } 108 109 if (bookAcl != null) { 110 try { 111 BookAclEditorApple.load(form, bookAcl); 112 BookAclEditorApple.annotateAclSubjectValues(form, repository); 113 } catch (Throwable e) { 114 ((ValidationErrorAware)form.lookupWidget("editors/xml/xmlText")).setValidationError(new ValidationError(e.toString(), false)); 116 bookAcl = null; 117 } 118 } 119 120 if (bookAcl == null) { 121 form.setAttribute("ignore-editmode-change", Boolean.TRUE); 123 form.getChild("editmode").setValue("xml"); 124 form.lookupWidget("editors/xml/xmlText").setValue(new String (data, "UTF-8")); 126 } 127 } 128 129 private String getBookAclXml(Form form) { 130 BookAcl bookAcl = BookAclEditorApple.getBookAcl(form); 131 XmlOptions xmlOptions = new XmlOptions(); 132 xmlOptions.setSavePrettyPrint(); 133 xmlOptions.setCharacterEncoding("UTF-8"); 134 return bookAcl.getXml().xmlText(xmlOptions); 135 } 136 137 public void save(Form form, Document document) throws Exception { 138 Field editMode = (Field)form.getChild("editmode"); 139 if (editMode.getValue().equals("xml")) { 140 PartEditorHelper.save(form, document, "editors/xml/xmlText", "text/xml"); 141 } else if (editMode.getValue().equals("gui")) { 142 String xml = getBookAclXml(form); 143 PartEditorHelper.save(form, document, null, xml.getBytes("UTF-8"), "text/xml"); 144 } 145 } 146 } 147 | Popular Tags |