001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                            boolean repeatable = isRepeatable();
185    
186                            if (repeatable) {
187                                    return FieldConstants.getSerializable(getDataType(), values);
188                            }
189    
190                            return values.get(0);
191                    }
192                    catch (Exception e) {
193                            _log.error(e);
194                    }
195    
196                    return null;
197            }
198    
199            public Serializable getValue(Locale locale, int index) {
200                    List<Serializable> values = _getValues(locale);
201    
202                    if (index >= values.size()) {
203                            return null;
204                    }
205    
206                    return values.get(index);
207            }
208    
209            public List<Serializable> getValues(Locale locale) {
210                    return _getValues(locale);
211            }
212    
213            public Map<Locale, List<Serializable>> getValuesMap() {
214                    return _valuesMap;
215            }
216    
217            public boolean isPrivate() {
218                    try {
219                            DDMStructure ddmStructure = getDDMStructure();
220    
221                            return ddmStructure.isFieldPrivate(_name);
222                    }
223                    catch (Exception e) {
224                            return false;
225                    }
226            }
227    
228            public boolean isRepeatable() throws PortalException, SystemException {
229                    DDMStructure ddmStructure = getDDMStructure();
230    
231                    return ddmStructure.isFieldRepeatable(_name);
232            }
233    
234            public void setDDMStructureId(long ddmStructureId) {
235                    _ddmStructureId = ddmStructureId;
236            }
237    
238            public void setDefaultLocale(Locale defaultLocale) {
239                    _defaultLocale = defaultLocale;
240            }
241    
242            public void setName(String name) {
243                    _name = name;
244            }
245    
246            public void setValue(Locale locale, Serializable value) {
247                    List<Serializable> values = null;
248    
249                    if (value != null) {
250                            Class<?> clazz = value.getClass();
251    
252                            if (clazz.isArray()) {
253                                    values = ListUtil.fromArray((Serializable[])value);
254                            }
255                    }
256    
257                    if (values == null) {
258                            values = new ArrayList<Serializable>();
259    
260                            values.add(value);
261                    }
262    
263                    _valuesMap.put(locale, values);
264            }
265    
266            public void setValue(Serializable value) {
267                    setValue(LocaleUtil.getSiteDefault(), value);
268            }
269    
270            public void setValues(Locale locale, List<Serializable> values) {
271                    _valuesMap.put(locale, values);
272            }
273    
274            public void setValuesMap(Map<Locale, List<Serializable>> valuesMap) {
275                    _valuesMap = valuesMap;
276            }
277    
278            protected FieldRenderer getFieldRenderer()
279                    throws PortalException, SystemException {
280    
281                    DDMStructure ddmStructure = getDDMStructure();
282    
283                    String dataType = null;
284    
285                    if (ddmStructure != null) {
286                            dataType = getDataType();
287                    }
288    
289                    return FieldRendererFactory.getFieldRenderer(dataType);
290            }
291    
292            private List<Serializable> _getValues(Locale locale) {
293                    Set<Locale> availableLocales = getAvailableLocales();
294    
295                    if (!availableLocales.contains(locale)) {
296                            locale = getDefaultLocale();
297                    }
298    
299                    if (locale == null) {
300                            locale = LocaleUtil.getSiteDefault();
301                    }
302    
303                    List<Serializable> values = _valuesMap.get(locale);
304    
305                    if (values == null) {
306                            return Collections.emptyList();
307                    }
308    
309                    return values;
310            }
311    
312            private static Log _log = LogFactoryUtil.getLog(Field.class);
313    
314            private long _ddmStructureId;
315            private Locale _defaultLocale;
316            private String _name;
317            private Map<Locale, List<Serializable>> _valuesMap =
318                    new HashMap<Locale, List<Serializable>>();
319    
320    }