preparing new release
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / bookmark / EntityNode.java
1 package com.quantum.view.bookmark;
2
3 import java.sql.SQLException;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.HashMap;
7 import java.util.Iterator;
8 import java.util.List;
9 import java.util.Map;
10
11 import com.quantum.model.Column;
12 import com.quantum.model.Entity;
13 import com.quantum.model.EntityHolder;
14 import com.quantum.model.NotConnectedException;
15 import com.quantum.model.SchemaHolder;
16 import com.quantum.model.Table;
17 import com.quantum.model.View;
18
19 /**
20  * @author BC
21  */
22 public class EntityNode extends TreeNode implements EntityHolder {
23
24     private Entity entity;
25     private boolean longFormName;
26     
27     private List columns = Collections.synchronizedList(new ArrayList());
28     private List foreignKeys = Collections.synchronizedList(new ArrayList());
29     boolean initialized = false;
30     
31     public EntityNode(TreeNode parent, Entity entity) {
32         this(parent, entity, false);
33     }
34     public EntityNode(TreeNode parent, Entity entity, boolean longFormName) {
35         super(parent);
36         this.entity = entity;
37         this.longFormName = longFormName;
38         
39         if (parent instanceof SchemaHolder) {
40             SchemaHolder schemaHolder = (SchemaHolder) parent;
41             if (!schemaHolder.getSchema().getDisplayName().equals(entity.getSchema())) {
42                 this.longFormName = true;
43             }
44         }
45     }
46
47     public Object[] getChildren() throws NotConnectedException, SQLException {
48          if (!isInitialized()) {
49                 initializeChildren();
50          }
51         
52         if (this.children.size() > 0) {
53             return (ColumnNode[]) this.children.toArray(new ColumnNode[this.children.size()]);
54         } else {
55             return BookmarkListNode.EMPTY_ARRAY;
56         }
57     }
58
59     protected synchronized void initializeChildren() throws NotConnectedException, SQLException {
60         boolean wasInitialized = isInitialized();
61         Map map = getChildrenAsMap();
62         Column[] columns = this.entity.getColumns();
63         this.children.clear();
64         for (int i = 0, length = (columns == null) ? 0 : columns.length;
65             i < length;
66             i++) {
67                 
68                 ColumnNode node = (ColumnNode) map.get(columns[i].getName());
69                 if (node == null) {
70                         this.children.add(new ColumnNode(this, columns[i]));
71                 } else {
72                         node.setColumn(columns[i]);
73                         this.children.add(node);
74                 }
75                 
76                 if (wasInitialized) {
77                         firePropertyChange("columns", null, null);
78                 }
79         }
80     }
81     
82     private Map getChildrenAsMap() {
83         Map map = new HashMap();
84         for (Iterator i = this.children.iterator(); i.hasNext();) {
85                         TreeNode node = (TreeNode) i.next();
86                         map.put(node.getName(), node);
87                 }
88         return map;
89     }
90
91     public boolean hasChildren() {
92         if (!isSequence()) {
93             return true;
94         } else {
95             return false;
96         }
97     }
98     
99     public Entity getEntity() {
100         return this.entity;
101     }
102     
103     public String getName() {
104         return this.entity.getName();
105     }
106     
107     public String getLabelName() {
108         return (this.longFormName) ? this.entity.getQualifiedName() : this.entity.getName();
109     }
110     
111     public boolean isTable() {
112         return Entity.TABLE_TYPE.equals(this.entity.getType());
113     }
114
115     public boolean isView() {
116         return Entity.VIEW_TYPE.equals(this.entity.getType());
117     }
118
119     public boolean isSequence() {
120         return Entity.SEQUENCE_TYPE.equals(this.entity.getType());
121     }
122
123     protected String getImageName() {
124         if (isSequence()) {
125             return "sequence.gif";
126         } else if (isView()) {
127             return "view.gif";
128         } else {
129             if (this.entity.exists() == null || this.entity.exists().booleanValue()){
130                 if (this.entity.isSynonym())
131                         return "big_syn_table.gif";
132                 else
133                         return "bigtable.gif";
134             } else
135                 return "missingtable.gif";
136    
137    
138         }
139     }
140
141     public String getLabelDecorations(LabelDecorationInstructions instructions) {
142         String decoration = null;
143         if (instructions.isSizeVisible()) {
144             Integer size = getSize();
145             if (size != null) {
146                 decoration = ((decoration == null) ? "" : decoration) 
147                     + "[" + size + "]";
148             }
149         }
150         return decoration;
151     }
152     
153     private Integer getSize() {
154         if (isTable()) {
155             return ((Table) this.entity).getSize();
156         } else if (isView()) {
157             return ((View) this.entity).getSize();
158         } else {
159             return null;
160         }
161     }
162     
163     public int compareTo(Object o) {
164         if (o instanceof EntityNode) {
165             EntityNode that = (EntityNode) o;
166             return this.entity.getQualifiedName().compareTo(
167                 that.entity.getQualifiedName());
168         } else {
169             return super.compareTo(o);
170         }
171     }
172         void setEntity(Entity entity) {
173                 this.entity = entity;
174         }
175 }