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;
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.ObjectInputStream;
27 import java.io.ObjectOutputStream;
28 import java.text.ParseException;
29 import java.text.SimpleDateFormat;
30 import java.util.Date;
32 import org.apache.commons.lang3.StringUtils;
33 import org.apache.http.client.utils.URIBuilder;
34 import org.glom.web.shared.TypedDataItem;
35 import org.glom.web.shared.libglom.Field.GlomFieldType;
36 import org.glom.web.shared.libglom.layout.LayoutItemField;
44 * This method safely converts longs from libglom into ints. This method was taken from stackoverflow:
46 * http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
48 public static int safeLongToInt(final long value) {
49 if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
50 throw new IllegalArgumentException(value + " cannot be cast to int without changing its value.");
55 public static String getFileName(final String fileURI) {
56 final String[] splitURI = fileURI.split(File.separator);
57 return splitURI[splitURI.length - 1];
60 static public Object deepCopy(final Object oldObj) {
61 ObjectOutputStream oos = null;
62 ObjectInputStream ois = null;
65 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
66 oos = new ObjectOutputStream(bos);
67 // serialize and pass the object
68 oos.writeObject(oldObj);
70 final ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
71 ois = new ObjectInputStream(bin);
73 // return the new object
74 return ois.readObject();
75 } catch (final Exception e) {
76 System.out.println("Exception in deepCopy:" + e);
82 } catch (final IOException e) {
83 System.out.println("Exception in deepCopy during finally: " + e);
89 /** Build the URL for the service that will return the binary data for an image.
91 * @param primaryKeyValue
95 public static String buildImageDataUrl(final TypedDataItem primaryKeyValue, final String documentID, final String tableName, final LayoutItemField field) {
96 final URIBuilder uriBuilder = buildImageDataUrlStart(documentID, tableName);
98 //TODO: Handle other types:
99 if(primaryKeyValue != null) {
100 uriBuilder.setParameter("value", Double.toString(primaryKeyValue.getNumber()));
103 uriBuilder.setParameter("field", field.getName());
104 return uriBuilder.toString();
107 /** Build the URL for the service that will return the binary data for an image.
109 * @param primaryKeyValue
113 public static String buildImageDataUrl(final String documentID, final String tableName, final String layoutName, final int[] path) {
114 final URIBuilder uriBuilder = buildImageDataUrlStart(documentID, tableName);
115 uriBuilder.setParameter("layout", layoutName);
116 uriBuilder.setParameter("layoutpath", buildLayoutPath(path));
117 return uriBuilder.toString();
125 private static URIBuilder buildImageDataUrlStart(final String documentID, final String tableName) {
126 final URIBuilder uriBuilder = new URIBuilder();
127 //uriBuilder.setHost(GWT.getModuleBaseURL());
128 uriBuilder.setPath("OnlineGlom/gwtGlomImages"); //The name of our images servlet. See OnlineGlomImages.
129 uriBuilder.setParameter("document", documentID);
130 uriBuilder.setParameter("table", tableName);
134 /** Build a :-separated string to represent the path as a string.
138 public static String buildLayoutPath(int[] path) {
139 if((path == null) || (path.length == 0)) {
143 String result = new String();
145 if(!result.isEmpty()) {
149 final String strIndex = Integer.toString(i);
156 /** Get an array of int indices from the :-separated string.
157 * See buildLayoutPath().
159 * @param attrLayoutPath
160 * @return The array of indices of the layout items.
162 public static int[] parseLayoutPath(final String attrLayoutPath) {
163 if(StringUtils.isEmpty(attrLayoutPath)) {
167 final String[] strIndices = attrLayoutPath.split(":");
168 final int[] indices = new int[strIndices.length];
169 for (int i = 0; i < strIndices.length; ++i) {
170 final String str = strIndices[i];
174 indices[i] = Integer.parseInt(str);
176 catch (final NumberFormatException nfe)
178 //TODO: Log the error.
186 public static void transformUnknownToActualType(final TypedDataItem dataItem, final GlomFieldType actualType) {
187 if(dataItem.getType() == actualType)
190 String unknownText = dataItem.getUnknown();
192 //Avoid repeated checks for null:
193 if (unknownText == null) {
199 // TODO: Is this really locale-independent?
201 if(!StringUtils.isEmpty(unknownText)) {
203 number = Double.parseDouble(unknownText);
204 } catch (final NumberFormatException e) {
209 dataItem.setNumber(number);
212 dataItem.setText(unknownText);
215 final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
218 date = formatter.parse(unknownText);
219 } catch (ParseException e) {
220 // TODO Auto-generated catch block
224 dataItem.setDate(date);
228 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
231 date = formatter.parse(unknownText);
232 } catch (ParseException e) {
233 // TODO Auto-generated catch block
241 final boolean bool = unknownText.equals("true");
242 dataItem.setBoolean(bool); //TODO
245 dataItem.setImageDataUrl(unknownText);
246 //setImageData(null);//TODO: Though this is only used for primary keys anyway.
251 break; //TODO: Warn because this is unusual?