KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
27
28 public class PartEditorHelper {
29
30     public static void load(Form form, Part part, String JavaDoc fieldName) throws Exception JavaDoc {
31         Field field = (Field)form.getChild(fieldName);
32         field.setValue(new String JavaDoc(part.getData(), "UTF-8"));
33     }
34
35     public static void save(Form form, Document document, String JavaDoc fieldName, String JavaDoc mimeType) throws Exception JavaDoc {
36         save(form, document, fieldName, null, mimeType);
37     }
38
39     /**
40      *
41      * @param data alternative data instead of the field value, useful if the fields' value has to be postprocessed
42      */

43     public static void save(Form form, Document document, String JavaDoc fieldName, byte[] data, String JavaDoc mimeType) throws Exception JavaDoc {
44         Field field = fieldName != null ? (Field)form.lookupWidget(fieldName) : null;
45         long partTypeId = ((PartType)form.getAttribute("partType")).getId();
46         if (field != null && (field.getValue() == null || HtmlHelper.isEmpty((String JavaDoc)field.getValue()))) {
47             document.deletePart(partTypeId);
48         } else {
49             byte[] newData = data != null ? data : ((String JavaDoc)field.getValue()).getBytes("UTF-8");
50
51             if (newData == null) {
52                 document.deletePart(partTypeId);
53                 return;
54             }
55
56             boolean mimeTypeChanged = false;
57             if (!document.isVariantNew()) {
58                 // Compare new data with previous data, so that we don't store new data
59
// if it hasn't changed.
60
Version lastVersion = document.getLastVersion();
61                 if (lastVersion.hasPart(partTypeId)) {
62                     Part part = lastVersion.getPart(partTypeId);
63                     mimeTypeChanged = !part.getMimeType().equals(mimeType);
64                     byte[] oldData = part.getData();
65                     if (Arrays.equals(newData, oldData))
66                         newData = null;
67                 }
68             }
69
70             if (newData != null)
71                 document.setPart(partTypeId, mimeType, newData);
72             else if (mimeTypeChanged)
73                 document.setPartMimeType(partTypeId, mimeType);
74         }
75     }
76 }
77
Popular Tags