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