fixed Bug 1024299
[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("className", jdbcDriver.getClassName());
41         
42         String[] fileNames = jdbcDriver.getJarFileNames();
43         for (int i = 0, length = fileNames == null ? 0 : fileNames.length; i < length; i++) {
44                 if (fileNames[i] != null) {
45                                 Element fileName = document.createElement("jar");
46                                 fileName.setAttribute("fileName", fileNames[i]);
47                                 driverNode.appendChild(fileName);
48                 }
49                 }
50         
51         bookmarkRoot.appendChild(driverNode);
52     }
53
54     public void convert(Element bookmarkRoot, Bookmark b) {
55         Document document = bookmarkRoot.getOwnerDocument();
56         Element bookmark = MetaDataXMLInterface.createElementText(bookmarkRoot,"bookmark", ""); //$NON-NLS-1$ //$NON-NLS-2$
57         MetaDataXMLInterface.createElementText(bookmark,"name", b.getName()); //$NON-NLS-1$
58         MetaDataXMLInterface.createElementText(bookmark,"username", b.getUsername()); //$NON-NLS-1$
59         MetaDataXMLInterface.createElementText(bookmark,"password", b.getPassword()); //$NON-NLS-1$
60         MetaDataXMLInterface.createElementText(bookmark,"prompt", b.getPromptForPassword() ? "true" : "false"); //$NON-NLS-1$
61         MetaDataXMLInterface.createElementText(bookmark,"connect", b.getConnect()); //$NON-NLS-1$
62                 MetaDataXMLInterface.createElementText(bookmark,"autoCommit", b.isAutoCommit() ? "true" : "false"); //$NON-NLS-1$
63                 MetaDataXMLInterface.createElementText(bookmark,"autoCommitPreference", b.getAutoCommitPreference()); //$NON-NLS-1$
64         MetaDataXMLInterface.createElementText(bookmark,"driver", b.getJDBCDriver().getClassName()); //$NON-NLS-1$
65         MetaDataXMLInterface.createElementText(bookmark,"type", b.getJDBCDriver().getType()); //$NON-NLS-1$
66         String[] fileNames = b.getJDBCDriver().getJarFileNames();
67         for (int i = 0, length = fileNames == null ? 0 : fileNames.length; i < length; i++) {
68                         MetaDataXMLInterface.createElementText(bookmark,"driverLocation", fileNames[i]);
69                 }
70         Element otherSchemas = (Element) bookmark.appendChild(document.createElement("Other_Schemas")); //$NON-NLS-1$
71         otherSchemas.setAttribute("schemaRule", 
72                         b.useAllSchemas() 
73                                         ? "useAll" 
74                                         : b.useUsernameAsSchema() ? "useDefault" : "useSelected");
75         
76         Schema[] schemas = b.getSchemaSelections();
77         for (int i = 0, length = (schemas == null) ? 0 : schemas.length; i < length; i++) {
78             MetaDataXMLInterface.createElementText(
79                 otherSchemas,"schema", schemas[i].getName()); //$NON-NLS-1$
80         }
81         
82         Entity[] quickList = b.getQuickListEntries();
83         Element quickListEntity = document.createElement("quickList");
84         for (int j = 0, length = (quickList == null) ? 0 : quickList.length;
85             j < length;
86             j++) {
87             ModelToXMLConverter.getInstance().convert(quickListEntity, quickList[j]);
88         }
89         bookmark.appendChild(quickListEntity);
90         convertQueryList(b, bookmark);
91     }
92
93     
94     public void convert(Element parent, Entity entity) {
95         convert(parent, entity, true);
96     }
97     public void convert(Element parent, Entity entity, boolean recurse) {
98         Document document = parent.getOwnerDocument();
99         Element element = document.createElement(getEntityDOMNodeName(entity));
100         element.setAttribute("name", entity.getName());
101         if (entity.getSchema() != null) {
102             element.setAttribute("schema", entity.getSchema());
103         }
104         if (recurse) {
105             try {
106                                 convert(element, entity.getColumns());
107                         } catch (NotConnectedException e) {
108                         } catch (SQLException e) {
109                         }
110         }
111         parent.appendChild(element);
112     }
113
114     public void convert(Element element, Column[] columns) {
115         for (int i = 0, length = (columns == null) ? 0 : columns.length;
116             i < length;
117             i++) {
118             convert(element, columns[i]);
119         }
120     }
121
122     public void convert(Element parent, Column column) {
123         Document document = parent.getOwnerDocument();
124         Element element = document.createElement("column");
125         element.setAttribute("name", column.getName());
126         element.setAttribute("type", column.getTypeName());
127         element.setAttribute("primaryKey", column.isPrimaryKey() ? "true" : "false");
128         element.setAttribute("nullable", column.isNullable() ? "true" : "false");
129         if (column.isNumeric()) {
130             if (column.getSize() > 0) {
131                 element.setAttribute("size", String.valueOf(column.getSize()));
132             }
133             if (column.getNumberOfFractionalDigits() > 0) {
134                 element.setAttribute("numberOfFractionalDigits", String.valueOf(column.getNumberOfFractionalDigits()));
135             }
136         } else {
137             if (column.getSize() > 0) {
138                 element.setAttribute("size", String.valueOf(column.getSize()));
139             }
140         }
141                 if (column.getRemarks().length() > 0)
142                         MetaDataXMLInterface.createElementText(element, "remarks", column.getRemarks());
143         parent.appendChild(element);
144     }
145
146
147     public String getEntityDOMNodeName(Entity entity) {
148         if (Entity.TABLE_TYPE.equals(entity.getType())) {
149             return "table";
150         } else if (Entity.VIEW_TYPE.equals(entity.getType())) {
151             return "view";
152         } else if (Entity.SEQUENCE_TYPE.equals(entity.getType())) {
153             return "sequence";
154         } else {
155             return "unknown";
156         }
157     }
158
159     public void convertQueryList(
160         Bookmark bookmark,
161         Element parent) {
162             
163         Document document = parent.getOwnerDocument();
164         String[] queryList = bookmark.getQueries();
165         Element queryListEntity = document.createElement("queryList");
166         for (int j = 0, length = (queryList == null) ? 0 : queryList.length;
167             j < length;
168             j++) {
169             Element query = document.createElement("query");
170             
171             // will have more possibilities later
172             Element queryString = document.createElement("queryString");
173             Text queryText = document.createTextNode(queryList[j]);
174             queryString.appendChild(queryText);
175             query.appendChild(queryString);
176             queryListEntity.appendChild(query);
177         }
178         parent.appendChild(queryListEntity);
179     }
180     
181 }