1 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) throws Exception { 31 Field field = (Field)form.getChild(fieldName); 32 field.setValue(new String (part.getData(), "UTF-8")); 33 } 34 35 public static void save(Form form, Document document, String fieldName, String mimeType) throws Exception { 36 save(form, document, fieldName, null, mimeType); 37 } 38 39 43 public static void save(Form form, Document document, String fieldName, byte[] data, String mimeType) throws Exception { 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 )field.getValue()))) { 47 document.deletePart(partTypeId); 48 } else { 49 byte[] newData = data != null ? data : ((String )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 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 |