Changes for variablemodificatin
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / model / xml / ModelToXMLConverter.java
1 package com.quantum.model.xml;
2
3 import java.sql.SQLException;
4
5 import com.quantum.model.Bookmark;
6 import com.quantum.model.Column;
7 import com.quantum.model.Entity;
8 import com.quantum.model.JDBCDriver;
9 import com.quantum.model.NotConnectedException;
10 import com.quantum.model.Schema;
11 import com.quantum.sql.metadata.MetaDataXMLInterface;
12
13 import org.w3c.dom.Document;
14 import org.w3c.dom.Element;
15 import org.w3c.dom.Text;
16
17 /**
18  * @author BC
19  */
20 public class ModelToXMLConverter {
21     
22     private static final ModelToXMLConverter instance = new ModelToXMLConverter();
23     
24     private ModelToXMLConverter() {
25     }
26     
27     public static ModelToXMLConverter getInstance() {
28         return ModelToXMLConverter.instance;
29     }
30
31     public void createRoot(Document document) {
32         document.appendChild(document.createElement("SAVED_DATA"));
33     }
34     public void convert(Element bookmarkRoot, JDBCDriver jdbcDriver) {
35         Document document = bookmarkRoot.getOwnerDocument();
36         Element driverNode = document.createElement("jdbcDriver");
37         driverNode.setAttribute("name", jdbcDriver.getName());
38         driverNode.setAttribute("version", jdbcDriver.getVersion());
39         driverNode.setAttribute("type", jdbcDriver.getType());
40         driverNode.setAttribute("jarFileName", jdbcDriver.getJarFileName());
41         driverNode.setAttribute("className", jdbcDriver.getClassName());
42         bookmarkRoot.appendChild(driverNode);
43     }
44
45     public void convert(Element bookmarkRoot, Bookmark b) {
46         Document document = bookmarkRoot.getOwnerDocument();
47         Element bookmark = MetaDataXMLInterface.createElementText(bookmarkRoot,"bookmark", ""); //$NON-NLS-1$ //$NON-NLS-2$
48         MetaDataXMLInterface.createElementText(bookmark,"name", b.getName()); //$NON-NLS-1$
49         MetaDataXMLInterface.createElementText(bookmark,"username", b.getUsername()); //$NON-NLS-1$
50         MetaDataXMLInterface.createElementText(bookmark,"password", b.getPassword()); //$NON-NLS-1$
51         MetaDataXMLInterface.createElementText(bookmark,"prompt", b.getPromptForPassword() ? "true" : "false"); //$NON-NLS-1$
52         MetaDataXMLInterface.createElementText(bookmark,"connect", b.getConnect()); //$NON-NLS-1$
53                 MetaDataXMLInterface.createElementText(bookmark,"autoCommit", b.isAutoCommit() ? "true" : "false"); //$NON-NLS-1$
54                 MetaDataXMLInterface.createElementText(bookmark,"autoCommitPreference", b.getAutoCommitPreference()); //$NON-NLS-1$
55         MetaDataXMLInterface.createElementText(bookmark,"driver", b.getJDBCDriver().getClassName()); //$NON-NLS-1$
56         MetaDataXMLInterface.createElementText(bookmark,"type", b.getJDBCDriver().getType()); //$NON-NLS-1$
57                 MetaDataXMLInterface.createElementText(bookmark,"driverLocation", b.getJDBCDriver().getJarFileName()); //$NON-NLS-1$
58         Element otherSchemas = (Element) bookmark.appendChild(document.createElement("Other_Schemas")); //$NON-NLS-1$
59         otherSchemas.setAttribute("schemaRule", 
60                         b.useAllSchemas() 
61                                         ? "useAll" 
62                                         : b.useUsernameAsSchema() ? "useDefault" : "useSelected");
63         
64         Schema[] schemas = b.getSchemaSelections();
65         for (int i = 0, length = (schemas == null) ? 0 : schemas.length; i < length; i++) {
66             MetaDataXMLInterface.createElementText(
67                 otherSchemas,"schema", schemas[i].getName()); //$NON-NLS-1$
68         }
69         
70         Entity[] quickList = b.getQuickListEntries();
71         Element quickListEntity = document.createElement("quickList");
72         for (int j = 0, length = (quickList == null) ? 0 : quickList.length;
73             j < length;
74             j++) {
75             ModelToXMLConverter.getInstance().convert(quickListEntity, quickList[j]);
76         }
77         bookmark.appendChild(quickListEntity);
78         convertQueryList(b, bookmark);
79     }
80
81     
82     public void convert(Element parent, Entity entity) {
83         convert(parent, entity, true);
84     }
85     public void convert(Element parent, Entity entity, boolean recurse) {
86         Document document = parent.getOwnerDocument();
87         Element element = document.createElement(getEntityDOMNodeName(entity));
88         element.setAttribute("name", entity.getName());
89         if (entity.getSchema() != null) {
90             element.setAttribute("schema", entity.getSchema());
91         }
92         if (recurse) {
93             try {
94                                 convert(element, entity.getColumns());
95                         } catch (NotConnectedException e) {
96                         } catch (SQLException e) {
97                         }
98         }
99         parent.appendChild(element);
100     }
101
102     public void convert(Element element, Column[] columns) {
103         for (int i = 0, length = (columns == null) ? 0 : columns.length;
104             i < length;
105             i++) {
106             convert(element, columns[i]);
107         }
108     }
109
110     public void convert(Element parent, Column column) {
111         Document document = parent.getOwnerDocument();
112         Element element = document.createElement("column");
113         element.setAttribute("name", column.getName());
114         element.setAttribute("type", column.getTypeName());
115         element.setAttribute("primaryKey", column.isPrimaryKey() ? "true" : "false");
116         element.setAttribute("nullable", column.isNullable() ? "true" : "false");
117         if (column.isNumeric()) {
118             if (column.getSize() > 0) {
119                 element.setAttribute("size", String.valueOf(column.getSize()));
120             }
121             if (column.getNumberOfFractionalDigits() > 0) {
122                 element.setAttribute("numberOfFractionalDigits", String.valueOf(column.getNumberOfFractionalDigits()));
123             }
124         } else {
125             if (column.getSize() > 0) {
126                 element.setAttribute("size", String.valueOf(column.getSize()));
127             }
128         }
129                 if (column.getRemarks().length() > 0)
130                         MetaDataXMLInterface.createElementText(element, "remarks", column.getRemarks());
131         parent.appendChild(element);
132     }
133
134
135     public String getEntityDOMNodeName(Entity entity) {
136         if (Entity.TABLE_TYPE.equals(entity.getType())) {
137             return "table";
138         } else if (Entity.VIEW_TYPE.equals(entity.getType())) {
139             return "view";
140         } else if (Entity.SEQUENCE_TYPE.equals(entity.getType())) {
141             return "sequence";
142         } else {
143             return "unknown";
144         }
145     }
146
147     public void convertQueryList(
148         Bookmark bookmark,
149         Element parent) {
150             
151         Document document = parent.getOwnerDocument();
152         String[] queryList = bookmark.getQueries();
153         Element queryListEntity = document.createElement("queryList");
154         for (int j = 0, length = (queryList == null) ? 0 : queryList.length;
155             j < length;
156             j++) {
157             Element query = document.createElement("query");
158             
159             // will have more possibilities later
160             Element queryString = document.createElement("queryString");
161             Text queryText = document.createTextNode(queryList[j]);
162             queryString.appendChild(queryText);
163             query.appendChild(queryString);
164             queryListEntity.appendChild(query);
165         }
166         parent.appendChild(queryListEntity);
167     }
168     
169 }