001    /**
002     * Copyright (c) 2000-present 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.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.ListUtil;
021    import com.liferay.portal.kernel.util.LocaleUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
024    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
025    
026    import java.io.Serializable;
027    
028    import java.util.ArrayList;
029    import java.util.Collections;
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                    _name = name;
060                    _valuesMap = valuesMap;
061                    _defaultLocale = defaultLocale;
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 (this == obj) {
096                            return true;
097                    }
098    
099                    if (!(obj instanceof Field)) {
100                            return false;
101                    }
102    
103                    Field field = (Field)obj;
104    
105                    if ((_ddmStructureId == field._ddmStructureId) &&
106                            Validator.equals(_name, field._name) &&
107                            Validator.equals(_valuesMap, field._valuesMap)) {
108    
109                            return true;
110                    }
111    
112                    return false;
113            }
114    
115            public Set<Locale> getAvailableLocales() {
116                    return _valuesMap.keySet();
117            }
118    
119            public String getDataType() throws PortalException {
120                    DDMStructure ddmStructure = getDDMStructure();
121    
122                    return ddmStructure.getFieldDataType(_name);
123            }
124    
125            public DDMStructure getDDMStructure() {
126                    return DDMStructureLocalServiceUtil.fetchStructure(_ddmStructureId);
127            }
128    
129            public long getDDMStructureId() {
130                    return _ddmStructureId;
131            }
132    
133            public Locale getDefaultLocale() {
134                    return _defaultLocale;
135            }
136    
137            public String getName() {
138                    return _name;
139            }
140    
141            public String getRenderedValue(Locale locale) throws PortalException {
142                    FieldRenderer fieldRenderer = getFieldRenderer();
143    
144                    return fieldRenderer.render(this, locale);
145            }
146    
147            public String getRenderedValue(Locale locale, int valueIndex)
148                    throws PortalException {
149    
150                    FieldRenderer fieldRenderer = getFieldRenderer();
151    
152                    return fieldRenderer.render(this, locale, valueIndex);
153            }
154    
155            public String getType() throws PortalException {
156                    DDMStructure ddmStructure = getDDMStructure();
157    
158                    return ddmStructure.getFieldType(_name);
159            }
160    
161            public Serializable getValue() {
162                    Locale defaultLocale = getDefaultLocale();
163    
164                    return getValue(defaultLocale);
165            }
166    
167            public Serializable getValue(Locale locale) {
168                    List<Serializable> values = _getValues(locale);
169    
170                    if (values.isEmpty()) {
171                            return null;
172                    }
173    
174                    try {
175                            DDMStructure ddmStructure = getDDMStructure();
176    
177                            if (ddmStructure == null) {
178                                    return values.get(0);
179                            }
180    
181                            if (isRepeatable() || (values.size() > 1)) {
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 {
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                    List<Serializable> values = null;
243    
244                    if (value != null) {
245                            Class<?> clazz = value.getClass();
246    
247                            if (clazz.isArray()) {
248                                    values = ListUtil.fromArray((Serializable[])value);
249                            }
250                    }
251    
252                    if (values == null) {
253                            values = new ArrayList<Serializable>();
254    
255                            values.add(value);
256                    }
257    
258                    _valuesMap.put(locale, values);
259            }
260    
261            public void setValue(Serializable value) {
262                    setValue(LocaleUtil.getSiteDefault(), value);
263            }
264    
265            public void setValues(Locale locale, List<Serializable> values) {
266                    _valuesMap.put(locale, values);
267            }
268    
269            public void setValuesMap(Map<Locale, List<Serializable>> valuesMap) {
270                    _valuesMap = valuesMap;
271            }
272    
273            protected FieldRenderer getFieldRenderer() throws PortalException {
274                    DDMStructure ddmStructure = getDDMStructure();
275    
276                    String dataType = null;
277    
278                    if (ddmStructure != null) {
279                            dataType = getDataType();
280                    }
281    
282                    return FieldRendererFactory.getFieldRenderer(dataType);
283            }
284    
285            private List<Serializable> _getValues(Locale locale) {
286                    Set<Locale> availableLocales = getAvailableLocales();
287    
288                    if (!availableLocales.contains(locale)) {
289                            locale = getDefaultLocale();
290                    }
291    
292                    if (locale == null) {
293                            locale = LocaleUtil.getSiteDefault();
294                    }
295    
296                    List<Serializable> values = _valuesMap.get(locale);
297    
298                    if (values == null) {
299                            return Collections.emptyList();
300                    }
301    
302                    return values;
303            }
304    
305            private static final Log _log = LogFactoryUtil.getLog(Field.class);
306    
307            private long _ddmStructureId;
308            private Locale _defaultLocale;
309            private String _name;
310            private Map<Locale, List<Serializable>> _valuesMap =
311                    new HashMap<Locale, List<Serializable>>();
312    
313    }