1 package net.sourceforge.phpdt.sql.view.bookmark;
3 import org.apache.crimson.tree.XmlDocument;
4 import org.w3c.dom.Element;
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;
11 * Defines a node for the Subset. It contains an editable ObjectMetaData
12 * where you can erase columns you are not interested in.
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;
24 public ObjectNode(SubsetNode parent, ObjectMetaData metadata, String bookmark, String name) {
26 this.bookmark = bookmark;
27 this.metadata = metadata;
35 * @see net.sourceforge.phpdt.view.bookmark.TreeNode#getChildren()
36 * We consider the columns of the metadata to be the children.
38 public Object[] getChildren() {
39 if (metadata != null && ColumnMetaData.getColumnsMetaData(metadata, this) != null) {
40 return ColumnMetaData.getColumnsMetaData(metadata, this).toArray();
42 return Root.EMPTY_ARRAY;
46 public Object getParent() {
50 public boolean hasChildren() {
51 if (metadata == null) return false;
52 return (ColumnMetaData.getColumnsMetaData(metadata, this) != null &&
53 ColumnMetaData.getColumnsMetaData(metadata, this).size() > 0);
56 public String getName() {
60 public String toString() {
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();
74 public int getSize() {
78 public void setSize(int size) {
82 public void setObjectMetadata(ObjectMetaData metadata) {
83 this.metadata = metadata;
88 public ObjectMetaData getMetaData() {
93 * @return The order of this ObjectNode inside the SubsetNode
95 public int getOrder() {
99 * Sets an ordering (inside the SubsetNode) to the ObjectNode
102 public void setOrder(int i) {
107 * @see java.lang.Object#equals(java.lang.Object)
109 public boolean equals(Object obj) {
110 if (!(obj instanceof ObjectNode)) return false;
111 return (getName().equals(((ObjectNode) obj).getName()));
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.
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);
137 * Exports an ObjectNode to an XMLDocument. The complement function is importXML()
138 * @param root Document to write the XML tags to
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);
150 * @return The name of the Bookmark associated with that ObjectNode
152 public String getBookmark() {
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
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$