Mercurial > hg > Members > nobuyasu > Consensus
view app/controllers/User.java @ 60:589bc7c508cc
add initialData method
author | one |
---|---|
date | Tue, 13 Nov 2012 03:41:28 +0900 |
parents | e2b703e173ab |
children | 290a5883ac5e |
line wrap: on
line source
package controllers; import java.util.HashMap; import java.util.HashSet; import com.tinkerpop.blueprints.Graph; import com.tinkerpop.blueprints.Vertex; import models.TPGraph; import models.UserModel; import play.libs.Json; import play.mvc.Controller; import play.mvc.Result; public class User extends Controller { public static Result createUser(String name) { TPGraph tpGraph = TPGraph.getInstance(); Graph graph = tpGraph.getGraph(); Vertex v = null; UserModel newUser = null; try { v = graph.addVertex(name); } catch (IllegalArgumentException e) { return status(CONFLICT, name+" already exists"); } newUser = new UserModel(v); tpGraph.setLabelToRootUser(newUser); // newUser.setName(name); // user node hasn't name property only TinkerGraph. return created(); } public static Result getUser(String name) { 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.getUserInfo(); return created(Json.toJson(hash)); } } public static Result getAllUsers() { TPGraph tpGraph = TPGraph.getInstance(); Object[] allUser = tpGraph.searchAllUser(); if (allUser == null) { return notFound("User does not exist."); } return ok(Json.toJson(allUser)); } public static Result getUserRequests(String name) { TPGraph tpGraph = TPGraph.getInstance(); Graph graph = tpGraph.getGraph(); Vertex v = graph.getVertex(name); if (v == null) { return notFound("User: "+name+" does not found"); } else { UserModel user = new UserModel(v); Object[] requests = user.getUserRequests(); if (requests == null) { return notFound("Requests not found"); } return created(Json.toJson(requests)); } } public static Result getUserConsensus(String name) { TPGraph tpGraph = TPGraph.getInstance(); Graph graph = tpGraph.getGraph(); Vertex v = graph.getVertex(name); if (v == null) { return notFound("user: "+name+" not found"); } else { UserModel user = new UserModel(v); HashSet<Object> set = user.getClaimsAndRequests(); if (set == null) { return notFound("Consensus not found"); } Object[] consensus = tpGraph.checkConsensus(set); return created(Json.toJson(consensus)); } } public static Result getUserClaims(String name) { TPGraph tpGraph = TPGraph.getInstance(); Graph graph = tpGraph.getGraph(); Vertex v = graph.getVertex(name); if (v == null) { return notFound("User "+name+" does not found"); } else { UserModel user = new UserModel(v); Object[] claims = user.getUserClaims(); if (claims == null) { return notFound("Claims does not found"); } return created(Json.toJson(claims)); } } }