view app/models/NodeModel.java @ 4:e12d0b6dbe02 draft default tip

add some files
author e085711
date Wed, 26 Sep 2012 14:43:57 +0900
parents
children
line wrap: on
line source

package models;

import java.util.HashMap;
import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;

import com.tinkerpop.blueprints.Vertex;

public class NodeModel {

	private Vertex vertex;
	private Object id;
	final String NODE_TYPE = "nodeType";
	
	JSONObject properties = new JSONObject();
	
	public NodeModel(Vertex vertex) {
		this.vertex = vertex;
		this.id = vertex.getId();
	}
	
	public JSONObject getProperties() throws JSONException {
		for (String key: vertex.getPropertyKeys() ) {
			properties.put(key, vertex.getProperty(key));
		}
		return properties;
	}
	
	public void setId(Object id) {
		this.id = id;
	}

	public Object getId() {
		return this.id;
	}
	
	public Vertex getVertex() {
		return this.vertex;
	}
	
	public void setProperty(String key, Object value) throws JSONException {
		properties.put(key, value);
	}
	
	public void setPropetiesFromJson(JSONObject jobj) throws JSONException {
		Iterator<String> iter = jobj.keys();
		for (String key=iter.next(); iter.hasNext(); key=iter.next()) {
			this.setProperty(key, jobj.get(key));
		}
	}
	
	public JSONObject getNodeJson() throws JSONException {
		JSONObject jobj = new JSONObject();
		jobj.put("id",this.id);
		jobj.put("data", this.properties);
		return jobj; 
	}
	
	
}