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.*;
019: import org.outerj.daisy.repository.*;
020: import org.outerj.daisy.repository.schema.RepositorySchema;
021:
022: import java.util.*;
023:
024: public class DocumentBinding {
025: private static final String FIELD_PREFIX = "field_";
026:
027: /**
028: * This load should be executed only once on a form, since it registers validators too.
029: */
030: public static void load(DocumentEditorForm form, Document document,
031: Repository repository, Locale locale) throws Exception {
032: if (document.getLastVersion() != null
033: && document.getLastVersion().getSyncedWith() != null) {
034: form.setSyncedWithLanguageId(document.getLastVersion()
035: .getSyncedWith().getLanguageId());
036: form.setSyncedWithVersionId(document.getLastVersion()
037: .getSyncedWith().getVersionId());
038: } else {
039: form.setSyncedWithLanguageId(-1);
040: form.setSyncedWithVersionId(-1);
041: }
042:
043: RepositorySchema repositorySchema = repository
044: .getRepositorySchema();
045: // first load part and field data
046: Form additionalPartsAndFieldsForm = form
047: .getAdditionalPartsAndFieldsForm();
048: Repeater additionalParts = (Repeater) additionalPartsAndFieldsForm
049: .getChild("additionalParts");
050: Part[] parts = document.getParts().getArray();
051: for (int i = 0; i < parts.length; i++) {
052: Part part = parts[i];
053: Form partForm = form.getPartForm(part.getTypeName());
054: if (partForm == null) {
055: // it is a part that is present in the document but not in the document type
056: Repeater.RepeaterRow row = additionalParts.addRow();
057: row.getChild("typeId").setValue(
058: new Long(part.getTypeId()));
059: row.getChild("label").setValue(
060: repositorySchema.getPartTypeById(
061: part.getTypeId(), false).getLabel(
062: locale));
063: row.getChild("size").setValue(new Long(part.getSize()));
064: row.getChild("mimeType").setValue(part.getMimeType());
065: } else {
066: ((PartEditor) partForm.getAttribute("partEditor"))
067: .load(partForm, document, part, repository);
068: }
069: }
070:
071: Repeater additionalFields = (Repeater) additionalPartsAndFieldsForm
072: .getChild("additionalFields");
073: org.outerj.daisy.repository.Field[] fields = document
074: .getFields().getArray();
075: Form fieldsForm = form.getFieldsForm();
076: for (int i = 0; i < fields.length; i++) {
077: org.outerj.daisy.repository.Field field = fields[i];
078: String widgetId = FIELD_PREFIX + field.getTypeId();
079: Widget widget = fieldsForm != null ? fieldsForm
080: .getChild(widgetId) : null;
081: if (widget == null) {
082: // it is a field that exists in the document but not in the document type
083: Repeater.RepeaterRow row = additionalFields.addRow();
084: row.getChild("typeId").setValue(
085: new Long(field.getTypeId()));
086: row.getChild("label").setValue(
087: repositorySchema.getFieldTypeById(
088: field.getTypeId(), false).getLabel(
089: locale));
090: row.getChild("value").setValue(
091: FieldHelper.getFormattedValue(field.getValue(),
092: field.getValueType(), locale,
093: repository));
094: } else {
095: ((FieldEditor) widget.getAttribute("fieldEditor"))
096: .load(document);
097: }
098: }
099:
100: form.setDocumentName(document.getName());
101:
102: Form linksForm = form.getLinksForm();
103: Repeater linksRepeater = (Repeater) linksForm.getChild("links");
104: Link[] links = document.getLinks().getArray();
105: for (int i = 0; i < links.length; i++) {
106: Repeater.RepeaterRow row = linksRepeater.addRow();
107: row.getChild("title").setValue(links[i].getTitle());
108: row.getChild("target").setValue(links[i].getTarget());
109: }
110:
111: Form miscForm = form.getMiscForm();
112: miscForm.getChild("private").setValue(
113: Boolean.valueOf(document.isPrivate()));
114: miscForm.getChild("retired").setValue(
115: Boolean.valueOf(document.isRetired()));
116: miscForm.getChild("referenceLanguageId").setValue(
117: document.getReferenceLanguageId());
118:
119: Repeater customFieldsRepeater = (Repeater) miscForm
120: .getChild("customFields");
121: Iterator customFieldsIt = document.getCustomFields().entrySet()
122: .iterator();
123: while (customFieldsIt.hasNext()) {
124: Repeater.RepeaterRow row = customFieldsRepeater.addRow();
125: Map.Entry entry = (Map.Entry) customFieldsIt.next();
126: row.getChild("name").setValue(entry.getKey());
127: row.getChild("value").setValue(entry.getValue());
128: }
129:
130: DocumentCollection[] collections = document.getCollections()
131: .getArray();
132: Long[] collectionIds = new Long[collections.length];
133: for (int i = 0; i < collections.length; i++) {
134: collectionIds[i] = new Long(collections[i].getId());
135: }
136: miscForm.getChild("collections").setValue(collectionIds);
137: }
138:
139: public static void save(DocumentEditorForm form, Document document,
140: Repository repository) throws Exception {
141: // save part and field data
142: Form fieldsForm = form.getFieldsForm();
143: if (fieldsForm != null) {
144: Iterator childWidgets = fieldsForm.getChildren();
145: while (childWidgets.hasNext()) {
146: Widget widget = (Widget) childWidgets.next();
147: if (widget.getId().startsWith(FIELD_PREFIX)) {
148: FieldEditor fieldEditor = (FieldEditor) widget
149: .getAttribute("fieldEditor");
150: fieldEditor.store(document);
151: }
152: }
153: }
154:
155: Form[] partForms = form.getPartForms();
156: for (int i = 0; i < partForms.length; i++) {
157: ((PartEditor) partForms[i].getAttribute("partEditor"))
158: .save(partForms[i], document);
159: }
160:
161: // remove obsolete parts and fields
162: Form additionalPartsAndFieldsForm = form
163: .getAdditionalPartsAndFieldsForm();
164: Repeater additionalParts = (Repeater) additionalPartsAndFieldsForm
165: .getChild("additionalParts");
166: for (int i = 0; i < additionalParts.getSize(); i++) {
167: Repeater.RepeaterRow row = additionalParts.getRow(i);
168: if (row.getChild("delete").getValue().equals(Boolean.TRUE)) {
169: document.deletePart(((Long) row.getChild("typeId")
170: .getValue()).longValue());
171: }
172: }
173: Repeater additionalFields = (Repeater) additionalPartsAndFieldsForm
174: .getChild("additionalFields");
175: for (int i = 0; i < additionalFields.getSize(); i++) {
176: Repeater.RepeaterRow row = additionalFields.getRow(i);
177: if (row.getChild("delete").getValue().equals(Boolean.TRUE)) {
178: document.deleteField(((Long) row.getChild("typeId")
179: .getValue()).longValue());
180: }
181: }
182:
183: document.setName(form.getDocumentName());
184: document.setNewSyncedWith(form.getSyncedWithLanguageId(), form
185: .getSyncedWithVersionId());
186: document
187: .setNewChangeType(form.getMajorChange() ? ChangeType.MAJOR
188: : ChangeType.MINOR);
189: document.setNewChangeComment(form.getChangeComment());
190:
191: //
192: // Links
193: //
194:
195: // first check if any links changed
196: boolean linksNeedUpdating = false;
197: Form linksForm = form.getLinksForm();
198: Repeater linksRepeater = (Repeater) linksForm.getChild("links");
199: Link[] links = document.getLinks().getArray();
200: if (linksRepeater.getSize() != links.length) {
201: linksNeedUpdating = true;
202: } else {
203: for (int i = 0; i < linksRepeater.getSize(); i++) {
204: if (!links[i].getTitle().equals(
205: linksRepeater.getWidget(i, "title").getValue())) {
206: linksNeedUpdating = true;
207: break;
208: }
209: if (!links[i].getTarget()
210: .equals(
211: linksRepeater.getWidget(i, "target")
212: .getValue())) {
213: linksNeedUpdating = true;
214: break;
215: }
216: }
217: }
218:
219: // if there were any link changes, re-add all links
220: if (linksNeedUpdating) {
221: document.clearLinks();
222: for (int i = 0; i < linksRepeater.getSize(); i++) {
223: String title = (String) linksRepeater.getWidget(i,
224: "title").getValue();
225: String target = (String) linksRepeater.getWidget(i,
226: "target").getValue();
227: document.addLink(title, target);
228: }
229: }
230:
231: Form miscForm = form.getMiscForm();
232: document.setPrivate(((Boolean) miscForm.getChild("private")
233: .getValue()).booleanValue());
234: document.setRetired(((Boolean) miscForm.getChild("retired")
235: .getValue()).booleanValue());
236: document.setReferenceLanguageId((Long) miscForm.getChild(
237: "referenceLanguageId").getValue());
238:
239: //
240: // User fields
241: //
242:
243: // first check if any user fields changed
244: boolean customFieldsNeedUpdating = false;
245: Repeater customFieldRepeater = (Repeater) miscForm
246: .getChild("customFields");
247: Map customFields = document.getCustomFields();
248: if (customFields.size() != customFieldRepeater.getSize()) {
249: customFieldsNeedUpdating = true;
250: } else {
251: for (int i = 0; i < customFieldRepeater.getSize(); i++) {
252: String name = (String) customFieldRepeater.getWidget(i,
253: "name").getValue();
254: String value = (String) customFieldRepeater.getWidget(
255: i, "value").getValue();
256: if (!value.equals(customFields.get(name))) {
257: customFieldsNeedUpdating = true;
258: break;
259: }
260: }
261: }
262:
263: // if there were any user field changes, re-add them all
264: if (customFieldsNeedUpdating) {
265: document.clearCustomFields();
266: for (int i = 0; i < customFieldRepeater.getSize(); i++) {
267: String name = (String) customFieldRepeater.getWidget(i,
268: "name").getValue();
269: String value = (String) customFieldRepeater.getWidget(
270: i, "value").getValue();
271: document.setCustomField(name, value);
272: }
273: }
274:
275: //
276: // Collections
277: //
278:
279: // first check if there were any collections changes
280: boolean collectionsNeedUpdating = false;
281: Object[] collectionIds = (Object[]) miscForm.getChild(
282: "collections").getValue();
283: DocumentCollection[] collections = document.getCollections()
284: .getArray();
285: if (collectionIds.length != collections.length) {
286: collectionsNeedUpdating = true;
287: } else {
288: for (int i = 0; i < collections.length; i++) {
289: boolean found = false;
290: for (int j = 0; j < collectionIds.length; j++) {
291: if (((Long) collectionIds[j]).longValue() == collections[i]
292: .getId()) {
293: found = true;
294: break;
295: }
296: }
297: if (!found) {
298: collectionsNeedUpdating = true;
299: break;
300: }
301: }
302: }
303:
304: // if there were any collection changes, re-add them all
305: if (collectionsNeedUpdating) {
306: CollectionManager collectionManager = repository
307: .getCollectionManager();
308: document.clearCollections();
309: for (int i = 0; i < collectionIds.length; i++) {
310: document.addToCollection(collectionManager
311: .getCollection(((Long) collectionIds[i])
312: .longValue(), false));
313: }
314: }
315:
316: // New version state
317: if (form.getPublishImmediately()) {
318: document.setNewVersionState(VersionState.PUBLISH);
319: } else {
320: document.setNewVersionState(VersionState.DRAFT);
321: }
322:
323: if (form.getMajorChange()) {
324: document.setNewChangeType(ChangeType.MAJOR);
325: } else {
326: document.setNewChangeType(ChangeType.MINOR);
327: }
328:
329: document.setNewChangeComment(form.getChangeComment());
330:
331: }
332:
333: }
|