updating SQL plugin with latest Quantum code
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / view / bookmark / ObjectNode.java
1 package net.sourceforge.phpdt.sql.view.bookmark;
2
3 import org.apache.crimson.tree.XmlDocument;
4 import org.w3c.dom.Element;
5
6 import net.sourceforge.phpdt.sql.Messages;
7 import net.sourceforge.phpdt.sql.sql.metadata.MetaDataXMLInterface;
8 import net.sourceforge.phpdt.sql.sql.metadata.ObjectMetaData;
9
10 /**
11  * Defines a node for the Subset. It contains an editable ObjectMetaData 
12  * where you can erase columns you are not interested in. 
13  * @author jparrai
14  *
15  */
16 public class ObjectNode implements TreeNode, Comparable {
17         private SubsetNode parent = null;
18         private String bookmark = null;
19         private String name = null;
20         private ObjectMetaData metadata = null;
21         private int size = -1;
22         private int order = 0;
23         
24         public ObjectNode(SubsetNode parent, ObjectMetaData metadata, String bookmark, String name) {
25                 this.parent = parent;
26                 this.bookmark = bookmark;
27                 this.metadata = metadata;
28                 this.name = name;
29         }
30         
31         public ObjectNode(){
32         }
33
34         /* (non-Javadoc)
35          * @see net.sourceforge.phpdt.view.bookmark.TreeNode#getChildren()
36          * We consider the columns of the metadata to be the children.
37          */
38         public Object[] getChildren() {
39                 if (metadata != null && ColumnMetaData.getColumnsMetaData(metadata, this) != null) {
40                         return ColumnMetaData.getColumnsMetaData(metadata, this).toArray();
41                 } else {
42                         return Root.EMPTY_ARRAY;
43                 }
44         }
45
46         public Object getParent() {
47                 return parent;
48         }
49
50         public boolean hasChildren() {
51                 if (metadata == null) return false;
52                 return (ColumnMetaData.getColumnsMetaData(metadata, this) != null && 
53                                 ColumnMetaData.getColumnsMetaData(metadata, this).size() > 0);
54         }
55         
56         public String getName() {
57                 return name;
58         }
59         
60         public String toString() {
61                 return name;
62         }
63
64         public int compareTo(Object o) {
65                 if (o instanceof ObjectNode) {
66                         ObjectNode node = (ObjectNode) o;
67                         if (node.getOrder() > getOrder()) return -1;
68                         if (node.getOrder() < getOrder()) return 1;
69                         // If the order is the same, we use alphabetical order 
70                         return name.compareTo(node.getName());
71                 } else throw new ClassCastException();
72         }
73         
74         public int getSize() {
75                 return size;
76         }
77
78         public void setSize(int size) {
79                 this.size = size;
80         }
81
82         public void setObjectMetadata(ObjectMetaData metadata) {
83                 this.metadata = metadata;
84         }
85         /**
86          * @return
87          */
88         public ObjectMetaData getMetaData() {
89                 return metadata;
90         }
91
92         /**
93          * @return The order of this ObjectNode inside the SubsetNode
94          */
95         public int getOrder() {
96                 return order;
97         }
98         /**
99          * Sets an ordering (inside the SubsetNode) to the ObjectNode
100          * @param i
101          */
102         public void setOrder(int i) {
103                 order = i;
104         }
105
106         /* (non-Javadoc)
107          * @see java.lang.Object#equals(java.lang.Object)
108          */
109         public boolean equals(Object obj) {
110                 if (!(obj instanceof ObjectNode)) return false;
111                 return (getName().equals(((ObjectNode) obj).getName()));
112         }
113         
114         /**
115          * Imports one ObjectNode from an XMLDocument. There must be a tag for the Table Name, another for the Bookmark
116          * name and other for the metadata. The complement function is exportXML()
117          * @param root Document to get the data from
118          * @param parent The SubsetNode to which to add the new ObjectNode
119          * @return The newly created ObjectNode, or null if some error.
120          */
121         public static ObjectNode importXML(Element root, SubsetNode parent){
122                 // Get the name tag value into objName
123                 String objName = MetaDataXMLInterface.getElementText(root, Messages.getString("ExportXMLAction.TableName")); //$NON-NLS-1$
124                 if (objName == "") return null;  //$NON-NLS-1$
125                 // Get the bookmark tag value into objName
126                 String bookmarkName = MetaDataXMLInterface.getElementText(root, Messages.getString("ExportXMLAction.BookmarkName")); //$NON-NLS-1$
127                 if (bookmarkName == "") return null; //$NON-NLS-1$
128                 ObjectMetaData metadata = new ObjectMetaData();
129                 // The rest of the tags go to be the metadata
130                 MetaDataXMLInterface.xmlToMetaData(metadata, root);
131                 // We can finally create the new ObjectNode with the collected data
132                 ObjectNode objectNode = new ObjectNode(parent, metadata, bookmarkName, objName);
133                 return objectNode;                                      
134         }
135         
136         /**
137          * Exports an ObjectNode to an XMLDocument. The complement function is importXML()
138          * @param root  Document to write the XML tags to
139          */
140         public void exportXML(Element root){
141                 XmlDocument doc = (XmlDocument) root.getOwnerDocument();
142                 Element sub = (Element) root.appendChild(doc.createElement(Messages.getString("ExportXMLAction.Table"))); //$NON-NLS-1$
143                 MetaDataXMLInterface.createElementText(sub,Messages.getString("ExportXMLAction.TableName"), getName()); //$NON-NLS-1$
144                 MetaDataXMLInterface.createElementText(sub,Messages.getString("ExportXMLAction.BookmarkName"), bookmark); //$NON-NLS-1$
145                 if (this.metadata != null)
146                         MetaDataXMLInterface.metaDataToXML(this.metadata, doc, sub);
147         }
148
149         /**
150          * @return The name of the Bookmark associated with that ObjectNode
151          */
152         public String getBookmark() {
153                 return bookmark;
154         }
155
156         /**
157          * Generates a query with all the columns in the metadata of the ObjectNode.
158          * "SELECT *" would not be valid because you can have less columns in the ObjectNode than in the table or view.
159          * @return      String with the Query
160          */
161         public String getQuery() {
162                 String result = new String(""); //$NON-NLS-1$
163                 result = "SELECT " + metadata.getColumnsString() + " FROM " + getName(); //$NON-NLS-1$ //$NON-NLS-2$
164                 return result;
165         }
166
167
168 }