001    /**
002     * Copyright (c) 2000-2010 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    import java.text.SimpleDateFormat;
028    
029    import java.util.Date;
030    
031    /**
032     * @author Edward Han
033     * @author Michael C. Han
034     * @author Brian Wing Shun Chan
035     */
036    public class ExpandoConverterUtil {
037    
038            public static Serializable getAttributeFromString(
039                    int type, String attribute) {
040    
041                    if (attribute == null) {
042                            return null;
043                    }
044    
045                    if (type == ExpandoColumnConstants.BOOLEAN) {
046                            return GetterUtil.getBoolean(attribute);
047                    }
048                    else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
049                            return GetterUtil.getBooleanValues(StringUtil.split(attribute));
050                    }
051                    else if (type == ExpandoColumnConstants.DATE) {
052                            return GetterUtil.getDate(attribute, _getDateFormat());
053                    }
054                    else if (type == ExpandoColumnConstants.DATE_ARRAY) {
055                            return GetterUtil.getDateValues(
056                                    StringUtil.split(attribute), _getDateFormat());
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 {
092                            return attribute;
093                    }
094            }
095    
096            public static String getStringFromAttribute(
097                    int type, Serializable attribute) {
098    
099                    if (attribute == null) {
100                            return StringPool.BLANK;
101                    }
102    
103                    if ((type == ExpandoColumnConstants.BOOLEAN) ||
104                            (type == ExpandoColumnConstants.DOUBLE) ||
105                            (type == ExpandoColumnConstants.FLOAT) ||
106                            (type == ExpandoColumnConstants.INTEGER) ||
107                            (type == ExpandoColumnConstants.LONG) ||
108                            (type == ExpandoColumnConstants.SHORT)) {
109    
110                            return String.valueOf(attribute);
111                    }
112                    else if ((type == ExpandoColumnConstants.BOOLEAN_ARRAY) ||
113                                     (type == ExpandoColumnConstants.DOUBLE_ARRAY) ||
114                                     (type == ExpandoColumnConstants.FLOAT_ARRAY) ||
115                                     (type == ExpandoColumnConstants.INTEGER_ARRAY) ||
116                                     (type == ExpandoColumnConstants.LONG_ARRAY) ||
117                                     (type == ExpandoColumnConstants.SHORT_ARRAY) ||
118                                     (type == ExpandoColumnConstants.STRING_ARRAY)) {
119    
120                            return StringUtil.merge(
121                                    ArrayUtil.toStringArray((Object[])attribute));
122                    }
123                    else if (type == ExpandoColumnConstants.DATE) {
124                            DateFormat dateFormat = _getDateFormat();
125    
126                            return dateFormat.format((Date)attribute);
127                    }
128                    else if (type == ExpandoColumnConstants.DATE_ARRAY) {
129                            return StringUtil.merge(
130                                    ArrayUtil.toStringArray((Date[])attribute, _getDateFormat()));
131                    }
132                    else {
133                            return attribute.toString();
134                    }
135            }
136    
137            private static DateFormat _getDateFormat() {
138                    return new SimpleDateFormat(DateUtil.ISO_8601_PATTERN);
139            }
140    
141    }