View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2007 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // BugReport Project
18  //
19  // Relation.java
20  // Since: 2007/03/28
21  //
22  // $URL$ 
23  // $Author$
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   * A Relation holds one or more DataTypes and denotes their relationships. For
48   * example, in relational databases, a table has a structure, e.g. (pid:integer,
49   * name:string, address:string)
50   * 
51   * You can construct this structure as follows: <code>
52   * Relation r = new Relation();
53   * r.add("pid", new IntegerType());
54   * r.add("name", new StringType());
55   * r.add("address", new StringType());
56   * </code>
57   * 
58   * JSON format example. <code>
59   * {"relation":[["id", "integer"], ["name", "string"], ...]}
60   * </code>
61   * 
62   * 
63   * @author leo
64   * 
65   */
66  public class Relation implements IsSerializable
67  {
68      /**
69       * The following line specifies the content of the List (dataType). This
70       * information is requried in the GWT compiler.
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 }