1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.expando.util;
16  
17  import com.liferay.portal.kernel.util.ArrayUtil;
18  import com.liferay.portal.kernel.util.DateUtil;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portlet.expando.model.ExpandoColumnConstants;
23  
24  import java.io.Serializable;
25  
26  import java.text.DateFormat;
27  import java.text.SimpleDateFormat;
28  
29  import java.util.Date;
30  
31  /**
32   * <a href="ExpandoConverterUtil.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Edward Han
35   * @author Michael C. Han
36   * @author Brian Wing Shun Chan
37   */
38  public class ExpandoConverterUtil {
39  
40      public static Serializable getAttributeFromString(
41          int type, String attribute) {
42  
43          if (attribute == null) {
44              return null;
45          }
46  
47          if (type == ExpandoColumnConstants.BOOLEAN) {
48              return GetterUtil.getBoolean(attribute);
49          }
50          else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
51              return GetterUtil.getBooleanValues(StringUtil.split(attribute));
52          }
53          else if (type == ExpandoColumnConstants.DATE) {
54              return GetterUtil.getDate(attribute, _getDateFormat());
55          }
56          else if (type == ExpandoColumnConstants.DATE_ARRAY) {
57              return GetterUtil.getDateValues(
58                  StringUtil.split(attribute), _getDateFormat());
59          }
60          else if (type == ExpandoColumnConstants.DOUBLE) {
61              return GetterUtil.getDouble(attribute);
62          }
63          else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
64              return GetterUtil.getDoubleValues(StringUtil.split(attribute));
65          }
66          else if (type == ExpandoColumnConstants.FLOAT) {
67              return GetterUtil.getFloat(attribute);
68          }
69          else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
70              return GetterUtil.getFloatValues(StringUtil.split(attribute));
71          }
72          else if (type == ExpandoColumnConstants.INTEGER) {
73              return GetterUtil.getInteger(attribute);
74          }
75          else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
76              return GetterUtil.getIntegerValues(StringUtil.split(attribute));
77          }
78          else if (type == ExpandoColumnConstants.LONG) {
79              return GetterUtil.getLong(attribute);
80          }
81          else if (type == ExpandoColumnConstants.LONG_ARRAY) {
82              return GetterUtil.getLongValues(StringUtil.split(attribute));
83          }
84          else if (type == ExpandoColumnConstants.SHORT) {
85              return GetterUtil.getShort(attribute);
86          }
87          else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
88              return GetterUtil.getShortValues(StringUtil.split(attribute));
89          }
90          else if (type == ExpandoColumnConstants.STRING_ARRAY) {
91              return StringUtil.split(attribute);
92          }
93          else {
94              return attribute;
95          }
96      }
97  
98      public static String getStringFromAttribute(
99          int type, Serializable attribute) {
100 
101         if (attribute == null) {
102             return StringPool.BLANK;
103         }
104 
105         if ((type == ExpandoColumnConstants.BOOLEAN) ||
106             (type == ExpandoColumnConstants.DOUBLE) ||
107             (type == ExpandoColumnConstants.FLOAT) ||
108             (type == ExpandoColumnConstants.INTEGER) ||
109             (type == ExpandoColumnConstants.LONG) ||
110             (type == ExpandoColumnConstants.SHORT)) {
111 
112             return String.valueOf(attribute);
113         }
114         else if ((type == ExpandoColumnConstants.BOOLEAN_ARRAY) ||
115                  (type == ExpandoColumnConstants.DOUBLE_ARRAY) ||
116                  (type == ExpandoColumnConstants.FLOAT_ARRAY) ||
117                  (type == ExpandoColumnConstants.INTEGER_ARRAY) ||
118                  (type == ExpandoColumnConstants.LONG_ARRAY) ||
119                  (type == ExpandoColumnConstants.SHORT_ARRAY) ||
120                  (type == ExpandoColumnConstants.STRING_ARRAY)) {
121 
122             return StringUtil.merge(
123                 ArrayUtil.toStringArray((Object[])attribute));
124         }
125         else if (type == ExpandoColumnConstants.DATE) {
126             DateFormat dateFormat = _getDateFormat();
127 
128             return dateFormat.format((Date)attribute);
129         }
130         else if (type == ExpandoColumnConstants.DATE_ARRAY) {
131             return StringUtil.merge(
132                 ArrayUtil.toStringArray((Date[])attribute, _getDateFormat()));
133         }
134         else {
135             return attribute.toString();
136         }
137     }
138 
139     private static DateFormat _getDateFormat() {
140         return new SimpleDateFormat(DateUtil.ISO_8601_PATTERN);
141     }
142 
143 }