001
014
015 package com.liferay.portlet.expando.model.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.util.GetterUtil;
020 import com.liferay.portal.kernel.util.StringBundler;
021 import com.liferay.portal.kernel.util.StringPool;
022 import com.liferay.portal.kernel.util.StringUtil;
023 import com.liferay.portlet.expando.ValueDataException;
024 import com.liferay.portlet.expando.model.ExpandoColumn;
025 import com.liferay.portlet.expando.model.ExpandoColumnConstants;
026 import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
027
028 import java.io.Serializable;
029
030 import java.util.Date;
031
032
036 public class ExpandoValueImpl extends ExpandoValueBaseImpl {
037
038 public ExpandoValueImpl() {
039 }
040
041 @Override
042 public boolean getBoolean() throws PortalException, SystemException {
043 validate(ExpandoColumnConstants.BOOLEAN);
044
045 return GetterUtil.getBoolean(getData());
046 }
047
048 @Override
049 public boolean[] getBooleanArray() throws PortalException, SystemException {
050 validate(ExpandoColumnConstants.BOOLEAN_ARRAY);
051
052 return GetterUtil.getBooleanValues(StringUtil.split(getData()));
053 }
054
055 @Override
056 public ExpandoColumn getColumn() throws PortalException, SystemException {
057 if (_column != null) {
058 return _column;
059 }
060
061 long columnId = getColumnId();
062
063 if (columnId <= 0) {
064 return null;
065 }
066
067 return ExpandoColumnLocalServiceUtil.getColumn(columnId);
068 }
069
070 @Override
071 public Date getDate() throws PortalException, SystemException {
072 validate(ExpandoColumnConstants.DATE);
073
074 return new Date(GetterUtil.getLong(getData()));
075 }
076
077 @Override
078 public Date[] getDateArray() throws PortalException, SystemException {
079 validate(ExpandoColumnConstants.DATE_ARRAY);
080
081 String[] data = StringUtil.split(getData());
082
083 Date[] dateArray = new Date[data.length];
084
085 for (int i = 0; i < data.length; i++) {
086 dateArray[i] = new Date(GetterUtil.getLong(data[i]));
087 }
088
089 return dateArray;
090 }
091
092 @Override
093 public double getDouble() throws PortalException, SystemException {
094 validate(ExpandoColumnConstants.DOUBLE);
095
096 return GetterUtil.getDouble(getData());
097 }
098
099 @Override
100 public double[] getDoubleArray() throws PortalException, SystemException {
101 validate(ExpandoColumnConstants.DOUBLE_ARRAY);
102
103 return GetterUtil.getDoubleValues(StringUtil.split(getData()));
104 }
105
106 @Override
107 public float getFloat() throws PortalException, SystemException {
108 validate(ExpandoColumnConstants.FLOAT);
109
110 return GetterUtil.getFloat(getData());
111 }
112
113 @Override
114 public float[] getFloatArray() throws PortalException, SystemException {
115 validate(ExpandoColumnConstants.FLOAT_ARRAY);
116
117 return GetterUtil.getFloatValues(StringUtil.split(getData()));
118 }
119
120 @Override
121 public int getInteger() throws PortalException, SystemException {
122 validate(ExpandoColumnConstants.INTEGER);
123
124 return GetterUtil.getInteger(getData());
125 }
126
127 @Override
128 public int[] getIntegerArray() throws PortalException, SystemException {
129 validate(ExpandoColumnConstants.INTEGER_ARRAY);
130
131 return GetterUtil.getIntegerValues(StringUtil.split(getData()));
132 }
133
134 @Override
135 public long getLong() throws PortalException, SystemException {
136 validate(ExpandoColumnConstants.LONG);
137
138 return GetterUtil.getLong(getData());
139 }
140
141 @Override
142 public long[] getLongArray() throws PortalException, SystemException {
143 validate(ExpandoColumnConstants.LONG_ARRAY);
144
145 return GetterUtil.getLongValues(StringUtil.split(getData()));
146 }
147
148 @Override
149 public Number getNumber() throws PortalException, SystemException {
150 validate(ExpandoColumnConstants.NUMBER);
151
152 return GetterUtil.getNumber(getData());
153 }
154
155 @Override
156 public Number[] getNumberArray() throws PortalException, SystemException {
157 validate(ExpandoColumnConstants.NUMBER_ARRAY);
158
159 return GetterUtil.getNumberValues(StringUtil.split(getData()));
160 }
161
162 @Override
163 public Serializable getSerializable()
164 throws PortalException, SystemException {
165
166 ExpandoColumn column = getColumn();
167
168 int type = column.getType();
169
170 if (type == ExpandoColumnConstants.BOOLEAN) {
171 return getBoolean();
172 }
173 else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
174 return getBooleanArray();
175 }
176 else if (type == ExpandoColumnConstants.DATE) {
177 return getDate();
178 }
179 else if (type == ExpandoColumnConstants.DATE_ARRAY) {
180 return getDateArray();
181 }
182 else if (type == ExpandoColumnConstants.DOUBLE) {
183 return getDouble();
184 }
185 else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
186 return getDoubleArray();
187 }
188 else if (type == ExpandoColumnConstants.FLOAT) {
189 return getFloat();
190 }
191 else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
192 return getFloatArray();
193 }
194 else if (type == ExpandoColumnConstants.INTEGER) {
195 return getInteger();
196 }
197 else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
198 return getIntegerArray();
199 }
200 else if (type == ExpandoColumnConstants.LONG) {
201 return getLong();
202 }
203 else if (type == ExpandoColumnConstants.LONG_ARRAY) {
204 return getLongArray();
205 }
206 else if (type == ExpandoColumnConstants.NUMBER) {
207 return getNumber();
208 }
209 else if (type == ExpandoColumnConstants.NUMBER_ARRAY) {
210 return getNumberArray();
211 }
212 else if (type == ExpandoColumnConstants.SHORT) {
213 return getShort();
214 }
215 else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
216 return getShortArray();
217 }
218 else if (type == ExpandoColumnConstants.STRING_ARRAY) {
219 return getStringArray();
220 }
221 else {
222 return getData();
223 }
224 }
225
226 @Override
227 public short getShort() throws PortalException, SystemException {
228 validate(ExpandoColumnConstants.SHORT);
229
230 return GetterUtil.getShort(getData());
231 }
232
233 @Override
234 public short[] getShortArray() throws PortalException, SystemException {
235 validate(ExpandoColumnConstants.SHORT_ARRAY);
236
237 return GetterUtil.getShortValues(StringUtil.split(getData()));
238 }
239
240 @Override
241 public String getString() throws PortalException, SystemException {
242 validate(ExpandoColumnConstants.STRING);
243
244 return getData();
245 }
246
247 @Override
248 public String[] getStringArray() throws PortalException, SystemException {
249 validate(ExpandoColumnConstants.STRING_ARRAY);
250
251 String[] dataArray = StringUtil.split(getData());
252
253 for (int i = 0; i < dataArray.length; i++) {
254 dataArray[i] = StringUtil.replace(
255 dataArray[i], _EXPANDO_COMMA, StringPool.COMMA);
256 }
257
258 return dataArray;
259 }
260
261 @Override
262 public void setBoolean(boolean data)
263 throws PortalException, SystemException {
264
265 validate(ExpandoColumnConstants.BOOLEAN);
266
267 setData(String.valueOf(data));
268 }
269
270 @Override
271 public void setBooleanArray(boolean[] data)
272 throws PortalException, SystemException {
273
274 validate(ExpandoColumnConstants.BOOLEAN_ARRAY);
275
276 setData(StringUtil.merge(data));
277 }
278
279 @Override
280 public void setColumn(ExpandoColumn column) {
281 _column = column;
282
283 setColumnId(_column.getColumnId());
284 }
285
286 @Override
287 public void setDate(Date data) throws PortalException, SystemException {
288 validate(ExpandoColumnConstants.DATE);
289
290 setData(String.valueOf(data.getTime()));
291 }
292
293 @Override
294 public void setDateArray(Date[] data)
295 throws PortalException, SystemException {
296
297 validate(ExpandoColumnConstants.DATE_ARRAY);
298
299 setData(StringUtil.merge(data));
300 }
301
302 @Override
303 public void setDouble(double data) throws PortalException, SystemException {
304 validate(ExpandoColumnConstants.DOUBLE);
305
306 setData(String.valueOf(data));
307 }
308
309 @Override
310 public void setDoubleArray(double[] data)
311 throws PortalException, SystemException {
312
313 validate(ExpandoColumnConstants.DOUBLE_ARRAY);
314
315 setData(StringUtil.merge(data));
316 }
317
318 @Override
319 public void setFloat(float data) throws PortalException, SystemException {
320 validate(ExpandoColumnConstants.FLOAT);
321
322 setData(String.valueOf(data));
323 }
324
325 @Override
326 public void setFloatArray(float[] data)
327 throws PortalException, SystemException {
328
329 validate(ExpandoColumnConstants.FLOAT_ARRAY);
330
331 setData(StringUtil.merge(data));
332 }
333
334 @Override
335 public void setInteger(int data) throws PortalException, SystemException {
336 validate(ExpandoColumnConstants.INTEGER);
337
338 setData(String.valueOf(data));
339 }
340
341 @Override
342 public void setIntegerArray(int[] data)
343 throws PortalException, SystemException {
344
345 validate(ExpandoColumnConstants.INTEGER_ARRAY);
346
347 setData(StringUtil.merge(data));
348 }
349
350 @Override
351 public void setLong(long data) throws PortalException, SystemException {
352 validate(ExpandoColumnConstants.LONG);
353
354 setData(String.valueOf(data));
355 }
356
357 @Override
358 public void setLongArray(long[] data)
359 throws PortalException, SystemException {
360
361 validate(ExpandoColumnConstants.LONG_ARRAY);
362
363 setData(StringUtil.merge(data));
364 }
365
366 @Override
367 public void setNumber(Number data) throws PortalException, SystemException {
368 validate(ExpandoColumnConstants.NUMBER);
369
370 setData(String.valueOf(data));
371 }
372
373 @Override
374 public void setNumberArray(Number[] data)
375 throws PortalException, SystemException {
376
377 validate(ExpandoColumnConstants.NUMBER_ARRAY);
378
379 setData(StringUtil.merge(data));
380 }
381
382 @Override
383 public void setShort(short data) throws PortalException, SystemException {
384 validate(ExpandoColumnConstants.SHORT);
385
386 setData(String.valueOf(data));
387 }
388
389 @Override
390 public void setShortArray(short[] data)
391 throws PortalException, SystemException {
392
393 validate(ExpandoColumnConstants.SHORT_ARRAY);
394
395 setData(StringUtil.merge(data));
396 }
397
398 @Override
399 public void setString(String data) throws PortalException, SystemException {
400 validate(ExpandoColumnConstants.STRING);
401
402 setData(data);
403 }
404
405 @Override
406 public void setStringArray(String[] data)
407 throws PortalException, SystemException {
408
409 validate(ExpandoColumnConstants.STRING_ARRAY);
410
411 if (data != null) {
412 for (int i = 0; i < data.length; i++) {
413 data[i] = StringUtil.replace(
414 data[i], StringPool.COMMA, _EXPANDO_COMMA);
415 }
416 }
417
418 setData(StringUtil.merge(data));
419 }
420
421 protected void validate(int type) throws PortalException, SystemException {
422 ExpandoColumn column = getColumn();
423
424 if (column == null) {
425 return;
426 }
427
428 if (column.getType() == type) {
429 return;
430 }
431
432 StringBundler sb = new StringBundler(6);
433
434 sb.append("Column ");
435 sb.append(getColumnId());
436 sb.append(" has type ");
437 sb.append(ExpandoColumnConstants.getTypeLabel(column.getType()));
438 sb.append(" and is not compatible with type ");
439 sb.append(ExpandoColumnConstants.getTypeLabel(type));
440
441 throw new ValueDataException(sb.toString());
442 }
443
444 private static final String _EXPANDO_COMMA = "[$LIFERAY_EXPANDO_COMMA$]";
445
446 private transient ExpandoColumn _column;
447
448 }