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.expando.model.impl;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.TextFormatter;
021    import com.liferay.portal.kernel.util.UnicodeProperties;
022    import com.liferay.portlet.expando.model.ExpandoColumnConstants;
023    import com.liferay.portlet.expando.model.ExpandoValue;
024    
025    import java.io.IOException;
026    import java.io.Serializable;
027    
028    import java.util.Locale;
029    
030    /**
031     * @author Raymond Aug??
032     * @author Brian Wing Shun Chan
033     */
034    public class ExpandoColumnImpl extends ExpandoColumnBaseImpl {
035    
036            public ExpandoColumnImpl() {
037            }
038    
039            @Override
040            public Serializable getDefaultValue() {
041                    try {
042                            ExpandoValue value = new ExpandoValueImpl();
043    
044                            value.setColumnId(getColumnId());
045                            value.setData(getDefaultData());
046    
047                            int type = getType();
048    
049                            if (type == ExpandoColumnConstants.BOOLEAN) {
050                                    return value.getBoolean();
051                            }
052                            else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
053                                    return value.getBooleanArray();
054                            }
055                            else if (type == ExpandoColumnConstants.DATE) {
056                                    return value.getDate();
057                            }
058                            else if (type == ExpandoColumnConstants.DATE_ARRAY) {
059                                    return value.getDateArray();
060                            }
061                            else if (type == ExpandoColumnConstants.DOUBLE) {
062                                    return value.getDouble();
063                            }
064                            else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
065                                    return value.getDoubleArray();
066                            }
067                            else if (type == ExpandoColumnConstants.FLOAT) {
068                                    return value.getFloat();
069                            }
070                            else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
071                                    return value.getFloatArray();
072                            }
073                            else if (type == ExpandoColumnConstants.INTEGER) {
074                                    return value.getInteger();
075                            }
076                            else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
077                                    return value.getIntegerArray();
078                            }
079                            else if (type == ExpandoColumnConstants.LONG) {
080                                    return value.getLong();
081                            }
082                            else if (type == ExpandoColumnConstants.LONG_ARRAY) {
083                                    return value.getLongArray();
084                            }
085                            else if (type == ExpandoColumnConstants.NUMBER) {
086                                    return value.getNumber();
087                            }
088                            else if (type == ExpandoColumnConstants.NUMBER_ARRAY) {
089                                    return value.getNumberArray();
090                            }
091                            else if (type == ExpandoColumnConstants.SHORT) {
092                                    return value.getShort();
093                            }
094                            else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
095                                    return value.getShortArray();
096                            }
097                            else if (type == ExpandoColumnConstants.STRING_ARRAY) {
098                                    return value.getStringArray();
099                            }
100                            else if (type == ExpandoColumnConstants.STRING_ARRAY_LOCALIZED) {
101                                    return (Serializable)value.getStringArrayMap();
102                            }
103                            else if (type == ExpandoColumnConstants.STRING_LOCALIZED) {
104                                    return (Serializable)value.getStringMap();
105                            }
106                            else {
107                                    return value.getString();
108                            }
109                    }
110                    catch (Exception e) {
111                            return null;
112                    }
113            }
114    
115            @Override
116            public String getDisplayName(Locale locale) {
117                    String name = getName();
118    
119                    String displayName = LanguageUtil.get(locale, name);
120    
121                    if (name.equals(displayName)) {
122                            displayName = TextFormatter.format(name, TextFormatter.J);
123                    }
124    
125                    return displayName;
126            }
127    
128            @Override
129            public String getTypeSettings() {
130                    if (_typeSettingsProperties == null) {
131                            return super.getTypeSettings();
132                    }
133                    else {
134                            return _typeSettingsProperties.toString();
135                    }
136            }
137    
138            @Override
139            public UnicodeProperties getTypeSettingsProperties() {
140                    if (_typeSettingsProperties == null) {
141                            _typeSettingsProperties = new UnicodeProperties(true);
142    
143                            try {
144                                    _typeSettingsProperties.load(super.getTypeSettings());
145                            }
146                            catch (IOException ioe) {
147                                    _log.error(ioe, ioe);
148                            }
149                    }
150    
151                    return _typeSettingsProperties;
152            }
153    
154            @Override
155            public void setTypeSettings(String typeSettings) {
156                    _typeSettingsProperties = null;
157    
158                    super.setTypeSettings(typeSettings);
159            }
160    
161            @Override
162            public void setTypeSettingsProperties(
163                    UnicodeProperties typeSettingsProperties) {
164    
165                    _typeSettingsProperties = typeSettingsProperties;
166    
167                    super.setTypeSettings(_typeSettingsProperties.toString());
168            }
169    
170            private static Log _log = LogFactoryUtil.getLog(ExpandoColumnImpl.class);
171    
172            private UnicodeProperties _typeSettingsProperties;
173    
174    }