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 (this == obj) {
097                            return true;
098                    }
099    
100                    if (!(obj instanceof Field)) {
101                            return false;
102                    }
103    
104                    Field field = (Field)obj;
105    
106                    if ((_ddmStructureId == field._ddmStructureId) &&
107                            Validator.equals(_name, field._name) &&
108                            Validator.equals(_valuesMap, field._valuesMap)) {
109    
110                            return true;
111                    }
112    
113                    return false;
114            }
115    
116            public Set<Locale> getAvailableLocales() {
117                    return _valuesMap.keySet();
118            }
119    
120            public String getDataType() throws PortalException, SystemException {
121                    DDMStructure ddmStructure = getDDMStructure();
122    
123                    return ddmStructure.getFieldDataType(_name);
124            }
125    
126            public DDMStructure getDDMStructure() throws SystemException {
127                    return DDMStructureLocalServiceUtil.fetchStructure(_ddmStructureId);
128            }
129    
130            public long getDDMStructureId() {
131                    return _ddmStructureId;
132            }
133    
134            public Locale getDefaultLocale() {
135                    return _defaultLocale;
136            }
137    
138            public String getName() {
139                    return _name;
140            }
141    
142            public String getRenderedValue(Locale locale)
143                    throws PortalException, SystemException {
144    
145                    FieldRenderer fieldRenderer = getFieldRenderer();
146    
147                    return fieldRenderer.render(this, locale);
148            }
149    
150            public String getRenderedValue(Locale locale, int valueIndex)
151                    throws PortalException, SystemException {
152    
153                    FieldRenderer fieldRenderer = getFieldRenderer();
154    
155                    return fieldRenderer.render(this, locale, valueIndex);
156            }
157    
158            public String getType() throws PortalException, SystemException {
159                    DDMStructure ddmStructure = getDDMStructure();
160    
161                    return ddmStructure.getFieldType(_name);
162            }
163    
164            public Serializable getValue() {
165                    Locale defaultLocale = getDefaultLocale();
166    
167                    return getValue(defaultLocale);
168            }
169    
170            public Serializable getValue(Locale locale) {
171                    List<Serializable> values = _getValues(locale);
172    
173                    if (values.isEmpty()) {
174                            return null;
175                    }
176    
177                    try {
178                            DDMStructure ddmStructure = getDDMStructure();
179    
180                            if (ddmStructure == null) {
181                                    return values.get(0);
182                            }
183    
184                            if (isRepeatable() || (values.size() > 1)) {
185                                    return FieldConstants.getSerializable(getDataType(), values);
186                            }
187    
188                            return values.get(0);
189                    }
190                    catch (Exception e) {
191                            _log.error(e);
192                    }
193    
194                    return null;
195            }
196    
197            public Serializable getValue(Locale locale, int index) {
198                    List<Serializable> values = _getValues(locale);
199    
200                    if (index >= values.size()) {
201                            return null;
202                    }
203    
204                    return values.get(index);
205            }
206    
207            public List<Serializable> getValues(Locale locale) {
208                    return _getValues(locale);
209            }
210    
211            public Map<Locale, List<Serializable>> getValuesMap() {
212                    return _valuesMap;
213            }
214    
215            public boolean isPrivate() {
216                    try {
217                            DDMStructure ddmStructure = getDDMStructure();
218    
219                            return ddmStructure.isFieldPrivate(_name);
220                    }
221                    catch (Exception e) {
222                            return false;
223                    }
224            }
225    
226            public boolean isRepeatable() throws PortalException, SystemException {
227                    DDMStructure ddmStructure = getDDMStructure();
228    
229                    return ddmStructure.isFieldRepeatable(_name);
230            }
231    
232            public void setDDMStructureId(long ddmStructureId) {
233                    _ddmStructureId = ddmStructureId;
234            }
235    
236            public void setDefaultLocale(Locale defaultLocale) {
237                    _defaultLocale = defaultLocale;
238            }
239    
240            public void setName(String name) {
241                    _name = name;
242            }
243    
244            public void setValue(Locale locale, Serializable value) {
245                    List<Serializable> values = null;
246    
247                    if (value != null) {
248                            Class<?> clazz = value.getClass();
249    
250                            if (clazz.isArray()) {
251                                    values = ListUtil.fromArray((Serializable[])value);
252                            }
253                    }
254    
255                    if (values == null) {
256                            values = new ArrayList<Serializable>();
257    
258                            values.add(value);
259                    }
260    
261                    _valuesMap.put(locale, values);
262            }
263    
264            public void setValue(Serializable value) {
265                    setValue(LocaleUtil.getSiteDefault(), value);
266            }
267    
268            public void setValues(Locale locale, List<Serializable> values) {
269                    _valuesMap.put(locale, values);
270            }
271    
272            public void setValuesMap(Map<Locale, List<Serializable>> valuesMap) {
273                    _valuesMap = valuesMap;
274            }
275    
276            protected FieldRenderer getFieldRenderer()
277                    throws PortalException, SystemException {
278    
279                    DDMStructure ddmStructure = getDDMStructure();
280    
281                    String dataType = null;
282    
283                    if (ddmStructure != null) {
284                            dataType = getDataType();
285                    }
286    
287                    return FieldRendererFactory.getFieldRenderer(dataType);
288            }
289    
290            private List<Serializable> _getValues(Locale locale) {
291                    Set<Locale> availableLocales = getAvailableLocales();
292    
293                    if (!availableLocales.contains(locale)) {
294                            locale = getDefaultLocale();
295                    }
296    
297                    if (locale == null) {
298                            locale = LocaleUtil.getSiteDefault();
299                    }
300    
301                    List<Serializable> values = _valuesMap.get(locale);
302    
303                    if (values == null) {
304                            return Collections.emptyList();
305                    }
306    
307                    return values;
308            }
309    
310            private static Log _log = LogFactoryUtil.getLog(Field.class);
311    
312            private long _ddmStructureId;
313            private Locale _defaultLocale;
314            private String _name;
315            private Map<Locale, List<Serializable>> _valuesMap =
316                    new HashMap<Locale, List<Serializable>>();
317    
318    }