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