001    /**
002     * Copyright (c) 2000-present 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.dynamicdatamapping.storage;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.Validator;
020    
021    import java.io.Serializable;
022    
023    import java.util.Date;
024    import java.util.List;
025    
026    /**
027     * @author Marcellus Tavares
028     * @author Eduardo Lundgren
029     */
030    public class FieldConstants {
031    
032            public static final String BOOLEAN = "boolean";
033    
034            public static final String DATA_TYPE = "dataType";
035    
036            public static final String DATE = "date";
037    
038            public static final String DOCUMENT_LIBRARY = "document-library";
039    
040            public static final String DOUBLE = "double";
041    
042            public static final String EDITABLE = "editable";
043    
044            public static final String FLOAT = "float";
045    
046            public static final String HTML = "html";
047    
048            public static final String IMAGE = "image";
049    
050            public static final String INTEGER = "integer";
051    
052            public static final String LABEL = "label";
053    
054            public static final String LONG = "long";
055    
056            public static final String NAME = "name";
057    
058            public static final String NUMBER = "number";
059    
060            public static final String PREDEFINED_VALUE = "predefinedValue";
061    
062            public static final String PRIVATE = "private";
063    
064            public static final String REQUIRED = "required";
065    
066            public static final String SHORT = "short";
067    
068            public static final String SHOW = "showLabel";
069    
070            public static final String SORTABLE = "sortable";
071    
072            public static final String STRING = "string";
073    
074            public static final String TYPE = "type";
075    
076            public static final String VALUE = "value";
077    
078            public static final Serializable getSerializable(
079                    String type, List<Serializable> values) {
080    
081                    if (type.equals(FieldConstants.BOOLEAN)) {
082                            return values.toArray(new Boolean[values.size()]);
083                    }
084                    else if (type.equals(FieldConstants.DATE)) {
085                            return values.toArray(new Date[values.size()]);
086                    }
087                    else if (type.equals(FieldConstants.DOUBLE)) {
088                            return values.toArray(new Double[values.size()]);
089                    }
090                    else if (type.equals(FieldConstants.FLOAT)) {
091                            return values.toArray(new Float[values.size()]);
092                    }
093                    else if (type.equals(FieldConstants.INTEGER)) {
094                            return values.toArray(new Integer[values.size()]);
095                    }
096                    else if (type.equals(FieldConstants.LONG)) {
097                            return values.toArray(new Long[values.size()]);
098                    }
099                    else if (type.equals(FieldConstants.NUMBER)) {
100                            return values.toArray(new Number[values.size()]);
101                    }
102                    else if (type.equals(FieldConstants.SHORT)) {
103                            return values.toArray(new Short[values.size()]);
104                    }
105                    else {
106                            return values.toArray(new String[values.size()]);
107                    }
108            }
109    
110            public static final Serializable getSerializable(
111                    String type, String value) {
112    
113                    if (isNumericType(type) && Validator.isNull(value)) {
114                            return StringPool.BLANK;
115                    }
116    
117                    if (type.equals(BOOLEAN)) {
118                            return GetterUtil.getBoolean(value);
119                    }
120                    else if (type.equals(DATE) && Validator.isNotNull(value)) {
121                            return new Date(GetterUtil.getLong(value));
122                    }
123                    else if (type.equals(DOUBLE)) {
124                            return GetterUtil.getDouble(value);
125                    }
126                    else if (type.equals(FLOAT)) {
127                            return GetterUtil.getFloat(value);
128                    }
129                    else if (type.equals(INTEGER)) {
130                            return GetterUtil.getInteger(value);
131                    }
132                    else if (type.equals(LONG)) {
133                            return GetterUtil.getLong(value);
134                    }
135                    else if (type.equals(NUMBER)) {
136                            return GetterUtil.getNumber(value);
137                    }
138                    else if (type.equals(SHORT)) {
139                            return GetterUtil.getShort(value);
140                    }
141                    else {
142                            return value;
143                    }
144            }
145    
146            public static final boolean isNumericType(String type) {
147                    if (type.equals(DOUBLE) || type.equals(FLOAT) || type.equals(INTEGER) ||
148                            type.equals(LONG) || type.equals(NUMBER) || type.equals(SHORT)) {
149    
150                            return true;
151                    }
152    
153                    return false;
154            }
155    
156    }