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