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