Mercurial > hg > Members > nobuyasu > Consensus
changeset 8:7b314898fddd
create action User.getUser()
author | one |
---|---|
date | Mon, 01 Oct 2012 19:53:43 +0900 |
parents | 2122c50278bd |
children | d050b7fb4cda |
files | app/Global.java app/controllers/Application.java app/controllers/User.java app/models/NodeModel.java app/models/TPGraph.java app/models/UserModel.java conf/routes |
diffstat | 7 files changed, 96 insertions(+), 31 deletions(-) [+] |
line wrap: on
line diff
--- a/app/Global.java Mon Oct 01 18:51:30 2012 +0900 +++ b/app/Global.java Mon Oct 01 19:53:43 2012 +0900 @@ -16,9 +16,9 @@ tpgraph.setPath(null); Graph graph = tpgraph.newGraph(); Vertex claimV = graph.addVertex(null); - tpgraph.setClaimRootVertex(claimV); + tpgraph.setClaimRootId(claimV.getId()); Vertex userV = graph.addVertex(null); - tpgraph.setUserRootVertex(userV); + tpgraph.setUserRootId(userV.getId()); } @Override
--- a/app/controllers/Application.java Mon Oct 01 18:51:30 2012 +0900 +++ b/app/controllers/Application.java Mon Oct 01 19:53:43 2012 +0900 @@ -1,5 +1,7 @@ package controllers; +import java.util.HashMap; + import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.node.ObjectNode; @@ -40,12 +42,14 @@ } } - public static Result create() { - String name = "test"; - System.out.println("name = "+name); - - return ok(name); - } + public static Result test() { + HashMap<Object,Object> hash = new HashMap<Object,Object>(); + hash.put("key1", "value1"); + hash.put("key2", "value2"); + return ok(Json.toJson(hash)); + } + + }
--- a/app/controllers/User.java Mon Oct 01 18:51:30 2012 +0900 +++ b/app/controllers/User.java Mon Oct 01 19:53:43 2012 +0900 @@ -1,11 +1,14 @@ package controllers; +import java.util.HashMap; + import com.tinkerpop.blueprints.Graph; import com.tinkerpop.blueprints.Vertex; import models.NodeModel; import models.TPGraph; import models.UserModel; +import play.libs.Json; import play.mvc.Controller; import play.mvc.Result; @@ -15,27 +18,36 @@ TPGraph tpGraph = TPGraph.getInstance(); Graph graph = tpGraph.getGraph(); - UserModel newUser = null; + Vertex v = null; + UserModel newUser = null; try { - Vertex v = graph.addVertex(name); - newUser = new UserModel(v); + v = graph.addVertex(name); } catch (IllegalArgumentException e) { return status(CONFLICT, name+" is already exists"); } - newUser.setName(name); - return created("vertex Id = "+ newUser.getId()); + newUser = new UserModel(v); + newUser.setUserInfo(); + tpGraph.setLabelToRootUser(newUser); +// newUser.setName(name); // user node hasn't name property(only TinkerGraph). + return created(); } - private boolean checkUser(String name) { - + + public static Result getUser(String name) { - - return true; + TPGraph tpGraph = TPGraph.getInstance(); + Graph graph = tpGraph.getGraph(); + Vertex v = graph.getVertex(name); + if (v == null) { + return notFound(); + } else { + UserModel user = new UserModel(v); + HashMap<Object,Object> hash = user.getUserProperty(); + return created(Json.toJson(hash)); + } } - - }
--- a/app/models/NodeModel.java Mon Oct 01 18:51:30 2012 +0900 +++ b/app/models/NodeModel.java Mon Oct 01 19:53:43 2012 +0900 @@ -12,7 +12,6 @@ protected final String ID = "id"; - protected final String NAME = "name"; protected final String TOULMIN = "toulmin"; protected final String TITLE = "title"; protected final String CONTENTS = "contents";
--- a/app/models/TPGraph.java Mon Oct 01 18:51:30 2012 +0900 +++ b/app/models/TPGraph.java Mon Oct 01 19:53:43 2012 +0900 @@ -1,5 +1,7 @@ package models; +import scala.reflect.generic.Trees.This; + import com.tinkerpop.blueprints.Graph; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.tg.TinkerGraph; @@ -10,6 +12,8 @@ private Object claimRootId; private Object userRootId; + protected final String CHILD = "child"; + private TPGraph() { } @@ -39,26 +43,44 @@ return graph; } - public void setClaimRootVertex(Vertex v) { - this.claimRootId = v.getId(); + public void setClaimRootId(Object id) { + this.claimRootId = id; } - public Object getClaimRootVertex() { - return this.claimRootId; + public void setUserRootId(Object id) { + this.userRootId = id; } - public void setUserRootVertex(Vertex v) { - this.userRootId = v.getId(); + public Object getUserRootId() { + return userRootId; + } + + public Vertex getClaimRootVertex() { + return graph.getVertex(userRootId); } - public Object getUserRootVertex() { - return userRootId; + public Vertex getUserRootVertex() { + return graph.getVertex(userRootId); } + + public void setLabelToRootUser(UserModel user) { + Vertex rootUser = getUserRootVertex(); + + /* + * rootUser ---child---> newUser + */ + graph.addEdge(null, rootUser, user.getVertex(), CHILD); + } + + public void shutdownGraph() { graph.shutdown(); } + + + }
--- a/app/models/UserModel.java Mon Oct 01 18:51:30 2012 +0900 +++ b/app/models/UserModel.java Mon Oct 01 19:53:43 2012 +0900 @@ -1,20 +1,44 @@ package models; +import java.util.HashMap; + import com.tinkerpop.blueprints.Vertex; public class UserModel extends NodeModel { - + protected final String CONSENSUS = "consensus"; + protected final String CLAIMS = "claims"; + protected final String REQUESTS = "requests"; + + protected HashMap<Object,Object> properties = new HashMap<Object,Object>(); public UserModel(Vertex vertex) { super(vertex); } + + public void setUserInfo() { + this.vertex.setProperty(CONSENSUS, ""); + this.vertex.setProperty(CLAIMS, ""); + this.vertex.setProperty(REQUESTS, ""); + } + + + public HashMap<Object,Object> getUserProperty() { + for (String key : vertex.getPropertyKeys()) { + properties.put(key, vertex.getProperty(key)); + } + return properties; + } + + + +/* public Vertex setName(String name) { this.vertex.setProperty(NAME, name); return vertex; } - +*/
--- a/conf/routes Mon Oct 01 18:51:30 2012 +0900 +++ b/conf/routes Mon Oct 01 19:53:43 2012 +0900 @@ -4,8 +4,12 @@ # Home page GET / controllers.Application.index() -POST /db/data/node controllers.Application.hello() PUT /users/create/:name controllers.User.create(name: String) +GET /users/browse/:name controllers.User.getUser(name: String) + +# test action +POST /hello controllers.Application.hello() +GET /test controllers.Application.test() # Map static resources from the /public folder to the /assets URL path