001: package com.technoetic.xplanner.file;
002:
003: import com.technoetic.xplanner.db.hibernate.ThreadSession;
004: import net.sf.hibernate.Hibernate;
005: import net.sf.hibernate.HibernateException;
006: import net.sf.hibernate.Session;
007: import net.sf.hibernate.lob.BlobImpl;
008:
009: import java.io.InputStream;
010: import java.util.List;
011:
012: // Repository for files and directories
013: public class FileSystemImpl implements FileSystem {
014: public Directory getRootDirectory() throws HibernateException {
015: Session session = ThreadSession.get();
016: List dirs = session.createQuery(
017: "from dir in " + Directory.class
018: + " where dir.parent is null").setMaxResults(1)
019: .list();
020: if (dirs.iterator().hasNext()) {
021: return (Directory) dirs.iterator().next();
022: } else {
023: Directory root = new Directory();
024: root.setName("");
025: session.save(root);
026: session.flush();
027: session.refresh(root);
028: return root;
029: }
030: }
031:
032: public Directory getDirectory(Session session, int directoryId)
033: throws HibernateException {
034: return (Directory) session.load(Directory.class, new Integer(
035: directoryId));
036: }
037:
038: public Directory getDirectory(String path)
039: throws HibernateException {
040: String[] pathElements = path.split("/");
041: Directory dir = getRootDirectory();
042: Session session = ThreadSession.get();
043: for (int i = 1; i < pathElements.length; i++) {
044: List subdirectory = session
045: .createQuery(
046: "from dir in "
047: + Directory.class
048: + " where dir.parent = :parent and dir.name = :name")
049: .setParameter("parent", dir,
050: Hibernate.entity(Directory.class))
051: .setParameter("name", pathElements[i])
052: .setMaxResults(1).list();
053: if (subdirectory.size() > 0) {
054: dir = (Directory) subdirectory.get(0);
055: } else {
056: dir = createDirectory(session, dir, pathElements[i]);
057: }
058: }
059: return dir;
060: }
061:
062: public File createFile(Directory directory, String name,
063: String contentType, long size, InputStream data)
064: throws HibernateException {
065: File file = new File();
066: file.setName(name);
067: file.setContentType(contentType);
068: file.setFileSize(size);
069: if (data != null) {
070: file.setData(new BlobImpl(data, (int) size));
071: }
072: ThreadSession.get().save(file);
073: directory.getFiles().add(file);
074: return file;
075: }
076:
077: public File createFile(Session session, int directoryId,
078: String name, String contentType, long size, InputStream data)
079: throws HibernateException {
080: Directory directory = (Directory) session.load(Directory.class,
081: new Integer(directoryId));
082: return createFile(directory, name, contentType, size, data);
083: }
084:
085: public File getFile(Session session, int fileId)
086: throws HibernateException {
087: return (File) session.load(File.class, new Integer(fileId));
088: }
089:
090: public void deleteFile(Session session, int fileId)
091: throws HibernateException {
092: session.delete("from file in " + File.class + " where id = ?",
093: new Integer(fileId), Hibernate.INTEGER);
094: }
095:
096: public Directory createDirectory(Session session,
097: int parentDirectoryId, String name)
098: throws HibernateException {
099: Directory parent = getDirectory(session, parentDirectoryId);
100: return createDirectory(session, parent, name);
101: }
102:
103: public Directory createDirectory(Session session, Directory parent,
104: String name) throws HibernateException {
105: Directory subdirectory = new Directory();
106: subdirectory.setName(name);
107: session.save(subdirectory);
108: session.flush();
109: session.refresh(subdirectory);
110: parent.getSubdirectories().add(subdirectory);
111: return subdirectory;
112: }
113:
114: public void deleteDirectory(Session session, int directoryId)
115: throws HibernateException {
116: session.delete("from dir in " + Directory.class
117: + " where id = ?", new Integer(directoryId),
118: Hibernate.INTEGER);
119: }
120: }
|