latest quantum sources 2.3.2
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / model / xml / TorqueConverter.java
1 package com.quantum.model.xml;
2
3 import java.lang.reflect.Field;
4 import java.lang.reflect.Modifier;
5 import java.sql.SQLException;
6 import java.sql.Types;
7
8 import com.quantum.model.Bookmark;
9 import com.quantum.model.Column;
10 import com.quantum.model.Entity;
11 import com.quantum.model.NotConnectedException;
12 import com.quantum.model.Schema;
13
14 import org.w3c.dom.Document;
15 import org.w3c.dom.Element;
16
17 /**
18  * @author BC
19  */
20 public class TorqueConverter {
21     public void createRoot(Document document) {
22         Element root = document.createElement("database");
23         document.appendChild(root);
24     }
25     public void convert(Element root, Bookmark bookmark, Schema schema) {
26         try {
27             Entity[] tables = bookmark.getEntitiesForSchema(
28                 schema, Entity.TABLE_TYPE);
29                 
30             for (int i = 0, length = (tables == null) ? 0 : tables.length;
31                 i < length;
32                 i++) {
33                 convert(root, tables[i]);
34             }
35         } catch (SQLException e) {
36         }
37     }
38
39     public void convert(Element root, Entity entity) {
40         Element table = root.getOwnerDocument().createElement("table");
41         table.setAttribute("name", entity.getName());
42         
43                 try {
44                         Column[] columns = entity.getColumns();
45                         for (int i = 0, length = (columns == null) ? 0 : columns.length;
46                     i < length;
47                     i++) {
48                     convert(table, columns[i]);
49                 }
50                 } catch (NotConnectedException e) {
51                 } catch (SQLException e) {
52                 }
53         
54         root.appendChild(table);
55     }
56
57     public void convert(Element root, Column column) {
58         Element element = root.getOwnerDocument().createElement("column");
59         element.setAttribute("name", column.getName());
60         if (column.isPrimaryKey()) {
61             element.setAttribute("primaryKey", "true");
62         }
63         element.setAttribute("required", column.isNullable() ? "false" : "true");
64         if (column.isNumeric() && column.getNumberOfFractionalDigits() > 0) {
65             element.setAttribute("size", String.valueOf(column.getSize()) + 
66                 "," + String.valueOf(column.getNumberOfFractionalDigits()));
67         } else if (column.getSize() >= 0) {
68             element.setAttribute("size", String.valueOf(column.getSize()));
69         }
70         element.setAttribute("type", getStandardType(column.getType()));
71         
72         root.appendChild(element);
73     }
74     
75     private String getStandardType(int type) {
76         String result = null;
77         try {
78             Field[] fields = Types.class.getFields();
79             for (int i = 0, length = (fields == null) ? 0 : fields.length;
80                 result == null & i < length;
81                 i++) {
82                 if (fields[i].getDeclaringClass() == Integer.TYPE &&
83                     Modifier.isStatic(fields[i].getModifiers()) &&
84                     Modifier.isPublic(fields[i].getModifiers()) &&
85                     type == fields[i].getLong(null)) {
86                     
87                     result = fields[i].getName();
88                 }
89             }
90         } catch (IllegalAccessException e) {
91             // shouldn't happen
92         }
93         return result;
94     }
95 }