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