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.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     */
028    public class FieldConstants {
029    
030            public static final String BOOLEAN = "boolean";
031    
032            public static final String DATA_TYPE = "dataType";
033    
034            public static final String DATE = "date";
035    
036            public static final String DOCUMENT_LIBRARY = "document-library";
037    
038            public static final String DOUBLE = "double";
039    
040            public static final String EDITABLE = "editable";
041    
042            public static final String FILE_UPLOAD = "file-upload";
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 REQUIRED = "required";
063    
064            public static final String SHORT = "short";
065    
066            public static final String SHOW = "showLabel";
067    
068            public static final String SORTABLE = "sortable";
069    
070            public static final String STRING = "string";
071    
072            public static final String TYPE = "type";
073    
074            public static final String VALUE = "value";
075    
076            public static final Serializable getSerializable(
077                    String type, List<Serializable> values) {
078    
079                    if (type.equals(FieldConstants.BOOLEAN)) {
080                            return 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 values.toArray(new Double[values.size()]);
087                    }
088                    else if (type.equals(FieldConstants.FLOAT)) {
089                            return values.toArray(new Float[values.size()]);
090                    }
091                    else if (type.equals(FieldConstants.INTEGER)) {
092                            return values.toArray(new Integer[values.size()]);
093                    }
094                    else if (type.equals(FieldConstants.LONG)) {
095                            return values.toArray(new Long[values.size()]);
096                    }
097                    else if (type.equals(FieldConstants.NUMBER)) {
098                            return values.toArray(new Number[values.size()]);
099                    }
100                    else if (type.equals(FieldConstants.SHORT)) {
101                            return values.toArray(new Short[values.size()]);
102                    }
103                    else {
104                            return values.toArray(new String[values.size()]);
105                    }
106            }
107    
108            public static final Serializable getSerializable(
109                    String type, String value) {
110    
111                    if (type.equals(BOOLEAN)) {
112                            return GetterUtil.getBoolean(value);
113                    }
114                    else if (type.equals(DATE) && Validator.isNotNull(value)) {
115                            return new Date(GetterUtil.getLong(value));
116                    }
117                    else if (type.equals(DOUBLE)) {
118                            return GetterUtil.getDouble(value);
119                    }
120                    else if (type.equals(FLOAT)) {
121                            return GetterUtil.getFloat(value);
122                    }
123                    else if (type.equals(INTEGER)) {
124                            return GetterUtil.getInteger(value);
125                    }
126                    else if (type.equals(LONG)) {
127                            return GetterUtil.getLong(value);
128                    }
129                    else if (type.equals(NUMBER)) {
130                            return GetterUtil.getNumber(value);
131                    }
132                    else if (type.equals(SHORT)) {
133                            return GetterUtil.getShort(value);
134                    }
135                    else {
136                            return value;
137                    }
138            }
139    
140    }