Author: glandais Date: 2007-12-06 18:32:23 +0000 (Thu, 06 Dec 2007) New Revision: 48 Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/AttachmentHandler.java trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/fs/ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/fs/FileSystemAttachmentHandler.java trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/type/ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/type/ContentType.java trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/type/ContentTypeFactory.java trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/type/RawType.java Log: Gestion du contenu attach?\195?\169 Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/AttachmentHandler.java =================================================================== --- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/AttachmentHandler.java (rev 0) +++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/AttachmentHandler.java 2007-12-06 18:32:23 UTC (rev 48) @@ -0,0 +1,18 @@ +package org.cemagref.simexplorer.si.storage.attachment; + +import java.io.InputStream; +import java.io.OutputStream; + +import org.cemagref.simexplorer.si.storage.entities.FileEntity; + +public abstract class AttachmentHandler { + + public abstract void storeData(FileEntity entity, String field, + InputStream is) throws Exception; + + public abstract InputStream retrieveData(FileEntity entity, String field) + throws Exception; + + public abstract void deleteData(FileEntity entity, String field) throws Exception; + +} \ No newline at end of file Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/fs/FileSystemAttachmentHandler.java =================================================================== --- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/fs/FileSystemAttachmentHandler.java (rev 0) +++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/fs/FileSystemAttachmentHandler.java 2007-12-06 18:32:23 UTC (rev 48) @@ -0,0 +1,72 @@ +package org.cemagref.simexplorer.si.storage.attachment.fs; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStream; + +import org.cemagref.simexplorer.si.storage.attachment.AttachmentHandler; +import org.cemagref.simexplorer.si.storage.entities.FileEntity; +import org.cemagref.simexplorer.si.storage.entities.LoggableElement; + +public class FileSystemAttachmentHandler extends AttachmentHandler { + + private static String baseFolder = "./data/"; + + private File getFile(FileEntity entity, String field) throws Exception { + String resultPath = baseFolder; + + LoggableElement loggableElement = null; + if (entity instanceof LoggableElement) { + loggableElement = (LoggableElement) entity; + } else { + loggableElement = entity.getOwner(); + } + + resultPath = resultPath + loggableElement.getUuid() + "/" + + loggableElement.getVersion().toString() + "/" + field; + + File result = new File(resultPath); + File resultFolder = new File(result.getParent()); + if (!resultFolder.exists()) { + resultFolder.mkdirs(); + //result.createNewFile(); + } + + return result; + } + + @Override + public InputStream retrieveData(FileEntity entity, String field) + throws Exception { + FileInputStream fis = new FileInputStream(getFile(entity, field)); + return fis; + } + + @Override + public void storeData(FileEntity entity, String field, InputStream is) + throws Exception { + FileOutputStream fos = new FileOutputStream(getFile(entity, field)); + + BufferedInputStream bin = new BufferedInputStream(is); + BufferedOutputStream bout = new BufferedOutputStream(fos); + + while (true) { + int datum = bin.read(); + if (datum == -1) + break; + bout.write(datum); + } + bout.flush(); + + fos.close(); + } + + @Override + public void deleteData(FileEntity entity, String field) throws Exception { + getFile(entity, field).delete(); + } + +} Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/type/ContentType.java =================================================================== --- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/type/ContentType.java (rev 0) +++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/type/ContentType.java 2007-12-06 18:32:23 UTC (rev 48) @@ -0,0 +1,28 @@ +package org.cemagref.simexplorer.si.storage.attachment.type; + +import java.io.InputStream; + +public abstract class ContentType { + + private String mimeType; + private String description; + + public abstract InputStream renderToText(InputStream is); + + public String getMimeType() { + return mimeType; + } + + public void setMimeType(String mimeType) { + this.mimeType = mimeType; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + +} Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/type/ContentTypeFactory.java =================================================================== --- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/type/ContentTypeFactory.java (rev 0) +++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/type/ContentTypeFactory.java 2007-12-06 18:32:23 UTC (rev 48) @@ -0,0 +1,22 @@ +package org.cemagref.simexplorer.si.storage.attachment.type; + +import java.util.HashMap; +import java.util.Map; + +public class ContentTypeFactory { + + private static Map<String, ContentType> contentTypes = null; + + public static ContentType getContentTypeInstance(Class<? extends ContentType> contentTypeClass) throws Exception { + if (contentTypes == null) { + contentTypes = new HashMap<String, ContentType>(); + } + ContentType result = contentTypes.get(contentTypeClass.getSimpleName()); + if (result == null) { + result = contentTypeClass.newInstance(); + contentTypes.put(contentTypeClass.getSimpleName(), result); + } + return result; + } + +} Added: trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/type/RawType.java =================================================================== --- trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/type/RawType.java (rev 0) +++ trunk/simexplorer-si-storage/src/java/org/cemagref/simexplorer/si/storage/attachment/type/RawType.java 2007-12-06 18:32:23 UTC (rev 48) @@ -0,0 +1,18 @@ +package org.cemagref.simexplorer.si.storage.attachment.type; + +import java.io.InputStream; + +public class RawType extends ContentType { + + public RawType() { + super(); + setDescription("rawtype"); + setMimeType("text/text"); + } + + @Override + public InputStream renderToText(InputStream is) { + return is; + } + +}