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.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
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 Serializable getAttributeFromStringArray(
097 int type, String[] attribute) {
098
099 if ((attribute == null) || (attribute.length == 0)) {
100 return null;
101 }
102
103 if (type == ExpandoColumnConstants.BOOLEAN) {
104 return GetterUtil.getBoolean(attribute[0]);
105 }
106 else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
107 return GetterUtil.getBooleanValues(attribute);
108 }
109 else if (type == ExpandoColumnConstants.DATE) {
110 return GetterUtil.getDate(attribute[0], _getDateFormat());
111 }
112 else if (type == ExpandoColumnConstants.DATE_ARRAY) {
113 return GetterUtil.getDateValues(attribute, _getDateFormat());
114 }
115 else if (type == ExpandoColumnConstants.DOUBLE) {
116 return GetterUtil.getDouble(attribute[0]);
117 }
118 else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
119 return GetterUtil.getDoubleValues(attribute);
120 }
121 else if (type == ExpandoColumnConstants.FLOAT) {
122 return GetterUtil.getFloat(attribute[0]);
123 }
124 else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
125 return GetterUtil.getFloatValues(attribute);
126 }
127 else if (type == ExpandoColumnConstants.INTEGER) {
128 return GetterUtil.getInteger(attribute[0]);
129 }
130 else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
131 return GetterUtil.getIntegerValues(attribute);
132 }
133 else if (type == ExpandoColumnConstants.LONG) {
134 return GetterUtil.getLong(attribute[0]);
135 }
136 else if (type == ExpandoColumnConstants.LONG_ARRAY) {
137 return GetterUtil.getLongValues(attribute);
138 }
139 else if (type == ExpandoColumnConstants.SHORT) {
140 return GetterUtil.getShort(attribute[0]);
141 }
142 else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
143 return GetterUtil.getShortValues(attribute);
144 }
145 else if (type == ExpandoColumnConstants.STRING) {
146 return attribute[0];
147 }
148 else {
149 return attribute;
150 }
151 }
152
153 public static String getStringFromAttribute(
154 int type, Serializable attribute) {
155
156 if (attribute == null) {
157 return StringPool.BLANK;
158 }
159
160 if ((type == ExpandoColumnConstants.BOOLEAN) ||
161 (type == ExpandoColumnConstants.DOUBLE) ||
162 (type == ExpandoColumnConstants.FLOAT) ||
163 (type == ExpandoColumnConstants.INTEGER) ||
164 (type == ExpandoColumnConstants.LONG) ||
165 (type == ExpandoColumnConstants.SHORT)) {
166
167 return String.valueOf(attribute);
168 }
169 else if ((type == ExpandoColumnConstants.BOOLEAN_ARRAY) ||
170 (type == ExpandoColumnConstants.DOUBLE_ARRAY) ||
171 (type == ExpandoColumnConstants.FLOAT_ARRAY) ||
172 (type == ExpandoColumnConstants.INTEGER_ARRAY) ||
173 (type == ExpandoColumnConstants.LONG_ARRAY) ||
174 (type == ExpandoColumnConstants.SHORT_ARRAY) ||
175 (type == ExpandoColumnConstants.STRING_ARRAY)) {
176
177 return StringUtil.merge(
178 ArrayUtil.toStringArray((Object[])attribute));
179 }
180 else if (type == ExpandoColumnConstants.DATE) {
181 DateFormat dateFormat = _getDateFormat();
182
183 return dateFormat.format((Date)attribute);
184 }
185 else if (type == ExpandoColumnConstants.DATE_ARRAY) {
186 return StringUtil.merge(
187 ArrayUtil.toStringArray((Date[])attribute, _getDateFormat()));
188 }
189 else {
190 return attribute.toString();
191 }
192 }
193
194 private static DateFormat _getDateFormat() {
195 return new SimpleDateFormat(DateUtil.ISO_8601_PATTERN);
196 }
197
198 }