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