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