1 package com.quantum.view.subset;
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;
10 import org.w3c.dom.Document;
11 import org.w3c.dom.Element;
14 * Defines a node for the Subset. It contains an editable ObjectMetaData
15 * where you can erase columns you are not interested in.
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;
25 public ObjectNode(SubsetNode parent, ObjectMetaData metadata, String bookmark, String name, String schema) {
27 this.metadata = metadata;
28 this.entity = new EntitySubset(name, schema, bookmark);
33 public String getType(){
34 return IQuantumConstants.View;
38 * @see com.quantum.view.bookmark.TreeNode#getChildren()
39 * We consider the columns of the metadata to be the children.
41 public Object[] getChildren() {
42 // if (metadata != null && ColumnMetaData.getColumnsMetaDataNode(metadata, this) != null) {
43 // return ColumnMetaData.getColumnsMetaDataNode(metadata, this).toArray();
45 return SubsetRoot.EMPTY_ARRAY;
49 public Object getParent() {
53 public boolean hasChildren() {
54 // if (metadata == null) return false;
55 // return (ColumnMetaData.getColumnsMetaDataNode(metadata, this) != null &&
56 // ColumnMetaData.getColumnsMetaDataNode(metadata, this).size() > 0);
60 public String getName() {
61 return this.entity.getName();
64 public String toString() {
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();
79 public void setObjectMetadata(ObjectMetaData metadata) {
80 this.metadata = metadata;
85 public ObjectMetaData getMetaData() {
90 * @return The order of this ObjectNode inside the SubsetNode
92 public int getOrder() {
96 * Sets an ordering (inside the SubsetNode) to the ObjectNode
99 public void setOrder(int i) {
104 * @see java.lang.Object#equals(java.lang.Object)
106 public boolean equals(Object obj) {
107 if (!(obj instanceof ObjectNode)) return false;
108 return (getEntity().getCondQualifiedName().equals(
109 ((ObjectNode) obj).getEntity().getCondQualifiedName()));
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.
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);
136 * Exports an ObjectNode to an XMLDocument. The complement function is importXML()
137 * @param root Document to write the XML tags to
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);
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
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$
161 * @see com.quantum.model.EntityHolder#getEntity()
163 public Entity getEntity() {