view app/controllers/User.java @ 81:3ebc3061f869 draft

create getUserLatestClaims.
author one
date Fri, 08 Mar 2013 21:12:37 +0900
parents 11dc5846b8c7
children 3440be06e501
line wrap: on
line source

package controllers;

import java.util.HashMap;
import java.util.HashSet;

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();
		Vertex v = null;
		UserModel newUser = null;
		try {
			v = tpGraph.addVertex(name);
		} catch (IllegalArgumentException e) {
			return status(CONFLICT, name + " already exists");
		}
		newUser = new UserModel(v);
		tpGraph.setLabelFromRootUser(newUser);
		// newUser.setName(name); // user node hasn't name property only
		// TinkerGraph.
		return created();
	}

	public static Result getUser(String name) {
		TPGraph tpGraph = TPGraph.getInstance();
		Vertex v = tpGraph.getVertex(name);
		if (v == null) {
			return notFound();
		}
		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();
		Vertex v = tpGraph.getVertex(name);
		if (v == null) {
			return notFound("User: " + name + " does not found");
		}
		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();
		Vertex v = tpGraph.getVertex(name);
		if (v == null) {
			return notFound("user: " + name + " not found");
		}
		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));
	}

	/*
	 * TODO: Code Refactoring. getUserLatestConsensus method is almost the same
	 * getUserConsensus method.
	 */
	public static Result getUserLatestConsensus(String name) {
		TPGraph tpGraph = TPGraph.getInstance();
		Vertex v = tpGraph.getVertex(name);
		if (v == null) {
			return notFound("user: " + name + " not found");
		}
		UserModel user = new UserModel(v);
		HashSet<Object> set = user.getLatestClaimsAndRequests();
		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();
		Vertex v = tpGraph.getVertex(name);
		if (v == null) {
			return notFound("User " + name + " does not found");
		}
		UserModel user = new UserModel(v);
		Object[] claims = user.getUserClaims();
		if (claims == null) {
			return notFound("Claims does not found");
		}
		return created(Json.toJson(claims));
	}

	/*
	 * TODO: Code refactoring. getUserLatestClaims method is almost the same
	 * getUserClaims method.
	 */
	public static Result getUserLatestClaims(String name) {
		TPGraph tpGraph = TPGraph.getInstance();
		Vertex v = tpGraph.getVertex(name);
		if (v == null) {
			return notFound("User " + name + " does not found");
		}
		UserModel user = new UserModel(v);
		Object[] claims = user.getLatestUserClaims();
		if (claims == null) {
			return notFound("Claims does not found");
		}
		return created(Json.toJson(claims));
	}

}