001    /**
002     * Copyright (c) 2000-2012 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.dynamicdatamapping.storage;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.ListUtil;
022    import com.liferay.portal.kernel.util.LocaleUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
025    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
026    
027    import java.io.Serializable;
028    
029    import java.util.ArrayList;
030    import java.util.HashMap;
031    import java.util.List;
032    import java.util.Locale;
033    import java.util.Map;
034    import java.util.Set;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     * @author Marcellus Tavares
039     */
040    public class Field implements Serializable {
041    
042            public Field() {
043            }
044    
045            public Field(
046                    long ddmStructureId, String name, List<Serializable> values,
047                    Locale locale) {
048    
049                    _ddmStructureId = ddmStructureId;
050                    _name = name;
051                    _valuesMap.put(locale, values);
052            }
053    
054            public Field(
055                    long ddmStructureId, String name,
056                    Map<Locale, List<Serializable>> valuesMap, Locale defaultLocale) {
057    
058                    _ddmStructureId = ddmStructureId;
059                    _defaultLocale = defaultLocale;
060                    _name = name;
061                    _valuesMap = valuesMap;
062            }
063    
064            public Field(long ddmStructureId, String name, Serializable value) {
065                    _ddmStructureId = ddmStructureId;
066                    _name = name;
067    
068                    setValue(value);
069            }
070    
071            public Field(String name, Serializable value) {
072                    this(0, name, value);
073            }
074    
075            public void addValue(Locale locale, Serializable value) {
076                    List<Serializable> values = _valuesMap.get(locale);
077    
078                    if (values == null) {
079                            values = new ArrayList<Serializable>();
080    
081                            _valuesMap.put(locale, values);
082                    }
083    
084                    values.add(value);
085            }
086    
087            public void addValues(Locale locale, List<Serializable> values) {
088                    for (Serializable value : values) {
089                            addValue(locale, value);
090                    }
091            }
092    
093            @Override
094            public boolean equals(Object obj) {
095                    if (!(obj instanceof Field)) {
096                            return false;
097                    }
098    
099                    Field field = (Field)obj;
100    
101                    if ((_ddmStructureId == field._ddmStructureId) &&
102                            Validator.equals(_name, field._name) &&
103                            Validator.equals(_valuesMap, field._valuesMap)) {
104    
105                            return true;
106                    }
107    
108                    return false;
109            }
110    
111            public Set<Locale> getAvailableLocales() {
112                    return _valuesMap.keySet();
113            }
114    
115            public String getDataType() throws PortalException, SystemException {
116                    DDMStructure ddmStructure = getDDMStructure();
117    
118                    return ddmStructure.getFieldDataType(_name);
119            }
120    
121            public DDMStructure getDDMStructure() throws SystemException {
122                    return DDMStructureLocalServiceUtil.fetchStructure(_ddmStructureId);
123            }
124    
125            public long getDDMStructureId() {
126                    return _ddmStructureId;
127            }
128    
129            public Locale getDefaultLocale() {
130                    return _defaultLocale;
131            }
132    
133            public String getName() {
134                    return _name;
135            }
136    
137            public String getRenderedValue(Locale locale)
138                    throws PortalException, SystemException {
139    
140                    FieldRenderer fieldRenderer = getFieldRenderer();
141    
142                    return fieldRenderer.render(this, locale);
143            }
144    
145            public String getRenderedValue(Locale locale, int valueIndex)
146                    throws PortalException, SystemException {
147    
148                    FieldRenderer fieldRenderer = getFieldRenderer();
149    
150                    return fieldRenderer.render(this, locale, valueIndex);
151            }
152    
153            public String getType() throws PortalException, SystemException {
154                    DDMStructure ddmStructure = getDDMStructure();
155    
156                    return ddmStructure.getFieldType(_name);
157            }
158    
159            public Serializable getValue() {
160                    Locale defaultLocale = getDefaultLocale();
161    
162                    return getValue(defaultLocale);
163            }
164    
165            public Serializable getValue(Locale locale) {
166                    List<Serializable> values = _getValues(locale);
167    
168                    if (values.isEmpty()) {
169                            return null;
170                    }
171    
172                    try {
173                            DDMStructure ddmStructure = getDDMStructure();
174    
175                            if (ddmStructure == null) {
176                                    return values.get(0);
177                            }
178    
179                            boolean repeatable = isRepeatable();
180    
181                            if (repeatable) {
182                                    return FieldConstants.getSerializable(getDataType(), values);
183                            }
184    
185                            return values.get(0);
186                    }
187                    catch (Exception e) {
188                            _log.error(e);
189                    }
190    
191                    return null;
192            }
193    
194            public Serializable getValue(Locale locale, int index) {
195                    List<Serializable> values = _getValues(locale);
196    
197                    if (index >= values.size()) {
198                            return null;
199                    }
200    
201                    return values.get(index);
202            }
203    
204            public List<Serializable> getValues(Locale locale) {
205                    return _getValues(locale);
206            }
207    
208            public Map<Locale, List<Serializable>> getValuesMap() {
209                    return _valuesMap;
210            }
211    
212            public boolean isPrivate() {
213                    try {
214                            DDMStructure ddmStructure = getDDMStructure();
215    
216                            return ddmStructure.isFieldPrivate(_name);
217                    }
218                    catch (Exception e) {
219                            return false;
220                    }
221            }
222    
223            public boolean isRepeatable() throws PortalException, SystemException {
224                    DDMStructure ddmStructure = getDDMStructure();
225    
226                    return ddmStructure.isFieldRepeatable(_name);
227            }
228    
229            public void setDDMStructureId(long ddmStructureId) {
230                    _ddmStructureId = ddmStructureId;
231            }
232    
233            public void setDefaultLocale(Locale defaultLocale) {
234                    _defaultLocale = defaultLocale;
235            }
236    
237            public void setName(String name) {
238                    _name = name;
239            }
240    
241            public void setValue(Locale locale, Serializable value) {
242                    Class<?> clazz = value.getClass();
243    
244                    List<Serializable> values = null;
245    
246                    if (clazz.isArray()) {
247                            values = ListUtil.fromArray((Serializable[])value);
248                    }
249                    else {
250                            values = new ArrayList<Serializable>();
251    
252                            values.add(value);
253                    }
254    
255                    _valuesMap.put(locale, values);
256            }
257    
258            public void setValue(Serializable value) {
259                    setValue(LocaleUtil.getDefault(), value);
260            }
261    
262            public void setValues(Locale locale, List<Serializable> values) {
263                    _valuesMap.put(locale, values);
264            }
265    
266            public void setValuesMap(Map<Locale, List<Serializable>> valuesMap) {
267                    _valuesMap = valuesMap;
268            }
269    
270            protected FieldRenderer getFieldRenderer()
271                    throws PortalException, SystemException {
272    
273                    DDMStructure ddmStructure = getDDMStructure();
274    
275                    String dataType = null;
276    
277                    if (ddmStructure != null) {
278                            dataType = getDataType();
279                    }
280    
281                    return FieldRendererFactory.getFieldRenderer(dataType);
282            }
283    
284            private List<Serializable> _getValues(Locale locale) {
285                    Set<Locale> availableLocales = getAvailableLocales();
286    
287                    if (!availableLocales.contains(locale)) {
288                            locale = getDefaultLocale();
289                    }
290    
291                    if (locale == null) {
292                            locale = LocaleUtil.getDefault();
293                    }
294    
295                    return _valuesMap.get(locale);
296            }
297    
298            private static Log _log = LogFactoryUtil.getLog(Field.class);
299    
300            private long _ddmStructureId;
301            private Locale _defaultLocale;
302            private String _name;
303            private Map<Locale, List<Serializable>> _valuesMap =
304                    new HashMap<Locale, List<Serializable>>();
305    
306    }