1 16 package org.outerj.daisy.frontend.editor; 17 18 import org.outerj.daisy.repository.schema.*; 19 import org.outerj.daisy.repository.Repository; 20 import org.outerj.daisy.frontend.util.FormHelper; 21 import org.outerj.daisy.frontend.util.XmlObjectXMLizable; 22 import org.apache.cocoon.forms.formmodel.*; 23 import org.apache.cocoon.forms.event.ActionListener; 24 import org.apache.cocoon.forms.event.ActionEvent; 25 import org.apache.cocoon.forms.FormManager; 26 import org.apache.cocoon.components.flow.util.PipelineUtil; 27 import org.apache.cocoon.components.LifecycleHelper; 28 import org.apache.avalon.framework.service.ServiceManager; 29 import org.apache.avalon.framework.context.Context; 30 import org.apache.avalon.framework.logger.Logger; 31 import org.apache.excalibur.xml.sax.XMLizable; 32 import org.apache.xmlbeans.XmlCursor; 33 import org.apache.xmlbeans.XmlObject; 34 35 import java.util.*; 36 37 40 public class DocumentEditorFormBuilder { 41 private DocumentType documentType; 42 private long docBranchId; 43 private long docLangId; 44 private ServiceManager serviceManager; 45 private Context context; 46 private Locale locale; 47 private DocumentEditorForm documentEditorForm; 48 private Repository repository; 49 private Logger logger; 50 51 private DocumentEditorFormBuilder(DocumentType documentType, long docBranchId, long docLangId, 52 ServiceManager serviceManager, Context context, Locale locale, Repository repository, Logger logger) { 53 this.documentType = documentType; 54 this.serviceManager = serviceManager; 55 this.context = context; 56 this.locale = locale; 57 this.repository = repository; 58 this.logger = logger; 59 this.docBranchId = docBranchId; 60 this.docLangId = docLangId; 61 } 62 63 public static DocumentEditorForm build(DocumentType documentType, long docBranchId, long docLangId, 64 ServiceManager serviceManager, Context context, Locale locale, Repository repository, Logger logger) throws Exception { 65 return new DocumentEditorFormBuilder(documentType, docBranchId, docLangId, serviceManager, context, locale, repository, logger).build(); 66 } 67 68 69 private DocumentEditorForm build() throws Exception { 70 documentEditorForm = new DocumentEditorForm(documentType, docBranchId, docLangId, repository); 71 72 Form linksForm = getLinksForm(); 73 Form miscForm = getMiscForm(); 74 Form additionalPartsAndFieldsForm = getAdditionalPartsAndFieldsForm(); 75 Form fieldsForm = getFieldsForm(); 76 77 documentEditorForm.setLinksForm(linksForm); 78 documentEditorForm.setMiscForm(miscForm); 79 documentEditorForm.setAdditionalPartsAndFieldsForm(additionalPartsAndFieldsForm); 80 if (fieldsForm != null) 81 documentEditorForm.setFieldsForm(fieldsForm); 82 83 PartTypeUse[] partTypeUses = documentType.getPartTypeUses(); 84 for (int i = 0; i < partTypeUses.length; i++) { 85 PartType partType = partTypeUses[i].getPartType(); 86 PartEditor partEditor = PartEditorManager.getPartEditor(partType.getName(), serviceManager, context, logger); 87 if (partEditor == null) { 88 if (partType.isDaisyHtml()) 89 partEditor = new HtmlPartEditor.Factory().getPartEditor(Collections.EMPTY_MAP, serviceManager, context); 90 else 91 partEditor = new UploadPartEditor.Factory().getPartEditor(Collections.EMPTY_MAP, serviceManager, context); 92 } 93 Form partEditorForm = partEditor.getForm(partTypeUses[i], documentEditorForm, repository); 94 partEditorForm.setAttribute("partType", partType); 95 partEditorForm.setAttribute("partEditor", partEditor); 96 documentEditorForm.addPartForm(partTypeUses[i].getPartType().getName(), partEditorForm, 97 partEditor.getFormTemplate(), 98 partTypeUses[i].getPartType().getLabel(locale), 99 partTypeUses[i].getPartType().getDescription(locale), 100 partTypeUses[i].isRequired()); 101 } 102 103 return documentEditorForm; 104 } 105 106 private Form getLinksForm() throws Exception { 107 final Form form = FormHelper.createForm(serviceManager, "resources/form/doceditor_links_definition.xml"); 108 109 Action addLink = (Action)form.getChild("addLink"); 110 addLink.addActionListener(new ActionListener() { 111 public void actionPerformed(ActionEvent event) { 112 Repeater links = (Repeater)form.getChild("links"); 113 links.addRow(); 114 } 115 }); 116 117 return form; 118 } 119 120 private Form getMiscForm() throws Exception { 121 final Form form = FormHelper.createForm(serviceManager, "resources/form/doceditor_misc_definition.xml"); 122 123 Action addCustomField = (Action)form.getChild("addCustomField"); 124 addCustomField.addActionListener(new ActionListener() { 125 public void actionPerformed(ActionEvent event) { 126 Repeater customFields = (Repeater)form.getChild("customFields"); 127 customFields.addRow(); 128 } 129 }); 130 131 return form; 132 } 133 134 private Form getAdditionalPartsAndFieldsForm() throws Exception { 135 final Form form = FormHelper.createForm(serviceManager, "resources/form/doceditor_additionalPartsAndFields_definition.xml"); 136 return form; 137 } 138 139 private Form getFieldsForm() throws Exception { 140 if (documentType.getFieldTypeUses().length == 0) 141 return null; 142 143 Form form = createFieldEditorForm(documentType); 144 form.addValidator(new CheckFieldsFormValidator(documentEditorForm)); 145 return form; 146 } 147 148 151 private Form createFieldEditorForm(DocumentType documentType) throws Exception { 152 FieldTypeUse[] fieldTypeUses = documentType.getFieldTypeUses(); 153 FieldInfo[] fieldInfos = new FieldInfo[fieldTypeUses.length]; 154 for (int i = 0; i < fieldTypeUses.length; i++) { 155 FieldType fieldType = fieldTypeUses[i].getFieldType(); 156 fieldInfos[i] = new FieldInfo(fieldTypeUses[i], FieldEditorManager.getFieldEditor(fieldType, serviceManager, context, logger), locale); 157 } 158 159 PipelineUtil pipelineUtil = new PipelineUtil(); 160 FormManager formManager = null; 161 try { 162 LifecycleHelper.setupComponent(pipelineUtil, null, context, serviceManager, null, false); 163 164 Map viewData = new HashMap(); 165 viewData.put("fieldInfos", fieldInfos); 166 167 org.w3c.dom.Document formDocument = pipelineUtil.processToDOM("FieldEditorFormDefinition", viewData); 168 169 formManager = (FormManager)serviceManager.lookup(FormManager.ROLE); 170 Form form = formManager.createForm(formDocument.getDocumentElement()); 171 172 form.setAttribute("fieldInfos", fieldInfos); 174 175 Iterator widgetIt = form.getChildren(); 177 while (widgetIt.hasNext()) { 178 Widget widget = (Widget)widgetIt.next(); 179 if (widget.getId().startsWith("field_")) { 180 long fieldTypeId = Long.parseLong(widget.getId().substring(6)); for (int i = 0; i < fieldInfos.length; i++) { 182 if (fieldInfos[i].getFieldType().getId() == fieldTypeId) { 183 widget.setAttribute("fieldEditor", fieldInfos[i].getFieldEditor()); 184 fieldInfos[i].getFieldEditor().init(widget, documentEditorForm); 185 break; 186 } 187 } 188 } 189 } 190 191 return form; 192 } finally { 193 LifecycleHelper.dispose(pipelineUtil); 194 if (formManager != null) 195 serviceManager.release(formManager); 196 } 197 } 198 199 public static class FieldInfo { 200 private FieldTypeUse fieldTypeUse; 201 private FieldEditor fieldEditor; 202 private XmlObject fieldTypeXml; 203 204 public FieldInfo(FieldTypeUse fieldTypeUse, FieldEditor fieldEditor, Locale locale) { 205 this.fieldTypeUse = fieldTypeUse; 206 this.fieldEditor = fieldEditor; 207 208 FieldType fieldType = fieldTypeUse.getFieldType(); 209 fieldTypeXml = fieldType.getXml(); 210 String label = fieldType.getLabel(locale); 211 String description = fieldType.getDescription(locale); 212 XmlCursor cursor = fieldTypeXml.newCursor(); 213 while (!cursor.isStart()) 214 cursor.toNextToken(); 215 cursor.toNextToken(); 216 cursor.insertAttributeWithValue("label", label); 217 if (description != null) 218 cursor.insertAttributeWithValue("description", description); 219 cursor.dispose(); 220 } 221 222 public FieldType getFieldType() { 223 return fieldTypeUse.getFieldType(); 224 } 225 226 public FieldTypeUse getFieldTypeUse() { 227 return fieldTypeUse; 228 } 229 230 public FieldEditor getFieldEditor() { 231 return fieldEditor; 232 } 233 234 public XMLizable getFieldTypeXml() { 235 return new XmlObjectXMLizable(fieldTypeXml); 236 } 237 } 238 } 239 | Popular Tags |