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.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.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 boolean[] getBooleanArray() throws PortalException, SystemException {
48          validate(ExpandoColumnConstants.BOOLEAN_ARRAY);
49  
50          return GetterUtil.getBooleanValues(StringUtil.split(getData()));
51      }
52  
53      public Date getDate() throws PortalException, SystemException {
54          validate(ExpandoColumnConstants.DATE);
55  
56          return new Date(GetterUtil.getLong(getData()));
57      }
58  
59      public Date[] getDateArray() throws PortalException, SystemException {
60          validate(ExpandoColumnConstants.DATE_ARRAY);
61  
62          String[] data = StringUtil.split(getData());
63  
64          Date[] dateArray = new Date[data.length];
65  
66          for (int i = 0; i < data.length; i++) {
67              dateArray[i] = new Date(GetterUtil.getLong(data[i]));
68          }
69  
70          return dateArray;
71      }
72  
73      public double getDouble() throws PortalException, SystemException {
74          validate(ExpandoColumnConstants.DOUBLE);
75  
76          return GetterUtil.getDouble(getData());
77      }
78  
79      public double[] getDoubleArray() throws PortalException, SystemException {
80          validate(ExpandoColumnConstants.DOUBLE_ARRAY);
81  
82          return GetterUtil.getDoubleValues(StringUtil.split(getData()));
83      }
84  
85      public float getFloat() throws PortalException, SystemException {
86          validate(ExpandoColumnConstants.FLOAT);
87  
88          return GetterUtil.getFloat(getData());
89      }
90  
91      public float[] getFloatArray() throws PortalException, SystemException {
92          validate(ExpandoColumnConstants.FLOAT_ARRAY);
93  
94          return GetterUtil.getFloatValues(StringUtil.split(getData()));
95      }
96  
97      public int getInteger() throws PortalException, SystemException {
98          validate(ExpandoColumnConstants.INTEGER);
99  
100         return GetterUtil.getInteger(getData());
101     }
102 
103     public int[] getIntegerArray() throws PortalException, SystemException {
104         validate(ExpandoColumnConstants.INTEGER_ARRAY);
105 
106         return GetterUtil.getIntegerValues(StringUtil.split(getData()));
107     }
108 
109     public long getLong() throws PortalException, SystemException {
110         validate(ExpandoColumnConstants.LONG);
111 
112         return GetterUtil.getLong(getData());
113     }
114 
115     public long[] getLongArray() throws PortalException, SystemException {
116         validate(ExpandoColumnConstants.LONG_ARRAY);
117 
118         return GetterUtil.getLongValues(StringUtil.split(getData()));
119     }
120 
121     public short getShort() throws PortalException, SystemException {
122         validate(ExpandoColumnConstants.SHORT);
123 
124         return GetterUtil.getShort(getData());
125     }
126 
127     public short[] getShortArray() throws PortalException, SystemException {
128         validate(ExpandoColumnConstants.SHORT_ARRAY);
129 
130         return GetterUtil.getShortValues(StringUtil.split(getData()));
131     }
132 
133     public String getString() throws PortalException, SystemException {
134         validate(ExpandoColumnConstants.STRING);
135 
136         return getData();
137     }
138 
139     public String[] getStringArray() throws PortalException, SystemException {
140         validate(ExpandoColumnConstants.STRING_ARRAY);
141 
142         return StringUtil.split(getData());
143     }
144 
145     public void setBoolean(boolean data)
146         throws PortalException, SystemException {
147 
148         validate(ExpandoColumnConstants.BOOLEAN);
149 
150         setData(String.valueOf(data));
151     }
152 
153     public void setBooleanArray(boolean[] data)
154         throws PortalException, SystemException {
155 
156         validate(ExpandoColumnConstants.BOOLEAN_ARRAY);
157 
158         setData(StringUtil.merge(data));
159     }
160 
161     public void setDate(Date data) throws PortalException, SystemException {
162         validate(ExpandoColumnConstants.DATE);
163 
164         setData(String.valueOf(data.getTime()));
165     }
166 
167     public void setDateArray(Date[] data)
168         throws PortalException, SystemException {
169 
170         validate(ExpandoColumnConstants.DATE_ARRAY);
171 
172         setData(StringUtil.merge(data));
173     }
174 
175     public void setDouble(double data) throws PortalException, SystemException {
176         validate(ExpandoColumnConstants.DOUBLE);
177 
178         setData(String.valueOf(data));
179     }
180 
181     public void setDoubleArray(double[] data)
182         throws PortalException, SystemException {
183 
184         validate(ExpandoColumnConstants.DOUBLE_ARRAY);
185 
186         setData(StringUtil.merge(data));
187     }
188 
189     public void setFloat(float data) throws PortalException, SystemException {
190         validate(ExpandoColumnConstants.FLOAT);
191 
192         setData(String.valueOf(data));
193     }
194 
195     public void setFloatArray(float[] data)
196         throws PortalException, SystemException {
197 
198         validate(ExpandoColumnConstants.FLOAT_ARRAY);
199 
200         setData(StringUtil.merge(data));
201     }
202 
203     public void setInteger(int data) throws PortalException, SystemException {
204         validate(ExpandoColumnConstants.INTEGER);
205 
206         setData(String.valueOf(data));
207     }
208 
209     public void setIntegerArray(int[] data)
210         throws PortalException, SystemException {
211 
212         validate(ExpandoColumnConstants.INTEGER_ARRAY);
213 
214         setData(StringUtil.merge(data));
215     }
216 
217     public void setLong(long data) throws PortalException, SystemException {
218         validate(ExpandoColumnConstants.LONG);
219 
220         setData(String.valueOf(data));
221     }
222 
223     public void setLongArray(long[] data)
224         throws PortalException, SystemException {
225 
226         validate(ExpandoColumnConstants.LONG_ARRAY);
227 
228         setData(StringUtil.merge(data));
229     }
230 
231     public void setShort(short data) throws PortalException, SystemException {
232         validate(ExpandoColumnConstants.SHORT);
233 
234         setData(String.valueOf(data));
235     }
236 
237     public void setShortArray(short[] data)
238         throws PortalException, SystemException {
239 
240         validate(ExpandoColumnConstants.SHORT_ARRAY);
241 
242         setData(StringUtil.merge(data));
243     }
244 
245     public void setString(String data) throws PortalException, SystemException {
246         validate(ExpandoColumnConstants.STRING);
247 
248         setData(data);
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 compatible with type " +
268                             ExpandoColumnConstants.getTypeLabel(type));
269         }
270     }
271 
272 }