Document: Static image items: Store an image URL, using the layout path.
[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.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.File;
25 import java.io.IOException;
26 import java.io.ObjectInputStream;
27 import java.io.ObjectOutputStream;
28
29 import org.apache.commons.lang3.StringUtils;
30 import org.apache.http.client.utils.URIBuilder;
31 import org.glom.web.shared.TypedDataItem;
32 import org.glom.web.shared.libglom.layout.LayoutItemField;
33
34 /**
35  *
36  */
37 public class Utils {
38
39         /*
40          * This method safely converts longs from libglom into ints. This method was taken from stackoverflow:
41          * 
42          * http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
43          */
44         public static int safeLongToInt(final long value) {
45                 if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
46                         throw new IllegalArgumentException(value + " cannot be cast to int without changing its value.");
47                 }
48                 return (int) value;
49         }
50
51         public static String getFileName(final String fileURI) {
52                 final String[] splitURI = fileURI.split(File.separator);
53                 return splitURI[splitURI.length - 1];
54         }
55
56         static public Object deepCopy(final Object oldObj) {
57                 ObjectOutputStream oos = null;
58                 ObjectInputStream ois = null;
59
60                 try {
61                         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
62                         oos = new ObjectOutputStream(bos);
63                         // serialize and pass the object
64                         oos.writeObject(oldObj);
65                         oos.flush();
66                         final ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
67                         ois = new ObjectInputStream(bin);
68
69                         // return the new object
70                         return ois.readObject();
71                 } catch (final Exception e) {
72                         System.out.println("Exception in deepCopy:" + e);
73                         return null;
74                 } finally {
75                         try {
76                                 oos.close();
77                                 ois.close();
78                         } catch (final IOException e) {
79                                 System.out.println("Exception in deepCopy during finally: " + e);
80                                 return null;
81                         }
82                 }
83         }
84
85         /** Build the URL for the service that will return the binary data for an image.
86          * 
87          * @param primaryKeyValue
88          * @param field
89          * @return
90          */
91         public static String buildImageDataUrl(final String documentID, final String tableName, final String layoutName, final int[] path) {
92                 final URIBuilder uriBuilder = buildImageDataUrlStart(documentID, tableName);
93                 uriBuilder.setParameter("layout", layoutName);
94                 uriBuilder.setParameter("layoutpath", buildLayoutPath(path));
95                 return uriBuilder.toString();
96         }
97
98         /**
99          * @param documentID
100          * @param tableName
101          * @return
102          */
103         private static URIBuilder buildImageDataUrlStart(final String documentID, final String tableName) {
104                 final URIBuilder uriBuilder = new URIBuilder();
105                 //uriBuilder.setHost(GWT.getModuleBaseURL());
106                 uriBuilder.setPath("OnlineGlom/gwtGlomImages"); //The name of our images servlet. See OnlineGlomImages.
107                 uriBuilder.setParameter("document", documentID);
108                 uriBuilder.setParameter("table", tableName);
109                 return uriBuilder;
110         }
111
112         /** Build a :-separated string to represent the path as a string.
113          * @param path
114          * @return
115          */
116         public static String buildLayoutPath(int[] path) {
117                 if((path == null) || (path.length == 0)) {
118                         return null;
119                 }
120
121                 String result = new String();
122                 for(int i:path) {
123                         if(!result.isEmpty()) {
124                                 result += ":";
125                         }
126                         
127                         final String strIndex = Integer.toString(i);
128                         result += strIndex;
129                 }
130                 
131                 return result;
132         }
133
134         /** Get an array of int indices from the :-separated string.
135          * See buildLayoutPath().
136          * 
137          * @param attrLayoutPath
138          * @return The array of indices of the layout items.
139          */
140         public static int[] parseLayoutPath(final String attrLayoutPath) {
141                 if(StringUtils.isEmpty(attrLayoutPath)) {
142                         return null;
143                 }
144                 
145                 final String[] strIndices = attrLayoutPath.split(":");
146                 final int[] indices = new int[strIndices.length];
147                 for (int i = 0; i < strIndices.length; ++i) {
148                         final String str = strIndices[i];
149         
150                         try
151                         {
152                                 indices[i] = Integer.parseInt(str);
153                         }
154                         catch (final NumberFormatException nfe)
155                         {
156                                 //TODO: Log the error.
157                                 return null;
158                         }
159                 }
160         
161                 return indices;
162         }
163
164 }