diff --git a/modules/postgres/app/edu/berkeley/ground/postgres/controllers/EdgeController.java b/modules/postgres/app/edu/berkeley/ground/postgres/controllers/EdgeController.java index 5fb9d479..0fa91d5c 100644 --- a/modules/postgres/app/edu/berkeley/ground/postgres/controllers/EdgeController.java +++ b/modules/postgres/app/edu/berkeley/ground/postgres/controllers/EdgeController.java @@ -9,13 +9,7 @@ import edu.berkeley.ground.common.util.IdGenerator; import edu.berkeley.ground.postgres.dao.core.PostgresEdgeDao; import edu.berkeley.ground.postgres.dao.core.PostgresEdgeVersionDao; -import edu.berkeley.ground.postgres.util.GroundUtils; -import edu.berkeley.ground.postgres.util.PostgresUtils; -import java.util.List; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionException; -import java.util.concurrent.CompletionStage; -import javax.inject.Inject; +import edu.berkeley.ground.postgres.util.*; import play.cache.CacheApi; import play.db.Database; import play.libs.Json; @@ -24,21 +18,27 @@ import play.mvc.Result; import play.mvc.Results; +import javax.inject.Inject; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.CompletionStage; + public class EdgeController extends Controller { private CacheApi cache; private ActorSystem actorSystem; - private PostgresEdgeDao postgresEdgeDao; private PostgresEdgeVersionDao postgresEdgeVersionDao; + private SharedFileState sharedFileState; @Inject final void injectUtils(final CacheApi cache, final Database dbSource, final ActorSystem actorSystem, final IdGenerator idGenerator) { this.actorSystem = actorSystem; this.cache = cache; - this.postgresEdgeDao = new PostgresEdgeDao(dbSource, idGenerator); this.postgresEdgeVersionDao = new PostgresEdgeVersionDao(dbSource, idGenerator); + this.sharedFileState = new SharedFileState(new Tree(new TreeNode("root", "root"))); } public final CompletionStage getEdge(final String sourceKey) { @@ -60,11 +60,13 @@ public final CompletionStage getEdge(final String sourceKey) { @BodyParser.Of(BodyParser.Json.class) public final CompletionStage addEdge() { + String currentPath = sharedFileState.getCwd(); return CompletableFuture.supplyAsync( () -> { JsonNode json = request().body().asJson(); Edge edge = Json.fromJson(json, Edge.class); - + String newName = currentPath + "/" + String.valueOf(json.get("name")); + ((ObjectNode) json).put("name", newName); try { edge = this.postgresEdgeDao.create(edge); } catch (GroundException e) { diff --git a/modules/postgres/app/edu/berkeley/ground/postgres/controllers/FileController.java b/modules/postgres/app/edu/berkeley/ground/postgres/controllers/FileController.java new file mode 100644 index 00000000..0c1659c6 --- /dev/null +++ b/modules/postgres/app/edu/berkeley/ground/postgres/controllers/FileController.java @@ -0,0 +1,45 @@ +package edu.berkeley.ground.postgres.controllers; + +import akka.actor.ActorSystem; +import edu.berkeley.ground.common.exception.GroundException; +import edu.berkeley.ground.postgres.util.SharedFileState; +import edu.berkeley.ground.postgres.util.Tree; +import edu.berkeley.ground.postgres.util.TreeNode; +import play.cache.CacheApi; +import play.db.Database; + +import javax.inject.Inject; + +public class FileController { + private CacheApi cache; + private ActorSystem actorSystem; + private String global; + private SharedFileState sharedFileState; + + + @Inject + final void injectUtils(final CacheApi cache, final Database dbSource, final String global, final String fileLeaf) { + this.actorSystem = actorSystem; + this.cache = cache; + this.global = global; + sharedFileState = new SharedFileState( new Tree( + new TreeNode(global, global))); // represents global directory + } + + public SharedFileState getSharedFileState() { + return sharedFileState; + } + + public void setCwd(String path) throws GroundException { + sharedFileState.setCwd(path); + } + + public String getCwd(String path) { + return sharedFileState.getCwd(); + } + + public void addFile(String path) { + sharedFileState.addFile(path); + } + +} diff --git a/modules/postgres/app/edu/berkeley/ground/postgres/controllers/GraphController.java b/modules/postgres/app/edu/berkeley/ground/postgres/controllers/GraphController.java index 9b743ee6..f20710be 100644 --- a/modules/postgres/app/edu/berkeley/ground/postgres/controllers/GraphController.java +++ b/modules/postgres/app/edu/berkeley/ground/postgres/controllers/GraphController.java @@ -20,13 +20,7 @@ import edu.berkeley.ground.common.util.IdGenerator; import edu.berkeley.ground.postgres.dao.core.PostgresGraphDao; import edu.berkeley.ground.postgres.dao.core.PostgresGraphVersionDao; -import edu.berkeley.ground.postgres.util.GroundUtils; -import edu.berkeley.ground.postgres.util.PostgresUtils; -import java.util.List; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionException; -import java.util.concurrent.CompletionStage; -import javax.inject.Inject; +import edu.berkeley.ground.postgres.util.*; import play.cache.CacheApi; import play.db.Database; import play.libs.Json; @@ -35,6 +29,12 @@ import play.mvc.Result; import play.mvc.Results; +import javax.inject.Inject; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.CompletionStage; + public class GraphController extends Controller { private CacheApi cache; @@ -42,15 +42,15 @@ public class GraphController extends Controller { private PostgresGraphDao postgresGraphDao; private PostgresGraphVersionDao postgresGraphVersionDao; + private SharedFileState sharedFileState; @Inject final void injectUtils(final CacheApi cache, final Database dbSource, final ActorSystem actorSystem, final IdGenerator idGenerator) { this.actorSystem = actorSystem; this.cache = cache; - this.postgresGraphDao = new PostgresGraphDao(dbSource, idGenerator); - this.postgresGraphVersionDao = new PostgresGraphVersionDao(dbSource, idGenerator); + this.sharedFileState = new SharedFileState(new Tree(new TreeNode("root", "root"))); } public final CompletionStage getGraph(String sourceKey) { @@ -88,9 +88,12 @@ public final CompletionStage getGraphVersion(Long id) { @BodyParser.Of(BodyParser.Json.class) public final CompletionStage addGraph() { + String currentPath = sharedFileState.getCwd(); return CompletableFuture.supplyAsync( () -> { JsonNode json = request().body().asJson(); + String newName = currentPath + "/" + String.valueOf(json.get("name")); + ((ObjectNode) json).put("name", newName); Graph graph = Json.fromJson(json, Graph.class); try { diff --git a/modules/postgres/app/edu/berkeley/ground/postgres/controllers/LineageEdgeController.java b/modules/postgres/app/edu/berkeley/ground/postgres/controllers/LineageEdgeController.java index e212af42..f89ac9d7 100644 --- a/modules/postgres/app/edu/berkeley/ground/postgres/controllers/LineageEdgeController.java +++ b/modules/postgres/app/edu/berkeley/ground/postgres/controllers/LineageEdgeController.java @@ -9,13 +9,7 @@ import edu.berkeley.ground.common.util.IdGenerator; import edu.berkeley.ground.postgres.dao.usage.PostgresLineageEdgeDao; import edu.berkeley.ground.postgres.dao.usage.PostgresLineageEdgeVersionDao; -import edu.berkeley.ground.postgres.util.GroundUtils; -import edu.berkeley.ground.postgres.util.PostgresUtils; -import java.util.List; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionException; -import java.util.concurrent.CompletionStage; -import javax.inject.Inject; +import edu.berkeley.ground.postgres.util.*; import play.cache.CacheApi; import play.db.Database; import play.libs.Json; @@ -24,6 +18,12 @@ import play.mvc.Result; import play.mvc.Results; +import javax.inject.Inject; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.CompletionStage; + public class LineageEdgeController extends Controller { private CacheApi cache; @@ -31,6 +31,7 @@ public class LineageEdgeController extends Controller { private PostgresLineageEdgeDao postgresLineageEdgeDao; private PostgresLineageEdgeVersionDao postgresLineageEdgeVersionDao; + private SharedFileState sharedFileState; @Inject final void injectUtils(final CacheApi cache, final Database dbSource, final ActorSystem actorSystem, final IdGenerator idGenerator) { @@ -39,6 +40,7 @@ final void injectUtils(final CacheApi cache, final Database dbSource, final Acto this.postgresLineageEdgeDao = new PostgresLineageEdgeDao(dbSource, idGenerator); this.postgresLineageEdgeVersionDao = new PostgresLineageEdgeVersionDao(dbSource, idGenerator); + this.sharedFileState = new SharedFileState(new Tree(new TreeNode("root", "root"))); } public final CompletionStage getLineageEdge(String sourceKey) { @@ -77,9 +79,12 @@ public final CompletionStage getLineageEdgeVersion(Long id) { @BodyParser.Of(BodyParser.Json.class) public final CompletionStage createLineageEdge() { + String currentPath = sharedFileState.getCwd(); return CompletableFuture.supplyAsync( () -> { JsonNode json = request().body().asJson(); + String newName = currentPath + "/" + String.valueOf(json.get("name")); + ((ObjectNode) json).put("name", newName); LineageEdge lineageEdge = Json.fromJson(json, LineageEdge.class); try { lineageEdge = this.postgresLineageEdgeDao.create(lineageEdge); diff --git a/modules/postgres/app/edu/berkeley/ground/postgres/controllers/LineageGraphController.java b/modules/postgres/app/edu/berkeley/ground/postgres/controllers/LineageGraphController.java index 03f5f289..18bc077b 100644 --- a/modules/postgres/app/edu/berkeley/ground/postgres/controllers/LineageGraphController.java +++ b/modules/postgres/app/edu/berkeley/ground/postgres/controllers/LineageGraphController.java @@ -9,13 +9,7 @@ import edu.berkeley.ground.common.util.IdGenerator; import edu.berkeley.ground.postgres.dao.usage.PostgresLineageGraphDao; import edu.berkeley.ground.postgres.dao.usage.PostgresLineageGraphVersionDao; -import edu.berkeley.ground.postgres.util.GroundUtils; -import edu.berkeley.ground.postgres.util.PostgresUtils; -import java.util.List; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionException; -import java.util.concurrent.CompletionStage; -import javax.inject.Inject; +import edu.berkeley.ground.postgres.util.*; import play.cache.CacheApi; import play.db.Database; import play.libs.Json; @@ -24,6 +18,12 @@ import play.mvc.Result; import play.mvc.Results; +import javax.inject.Inject; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.CompletionStage; + public class LineageGraphController extends Controller { private CacheApi cache; @@ -31,6 +31,7 @@ public class LineageGraphController extends Controller { private PostgresLineageGraphDao postgresLineageGraphDao; private PostgresLineageGraphVersionDao postgresLineageGraphVersionDao; + private SharedFileState sharedFileState; @Inject final void injectUtils(final CacheApi cache, final Database dbSource, @@ -40,6 +41,7 @@ final void injectUtils(final CacheApi cache, final Database dbSource, this.postgresLineageGraphDao = new PostgresLineageGraphDao(dbSource, idGenerator); this.postgresLineageGraphVersionDao = new PostgresLineageGraphVersionDao(dbSource, idGenerator); + this.sharedFileState = new SharedFileState(new Tree(new TreeNode("root", "root"))); } public final CompletionStage getLineageGraph(String sourceKey) { @@ -78,9 +80,12 @@ public final CompletionStage getLineageGraphVersion(Long id) { @BodyParser.Of(BodyParser.Json.class) public final CompletionStage createLineageGraph() { + String currentPath = sharedFileState.getCwd(); return CompletableFuture.supplyAsync( () -> { JsonNode json = request().body().asJson(); + String newName = currentPath + "/" + String.valueOf(json.get("name")); + ((ObjectNode) json).put("name", newName); LineageGraph lineageGraph = Json.fromJson(json, LineageGraph.class); try { lineageGraph = this.postgresLineageGraphDao.create(lineageGraph); diff --git a/modules/postgres/app/edu/berkeley/ground/postgres/controllers/NodeController.java b/modules/postgres/app/edu/berkeley/ground/postgres/controllers/NodeController.java index 36985fbc..299c0e18 100644 --- a/modules/postgres/app/edu/berkeley/ground/postgres/controllers/NodeController.java +++ b/modules/postgres/app/edu/berkeley/ground/postgres/controllers/NodeController.java @@ -9,13 +9,7 @@ import edu.berkeley.ground.common.util.IdGenerator; import edu.berkeley.ground.postgres.dao.core.PostgresNodeDao; import edu.berkeley.ground.postgres.dao.core.PostgresNodeVersionDao; -import edu.berkeley.ground.postgres.util.GroundUtils; -import edu.berkeley.ground.postgres.util.PostgresUtils; -import java.util.List; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionException; -import java.util.concurrent.CompletionStage; -import javax.inject.Inject; +import edu.berkeley.ground.postgres.util.*; import play.cache.CacheApi; import play.db.Database; import play.libs.Json; @@ -24,6 +18,12 @@ import play.mvc.Result; import play.mvc.Results; +import javax.inject.Inject; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.CompletionStage; + public class NodeController extends Controller { private CacheApi cache; @@ -31,6 +31,7 @@ public class NodeController extends Controller { private PostgresNodeDao postgresNodeDao; private PostgresNodeVersionDao postgresNodeVersionDao; + private SharedFileState sharedFileState; @Inject final void injectUtils(final CacheApi cache, final Database dbSource, final ActorSystem actorSystem, final IdGenerator idGenerator) { @@ -39,6 +40,7 @@ final void injectUtils(final CacheApi cache, final Database dbSource, final Acto this.postgresNodeDao = new PostgresNodeDao(dbSource, idGenerator); this.postgresNodeVersionDao = new PostgresNodeVersionDao(dbSource, idGenerator); + this.sharedFileState = new SharedFileState(new Tree(new TreeNode("root", "root"))); } public final CompletionStage getNode(String sourceKey) { @@ -60,9 +62,12 @@ public final CompletionStage getNode(String sourceKey) { @BodyParser.Of(BodyParser.Json.class) public final CompletionStage addNode() { + String currentPath = sharedFileState.getCwd(); return CompletableFuture.supplyAsync( () -> { JsonNode json = request().body().asJson(); + String newName = currentPath + "/" + String.valueOf(json.get("name")); + ((ObjectNode) json).put("name", newName); Node node = Json.fromJson(json, Node.class); try { node = this.postgresNodeDao.create(node); diff --git a/modules/postgres/app/edu/berkeley/ground/postgres/controllers/StructureController.java b/modules/postgres/app/edu/berkeley/ground/postgres/controllers/StructureController.java index b9c5ec4a..40225871 100644 --- a/modules/postgres/app/edu/berkeley/ground/postgres/controllers/StructureController.java +++ b/modules/postgres/app/edu/berkeley/ground/postgres/controllers/StructureController.java @@ -20,13 +20,7 @@ import edu.berkeley.ground.common.util.IdGenerator; import edu.berkeley.ground.postgres.dao.core.PostgresStructureDao; import edu.berkeley.ground.postgres.dao.core.PostgresStructureVersionDao; -import edu.berkeley.ground.postgres.util.GroundUtils; -import edu.berkeley.ground.postgres.util.PostgresUtils; -import java.util.List; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionException; -import java.util.concurrent.CompletionStage; -import javax.inject.Inject; +import edu.berkeley.ground.postgres.util.*; import play.cache.CacheApi; import play.db.Database; import play.libs.Json; @@ -35,6 +29,12 @@ import play.mvc.Result; import play.mvc.Results; +import javax.inject.Inject; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.CompletionStage; + public class StructureController extends Controller { private CacheApi cache; @@ -42,6 +42,7 @@ public class StructureController extends Controller { private PostgresStructureDao postgresStructureDao; private PostgresStructureVersionDao postgresStructureVersionDao; + private SharedFileState sharedFileState; @Inject final void injectUtils(final CacheApi cache, final Database dbSource, final ActorSystem actorSystem, final IdGenerator idGenerator) { @@ -50,6 +51,7 @@ final void injectUtils(final CacheApi cache, final Database dbSource, final Acto this.postgresStructureDao = new PostgresStructureDao(dbSource, idGenerator); this.postgresStructureVersionDao = new PostgresStructureVersionDao(dbSource, idGenerator); + this.sharedFileState = new SharedFileState(new Tree(new TreeNode("root", "root"))); } public final CompletionStage getStructure(String sourceKey) { @@ -86,9 +88,12 @@ public final CompletionStage getStructureVersion(Long id) { @BodyParser.Of(BodyParser.Json.class) public final CompletionStage addStructure() { + String currentPath = sharedFileState.getCwd(); return CompletableFuture.supplyAsync( () -> { JsonNode json = request().body().asJson(); + String newName = currentPath + "/" + String.valueOf(json.get("name")); + ((ObjectNode) json).put("name", newName); Structure structure = Json.fromJson(json, Structure.class); try { diff --git a/modules/postgres/app/edu/berkeley/ground/postgres/util/SharedFileState.java b/modules/postgres/app/edu/berkeley/ground/postgres/util/SharedFileState.java new file mode 100644 index 00000000..46984be5 --- /dev/null +++ b/modules/postgres/app/edu/berkeley/ground/postgres/util/SharedFileState.java @@ -0,0 +1,29 @@ +package edu.berkeley.ground.postgres.util; + +import edu.berkeley.ground.common.exception.GroundException; + +import java.io.FileNotFoundException; + +public class SharedFileState { + private Tree tree; + public SharedFileState(Tree fileTree) { + tree = fileTree; + } + public Tree getTree() { + return tree; + } + public void setCwd(String path) throws GroundException { + boolean hasSet = tree.setCwd(path); + if (!hasSet) { + throw new GroundException(new FileNotFoundException(path + " does not exist in file tree")); + } + } + + public String getCwd() { + return tree.getCwd().getIncrementalPath(); + } + + public void addFile(String path) { + tree.addElement(path); + } +} diff --git a/modules/postgres/app/edu/berkeley/ground/postgres/util/Tree.java b/modules/postgres/app/edu/berkeley/ground/postgres/util/Tree.java new file mode 100644 index 00000000..f1887091 --- /dev/null +++ b/modules/postgres/app/edu/berkeley/ground/postgres/util/Tree.java @@ -0,0 +1,73 @@ +package edu.berkeley.ground.postgres.util; + +public class Tree { + + TreeNode root; + TreeNode commonRoot; + TreeNode cwd; + + public Tree( TreeNode root ) { + this.root = root; + commonRoot = null; + cwd = null; + } + + public void addElement( String elementValue ) { + String[] list = elementValue.split("/"); + root.addElement(root.incrementalPath, list); + + } + + public void printTree() { + getCommonRoot(); + commonRoot.printNode(0); + } + + public TreeNode getCommonRoot() { + if ( commonRoot != null) + return commonRoot; + else { + TreeNode current = root; + while ( current.leafs.size() <= 0 ) { + current = current.children.get(0); + } + commonRoot = current; + return commonRoot; + } + } + + public boolean setCwd(String path) { + String[] listDirectories = path.split("/"); + for (String dir : listDirectories) { + System.out.println(dir); + } + String global = root.data; + String[] globalDirectory = global.split("/"); + + TreeNode currentNode = root; + for (int i = globalDirectory.length; i < listDirectories.length; i++) { + String nextDir = listDirectories[i]; + for (int j = 0; j < currentNode.children.size(); j++) { + TreeNode child = currentNode.children.get(j); + String dir = child.data; + if (dir.equals(nextDir)) { + currentNode = child; + break; + } else if (j == currentNode.children.size() - 1) { + return false; + } + } + } + + cwd = currentNode; + return true; + } + + public TreeNode getCwd() { + if (cwd == null) { + return root; + } else { + return cwd; + } + } +} diff --git a/modules/postgres/app/edu/berkeley/ground/postgres/util/TreeNode.java b/modules/postgres/app/edu/berkeley/ground/postgres/util/TreeNode.java new file mode 100644 index 00000000..4123b922 --- /dev/null +++ b/modules/postgres/app/edu/berkeley/ground/postgres/util/TreeNode.java @@ -0,0 +1,72 @@ +package edu.berkeley.ground.postgres.util; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + + +public class TreeNode { + + List children; + List leafs; + String data; + String incrementalPath; + + public TreeNode( String nodeValue, String incrementalPath ) { + children = new ArrayList(); + leafs = new ArrayList(); + data = nodeValue; + this. incrementalPath = incrementalPath; + } + + public boolean isLeaf() { + return children.isEmpty() && leafs.isEmpty(); + } + + public void addElement(String currentPath, String[] list) { + while( list[0] == null || list[0].equals("") ) + list = Arrays.copyOfRange(list, 1, list.length); + + TreeNode currentChild = new TreeNode(list[0], currentPath+"/"+list[0]); + if ( list.length == 1 ) { + leafs.add( currentChild ); + return; + } else { + int index = children.indexOf( currentChild ); + if ( index == -1 ) { + children.add( currentChild ); + currentChild.addElement(currentChild.incrementalPath, Arrays.copyOfRange(list, 1, list.length)); + } else { + TreeNode nextChild = children.get(index); + nextChild.addElement(currentChild.incrementalPath, Arrays.copyOfRange(list, 1, list.length)); + } + } + } + + @Override + public boolean equals(Object obj) { + TreeNode cmpObj = (TreeNode)obj; + return incrementalPath.equals( cmpObj.incrementalPath ) && data.equals( cmpObj.data ); + } + + public void printNode( int increment ) { + for (int i = 0; i < increment; i++) { + System.out.print(" "); + } + System.out.println(incrementalPath + (isLeaf() ? " -> " + data : "") ); + for( TreeNode n: children) + n.printNode(increment+2); + for( TreeNode n: leafs) + n.printNode(increment+2); + } + + public String getIncrementalPath() { + return incrementalPath; + } + + @Override + public String toString() { + return data; + } +} +