1 package com.quantum.view.subset;
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;
9 import org.w3c.dom.Document;
10 import org.w3c.dom.Element;
13 * Defines a node for the Subset. It contains an editable ObjectMetaData
14 * where you can erase columns you are not interested in.
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;
24 public ObjectNode(SubsetNode parent, ObjectMetaData metadata, String bookmark, String name, String schema) {
26 this.metadata = metadata;
27 this.entity = new EntitySubset(name, schema, bookmark);
32 public String getType(){
33 return Entity.VIEW_TYPE;
37 * @see com.quantum.view.bookmark.TreeNode#getChildren()
38 * We consider the columns of the metadata to be the children.
40 public Object[] getChildren() {
41 // if (metadata != null && ColumnMetaData.getColumnsMetaDataNode(metadata, this) != null) {
42 // return ColumnMetaData.getColumnsMetaDataNode(metadata, this).toArray();
44 return SubsetRoot.EMPTY_ARRAY;
48 public Object getParent() {
52 public boolean hasChildren() {
53 // if (metadata == null) return false;
54 // return (ColumnMetaData.getColumnsMetaDataNode(metadata, this) != null &&
55 // ColumnMetaData.getColumnsMetaDataNode(metadata, this).size() > 0);
59 public String getName() {
60 return this.entity.getName();
63 public String toString() {
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();
78 public void setObjectMetadata(ObjectMetaData metadata) {
79 this.metadata = metadata;
84 public ObjectMetaData getMetaData() {
89 * @return The order of this ObjectNode inside the SubsetNode
91 public int getOrder() {
95 * Sets an ordering (inside the SubsetNode) to the ObjectNode
98 public void setOrder(int i) {
103 * @see java.lang.Object#equals(java.lang.Object)
105 public boolean equals(Object obj) {
106 if (!(obj instanceof ObjectNode)) return false;
107 return (getEntity().getQualifiedName().equals(
108 ((ObjectNode) obj).getEntity().getQualifiedName()));
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.
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);
135 * Exports an ObjectNode to an XMLDocument. The complement function is importXML()
136 * @param root Document to write the XML tags to
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);
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
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$
160 * @see com.quantum.model.EntityHolder#getEntity()
162 public Entity getEntity() {