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.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.StringUtil;
021    import com.liferay.portlet.expando.ValueDataException;
022    import com.liferay.portlet.expando.model.ExpandoColumn;
023    import com.liferay.portlet.expando.model.ExpandoColumnConstants;
024    import com.liferay.portlet.expando.model.ExpandoValue;
025    import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
026    
027    import java.util.Date;
028    
029    /**
030     * @author Raymond Augé
031     * @author Brian Wing Shun Chan
032     */
033    public class ExpandoValueImpl
034            extends ExpandoValueModelImpl implements ExpandoValue {
035    
036            public ExpandoValueImpl() {
037            }
038    
039            public boolean getBoolean() throws PortalException, SystemException {
040                    validate(ExpandoColumnConstants.BOOLEAN);
041    
042                    return GetterUtil.getBoolean(getData());
043            }
044    
045            public boolean[] getBooleanArray() throws PortalException, SystemException {
046                    validate(ExpandoColumnConstants.BOOLEAN_ARRAY);
047    
048                    return GetterUtil.getBooleanValues(StringUtil.split(getData()));
049            }
050    
051            public Date getDate() throws PortalException, SystemException {
052                    validate(ExpandoColumnConstants.DATE);
053    
054                    return new Date(GetterUtil.getLong(getData()));
055            }
056    
057            public Date[] getDateArray() throws PortalException, SystemException {
058                    validate(ExpandoColumnConstants.DATE_ARRAY);
059    
060                    String[] data = StringUtil.split(getData());
061    
062                    Date[] dateArray = new Date[data.length];
063    
064                    for (int i = 0; i < data.length; i++) {
065                            dateArray[i] = new Date(GetterUtil.getLong(data[i]));
066                    }
067    
068                    return dateArray;
069            }
070    
071            public double getDouble() throws PortalException, SystemException {
072                    validate(ExpandoColumnConstants.DOUBLE);
073    
074                    return GetterUtil.getDouble(getData());
075            }
076    
077            public double[] getDoubleArray() throws PortalException, SystemException {
078                    validate(ExpandoColumnConstants.DOUBLE_ARRAY);
079    
080                    return GetterUtil.getDoubleValues(StringUtil.split(getData()));
081            }
082    
083            public float getFloat() throws PortalException, SystemException {
084                    validate(ExpandoColumnConstants.FLOAT);
085    
086                    return GetterUtil.getFloat(getData());
087            }
088    
089            public float[] getFloatArray() throws PortalException, SystemException {
090                    validate(ExpandoColumnConstants.FLOAT_ARRAY);
091    
092                    return GetterUtil.getFloatValues(StringUtil.split(getData()));
093            }
094    
095            public int getInteger() throws PortalException, SystemException {
096                    validate(ExpandoColumnConstants.INTEGER);
097    
098                    return GetterUtil.getInteger(getData());
099            }
100    
101            public int[] getIntegerArray() throws PortalException, SystemException {
102                    validate(ExpandoColumnConstants.INTEGER_ARRAY);
103    
104                    return GetterUtil.getIntegerValues(StringUtil.split(getData()));
105            }
106    
107            public long getLong() throws PortalException, SystemException {
108                    validate(ExpandoColumnConstants.LONG);
109    
110                    return GetterUtil.getLong(getData());
111            }
112    
113            public long[] getLongArray() throws PortalException, SystemException {
114                    validate(ExpandoColumnConstants.LONG_ARRAY);
115    
116                    return GetterUtil.getLongValues(StringUtil.split(getData()));
117            }
118    
119            public short getShort() throws PortalException, SystemException {
120                    validate(ExpandoColumnConstants.SHORT);
121    
122                    return GetterUtil.getShort(getData());
123            }
124    
125            public short[] getShortArray() throws PortalException, SystemException {
126                    validate(ExpandoColumnConstants.SHORT_ARRAY);
127    
128                    return GetterUtil.getShortValues(StringUtil.split(getData()));
129            }
130    
131            public String getString() throws PortalException, SystemException {
132                    validate(ExpandoColumnConstants.STRING);
133    
134                    return getData();
135            }
136    
137            public String[] getStringArray() throws PortalException, SystemException {
138                    validate(ExpandoColumnConstants.STRING_ARRAY);
139    
140                    return StringUtil.split(getData());
141            }
142    
143            public void setBoolean(boolean data)
144                    throws PortalException, SystemException {
145    
146                    validate(ExpandoColumnConstants.BOOLEAN);
147    
148                    setData(String.valueOf(data));
149            }
150    
151            public void setBooleanArray(boolean[] data)
152                    throws PortalException, SystemException {
153    
154                    validate(ExpandoColumnConstants.BOOLEAN_ARRAY);
155    
156                    setData(StringUtil.merge(data));
157            }
158    
159            public void setDate(Date data) throws PortalException, SystemException {
160                    validate(ExpandoColumnConstants.DATE);
161    
162                    setData(String.valueOf(data.getTime()));
163            }
164    
165            public void setDateArray(Date[] data)
166                    throws PortalException, SystemException {
167    
168                    validate(ExpandoColumnConstants.DATE_ARRAY);
169    
170                    setData(StringUtil.merge(data));
171            }
172    
173            public void setDouble(double data) throws PortalException, SystemException {
174                    validate(ExpandoColumnConstants.DOUBLE);
175    
176                    setData(String.valueOf(data));
177            }
178    
179            public void setDoubleArray(double[] data)
180                    throws PortalException, SystemException {
181    
182                    validate(ExpandoColumnConstants.DOUBLE_ARRAY);
183    
184                    setData(StringUtil.merge(data));
185            }
186    
187            public void setFloat(float data) throws PortalException, SystemException {
188                    validate(ExpandoColumnConstants.FLOAT);
189    
190                    setData(String.valueOf(data));
191            }
192    
193            public void setFloatArray(float[] data)
194                    throws PortalException, SystemException {
195    
196                    validate(ExpandoColumnConstants.FLOAT_ARRAY);
197    
198                    setData(StringUtil.merge(data));
199            }
200    
201            public void setInteger(int data) throws PortalException, SystemException {
202                    validate(ExpandoColumnConstants.INTEGER);
203    
204                    setData(String.valueOf(data));
205            }
206    
207            public void setIntegerArray(int[] data)
208                    throws PortalException, SystemException {
209    
210                    validate(ExpandoColumnConstants.INTEGER_ARRAY);
211    
212                    setData(StringUtil.merge(data));
213            }
214    
215            public void setLong(long data) throws PortalException, SystemException {
216                    validate(ExpandoColumnConstants.LONG);
217    
218                    setData(String.valueOf(data));
219            }
220    
221            public void setLongArray(long[] data)
222                    throws PortalException, SystemException {
223    
224                    validate(ExpandoColumnConstants.LONG_ARRAY);
225    
226                    setData(StringUtil.merge(data));
227            }
228    
229            public void setShort(short data) throws PortalException, SystemException {
230                    validate(ExpandoColumnConstants.SHORT);
231    
232                    setData(String.valueOf(data));
233            }
234    
235            public void setShortArray(short[] data)
236                    throws PortalException, SystemException {
237    
238                    validate(ExpandoColumnConstants.SHORT_ARRAY);
239    
240                    setData(StringUtil.merge(data));
241            }
242    
243            public void setString(String data) throws PortalException, SystemException {
244                    validate(ExpandoColumnConstants.STRING);
245    
246                    setData(data);
247            }
248    
249            public void setStringArray(String[] data)
250                    throws PortalException, SystemException {
251    
252                    validate(ExpandoColumnConstants.STRING_ARRAY);
253    
254                    setData(StringUtil.merge(data));
255            }
256    
257            protected void validate(int type) throws PortalException, SystemException {
258                    ExpandoColumn column = ExpandoColumnLocalServiceUtil.getColumn(
259                            getColumnId());
260    
261                    if (column.getType() != type) {
262                            throw new ValueDataException(
263                                    "Column " + getColumnId() + " has type " +
264                                            ExpandoColumnConstants.getTypeLabel(column.getType()) +
265                                                    " and is not compatible with type " +
266                                                            ExpandoColumnConstants.getTypeLabel(type));
267                    }
268            }
269    
270    }