1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.expando.model.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.StringUtil;
21  import com.liferay.portlet.expando.ValueDataException;
22  import com.liferay.portlet.expando.model.ExpandoColumn;
23  import com.liferay.portlet.expando.model.ExpandoColumnConstants;
24  import com.liferay.portlet.expando.model.ExpandoValue;
25  import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
26  
27  import java.util.Date;
28  
29  /**
30   * <a href="ExpandoValueImpl.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Raymond Augé
33   * @author Brian Wing Shun Chan
34   */
35  public class ExpandoValueImpl
36      extends ExpandoValueModelImpl implements ExpandoValue {
37  
38      public ExpandoValueImpl() {
39      }
40  
41      public boolean getBoolean() throws PortalException, SystemException {
42          validate(ExpandoColumnConstants.BOOLEAN);
43  
44          return GetterUtil.getBoolean(getData());
45      }
46  
47      public void setBoolean(boolean data)
48          throws PortalException, SystemException {
49  
50          validate(ExpandoColumnConstants.BOOLEAN);
51  
52          setData(String.valueOf(data));
53      }
54  
55      public boolean[] getBooleanArray() throws PortalException, SystemException {
56          validate(ExpandoColumnConstants.BOOLEAN_ARRAY);
57  
58          return GetterUtil.getBooleanValues(StringUtil.split(getData()));
59      }
60  
61      public void setBooleanArray(boolean[] data)
62          throws PortalException, SystemException {
63  
64          validate(ExpandoColumnConstants.BOOLEAN_ARRAY);
65  
66          setData(StringUtil.merge(data));
67      }
68  
69      public Date getDate() throws PortalException, SystemException {
70          validate(ExpandoColumnConstants.DATE);
71  
72          return new Date(GetterUtil.getLong(getData()));
73      }
74  
75      public void setDate(Date data) throws PortalException, SystemException {
76          validate(ExpandoColumnConstants.DATE);
77  
78          setData(String.valueOf(data.getTime()));
79      }
80  
81      public Date[] getDateArray() throws PortalException, SystemException {
82          validate(ExpandoColumnConstants.DATE_ARRAY);
83  
84          String[] data = StringUtil.split(getData());
85  
86          Date[] dateArray = new Date[data.length];
87  
88          for (int i = 0; i < data.length; i++) {
89              dateArray[i] = new Date(GetterUtil.getLong(data[i]));
90          }
91  
92          return dateArray;
93      }
94  
95      public void setDateArray(Date[] data)
96          throws PortalException, SystemException {
97  
98          validate(ExpandoColumnConstants.DATE_ARRAY);
99  
100         setData(StringUtil.merge(data));
101     }
102 
103     public double getDouble() throws PortalException, SystemException {
104         validate(ExpandoColumnConstants.DOUBLE);
105 
106         return GetterUtil.getDouble(getData());
107     }
108 
109     public void setDouble(double data) throws PortalException, SystemException {
110         validate(ExpandoColumnConstants.DOUBLE);
111 
112         setData(String.valueOf(data));
113     }
114 
115     public double[] getDoubleArray() throws PortalException, SystemException {
116         validate(ExpandoColumnConstants.DOUBLE_ARRAY);
117 
118         return GetterUtil.getDoubleValues(StringUtil.split(getData()));
119     }
120 
121     public void setDoubleArray(double[] data)
122         throws PortalException, SystemException {
123 
124         validate(ExpandoColumnConstants.DOUBLE_ARRAY);
125 
126         setData(StringUtil.merge(data));
127     }
128 
129     public float getFloat() throws PortalException, SystemException {
130         validate(ExpandoColumnConstants.FLOAT);
131 
132         return GetterUtil.getFloat(getData());
133     }
134 
135     public void setFloat(float data) throws PortalException, SystemException {
136         validate(ExpandoColumnConstants.FLOAT);
137 
138         setData(String.valueOf(data));
139     }
140 
141     public float[] getFloatArray() throws PortalException, SystemException {
142         validate(ExpandoColumnConstants.FLOAT_ARRAY);
143 
144         return GetterUtil.getFloatValues(StringUtil.split(getData()));
145     }
146 
147     public void setFloatArray(float[] data)
148         throws PortalException, SystemException {
149 
150         validate(ExpandoColumnConstants.FLOAT_ARRAY);
151 
152         setData(StringUtil.merge(data));
153     }
154 
155     public int getInteger() throws PortalException, SystemException {
156         validate(ExpandoColumnConstants.INTEGER);
157 
158         return GetterUtil.getInteger(getData());
159     }
160 
161     public void setInteger(int data) throws PortalException, SystemException {
162         validate(ExpandoColumnConstants.INTEGER);
163 
164         setData(String.valueOf(data));
165     }
166 
167     public int[] getIntegerArray() throws PortalException, SystemException {
168         validate(ExpandoColumnConstants.INTEGER_ARRAY);
169 
170         return GetterUtil.getIntegerValues(StringUtil.split(getData()));
171     }
172 
173     public void setIntegerArray(int[] data)
174         throws PortalException, SystemException {
175 
176         validate(ExpandoColumnConstants.INTEGER_ARRAY);
177 
178         setData(StringUtil.merge(data));
179     }
180 
181     public long getLong() throws PortalException, SystemException {
182         validate(ExpandoColumnConstants.LONG);
183 
184         return GetterUtil.getLong(getData());
185     }
186 
187     public void setLong(long data) throws PortalException, SystemException {
188         validate(ExpandoColumnConstants.LONG);
189 
190         setData(String.valueOf(data));
191     }
192 
193     public long[] getLongArray() throws PortalException, SystemException {
194         validate(ExpandoColumnConstants.LONG_ARRAY);
195 
196         return GetterUtil.getLongValues(StringUtil.split(getData()));
197     }
198 
199     public void setLongArray(long[] data)
200         throws PortalException, SystemException {
201 
202         validate(ExpandoColumnConstants.LONG_ARRAY);
203 
204         setData(StringUtil.merge(data));
205     }
206 
207     public short getShort() throws PortalException, SystemException {
208         validate(ExpandoColumnConstants.SHORT);
209 
210         return GetterUtil.getShort(getData());
211     }
212 
213     public void setShort(short data) throws PortalException, SystemException {
214         validate(ExpandoColumnConstants.SHORT);
215 
216         setData(String.valueOf(data));
217     }
218 
219     public short[] getShortArray() throws PortalException, SystemException {
220         validate(ExpandoColumnConstants.SHORT_ARRAY);
221 
222         return GetterUtil.getShortValues(StringUtil.split(getData()));
223     }
224 
225     public void setShortArray(short[] data)
226         throws PortalException, SystemException {
227 
228         validate(ExpandoColumnConstants.SHORT_ARRAY);
229 
230         setData(StringUtil.merge(data));
231     }
232 
233     public String getString() throws PortalException, SystemException {
234         validate(ExpandoColumnConstants.STRING);
235 
236         return getData();
237     }
238 
239     public void setString(String data) throws PortalException, SystemException {
240         validate(ExpandoColumnConstants.STRING);
241 
242         setData(data);
243     }
244 
245     public String[] getStringArray() throws PortalException, SystemException {
246         validate(ExpandoColumnConstants.STRING_ARRAY);
247 
248         return StringUtil.split(getData());
249     }
250 
251     public void setStringArray(String[] data)
252         throws PortalException, SystemException {
253 
254         validate(ExpandoColumnConstants.STRING_ARRAY);
255 
256         setData(StringUtil.merge(data));
257     }
258 
259     protected void validate(int type) throws PortalException, SystemException {
260         ExpandoColumn column = ExpandoColumnLocalServiceUtil.getColumn(
261             getColumnId());
262 
263         if (column.getType() != type) {
264             throw new ValueDataException(
265                 "Column " + getColumnId() + " has type " +
266                     ExpandoColumnConstants.getTypeLabel(column.getType()) +
267                         " and is not compatbile with type " +
268                             ExpandoColumnConstants.getTypeLabel(type));
269         }
270     }
271 
272 }