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  // GenomeBrowser Project
18  //
19  // DatabaseCatalog.java
20  // Since: Jun 15, 2007
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.db;
26  
27  import java.util.ArrayList;
28  
29  import org.utgenome.gwt.utgb.client.UTGBClientException;
30  
31  import com.google.gwt.json.client.JSONObject;
32  import com.google.gwt.json.client.JSONParser;
33  import com.google.gwt.json.client.JSONValue;
34  
35  /**
36   * 
37   * read a JSON description of database catalog, a list of database relations.
38   * 
39   * JSON format example (json object consisting of pairs of table_name:relation)
40   * <code>
41   * { 
42   *   "table_1":{"relation":[["id", "integer"], ["name", "string"], ...]}, 
43   *   "table_2":{"relation":[["p_id", "integer"], ["phone", "string"], ...]}
44   * }
45   * </code>
46   * 
47   * @author leo
48   * 
49   */
50  public class DatabaseCatalog
51  {
52  
53      /**
54       * "table_1", "table_2"
55       */
56      private ArrayList<String> _tableNameList = new ArrayList<String>();
57      /**
58       * a list of relations, in the order of "table_1", "table_2", ...
59       */
60      private ArrayList<Relation> _relationList = new ArrayList<Relation>();
61  
62      public DatabaseCatalog(String jsonData) throws UTGBClientException
63      {
64          load(jsonData);
65      }
66  
67      public DatabaseCatalog()
68      {
69  
70      }
71  
72      /**
73       * parsing the given JSONData representing database schema
74       * 
75       * @param jsonData
76       * @throws UTGBClientException
77       */
78      public void load(String jsonData) throws UTGBClientException
79      {
80          _relationList.clear();
81          _tableNameList.clear();
82  
83          JSONValue json = JSONParser.parse(jsonData);
84          JSONObject jsonObj = json.isObject();
85          if (jsonObj == null)
86              throw new UTGBClientException("invalid json data:" + jsonData);
87  
88          // read relations
89          for (String tableName : jsonObj.keySet())
90          {
91              JSONValue relationValue = jsonObj.get(tableName);
92              Relation r = new Relation(relationValue.isObject());
93  
94              // update
95              _tableNameList.add(tableName);
96              _relationList.add(r);
97          }
98      }
99  
100     public ArrayList<String> getTableNameList()
101     {
102         return _tableNameList;
103     }
104 
105     public ArrayList<Relation> getRelationList()
106     {
107         return _relationList;
108     }
109 
110     public Relation getRelation(String tableName)
111     {
112         for (int i = 0; i < _tableNameList.size(); i++)
113         {
114             String t = (String) _tableNameList.get(i);
115             if (t.equals(tableName))
116                 return (Relation) _relationList.get(i);
117         }
118         return null;
119     }
120 }