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