001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.expando.model;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.LocaleUtil;
022    
023    import java.io.Serializable;
024    
025    import java.text.DateFormat;
026    
027    import java.util.Date;
028    
029    /**
030     * @author Raymond Aug??
031     * @author Alexander Chow
032     * @author Marcellus Tavares
033     */
034    public class ExpandoColumnConstants {
035    
036            public static final int BOOLEAN = 1;
037    
038            public static final int BOOLEAN_ARRAY = 2;
039    
040            public static final String BOOLEAN_ARRAY_LABEL =
041                    "custom.field.boolean.array";
042    
043            public static final String BOOLEAN_LABEL = "custom.field.boolean";
044    
045            public static final int DATE = 3;
046    
047            public static final int DATE_ARRAY = 4;
048    
049            public static final String DATE_ARRAY_LABEL =
050                    "custom.field.java.util.Date.array";
051    
052            public static final String DATE_LABEL = "custom.field.java.util.Date";
053    
054            public static final int DOUBLE = 5;
055    
056            public static final int DOUBLE_ARRAY = 6;
057    
058            public static final String DOUBLE_ARRAY_LABEL = "custom.field.double.array";
059    
060            public static final String DOUBLE_LABEL = "custom.field.double";
061    
062            public static final int FLOAT = 7;
063    
064            public static final int FLOAT_ARRAY = 8;
065    
066            public static final String FLOAT_ARRAY_LABEL = "custom.field.float.array";
067    
068            public static final String FLOAT_LABEL = "custom.field.float";
069    
070            public static final String INDEX_TYPE = "index-type";
071    
072            public static final int INDEX_TYPE_KEYWORD = 2;
073    
074            public static final int INDEX_TYPE_NONE = 0;
075    
076            public static final int INDEX_TYPE_TEXT = 1;
077    
078            public static final int INTEGER = 9;
079    
080            public static final int INTEGER_ARRAY = 10;
081    
082            public static final String INTEGER_ARRAY_LABEL = "custom.field.int.array";
083    
084            public static final String INTEGER_LABEL = "custom.field.int";
085    
086            public static final int LONG = 11;
087    
088            public static final int LONG_ARRAY = 12;
089    
090            public static final String LONG_ARRAY_LABEL = "custom.field.long.array";
091    
092            public static final String LONG_LABEL = "custom.field.long";
093    
094            public static final int NUMBER = 17;
095    
096            public static final int NUMBER_ARRAY = 18;
097    
098            public static final String NUMBER_ARRAY_LABEL = "custom.field.number.array";
099    
100            public static final String NUMBER_LABEL = "custom.field.number";
101    
102            public static final String PROPERTY_DISPLAY_TYPE = "display-type";
103    
104            public static final String PROPERTY_DISPLAY_TYPE_CHECKBOX = "checkbox";
105    
106            public static final String PROPERTY_DISPLAY_TYPE_RADIO = "radio";
107    
108            public static final String PROPERTY_DISPLAY_TYPE_SELECTION_LIST =
109                    "selection-list";
110    
111            public static final String PROPERTY_DISPLAY_TYPE_TEXT_BOX = "text-box";
112    
113            public static final String PROPERTY_HEIGHT = "height";
114    
115            public static final String PROPERTY_HIDDEN = "hidden";
116    
117            public static final String PROPERTY_SECRET = "secret";
118    
119            public static final String PROPERTY_VISIBLE_WITH_UPDATE_PERMISSION =
120                    "visible-with-update-permission";
121    
122            public static final String PROPERTY_WIDTH = "width";
123    
124            public static final int SHORT = 13;
125    
126            public static final int SHORT_ARRAY = 14;
127    
128            public static final String SHORT_ARRAY_LABEL = "custom.field.short.array";
129    
130            public static final String SHORT_LABEL = "custom.field.short";
131    
132            public static final int STRING = 15;
133    
134            public static final int STRING_ARRAY = 16;
135    
136            public static final String STRING_ARRAY_LABEL =
137                    "custom.field.java.lang.String.array";
138    
139            public static final String STRING_LABEL = "custom.field.java.lang.String";
140    
141            public static final int[] TYPES = new int[] {
142                    BOOLEAN, BOOLEAN_ARRAY, DATE, DATE_ARRAY, DOUBLE, DOUBLE_ARRAY, FLOAT,
143                    FLOAT_ARRAY, INTEGER, INTEGER_ARRAY, LONG, LONG_ARRAY, NUMBER,
144                    NUMBER_ARRAY, SHORT, SHORT_ARRAY, STRING, STRING_ARRAY
145            };
146    
147            public static final String UNKNOWN_LABEL = "Unknown";
148    
149            public static final Serializable getSerializable(int type, String value) {
150                    if (type == BOOLEAN) {
151                            return GetterUtil.getBoolean(value);
152                    }
153                    else if (type == BOOLEAN_ARRAY) {
154                            return new Boolean[] {GetterUtil.getBoolean(value)};
155                    }
156                    else if (type == DATE) {
157                            try {
158                                    DateFormat dateFormat = DateFormatFactoryUtil.getDateTime(
159                                            LocaleUtil.getDefault());
160    
161                                    return dateFormat.parse(value);
162                            }
163                            catch (Exception e) {
164                                    _log.warn("Unable to parse date " + value, e);
165                            }
166                    }
167                    else if (type == DATE_ARRAY) {
168                            Serializable dateSerializable = getSerializable(DATE, value);
169    
170                            if (dateSerializable instanceof Date) {
171                                    return new Date[] {(Date)dateSerializable};
172                            }
173                    }
174                    else if (type == DOUBLE) {
175                            return GetterUtil.getDouble(value);
176                    }
177                    else if (type == DOUBLE_ARRAY) {
178                            return new double[] {GetterUtil.getDouble(value)};
179                    }
180                    else if (type == FLOAT) {
181                            return GetterUtil.getFloat(value);
182                    }
183                    else if (type == FLOAT_ARRAY) {
184                            return new float[] {GetterUtil.getFloat(value)};
185                    }
186                    else if (type == INTEGER) {
187                            return GetterUtil.getInteger(value);
188                    }
189                    else if (type == INTEGER_ARRAY) {
190                            return new int[] {GetterUtil.getInteger(value)};
191                    }
192                    else if (type == LONG) {
193                            return GetterUtil.getLong(value);
194                    }
195                    else if (type == LONG_ARRAY) {
196                            return new long[] {GetterUtil.getLong(value)};
197                    }
198                    else if (type == NUMBER) {
199                            return GetterUtil.getNumber(value);
200                    }
201                    else if (type == NUMBER_ARRAY) {
202                            return new Number[] {GetterUtil.getNumber(value)};
203                    }
204                    else if (type == SHORT) {
205                            return GetterUtil.getShort(value);
206                    }
207                    else if (type == SHORT_ARRAY) {
208                            return new short[] {GetterUtil.getShort(value)};
209                    }
210                    else if (type == STRING_ARRAY) {
211                            return new String[] {value};
212                    }
213    
214                    return value;
215            }
216    
217            public static final String getTypeLabel(int type) {
218                    if (type == BOOLEAN) {
219                            return BOOLEAN_LABEL;
220                    }
221                    else if (type == BOOLEAN_ARRAY) {
222                            return BOOLEAN_ARRAY_LABEL;
223                    }
224                    else if (type == DATE) {
225                            return DATE_LABEL;
226                    }
227                    else if (type == DATE_ARRAY) {
228                            return DATE_ARRAY_LABEL;
229                    }
230                    else if (type == DOUBLE) {
231                            return DOUBLE_LABEL;
232                    }
233                    else if (type == DOUBLE_ARRAY) {
234                            return DOUBLE_ARRAY_LABEL;
235                    }
236                    else if (type == FLOAT) {
237                            return FLOAT_LABEL;
238                    }
239                    else if (type == FLOAT_ARRAY) {
240                            return FLOAT_ARRAY_LABEL;
241                    }
242                    else if (type == INTEGER) {
243                            return INTEGER_LABEL;
244                    }
245                    else if (type == INTEGER_ARRAY) {
246                            return INTEGER_ARRAY_LABEL;
247                    }
248                    else if (type == LONG) {
249                            return LONG_LABEL;
250                    }
251                    else if (type == LONG_ARRAY) {
252                            return LONG_ARRAY_LABEL;
253                    }
254                    else if (type == NUMBER) {
255                            return NUMBER_LABEL;
256                    }
257                    else if (type == NUMBER_ARRAY) {
258                            return NUMBER_ARRAY_LABEL;
259                    }
260                    else if (type == SHORT) {
261                            return SHORT_LABEL;
262                    }
263                    else if (type == SHORT_ARRAY) {
264                            return SHORT_ARRAY_LABEL;
265                    }
266                    else if (type == STRING) {
267                            return STRING_LABEL;
268                    }
269                    else if (type == STRING_ARRAY) {
270                            return STRING_ARRAY_LABEL;
271                    }
272    
273                    return UNKNOWN_LABEL;
274            }
275    
276            private static Log _log = LogFactoryUtil.getLog(
277                    ExpandoColumnConstants.class);
278    
279    }