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 {
101                                    return value.getString();
102                            }
103                    }
104                    catch (Exception e) {
105                            return null;
106                    }
107            }
108    
109            @Override
110            public String getDisplayName(Locale locale) {
111                    String name = getName();
112    
113                    String displayName = LanguageUtil.get(locale, name);
114    
115                    if (name.equals(displayName)) {
116                            displayName = TextFormatter.format(name, TextFormatter.J);
117                    }
118    
119                    return displayName;
120            }
121    
122            @Override
123            public String getTypeSettings() {
124                    if (_typeSettingsProperties == null) {
125                            return super.getTypeSettings();
126                    }
127                    else {
128                            return _typeSettingsProperties.toString();
129                    }
130            }
131    
132            @Override
133            public UnicodeProperties getTypeSettingsProperties() {
134                    if (_typeSettingsProperties == null) {
135                            _typeSettingsProperties = new UnicodeProperties(true);
136    
137                            try {
138                                    _typeSettingsProperties.load(super.getTypeSettings());
139                            }
140                            catch (IOException ioe) {
141                                    _log.error(ioe, ioe);
142                            }
143                    }
144    
145                    return _typeSettingsProperties;
146            }
147    
148            @Override
149            public void setTypeSettings(String typeSettings) {
150                    _typeSettingsProperties = null;
151    
152                    super.setTypeSettings(typeSettings);
153            }
154    
155            @Override
156            public void setTypeSettingsProperties(
157                    UnicodeProperties typeSettingsProperties) {
158    
159                    _typeSettingsProperties = typeSettingsProperties;
160    
161                    super.setTypeSettings(_typeSettingsProperties.toString());
162            }
163    
164            private static Log _log = LogFactoryUtil.getLog(ExpandoColumnImpl.class);
165    
166            private UnicodeProperties _typeSettingsProperties;
167    
168    }