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