added resources (refactor from extension)
diff --git a/src/main/java/de/ids_mannheim/korap/resources/Corpus.java b/src/main/java/de/ids_mannheim/korap/resources/Corpus.java
new file mode 100755
index 0000000..6c8dd75
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/resources/Corpus.java
@@ -0,0 +1,43 @@
+package de.ids_mannheim.korap.resources;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import java.util.Map;
+
+/**
+ * User: hanl
+ * Date: 11/8/13
+ * Time: 5:15 PM
+ */
+@Getter
+@Setter
+public class Corpus extends KustvaktResource {
+
+    private Map stats;
+
+    public Corpus() {
+        super();
+    }
+
+    public Corpus(Integer id, long created, int creator) {
+        super(id, creator, created);
+    }
+
+    public Corpus(Integer id, int creator) {
+        super(id, creator);
+    }
+
+    public Corpus(String pers_id, int creator) {
+        super(pers_id, creator);
+        // deprecated
+        this.setName(pers_id);
+    }
+
+    @Override
+    public Map toMap() {
+        Map res = super.toMap();
+        res.put("statistics", stats);
+        return res;
+    }
+}
diff --git a/src/main/java/de/ids_mannheim/korap/resources/Document.java b/src/main/java/de/ids_mannheim/korap/resources/Document.java
new file mode 100644
index 0000000..3700a1b
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/resources/Document.java
@@ -0,0 +1,69 @@
+package de.ids_mannheim.korap.resources;
+
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * @author hanl
+ * @date 05/11/2014
+ */
+@Getter
+@Setter
+public class Document extends KustvaktResource {
+
+    private String corpus;
+    private boolean disabled;
+
+    public Document(String persistentID) {
+        this.setId(-1);
+        this.setPersistentID(persistentID);
+        this.corpus = getCorpusID();
+        this.setDisabled(true);
+    }
+
+    public Document(String persistentID, boolean disabled) {
+        this(persistentID);
+        this.setDisabled(disabled);
+    }
+
+    private String getCorpusID() {
+        //WPD_SSS.07367
+        if (this.getPersistentID() != null)
+            return this.getPersistentID().split("_")[0];
+        return null;
+    }
+
+    @Override
+    public String toString() {
+        return "Document{" +
+                "id='" + this.getId() + "'" +
+                "persistentid='" + this.getPersistentID() + "'" +
+                "corpus='" + corpus + '\'' +
+                ", disabled=" + disabled +
+                '}';
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o)
+            return true;
+        if (o == null || getClass() != o.getClass())
+            return false;
+
+        Document document = (Document) o;
+
+        if (disabled != document.disabled)
+            return false;
+        if (!getPersistentID().equals(document.getPersistentID()))
+            return false;
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = getPersistentID().hashCode();
+        result = 31 * result + (disabled ? 1 : 0);
+        return result;
+    }
+}
diff --git a/src/main/java/de/ids_mannheim/korap/resources/Foundry.java b/src/main/java/de/ids_mannheim/korap/resources/Foundry.java
new file mode 100644
index 0000000..b86874d
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/resources/Foundry.java
@@ -0,0 +1,32 @@
+package de.ids_mannheim.korap.resources;
+
+/**
+ * @author hanl
+ * @date 25/06/2014
+ */
+public class Foundry extends KustvaktResource {
+
+    public Foundry() {
+        super();
+    }
+
+    public Foundry(Integer id, int creator, long created) {
+        super(id, creator, created);
+    }
+
+    public Foundry(Integer id, int creator) {
+        super(id, creator);
+    }
+
+    public Foundry(Integer id, String persistentID, int creator) {
+        super(id, creator);
+        this.setName(persistentID);
+        this.setPersistentID(persistentID);
+    }
+
+    public Foundry(String persistentID,int creator) {
+        super(persistentID, creator);
+        this.setName(persistentID);
+    }
+
+}
diff --git a/src/main/java/de/ids_mannheim/korap/resources/KustvaktResource.java b/src/main/java/de/ids_mannheim/korap/resources/KustvaktResource.java
new file mode 100644
index 0000000..ba76ad2
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/resources/KustvaktResource.java
@@ -0,0 +1,192 @@
+package de.ids_mannheim.korap.resources;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import de.ids_mannheim.korap.utils.TimeUtils;
+import lombok.Getter;
+import lombok.Setter;
+import org.joda.time.DateTime;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Created by hanl on 5/21/14.
+ */
+@Getter
+@Setter
+public abstract class KustvaktResource {
+
+    @JsonIgnore
+    private Integer id;
+    private String persistentID;
+    private String name;
+    private String description;
+    @JsonIgnore
+    private Integer owner;
+    protected long created;
+    @Deprecated
+    private boolean managed;
+    @Deprecated
+    private boolean shared;
+    private String path;
+    // parents persistentid
+    private String parentID;
+
+    //    private static RandomStringUtils utils = new RandomStringUtils();
+
+    protected KustvaktResource() {
+        this.created = TimeUtils.getNow().getMillis();
+        this.id = -1;
+        this.parentID = null;
+    }
+
+    public KustvaktResource(Integer id, int creator, long created) {
+        this.created = created;
+        this.owner = creator;
+        this.id = id;
+        this.parentID = null;
+    }
+
+    public KustvaktResource(Integer id, int creator) {
+        this.created = TimeUtils.getNow().getMillis();
+        this.id = id;
+        this.owner = creator;
+        this.parentID = null;
+    }
+
+    public KustvaktResource(String persistentID, int creator) {
+        this();
+        this.owner = creator;
+        this.persistentID = persistentID;
+    }
+
+    @Override
+    public boolean equals(Object other) {
+        return other instanceof KustvaktResource && this.id
+                .equals(((KustvaktResource) other).getId());
+    }
+
+    @Override
+    public int hashCode() {
+        int result = id.hashCode();
+        return result;
+    }
+
+    /**
+     * Merges another resource with this resource instance
+     * Every implementation of KorAPResource should override this method!
+     *
+     * @param other
+     */
+    public void merge(KustvaktResource other) {
+        if (other == null)
+            return;
+
+        this.setId(this.getId() == null || this.getId() == -1 ?
+                other.getId() :
+                other.getId());
+        this.setPersistentID(this.getPersistentID() == null ?
+                other.getPersistentID() :
+                this.getPersistentID());
+        this.setName(this.getName() == null || this.getName().isEmpty() ?
+                other.getName() :
+                this.getName());
+        this.setDescription(
+                this.getDescription() == null || this.getDescription()
+                        .isEmpty() ?
+                        other.getDescription() :
+                        this.getDescription());
+        this.setCreated(this.getCreated() < other.getCreated() ?
+                this.getCreated() :
+                other.getCreated());
+        this.setPath(this.getPath() == null ? other.getPath() : this.getPath());
+        this.setOwner(
+                this.getOwner() == null ? other.getOwner() : this.getOwner());
+        this.setManaged(
+                !this.isManaged() ? other.isManaged() : this.isManaged());
+        this.setShared(!this.isShared() ? other.isShared() : this.isShared());
+    }
+
+    /**
+     * Checks this instance for null parameter and replaces them with default values.
+     * Every implementation of KorAPResource should override this method!
+     *
+     * @return
+     */
+    public void checkNull() {
+        setCreated(this.getCreated() == 0L ?
+                TimeUtils.getNow().getMillis() :
+                this.getCreated());
+        setName(this.getName() == null ? "" : this.getName());
+    }
+
+    protected String createID() {
+        //        return utils.randomAlphanumeric(10);
+        return "";
+    }
+
+    /**
+     * this method is used to return field information about the class
+     * All subclasses should override this method
+     *
+     * @return
+     */
+    public Map toMap() {
+        Map m = new HashMap();
+        m.put("id", persistentID);
+        m.put("name", name);
+        m.put("path", path);
+        m.put("description", description);
+        m.put("created", TimeUtils.format(new DateTime(created)));
+        m.put("managed", managed);
+        m.put("shared", shared);
+        return m;
+    }
+
+    @Override
+    public String toString() {
+        return "Resource{" +
+                "id='" + id + '\'' +
+                "persistentID='" + persistentID + '\'' +
+                ", name='" + name + '\'' +
+                ", created=" + created +
+                ", path=" + path +
+                ", owner=" + owner +
+                '}';
+    }
+
+    @Getter
+    public static class Container {
+        private final Class type;
+        //        private final Integer id;
+        private final String persistentID;
+        private final boolean set;
+
+        public Container(String persistentID, Class type) {
+            this.type = type;
+            //            this.id = id;
+            this.set = true;
+            this.persistentID = persistentID;
+        }
+
+        public Container(Class type) {
+            this.type = type;
+            //            this.id = id;
+            this.set = true;
+            this.persistentID = null;
+        }
+
+        public Container() {
+            this.set = false;
+            this.type = null;
+            //            this.id = null;
+            this.persistentID = null;
+        }
+
+        @Override
+        public String toString() {
+            return persistentID + "@" + type.getSimpleName();
+        }
+    }
+
+}
diff --git a/src/main/java/de/ids_mannheim/korap/resources/Layer.java b/src/main/java/de/ids_mannheim/korap/resources/Layer.java
new file mode 100644
index 0000000..df464c0
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/resources/Layer.java
@@ -0,0 +1,37 @@
+package de.ids_mannheim.korap.resources;
+
+/**
+ * @author hanl
+ * @date 25/06/2014
+ */
+public class Layer extends KustvaktResource {
+
+    public Layer() {
+        super();
+    }
+
+    public Layer(Integer id, int creator, long created) {
+        super(id, creator, created);
+    }
+
+    public Layer(Integer id, int creator) {
+        super(id, creator);
+    }
+
+    // layer name must not be unique!
+    public Layer(Integer id, String name, int creator) {
+        super(id, creator);
+        this.setName(name);
+    }
+
+    public Layer(String persistentID, String name, int creator) {
+        super(persistentID, creator);
+        this.setPersistentID(persistentID);
+        this.setName(name);
+    }
+
+    public Layer(String persistentID, int creator) {
+        super(persistentID, creator);
+    }
+
+}
diff --git a/src/main/java/de/ids_mannheim/korap/resources/Permissions.java b/src/main/java/de/ids_mannheim/korap/resources/Permissions.java
new file mode 100644
index 0000000..f039c61
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/resources/Permissions.java
@@ -0,0 +1,45 @@
+package de.ids_mannheim.korap.resources;
+
+/**
+ * @author hanl
+ * @date 21/01/2014
+ */
+public class Permissions {
+
+   public static enum PERMISSIONS {
+        //fixme: add read_policy permission to allow read policy permissions
+        READ, WRITE, DELETE, READ_POLICY, CREATE_POLICY, MODIFY_POLICY, DELETE_POLICY
+    }
+
+    public static final byte READ = 1;
+    public static final byte WRITE = 2;
+    public static final byte DELETE = 4;
+    public static final byte READ_POLICY = 8;
+    public static final byte CREATE_POLICY = 16;
+    public static final byte MODIFY_POLICY = 32;
+    public static final byte DELETE_POLICY = 64;
+
+
+    public static Byte getByte(PERMISSIONS perm) {
+        switch (perm) {
+            case READ:
+                return READ;
+            case WRITE:
+                return WRITE;
+            case DELETE:
+                return DELETE;
+            case READ_POLICY:
+                return READ_POLICY;
+            case DELETE_POLICY:
+                return DELETE_POLICY;
+            case MODIFY_POLICY:
+                return MODIFY_POLICY;
+            case CREATE_POLICY:
+                return CREATE_POLICY;
+            default:
+                return 0;
+        }
+    }
+
+
+}
diff --git a/src/main/java/de/ids_mannheim/korap/resources/Relation.java b/src/main/java/de/ids_mannheim/korap/resources/Relation.java
new file mode 100644
index 0000000..1d2c7a8
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/resources/Relation.java
@@ -0,0 +1,9 @@
+package de.ids_mannheim.korap.resources;
+
+/**
+ * @author hanl
+ * @date 29/04/2014
+ */
+public enum Relation {
+    OR, AND
+}
diff --git a/src/main/java/de/ids_mannheim/korap/resources/ResourceFactory.java b/src/main/java/de/ids_mannheim/korap/resources/ResourceFactory.java
new file mode 100755
index 0000000..1148ac0
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/resources/ResourceFactory.java
@@ -0,0 +1,137 @@
+package de.ids_mannheim.korap.resources;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@SuppressWarnings("all")
+public class ResourceFactory {
+
+    public static final List<Class<? extends KustvaktResource>> subTypes = new ArrayList<>();
+    public static final int CORPUS = 0;
+    public static final int FOUNDRY = 1;
+    public static final int LAYER = 2;
+    public static final int VIRTUALCOLLECTION = 3;
+    public static final int USERQUERY = 4;
+
+    static {
+        subTypes.add(CORPUS, Corpus.class);
+        subTypes.add(FOUNDRY, Foundry.class);
+        subTypes.add(LAYER, Layer.class);
+        subTypes.add(VIRTUALCOLLECTION, VirtualCollection.class);
+//        subTypes.add(USERQUERY, UserQuery.class);
+    }
+
+    public static KustvaktResource getResource(
+            Class<? extends KustvaktResource> clazz) {
+        try {
+            return (KustvaktResource) clazz.newInstance();
+        }catch (InstantiationException | IllegalAccessException e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+    public static int getResourceMapping(Class<? extends KustvaktResource> r) {
+        int value = -1;
+        if (r != null) {
+            for (int i = 0; i < subTypes.size(); i++) {
+                if (subTypes.get(i).getName().equals(r.getName()))
+                    value = i;
+            }
+        }
+        return value;
+    }
+
+    public static KustvaktResource getResource(String type) {
+        return getResource(getResourceClass(type));
+    }
+
+    public static KustvaktResource getResource(int j) {
+        Class s = subTypes.get(j);
+        if (s != null) {
+            return getResource(s);
+        }
+        return null;
+    }
+
+
+    public static <T extends KustvaktResource> Class<T> getResourceClass(
+            String type) {
+        for (Class value : subTypes) {
+            if (value == VirtualCollection.class && type
+                    .equalsIgnoreCase("collection"))
+                return (Class<T>) VirtualCollection.class;
+                    //todo
+//            else if (value == UserQuery.class && type.equalsIgnoreCase("query"))
+//                return (Class<T>) UserQuery.class;
+            else if (value.getSimpleName().equalsIgnoreCase(type.trim())) {
+                return value; // do nothing
+            }
+        }
+        return null;
+    }
+    // all deprecated!
+
+    public static VirtualCollection getCachedCollection(String query) {
+        VirtualCollection v = new VirtualCollection();
+        v.setQuery(query);
+        v.setName("");
+        v.setDescription("");
+        v.setPersistentID(v.createID());
+        return v;
+    }
+
+    public static VirtualCollection getPermanentCollection(
+            VirtualCollection mergable, String corpusName, String description,
+            Integer owner) {
+        VirtualCollection v = new VirtualCollection();
+        v.merge(mergable);
+        v.setPersistentID(v.createID());
+        v.setName(corpusName);
+        v.setDescription(description);
+        v.setOwner(owner);
+        return v;
+    }
+
+    public static VirtualCollection createCollection(String name, String query,
+            Integer owner) {
+        VirtualCollection v = new VirtualCollection();
+        v.setName(name);
+        v.setQuery(query);
+        v.setOwner(owner);
+        return v;
+    }
+
+    public static VirtualCollection createCollection(String name, String query,
+            long time, Integer owner) {
+        VirtualCollection v = new VirtualCollection(0, owner, time);
+        v.setName(name);
+        v.setQuery(query);
+        return v;
+    }
+
+    public static VirtualCollection getCollection(Integer collectionID,
+            boolean cache) {
+        VirtualCollection v = new VirtualCollection();
+        v.setId(collectionID);
+        v.setDescription("");
+        v.setName("");
+        return v;
+    }
+
+    public static VirtualCollection createContainer(String name,
+            String description, String query, Integer owner) {
+        VirtualCollection v = new VirtualCollection();
+        v.setName(name);
+        v.setDescription(description);
+        v.setQuery(query);
+        v.setOwner(owner);
+        v.setManaged(true);
+        v.setPersistentID(v.createID());
+        return v;
+    }
+
+    public static VirtualCollection getIDContainer(Integer id, Integer owner) {
+        return new VirtualCollection(id, owner);
+    }
+}
diff --git a/src/main/java/de/ids_mannheim/korap/resources/VirtualCollection.java b/src/main/java/de/ids_mannheim/korap/resources/VirtualCollection.java
new file mode 100755
index 0000000..c4d74e2
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/resources/VirtualCollection.java
@@ -0,0 +1,95 @@
+package de.ids_mannheim.korap.resources;
+
+import de.ids_mannheim.korap.utils.JsonUtils;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.commons.codec.digest.DigestUtils;
+
+import java.util.Map;
+
+@Getter
+@Setter
+public class VirtualCollection extends KustvaktResource {
+
+    private String query;
+    // use ehcache instead and only save persisted values in the database
+    //    @Deprecated
+    //    private boolean cache = false;
+    private Map stats;
+
+    protected VirtualCollection() {
+        super();
+        this.setPersistentID(this.createID());
+    }
+
+    protected VirtualCollection(Integer id, int creator, long created) {
+        super(id, creator, created);
+    }
+
+    public VirtualCollection(Integer id, int creator) {
+        super(id, creator);
+    }
+
+    public VirtualCollection(String persistentID, int creator) {
+        super(persistentID, creator);
+    }
+
+    public VirtualCollection(String query) {
+        this();
+        this.setQuery(query);
+        this.setPersistentID(this.createID());
+    }
+
+    @Override
+    protected String createID() {
+        if (this.query != null) {
+            String s = this.getQuery();
+            return DigestUtils.sha1Hex(s);
+        }
+        return super.createID();
+    }
+
+    @Override
+    public void merge(KustvaktResource resource) {
+        super.merge(resource);
+        if (resource == null | !(resource instanceof VirtualCollection))
+            return;
+        VirtualCollection other = (VirtualCollection) resource;
+        this.setStats(
+                this.getStats() == null ? other.getStats() : this.getStats());
+        this.setQuery(
+                this.getQuery() == null ? other.getQuery() : this.getQuery());
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public void checkNull() {
+        super.checkNull();
+        this.setDescription(
+                this.getDescription() == null ? "" : this.getDescription());
+        this.setQuery(this.query == null ? "" : this.getQuery());
+    }
+
+    @Override
+    public String toString() {
+        return "VirtualCollection{" +
+                "id='" + this.getId() + '\'' +
+                ", persistentID='" + this.getPersistentID() + '\'' +
+                ", created=" + created +
+                ", path=" + this.getPath() +
+                ", owner=" + this.getOwner() +
+                ", name='" + this.getName() + '\'' +
+                ", query='" + query + '\'' +
+                ", stats='" + stats + '\'' +
+                '}';
+    }
+
+    @Override
+    public Map toMap() {
+        Map res = super.toMap();
+        res.put("query", JsonUtils.readTree(query));
+        res.put("statistics", stats);
+        return res;
+    }
+
+}