Replace deprecated DOM.setElementAttribute().
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / shared / libglom / Field.java
1 package org.glom.web.shared.libglom;
2
3 import org.glom.web.shared.libglom.layout.Formatting;
4
5 public class Field extends Translatable {
6
7         private static final long serialVersionUID = 5297785500678189743L;
8
9         public enum GlomFieldType {
10                 TYPE_INVALID, TYPE_NUMERIC, TYPE_TEXT, TYPE_DATE, TYPE_TIME, TYPE_BOOLEAN, TYPE_IMAGE
11         };
12
13         private GlomFieldType glomFieldType; // TODO: = glom_field_type.TYPE_INVALID;
14         private boolean primaryKey = false;
15
16         // TODO: Add a setter, and remove final.
17         /*
18          * Don't make this final, because that breaks GWT serialization. See
19          * http://code.google.com/p/google-web-toolkit/issues/detail?id=1054
20          */
21         private/* final */boolean uniqueKey = false;
22
23         Formatting formatting = new Formatting(); // Not null, so we have some default formatting.
24
25         /**
26          * @return the formatting
27          */
28         public Formatting getFormatting() {
29                 return formatting;
30         }
31
32         /**
33          * @param formatting
34          *            the formatting to set
35          */
36         public void setFormatting(final Formatting formatting) {
37                 this.formatting = formatting;
38         }
39
40         /**
41          * @return
42          */
43         public boolean getPrimaryKey() {
44                 return primaryKey;
45         };
46
47         public void setPrimaryKey(final boolean primaryKey) {
48                 this.primaryKey = primaryKey;
49         }
50
51         /**
52          * @return
53          */
54         public GlomFieldType getGlomType() {
55                 return glomFieldType;
56         }
57
58         public void setGlomFieldType(final GlomFieldType fieldType) {
59                 this.glomFieldType = fieldType;
60         }
61
62         /**
63          * @return
64          */
65         public boolean getUniqueKey() {
66                 return uniqueKey;
67         }
68
69         public enum SqlDialect {
70                 POSTGRESQL,
71                 MYSQL
72         };
73
74         /**
75          * @return
76          */
77         public String getSqlType(SqlDialect sqlDialect) {
78                 // libglom uses libgda's map of Gda types and its API,
79                 // without hardcoding the actual SQL type names.
80                 
81                 if (sqlDialect == SqlDialect.POSTGRESQL) {
82                         // This is based on what libgda actually uses with PostgreSQL.
83                         switch (getGlomType()) {
84                         case TYPE_NUMERIC:
85                                 return "numeric";
86                         case TYPE_TEXT:
87                                 return "character varying";
88                         case TYPE_DATE:
89                                 return "date";
90                         case TYPE_TIME:
91                                 return "time with time zone";
92                         case TYPE_IMAGE:
93                                 return "bytea";
94                         default:
95                                 return "unknowntype";
96                         }
97                 } else { // MYSQL
98                         // This is based on what Glom actually uses with MySQL.
99                         switch (getGlomType()) {
100                         case TYPE_NUMERIC:
101                                 return "double";
102                         case TYPE_TEXT:
103                                 return "varchar(255)";
104                         case TYPE_DATE:
105                                 return "date";
106                         case TYPE_TIME:
107                                 return "time with time zone";
108                         case TYPE_IMAGE:
109                                 return "blob";
110                         default:
111                                 return "unknowntype";
112                         }
113                 }
114         }
115 }