1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.utgenome.gwt.utgb.client.db;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.utgenome.gwt.utgb.client.UTGBClientException;
31 import org.utgenome.gwt.utgb.client.db.datatype.BooleanType;
32 import org.utgenome.gwt.utgb.client.db.datatype.DataType;
33 import org.utgenome.gwt.utgb.client.db.datatype.DoubleType;
34 import org.utgenome.gwt.utgb.client.db.datatype.IntegerType;
35 import org.utgenome.gwt.utgb.client.db.datatype.PasswordType;
36 import org.utgenome.gwt.utgb.client.db.datatype.StringType;
37 import org.utgenome.gwt.utgb.client.db.datatype.TextType;
38 import org.utgenome.gwt.utgb.client.util.JSONUtil;
39
40 import com.google.gwt.json.client.JSONArray;
41 import com.google.gwt.json.client.JSONObject;
42 import com.google.gwt.json.client.JSONParser;
43 import com.google.gwt.json.client.JSONValue;
44 import com.google.gwt.user.client.rpc.IsSerializable;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public class Relation implements IsSerializable
67 {
68
69
70
71
72
73 private List<DataType> dataTypeList = new ArrayList<DataType>();
74
75 public Relation()
76 {}
77
78 public Relation(String jsonStr) throws UTGBClientException
79 {
80 JSONValue json = JSONParser.parse(jsonStr);
81 parse(json.isObject());
82 }
83
84 public Relation(JSONObject jsonObj) throws UTGBClientException
85 {
86 parse(jsonObj);
87 }
88
89 private void parse(JSONObject jsonObj) throws UTGBClientException
90 {
91 if (jsonObj == null)
92 throw new UTGBClientException("null json object");
93
94 if (!jsonObj.containsKey("relation"))
95 throw new UTGBClientException("no relation key found");
96 JSONArray relationList = jsonObj.get("relation").isArray();
97 for (int i = 0; i < relationList.size(); i++)
98 {
99 JSONArray dataTypePair = relationList.get(i).isArray();
100 if (dataTypePair == null || dataTypePair.size() != 2)
101 throw new UTGBClientException("data type must be json array with two elements: "
102 + relationList.toString());
103
104 String parameterName = JSONUtil.toStringValue(dataTypePair.get(0));
105 String typeName = JSONUtil.toStringValue(dataTypePair.get(1));
106
107 add(getDataType(parameterName, typeName));
108 }
109 }
110
111 public static DataType getDataType(String parameterName, String typeName) throws UTGBClientException
112 {
113 if (typeName.equals("boolean"))
114 return new BooleanType(parameterName);
115 else if (typeName.equals("double"))
116 return new DoubleType(parameterName);
117 else if (typeName.equals("string"))
118 return new StringType(parameterName);
119 else if (typeName.equals("password"))
120 return new PasswordType(parameterName);
121 else if (typeName.equals("text"))
122 return new TextType(parameterName);
123 else if (typeName.equals("integer"))
124 return new IntegerType(parameterName);
125 else
126 throw new UTGBClientException("unknown data type: " + typeName);
127 }
128
129 public void add(DataType dataType)
130 {
131 dataTypeList.add(dataType);
132 }
133
134 public DataType getDataType(int index)
135 {
136 return (DataType) dataTypeList.get(index);
137 }
138
139 public List<DataType> getDataTypeList()
140 {
141 return dataTypeList;
142 }
143
144 public String toString()
145 {
146 StringBuffer s = new StringBuffer();
147 s.append("(");
148 for (DataType dt : dataTypeList)
149 {
150 s.append(dt.toString());
151 s.append(" ");
152 }
153 s.append(")");
154 return s.toString();
155 }
156
157 }