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