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