14
|
1 package models;
|
|
2
|
29
|
3 import java.util.ArrayList;
|
36
|
4 import java.util.HashSet;
|
|
5 import java.util.Iterator;
|
29
|
6
|
16
|
7 import org.codehaus.jackson.JsonNode;
|
14
|
8 import org.codehaus.jackson.node.ObjectNode;
|
|
9
|
|
10 import play.libs.Json;
|
|
11
|
31
|
12 import com.tinkerpop.blueprints.Edge;
|
36
|
13 import com.tinkerpop.blueprints.Graph;
|
14
|
14 import com.tinkerpop.blueprints.Vertex;
|
29
|
15 import com.tinkerpop.gremlin.java.GremlinPipeline;
|
14
|
16
|
|
17 public class ClaimModel extends NodeModel {
|
|
18
|
|
19
|
|
20 public ClaimModel(Vertex vertex) {
|
|
21 super(vertex);
|
|
22 }
|
|
23
|
31
|
24 public ObjectNode getSimpleClaimInfo() {
|
29
|
25 ObjectNode property = Json.newObject();
|
|
26 property.put(TYPE, Json.toJson(getProperty(TYPE)));
|
|
27 property.put(STATUS, Json.toJson(getProperty(STATUS)));
|
|
28 property.put(TOULMIN, Json.toJson(getProperty(TOULMIN)));
|
30
|
29 property.put(L_AUTHOR, Json.toJson(getAuthorId()));
|
|
30 property.put(MENTIONS, Json.toJson(getMentionsId()));
|
31
|
31 property.put(USERS, Json.toJson(getUsersId()));
|
29
|
32 return property;
|
|
33 }
|
|
34
|
31
|
35 public ObjectNode getClaimInfoTraverse() {
|
|
36 ObjectNode property = Json.newObject();
|
|
37 property.put(TYPE, Json.toJson(getProperty(TYPE)));
|
|
38 property.put(STATUS, Json.toJson(getProperty(STATUS)));
|
|
39 property.put(TOULMIN, Json.toJson(getProperty(TOULMIN)));
|
|
40 property.put(L_AUTHOR, Json.toJson(getAuthorId()));
|
32
|
41 property.put(MENTIONS, Json.toJson(getClaimMentionsRecursive()));
|
31
|
42 property.put(USERS, Json.toJson(getUsersIdAndStatus()));
|
|
43 return property;
|
|
44 }
|
|
45
|
32
|
46 public Object[] getClaimMentionsRecursive() {
|
31
|
47 GremlinPipeline<Vertex,Edge> pipe = new GremlinPipeline<Vertex,Edge>();
|
|
48 pipe.start(vertex).outE(L_QUESTION,L_REFUTATION,L_SUGGESTION);
|
32
|
49 ArrayList<Object> array = new ArrayList<Object>();
|
31
|
50 for (Edge e:pipe) {
|
|
51 String label = e.getLabel();
|
32
|
52 ObjectNode info = Json.newObject();
|
|
53 info.put(TYPE, Json.toJson(label));
|
|
54 GremlinPipeline<Edge,Vertex> pipeChildVertex = new GremlinPipeline<Edge,Vertex>();
|
|
55 pipeChildVertex.start(e).inV();
|
|
56 ClaimModel childClaim = new ClaimModel(pipeChildVertex.next());
|
|
57 info.put(CLAIM, childClaim.getClaimInfoTraverse());
|
38
|
58 info.put(ID, Json.toJson(childClaim.getId()));
|
32
|
59 array.add(info);
|
31
|
60 }
|
32
|
61 return array.toArray();
|
30
|
62 }
|
32
|
63
|
30
|
64 public Object[] getInfoArray(String... labels) {
|
|
65 GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>();
|
|
66 pipe.start(vertex).out(labels);
|
|
67 ArrayList<Object> array = new ArrayList<Object>();
|
|
68 for (Vertex v : pipe) array.add(v.getId());
|
|
69 if (array.size() == 0) return null;
|
|
70 return array.toArray();
|
|
71 }
|
|
72
|
|
73 public Object[] getMentionsId() {
|
|
74 return getInfoArray(L_QUESTION,L_REFUTATION,L_SUGGESTION);
|
|
75 }
|
|
76
|
31
|
77 public Object[] getUsersId() {
|
30
|
78 return getInfoArray(L_REQUEST);
|
|
79 }
|
31
|
80
|
|
81 public Object[] getUsersIdAndStatus() {
|
|
82 GremlinPipeline<Vertex,Edge> pipeEdge = new GremlinPipeline<Vertex,Edge>();
|
|
83 pipeEdge.start(vertex).outE(L_REQUEST);
|
|
84 ArrayList<Object> array = new ArrayList<Object>();
|
|
85 for (Edge e : pipeEdge) {
|
|
86 GremlinPipeline<Edge,Vertex> pipeChildVertex = new GremlinPipeline<Edge,Vertex>();
|
|
87 ObjectNode info = Json.newObject();
|
32
|
88 pipeChildVertex.start(e).inV();
|
31
|
89 Vertex childVertex = pipeChildVertex.next();
|
|
90 info.put(ID, Json.toJson(childVertex.getId()));
|
|
91 info.put(STATUS, Json.toJson(e.getProperty(STATUS)));
|
|
92 array.add(info);
|
|
93 }
|
|
94 return array.toArray();
|
|
95 }
|
30
|
96
|
|
97 public Object[] getRequestUsersId() {
|
|
98 return getInfoArray(NodeModel.REQUESTS);
|
|
99 }
|
|
100
|
36
|
101 public void editRequestsEdgeUsers(Object[] updateUsers) {
|
|
102 TPGraph tpGraph = TPGraph.getInstance();
|
|
103 Object[] currentUsers = getUsersId();
|
|
104 HashSet<Object> currentUsersHashSet = new HashSet<Object>();
|
|
105 for (Object u : currentUsers) {
|
|
106 currentUsersHashSet.add(u);
|
|
107 }
|
|
108 for (Object updateUser : updateUsers) {
|
|
109 if (currentUsersHashSet.contains(updateUser)) {
|
|
110 currentUsersHashSet.remove(updateUser);
|
|
111 } else {
|
|
112 tpGraph.setLabelStatusToUser(this, updateUser.toString(), L_REQUEST, UNKNOWN);
|
|
113 }
|
|
114 }
|
|
115 tpGraph.deleteRequestEdge(this, currentUsersHashSet);
|
|
116 }
|
|
117
|
30
|
118 public Object getAuthorId() {
|
29
|
119 GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>();
|
|
120 pipe.start(vertex).out(L_AUTHOR);
|
32
|
121 if (!pipe.hasNext()) {
|
|
122 return null;
|
|
123 }
|
29
|
124 Vertex authorV = pipe.next();
|
|
125 return authorV.getId();
|
|
126 }
|
|
127
|
18
|
128 public void setClaimProperties(JsonNode toulmin, String type) {
|
29
|
129 String title = toulmin.findPath(TITLE).getTextValue();
|
|
130 String contents = toulmin.findPath(CONTENTS).getTextValue();
|
|
131 String q = toulmin.findPath(QUALIFIER).getTextValue(); // Qualifier
|
|
132 String d = toulmin.findPath(DATA).getTextValue(); // Data
|
|
133 String w = toulmin.findPath(WARRANT).getTextValue(); // Warrant
|
|
134 String b = toulmin.findPath(BACKING).getTextValue(); // Backing
|
|
135 String r = toulmin.findPath(REBUTTLE).getTextValue(); // Rebuttle
|
16
|
136 ObjectNode t = Json.newObject();
|
|
137 t.put(TITLE, title);
|
|
138 t.put(CONTENTS, contents);
|
|
139 t.put(QUALIFIER, q);
|
|
140 t.put(DATA, d);
|
|
141 t.put(WARRANT, w);
|
|
142 t.put(BACKING, b);
|
|
143 t.put(REBUTTLE, r);
|
14
|
144 setProperty(TYPE, type);
|
37
|
145 setProperty(STATUS, UNKNOWN); // Default Status is unknown.
|
16
|
146 setProperty(TOULMIN, t);
|
25
|
147 }
|
14
|
148 }
|