Remove all javadoc author tags.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / server / Utils.java
1 /*
2  * Copyright (C) 2011 Openismus GmbH
3  *
4  * This file is part of GWT-Glom.
5  *
6  * GWT-Glom is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with GWT-Glom.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package org.glom.web.server;
21
22 import java.io.File;
23
24 import org.glom.libglom.Field.glom_field_type;
25 import org.glom.libglom.Value;
26 import org.glom.web.shared.TypedDataItem;
27 import org.glom.web.shared.layout.LayoutItemField.GlomFieldType;
28
29 /**
30  *
31  */
32 public class Utils {
33
34         /*
35          * This method safely converts longs from libglom into ints. This method was taken from stackoverflow:
36          * 
37          * http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
38          */
39         public static int safeLongToInt(long value) {
40                 if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
41                         throw new IllegalArgumentException(value + " cannot be cast to int without changing its value.");
42                 }
43                 return (int) value;
44         }
45
46         public static String getFileName(String fileURI) {
47                 String[] splitURI = fileURI.split(File.separator);
48                 return splitURI[splitURI.length - 1];
49         }
50
51         public static Value getGlomTypeGdaValueForTypedDataItem(String documentID, String tableName,
52                         glom_field_type glomType, TypedDataItem dataItem) {
53                 Value gdaValue = null;
54
55                 switch (glomType) {
56                 case TYPE_NUMERIC:
57
58                         if (dataItem.isEmpty()) {
59                                 // No data has been set on the TypedDataItem. Use an empty value.
60                                 gdaValue = new Value();
61
62                         } else if (dataItem.getType() == GlomFieldType.TYPE_NUMERIC) {
63                                 // non-empty data, numeric type:
64                                 // Trust the data in the TypedDataItem because the types match.
65                                 gdaValue = new Value(dataItem.getNumber());
66
67                         } else if (dataItem.getType() == GlomFieldType.TYPE_INVALID) {
68                                 // non-empty data, invalid type:
69                                 // An invalid type that's not empty indicates that the TypeDataItem has been created from a URL string.
70                                 // The string will be converted into the Glom type (numeric).
71                                 try {
72                                         // non-locale specific string-to-number conversion:
73                                         // http://docs.oracle.com/javase/6/docs/api/java/lang/Double.html#valueOf%28java.lang.String%29
74                                         gdaValue = new Value(Double.parseDouble(dataItem.getUnknown()));
75                                 } catch (Exception e) {
76                                         // Use an empty Value when the number conversion doesn't work.
77                                         gdaValue = new Value();
78                                 }
79
80                         } else {
81                                 // non-empty data, mis-matched types:
82                                 // Don't use the data when the type doesn't match the type from the Glom document.
83                                 logTypeMismatchError(documentID, tableName, glomType, dataItem);
84                                 gdaValue = new Value(); // an empty Value
85                         }
86                         break;
87
88                 case TYPE_TEXT:
89
90                         if (dataItem.isEmpty()) {
91                                 // No data has been set on the TypedDataItem. Use an empty string value.
92                                 gdaValue = new Value("");
93
94                         } else if (dataItem.getType() == GlomFieldType.TYPE_TEXT) {
95                                 // non-empty data, text type:
96                                 // Trust the data in the TypedDataItem because the types match.
97                                 gdaValue = new Value(dataItem.getText());
98
99                         } else if (dataItem.getType() == GlomFieldType.TYPE_INVALID) {
100                                 // non-empty data, invalid type:
101                                 // An invalid type that's not empty indicates that primary key value has been created from a URL string.
102                                 // The string will be converted into the Glom type (text).
103                                 gdaValue = new Value(dataItem.getUnknown());
104
105                         } else {
106                                 // non-empty data, mis-matched types:
107                                 // Don't use the primary key value when the type doesn't match the type from the Glom document.
108                                 logTypeMismatchError(documentID, tableName, glomType, dataItem);
109                                 gdaValue = new Value(""); // an emtpy string Value
110                         }
111                         break;
112
113                 default:
114                         Log.error(documentID, tableName, "Unable to create a Gda Value of type: " + glomType
115                                         + " based on data of type: " + dataItem.getType() + ".");
116                         Log.warn(documentID, tableName, "The data item is being ignored. This is a Bug.");
117                         gdaValue = new Value(); // an empty Value
118                         break;
119                 }
120
121                 return gdaValue;
122         }
123
124         private static void logTypeMismatchError(String documentID, String tableName, glom_field_type glomType,
125                         TypedDataItem dataItem) {
126
127                 String dataItemString;
128                 switch (dataItem.getType()) {
129                 case TYPE_BOOLEAN:
130                         dataItemString = Boolean.toString(dataItem.getBoolean());
131                         break;
132                 case TYPE_NUMERIC:
133                         dataItemString = Double.toString(dataItem.getNumber());
134                         break;
135                 case TYPE_INVALID:
136                         dataItemString = dataItem.getUnknown();
137                         break;
138                 case TYPE_TEXT:
139                         dataItemString = dataItem.getText();
140                         break;
141                 default:
142                         dataItemString = "";
143                         break;
144                 }
145
146                 Log.error(documentID, tableName, "The data with type: " + dataItem.getType() + " and value: " + dataItemString
147                                 + " doesn't match the expected type from the Glom document: " + glomType + ".");
148                 Log.error(documentID, tableName, "The data item is being ignored. This is a bug.");
149         }
150
151 }