001    /**
002     * Copyright (c) 2000-2012 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.ArrayUtil;
018    import com.liferay.portal.kernel.util.GetterUtil;
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     */
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 FILE_UPLOAD = "file-upload";
044    
045            public static final String FLOAT = "float";
046    
047            public static final String HTML = "html";
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 PREDIFINED_VALUE = "predefinedValue";
060    
061            public static final String REQUIRED = "required";
062    
063            public static final String SHORT = "short";
064    
065            public static final String SHOW = "showLabel";
066    
067            public static final String SORTABLE = "sortable";
068    
069            public static final String STRING = "string";
070    
071            public static final String TYPE = "type";
072    
073            public static final String VALUE = "value";
074    
075            public static final Serializable getSerializable(
076                    String type, List<Serializable> values) {
077    
078                    if (type.equals(FieldConstants.BOOLEAN)) {
079                            return ArrayUtil.toArray(
080                                    values.toArray(new Boolean[values.size()]));
081                    }
082                    else if (type.equals(FieldConstants.DATE)) {
083                            return values.toArray(new Date[values.size()]);
084                    }
085                    else if (type.equals(FieldConstants.DOUBLE)) {
086                            return ArrayUtil.toArray(values.toArray(new Double[values.size()]));
087                    }
088                    else if (type.equals(FieldConstants.FLOAT)) {
089                            return ArrayUtil.toArray(values.toArray(new Float[values.size()]));
090                    }
091                    else if (type.equals(FieldConstants.INTEGER)) {
092                            return ArrayUtil.toArray(
093                                    values.toArray(new Integer[values.size()]));
094                    }
095                    else if (type.equals(FieldConstants.LONG)) {
096                            return ArrayUtil.toArray(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 ArrayUtil.toArray(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 (type.equals(BOOLEAN)) {
113                            return GetterUtil.getBoolean(value);
114                    }
115                    else if (type.equals(DATE) && Validator.isNotNull(value)) {
116                            return new Date(GetterUtil.getLong(value));
117                    }
118                    else if (type.equals(DOUBLE)) {
119                            return GetterUtil.getDouble(value);
120                    }
121                    else if (type.equals(FLOAT)) {
122                            return GetterUtil.getFloat(value);
123                    }
124                    else if (type.equals(INTEGER)) {
125                            return GetterUtil.getInteger(value);
126                    }
127                    else if (type.equals(LONG)) {
128                            return GetterUtil.getLong(value);
129                    }
130                    else if (type.equals(NUMBER)) {
131                            return GetterUtil.getNumber(value);
132                    }
133                    else if (type.equals(SHORT)) {
134                            return GetterUtil.getShort(value);
135                    }
136                    else {
137                            return value;
138                    }
139            }
140    
141    }