001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.dynamicdatamapping.storage;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    
023    import java.io.Serializable;
024    
025    import java.util.Collections;
026    import java.util.Date;
027    import java.util.List;
028    
029    /**
030     * @author Marcellus Tavares
031     * @author Eduardo Lundgren
032     */
033    public class FieldConstants {
034    
035            public static final String BOOLEAN = "boolean";
036    
037            public static final String DATA_TYPE = "dataType";
038    
039            public static final String DATE = "date";
040    
041            public static final String DOCUMENT_LIBRARY = "document-library";
042    
043            public static final String DOUBLE = "double";
044    
045            public static final String EDITABLE = "editable";
046    
047            public static final String FLOAT = "float";
048    
049            public static final String HTML = "html";
050    
051            public static final String IMAGE = "image";
052    
053            public static final String INTEGER = "integer";
054    
055            public static final String LABEL = "label";
056    
057            public static final String LONG = "long";
058    
059            public static final String NAME = "name";
060    
061            public static final String NUMBER = "number";
062    
063            public static final String PREDEFINED_VALUE = "predefinedValue";
064    
065            public static final String PRIVATE = "private";
066    
067            public static final String REQUIRED = "required";
068    
069            public static final String SHORT = "short";
070    
071            public static final String SHOW = "showLabel";
072    
073            public static final String SORTABLE = "sortable";
074    
075            public static final String STRING = "string";
076    
077            public static final String TYPE = "type";
078    
079            public static final String VALUE = "value";
080    
081            public static final Serializable getSerializable(
082                    String type, List<Serializable> values) {
083    
084                    if (Validator.isNull(type)) {
085                            if (_log.isDebugEnabled()) {
086                                    _log.debug("Invalid type " + type);
087                            }
088    
089                            return values.toArray(new String[values.size()]);
090                    }
091    
092                    if (isNumericType(type)) {
093                            values.removeAll(Collections.singleton(StringPool.BLANK));
094                    }
095    
096                    if (type.equals(FieldConstants.BOOLEAN)) {
097                            return values.toArray(new Boolean[values.size()]);
098                    }
099                    else if (type.equals(FieldConstants.DATE)) {
100                            values.removeAll(Collections.singleton(StringPool.BLANK));
101    
102                            return values.toArray(new Date[values.size()]);
103                    }
104                    else if (type.equals(FieldConstants.DOUBLE)) {
105                            return values.toArray(new Double[values.size()]);
106                    }
107                    else if (type.equals(FieldConstants.FLOAT)) {
108                            return values.toArray(new Float[values.size()]);
109                    }
110                    else if (type.equals(FieldConstants.INTEGER)) {
111                            return values.toArray(new Integer[values.size()]);
112                    }
113                    else if (type.equals(FieldConstants.LONG)) {
114                            return values.toArray(new Long[values.size()]);
115                    }
116                    else if (type.equals(FieldConstants.NUMBER)) {
117                            return values.toArray(new Number[values.size()]);
118                    }
119                    else if (type.equals(FieldConstants.SHORT)) {
120                            return values.toArray(new Short[values.size()]);
121                    }
122                    else {
123                            return values.toArray(new String[values.size()]);
124                    }
125            }
126    
127            public static final Serializable getSerializable(
128                    String type, String value) {
129    
130                    if (Validator.isNull(type)) {
131                            if (_log.isDebugEnabled()) {
132                                    _log.debug("Invalid type " + type);
133                            }
134    
135                            return value;
136                    }
137    
138                    if (isNumericType(type) && Validator.isNull(value)) {
139                            return StringPool.BLANK;
140                    }
141    
142                    if (type.equals(BOOLEAN)) {
143                            return GetterUtil.getBoolean(value);
144                    }
145                    else if (type.equals(DATE) && Validator.isNotNull(value)) {
146                            return new Date(GetterUtil.getLong(value));
147                    }
148                    else if (type.equals(DOUBLE)) {
149                            return GetterUtil.getDouble(value);
150                    }
151                    else if (type.equals(FLOAT)) {
152                            return GetterUtil.getFloat(value);
153                    }
154                    else if (type.equals(INTEGER)) {
155                            return GetterUtil.getInteger(value);
156                    }
157                    else if (type.equals(LONG)) {
158                            return GetterUtil.getLong(value);
159                    }
160                    else if (type.equals(NUMBER)) {
161                            return GetterUtil.getNumber(value);
162                    }
163                    else if (type.equals(SHORT)) {
164                            return GetterUtil.getShort(value);
165                    }
166                    else {
167                            return value;
168                    }
169            }
170    
171            public static final boolean isNumericType(String type) {
172                    if (type.equals(DOUBLE) || type.equals(FLOAT) || type.equals(INTEGER) ||
173                            type.equals(LONG) || type.equals(NUMBER) || type.equals(SHORT)) {
174    
175                            return true;
176                    }
177    
178                    return false;
179            }
180    
181            private static Log _log = LogFactoryUtil.getLog(FieldConstants.class);
182    
183    }