001    /**
002     * Copyright (c) 2000-present 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.util;
016    
017    import com.liferay.portal.kernel.util.ArrayUtil;
018    import com.liferay.portal.kernel.util.DateUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.LocalizationUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portlet.expando.model.ExpandoColumnConstants;
025    
026    import java.io.Serializable;
027    
028    import java.text.DateFormat;
029    
030    import java.util.Date;
031    
032    /**
033     * @author Edward Han
034     * @author Michael C. Han
035     * @author Brian Wing Shun Chan
036     */
037    public class ExpandoConverterUtil {
038    
039            public static Serializable getAttributeFromString(
040                    int type, String attribute) {
041    
042                    if (attribute == null) {
043                            return null;
044                    }
045    
046                    if (type == ExpandoColumnConstants.BOOLEAN) {
047                            return GetterUtil.getBoolean(attribute);
048                    }
049                    else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
050                            return GetterUtil.getBooleanValues(StringUtil.split(attribute));
051                    }
052                    else if (type == ExpandoColumnConstants.DATE) {
053                            return _getDate(attribute);
054                    }
055                    else if (type == ExpandoColumnConstants.DATE_ARRAY) {
056                            return _getDates(StringUtil.split(attribute));
057                    }
058                    else if (type == ExpandoColumnConstants.DOUBLE) {
059                            return GetterUtil.getDouble(attribute);
060                    }
061                    else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
062                            return GetterUtil.getDoubleValues(StringUtil.split(attribute));
063                    }
064                    else if (type == ExpandoColumnConstants.FLOAT) {
065                            return GetterUtil.getFloat(attribute);
066                    }
067                    else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
068                            return GetterUtil.getFloatValues(StringUtil.split(attribute));
069                    }
070                    else if (type == ExpandoColumnConstants.INTEGER) {
071                            return GetterUtil.getInteger(attribute);
072                    }
073                    else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
074                            return GetterUtil.getIntegerValues(StringUtil.split(attribute));
075                    }
076                    else if (type == ExpandoColumnConstants.LONG) {
077                            return GetterUtil.getLong(attribute);
078                    }
079                    else if (type == ExpandoColumnConstants.LONG_ARRAY) {
080                            return GetterUtil.getLongValues(StringUtil.split(attribute));
081                    }
082                    else if (type == ExpandoColumnConstants.SHORT) {
083                            return GetterUtil.getShort(attribute);
084                    }
085                    else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
086                            return GetterUtil.getShortValues(StringUtil.split(attribute));
087                    }
088                    else if (type == ExpandoColumnConstants.STRING_ARRAY) {
089                            return StringUtil.split(attribute);
090                    }
091                    else if (type == ExpandoColumnConstants.STRING_LOCALIZED) {
092                            return (Serializable)LocalizationUtil.getLocalizationMap(attribute);
093                    }
094                    else {
095                            return attribute;
096                    }
097            }
098    
099            public static Serializable getAttributeFromStringArray(
100                    int type, String[] attribute) {
101    
102                    if (ArrayUtil.isEmpty(attribute)) {
103                            return null;
104                    }
105    
106                    if (type == ExpandoColumnConstants.BOOLEAN) {
107                            return GetterUtil.getBoolean(attribute[0]);
108                    }
109                    else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
110                            return GetterUtil.getBooleanValues(attribute);
111                    }
112                    else if (type == ExpandoColumnConstants.DATE) {
113                            return _getDate(attribute[0]);
114                    }
115                    else if (type == ExpandoColumnConstants.DATE_ARRAY) {
116                            return _getDates(attribute);
117                    }
118                    else if (type == ExpandoColumnConstants.DOUBLE) {
119                            return GetterUtil.getDouble(attribute[0]);
120                    }
121                    else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
122                            return GetterUtil.getDoubleValues(attribute);
123                    }
124                    else if (type == ExpandoColumnConstants.FLOAT) {
125                            return GetterUtil.getFloat(attribute[0]);
126                    }
127                    else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
128                            return GetterUtil.getFloatValues(attribute);
129                    }
130                    else if (type == ExpandoColumnConstants.INTEGER) {
131                            return GetterUtil.getInteger(attribute[0]);
132                    }
133                    else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
134                            return GetterUtil.getIntegerValues(attribute);
135                    }
136                    else if (type == ExpandoColumnConstants.LONG) {
137                            return GetterUtil.getLong(attribute[0]);
138                    }
139                    else if (type == ExpandoColumnConstants.LONG_ARRAY) {
140                            return GetterUtil.getLongValues(attribute);
141                    }
142                    else if (type == ExpandoColumnConstants.SHORT) {
143                            return GetterUtil.getShort(attribute[0]);
144                    }
145                    else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
146                            return GetterUtil.getShortValues(attribute);
147                    }
148                    else if (type == ExpandoColumnConstants.STRING) {
149                            return attribute[0];
150                    }
151                    else {
152                            return attribute;
153                    }
154            }
155    
156            public static String getStringFromAttribute(
157                    int type, Serializable attribute) {
158    
159                    if (attribute == null) {
160                            return StringPool.BLANK;
161                    }
162    
163                    if ((type == ExpandoColumnConstants.BOOLEAN) ||
164                            (type == ExpandoColumnConstants.DOUBLE) ||
165                            (type == ExpandoColumnConstants.FLOAT) ||
166                            (type == ExpandoColumnConstants.INTEGER) ||
167                            (type == ExpandoColumnConstants.LONG) ||
168                            (type == ExpandoColumnConstants.SHORT)) {
169    
170                            return String.valueOf(attribute);
171                    }
172                    else if ((type == ExpandoColumnConstants.BOOLEAN_ARRAY) ||
173                                     (type == ExpandoColumnConstants.DOUBLE_ARRAY) ||
174                                     (type == ExpandoColumnConstants.FLOAT_ARRAY) ||
175                                     (type == ExpandoColumnConstants.INTEGER_ARRAY) ||
176                                     (type == ExpandoColumnConstants.LONG_ARRAY) ||
177                                     (type == ExpandoColumnConstants.SHORT_ARRAY) ||
178                                     (type == ExpandoColumnConstants.STRING_ARRAY)) {
179    
180                            return StringUtil.merge(
181                                    ArrayUtil.toStringArray((Object[])attribute));
182                    }
183                    else if (type == ExpandoColumnConstants.DATE) {
184                            DateFormat dateFormat = _getDateFormat();
185    
186                            return dateFormat.format((Date)attribute);
187                    }
188                    else if (type == ExpandoColumnConstants.DATE_ARRAY) {
189                            return StringUtil.merge(
190                                    ArrayUtil.toStringArray((Date[])attribute, _getDateFormat()));
191                    }
192                    else {
193                            return attribute.toString();
194                    }
195            }
196    
197            private static Date _getDate(String dateString) {
198                    if (Validator.isNumber(dateString)) {
199                            return new Date(GetterUtil.getLong(dateString));
200                    }
201    
202                    return GetterUtil.getDate(dateString, _getDateFormat());
203            }
204    
205            private static DateFormat _getDateFormat() {
206                    return DateUtil.getISO8601Format();
207            }
208    
209            private static Date[] _getDates(String[] dateStrings) {
210                    Date[] dates = new Date[dateStrings.length];
211    
212                    for (int i = 0; i < dateStrings.length; i++) {
213                            dates[i] = _getDate(dateStrings[i]);
214                    }
215    
216                    return dates;
217            }
218    
219    }