KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > editor > DocumentBinding


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.frontend.editor;
17
18 import org.apache.cocoon.forms.formmodel.*;
19 import org.outerj.daisy.repository.*;
20 import org.outerj.daisy.repository.schema.RepositorySchema;
21
22 import java.util.*;
23
24 public class DocumentBinding {
25     private static final String JavaDoc FIELD_PREFIX = "field_";
26
27     /**
28      * This load should be executed only once on a form, since it registers validators too.
29      */

30     public static void load(DocumentEditorForm form, Document document, Repository repository, Locale locale) throws Exception JavaDoc {
31         RepositorySchema repositorySchema = repository.getRepositorySchema();
32         // first load part and field data
33
Form additionalPartsAndFieldsForm = form.getAdditionalPartsAndFieldsForm();
34         Repeater additionalParts = (Repeater)additionalPartsAndFieldsForm.getChild("additionalParts");
35         Part[] parts = document.getParts().getArray();
36         for (int i = 0; i < parts.length; i++) {
37             Part part = parts[i];
38             Form partForm = form.getPartForm(part.getTypeName());
39             if (partForm == null) {
40                 // it is a part that is present in the document but not in the document type
41
Repeater.RepeaterRow row = additionalParts.addRow();
42                 row.getChild("typeId").setValue(new Long JavaDoc(part.getTypeId()));
43                 row.getChild("label").setValue(repositorySchema.getPartTypeById(part.getTypeId(), false).getLabel(locale));
44                 row.getChild("size").setValue(new Long JavaDoc(part.getSize()));
45                 row.getChild("mimeType").setValue(part.getMimeType());
46             } else {
47                 ((PartEditor)partForm.getAttribute("partEditor")).load(partForm, document, part, repository);
48             }
49         }
50
51         Repeater additionalFields = (Repeater)additionalPartsAndFieldsForm.getChild("additionalFields");
52         org.outerj.daisy.repository.Field[] fields = document.getFields().getArray();
53         Form fieldsForm = form.getFieldsForm();
54         for (int i = 0; i < fields.length; i++) {
55             org.outerj.daisy.repository.Field field = fields[i];
56             String JavaDoc widgetId = FIELD_PREFIX + field.getTypeId();
57             Widget widget = fieldsForm != null ? fieldsForm.getChild(widgetId) : null;
58             if (widget == null) {
59                 // it is a field that exists in the document but not in the document type
60
Repeater.RepeaterRow row = additionalFields.addRow();
61                 row.getChild("typeId").setValue(new Long JavaDoc(field.getTypeId()));
62                 row.getChild("label").setValue(repositorySchema.getFieldTypeById(field.getTypeId(), false).getLabel(locale));
63                 row.getChild("value").setValue(FieldHelper.getFormattedValue(field.getValue(), field.getValueType(), locale, repository));
64             } else {
65                 ((FieldEditor)widget.getAttribute("fieldEditor")).load(fieldsForm, field, document, repository);
66             }
67         }
68
69         form.setDocumentName(document.getName());
70
71         Form linksForm = form.getLinksForm();
72         Repeater linksRepeater = (Repeater)linksForm.getChild("links");
73         Link[] links = document.getLinks().getArray();
74         for (int i = 0; i < links.length; i++) {
75             Repeater.RepeaterRow row = linksRepeater.addRow();
76             row.getChild("title").setValue(links[i].getTitle());
77             row.getChild("target").setValue(links[i].getTarget());
78         }
79
80         Form miscForm = form.getMiscForm();
81         miscForm.getChild("private").setValue(Boolean.valueOf(document.isPrivate()));
82         miscForm.getChild("retired").setValue(Boolean.valueOf(document.isRetired()));
83
84         Repeater customFieldsRepeater = (Repeater)miscForm.getChild("customFields");
85         Iterator customFieldsIt = document.getCustomFields().entrySet().iterator();
86         while (customFieldsIt.hasNext()) {
87             Repeater.RepeaterRow row = customFieldsRepeater.addRow();
88             Map.Entry entry = (Map.Entry)customFieldsIt.next();
89             row.getChild("name").setValue(entry.getKey());
90             row.getChild("value").setValue(entry.getValue());
91         }
92
93         DocumentCollection[] collections = document.getCollections().getArray();
94         Long JavaDoc[] collectionIds = new Long JavaDoc[collections.length];
95         for (int i = 0; i < collections.length; i++) {
96             collectionIds[i] = new Long JavaDoc(collections[i].getId());
97         }
98         miscForm.getChild("collections").setValue(collectionIds);
99     }
100
101     public static void save(DocumentEditorForm form, Document document, Repository repository) throws Exception JavaDoc {
102         // save part and field data
103
Form fieldsForm = form.getFieldsForm();
104         if (fieldsForm != null) {
105             Iterator childWidgets = fieldsForm.getChildren();
106             while (childWidgets.hasNext()) {
107                 Widget widget = (Widget)childWidgets.next();
108                 if (widget.getId().startsWith(FIELD_PREFIX)) {
109                     FieldEditor fieldEditor = (FieldEditor)widget.getAttribute("fieldEditor");
110                     fieldEditor.save(fieldsForm, document, repository);
111                 }
112             }
113         }
114
115         Form[] partForms = form.getPartForms();
116         for (int i = 0; i < partForms.length; i++) {
117             ((PartEditor)partForms[i].getAttribute("partEditor")).save(partForms[i], document);
118         }
119
120         // remove obsolete parts and fields
121
Form additionalPartsAndFieldsForm = form.getAdditionalPartsAndFieldsForm();
122         Repeater additionalParts = (Repeater)additionalPartsAndFieldsForm.getChild("additionalParts");
123         for (int i = 0; i < additionalParts.getSize(); i++) {
124             Repeater.RepeaterRow row = additionalParts.getRow(i);
125             if (row.getChild("delete").getValue().equals(Boolean.TRUE)) {
126                 document.deletePart(((Long JavaDoc)row.getChild("typeId").getValue()).longValue());
127             }
128         }
129         Repeater additionalFields = (Repeater)additionalPartsAndFieldsForm.getChild("additionalFields");
130         for (int i = 0; i < additionalFields.getSize(); i++) {
131             Repeater.RepeaterRow row = additionalFields.getRow(i);
132             if (row.getChild("delete").getValue().equals(Boolean.TRUE)) {
133                 document.deleteField(((Long JavaDoc)row.getChild("typeId").getValue()).longValue());
134             }
135         }
136
137         document.setName(form.getDocumentName());
138
139         //
140
// Links
141
//
142

143         // first check if any links changed
144
boolean linksNeedUpdating = false;
145         Form linksForm = form.getLinksForm();
146         Repeater linksRepeater = (Repeater)linksForm.getChild("links");
147         Link[] links = document.getLinks().getArray();
148         if (linksRepeater.getSize() != links.length) {
149             linksNeedUpdating = true;
150         } else {
151             for (int i = 0; i < linksRepeater.getSize(); i++) {
152                 if (!links[i].getTitle().equals(linksRepeater.getWidget(i, "title").getValue())) {
153                     linksNeedUpdating = true;
154                     break;
155                 }
156                 if (!links[i].getTarget().equals(linksRepeater.getWidget(i, "target").getValue())) {
157                     linksNeedUpdating = true;
158                     break;
159                 }
160             }
161         }
162
163         // if there were any link changes, re-add all links
164
if (linksNeedUpdating) {
165             document.clearLinks();
166             for (int i = 0; i < linksRepeater.getSize(); i++) {
167                 String JavaDoc title = (String JavaDoc)linksRepeater.getWidget(i, "title").getValue();
168                 String JavaDoc target = (String JavaDoc)linksRepeater.getWidget(i, "target").getValue();
169                 document.addLink(title, target);
170             }
171         }
172
173
174         Form miscForm = form.getMiscForm();
175         document.setPrivate(((Boolean JavaDoc)miscForm.getChild("private").getValue()).booleanValue());
176         document.setRetired(((Boolean JavaDoc)miscForm.getChild("retired").getValue()).booleanValue());
177
178         //
179
// User fields
180
//
181

182         // first check if any user fields changed
183
boolean customFieldsNeedUpdating = false;
184         Repeater customFieldRepeater = (Repeater)miscForm.getChild("customFields");
185         Map customFields = document.getCustomFields();
186         if (customFields.size() != customFieldRepeater.getSize()) {
187             customFieldsNeedUpdating = true;
188         } else {
189             for (int i = 0; i < customFieldRepeater.getSize(); i++) {
190                 String JavaDoc name = (String JavaDoc)customFieldRepeater.getWidget(i, "name").getValue();
191                 String JavaDoc value = (String JavaDoc)customFieldRepeater.getWidget(i, "value").getValue();
192                 if (!value.equals(customFields.get(name))) {
193                     customFieldsNeedUpdating = true;
194                     break;
195                 }
196             }
197         }
198
199         // if there were any user field changes, re-add them all
200
if (customFieldsNeedUpdating) {
201             document.clearCustomFields();
202             for (int i = 0; i < customFieldRepeater.getSize(); i++) {
203                 String JavaDoc name = (String JavaDoc)customFieldRepeater.getWidget(i, "name").getValue();
204                 String JavaDoc value = (String JavaDoc)customFieldRepeater.getWidget(i, "value").getValue();
205                 document.setCustomField(name, value);
206             }
207         }
208
209         //
210
// Collections
211
//
212

213         // first check if there were any collections changes
214
boolean collectionsNeedUpdating = false;
215         Object JavaDoc[] collectionIds = (Object JavaDoc[])miscForm.getChild("collections").getValue();
216         DocumentCollection[] collections = document.getCollections().getArray();
217         if (collectionIds.length != collections.length) {
218             collectionsNeedUpdating = true;
219         } else {
220             for (int i = 0; i < collections.length; i++) {
221                 boolean found = false;
222                 for (int j = 0; j < collectionIds.length; j++) {
223                     if (((Long JavaDoc)collectionIds[j]).longValue() == collections[i].getId()) {
224                         found = true;
225                         break;
226                     }
227                 }
228                 if (!found) {
229                     collectionsNeedUpdating = true;
230                     break;
231                 }
232             }
233         }
234
235         // if there were any collection changes, re-add them all
236
if (collectionsNeedUpdating) {
237             CollectionManager collectionManager = repository.getCollectionManager();
238             document.clearCollections();
239             for (int i = 0; i < collectionIds.length; i++) {
240                 document.addToCollection(collectionManager.getCollection(((Long JavaDoc)collectionIds[i]).longValue(), false));
241             }
242         }
243
244         // New version state
245
if (form.getPublishImmediately()) {
246             document.setNewVersionState(VersionState.PUBLISH);
247         } else {
248             document.setNewVersionState(VersionState.DRAFT);
249         }
250
251     }
252
253 }
254
Popular Tags