01: /*
02: * Copyright 2004 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.Form;
19: import org.apache.cocoon.forms.formmodel.Field;
20: import org.outerj.daisy.repository.Document;
21: import org.outerj.daisy.repository.Version;
22: import org.outerj.daisy.repository.Part;
23: import org.outerj.daisy.repository.schema.PartType;
24: import org.outerj.daisy.frontend.HtmlHelper;
25:
26: import java.util.Arrays;
27:
28: public class PartEditorHelper {
29:
30: public static void load(Form form, Part part, String fieldName)
31: throws Exception {
32: Field field = (Field) form.getChild(fieldName);
33: field.setValue(new String(part.getData(), "UTF-8"));
34: }
35:
36: public static void save(Form form, Document document,
37: String fieldName, String mimeType) throws Exception {
38: save(form, document, fieldName, null, mimeType);
39: }
40:
41: /**
42: *
43: * @param data alternative data instead of the field value, useful if the fields' value has to be postprocessed
44: */
45: public static void save(Form form, Document document,
46: String fieldName, byte[] data, String mimeType)
47: throws Exception {
48: Field field = fieldName != null ? (Field) form
49: .lookupWidget(fieldName) : null;
50: long partTypeId = ((PartType) form.getAttribute("partType"))
51: .getId();
52: if (field == null && data == null) {
53: throw new IllegalArgumentException(
54: "Either field or data argument should be non-null");
55: } else if (field != null
56: && (field.getValue() == null || HtmlHelper
57: .isEmpty((String) field.getValue()))) {
58: document.deletePart(partTypeId);
59: } else {
60: byte[] newData = data != null ? data : ((String) field
61: .getValue()).getBytes("UTF-8");
62:
63: if (newData == null) {
64: document.deletePart(partTypeId);
65: return;
66: }
67:
68: boolean mimeTypeChanged = false;
69: if (!document.isVariantNew()) {
70: // Compare new data with previous data, so that we don't store new data
71: // if it hasn't changed.
72: Version lastVersion = document.getLastVersion();
73: if (lastVersion.hasPart(partTypeId)) {
74: Part part = lastVersion.getPart(partTypeId);
75: mimeTypeChanged = !part.getMimeType().equals(
76: mimeType);
77: byte[] oldData = part.getData();
78: if (Arrays.equals(newData, oldData))
79: newData = null;
80: }
81: }
82:
83: if (newData != null)
84: document.setPart(partTypeId, mimeType, newData);
85: else if (mimeTypeChanged)
86: document.setPartMimeType(partTypeId, mimeType);
87: }
88: }
89: }
|