001
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.portlet.expando.model.ExpandoColumnConstants;
024
025 import java.io.Serializable;
026
027 import java.text.DateFormat;
028
029 import java.util.Date;
030
031
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 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 GetterUtil.getDate(attribute[0], _getDateFormat());
114 }
115 else if (type == ExpandoColumnConstants.DATE_ARRAY) {
116 return GetterUtil.getDateValues(attribute, _getDateFormat());
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 DateFormat _getDateFormat() {
198 return DateUtil.getISO8601Format();
199 }
200
201 }