2 * Copyright (C) 2011 Openismus GmbH
4 * This file is part of GWT-Glom.
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.
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
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/>.
20 package org.glom.web.server;
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;
35 * This method safely converts longs from libglom into ints. This method was taken from stackoverflow:
37 * http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
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.");
46 public static String getFileName(String fileURI) {
47 String[] splitURI = fileURI.split(File.separator);
48 return splitURI[splitURI.length - 1];
51 public static Value getGlomTypeGdaValueForTypedDataItem(String documentID, String tableName,
52 glom_field_type glomType, TypedDataItem dataItem) {
53 Value gdaValue = null;
58 if (dataItem.isEmpty()) {
59 // No data has been set on the TypedDataItem. Use an empty value.
60 gdaValue = new Value();
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());
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).
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();
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
90 if (dataItem.isEmpty()) {
91 // No data has been set on the TypedDataItem. Use an empty string value.
92 gdaValue = new Value("");
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());
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());
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
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
124 private static void logTypeMismatchError(String documentID, String tableName, glom_field_type glomType,
125 TypedDataItem dataItem) {
127 String dataItemString;
128 switch (dataItem.getType()) {
130 dataItemString = Boolean.toString(dataItem.getBoolean());
133 dataItemString = Double.toString(dataItem.getNumber());
136 dataItemString = dataItem.getUnknown();
139 dataItemString = dataItem.getText();
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.");