View Javadoc

1   /*
2    * Copyright 2004-2005 The Apache Software Foundation or its licensors,
3    *                     as applicable.
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package net.sf.exorcist.midgard;
18  
19  import java.util.HashMap;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.Map;
23  import java.util.Set;
24  
25  public class MidgardType {
26  
27      private String name;
28  
29      private String table;
30  
31      private MidgardProperty timestamp;
32  
33      private MidgardProperty primary;
34  
35      private MidgardProperty parent;
36  
37      private Map properties = new HashMap();
38  
39      private Set children = new HashSet();
40  
41      public String getName() {
42          return name;
43      }
44  
45      public void setName(String name) {
46          this.name = name;
47      }
48  
49      public String getTable() {
50          return table;
51      }
52  
53      public void setTable(String table) {
54          this.table = table;
55      }
56  
57      public MidgardProperty getTimestamp() {
58          return timestamp;
59      }
60  
61      public void setTimestamp(MidgardProperty property) {
62          timestamp = property;
63      }
64  
65      public MidgardProperty getPrimary() {
66          return primary;
67      }
68  
69      public void setPrimary(MidgardProperty property) {
70          primary = property;
71      }
72  
73      public MidgardProperty getParent() {
74          return parent;
75      }
76  
77      public void setParent(MidgardProperty property) {
78          if (parent != null) {
79              parent.getLink().removeChild(this);
80          }
81          parent = property;
82          parent.getLink().addChild(this);
83      }
84  
85      public MidgardProperty getProperty(String name) {
86          return (MidgardProperty) properties.get(name);
87      }
88  
89      public Iterator getProperties() {
90          return properties.values().iterator();
91      }
92  
93      public void addProperty(MidgardProperty property) {
94          properties.put(property.getName(), property);
95      }
96  
97      public String getSQL() {
98          StringBuffer buffer = new StringBuffer();
99          buffer.append("SELECT ");
100         buffer.append(primary.getName());
101         Iterator iterator = properties.values().iterator();
102         while (iterator.hasNext()) {
103             buffer.append(", ");
104             MidgardProperty property = (MidgardProperty) iterator.next();
105             buffer.append(property.getName());
106         }
107         buffer.append(" FROM ");
108         buffer.append(table);
109         return buffer.toString();
110     }
111 
112     public Iterator getChildren() {
113         return children.iterator();
114     }
115 
116     public void addChild(MidgardType type) {
117         children.add(type);
118     }
119 
120     public void removeChild(MidgardType type) {
121         children.remove(type);
122     }
123 
124 }