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