001
014
015 package com.liferay.portlet.expando.model.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.lar.ImportExportThreadLocal;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.kernel.search.Indexer;
023 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
024 import com.liferay.portal.kernel.util.UnicodeProperties;
025 import com.liferay.portal.security.auth.CompanyThreadLocal;
026 import com.liferay.portal.service.ServiceContext;
027 import com.liferay.portal.util.PropsValues;
028 import com.liferay.portlet.expando.model.ExpandoBridge;
029 import com.liferay.portlet.expando.model.ExpandoColumn;
030 import com.liferay.portlet.expando.model.ExpandoColumnConstants;
031 import com.liferay.portlet.expando.model.ExpandoTable;
032 import com.liferay.portlet.expando.model.ExpandoTableConstants;
033 import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
034 import com.liferay.portlet.expando.service.ExpandoColumnServiceUtil;
035 import com.liferay.portlet.expando.service.ExpandoTableLocalServiceUtil;
036 import com.liferay.portlet.expando.service.ExpandoValueLocalServiceUtil;
037 import com.liferay.portlet.expando.service.ExpandoValueServiceUtil;
038
039 import java.io.Serializable;
040
041 import java.util.ArrayList;
042 import java.util.Arrays;
043 import java.util.Collection;
044 import java.util.Collections;
045 import java.util.Date;
046 import java.util.Enumeration;
047 import java.util.HashMap;
048 import java.util.List;
049 import java.util.Map;
050
051
054 public class ExpandoBridgeImpl implements ExpandoBridge {
055
056 public static boolean equals(
057 int type, Serializable serializable1, Serializable serializable2) {
058
059 if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
060 Boolean[] array1 = (Boolean[])serializable1;
061 Boolean[] array2 = (Boolean[])serializable2;
062
063 return Arrays.equals(array1, array2);
064 }
065 else if (type == ExpandoColumnConstants.DATE_ARRAY) {
066 Date[] array1 = (Date[])serializable1;
067 Date[] array2 = (Date[])serializable2;
068
069 return Arrays.equals(array1, array2);
070 }
071 else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
072 double[] array1 = (double[])serializable1;
073 double[] array2 = (double[])serializable2;
074
075 return Arrays.equals(array1, array2);
076 }
077 else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
078 float[] array1 = (float[])serializable1;
079 float[] array2 = (float[])serializable2;
080
081 return Arrays.equals(array1, array2);
082 }
083 else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
084 int[] array1 = (int[])serializable1;
085 int[] array2 = (int[])serializable2;
086
087 return Arrays.equals(array1, array2);
088 }
089 else if (type == ExpandoColumnConstants.LONG_ARRAY) {
090 long[] array1 = (long[])serializable1;
091 long[] array2 = (long[])serializable2;
092
093 return Arrays.equals(array1, array2);
094 }
095 else if (type == ExpandoColumnConstants.NUMBER_ARRAY) {
096 Number[] array1 = (Number[])serializable1;
097 Number[] array2 = (Number[])serializable2;
098
099 return Arrays.equals(array1, array2);
100 }
101 else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
102 short[] array1 = (short[])serializable1;
103 short[] array2 = (short[])serializable2;
104
105 return Arrays.equals(array1, array2);
106 }
107 else if (type == ExpandoColumnConstants.STRING_ARRAY) {
108 String[] array1 = (String[])serializable1;
109 String[] array2 = (String[])serializable2;
110
111 return Arrays.equals(array1, array2);
112 }
113
114 return serializable1.equals(serializable2);
115 }
116
117 public ExpandoBridgeImpl(long companyId, String className) {
118 this(companyId, className, 0);
119 }
120
121 public ExpandoBridgeImpl(long companyId, String className, long classPK) {
122 _companyId = companyId;
123
124 if (_companyId == 0) {
125 _companyId = CompanyThreadLocal.getCompanyId();
126 }
127
128 _className = className;
129 _classPK = classPK;
130
131 if (IndexerRegistryUtil.getIndexer(className) == null) {
132 setIndexEnabled(true);
133 }
134 }
135
136 @Override
137 public void addAttribute(String name) throws PortalException {
138 boolean secure =
139 PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_WRITE_CHECK_BY_DEFAULT;
140
141 if (ImportExportThreadLocal.isImportInProcess()) {
142 secure = false;
143 }
144
145 addAttribute(name, ExpandoColumnConstants.STRING, null, secure);
146 }
147
148 @Override
149 public void addAttribute(String name, boolean secure)
150 throws PortalException {
151
152 addAttribute(name, ExpandoColumnConstants.STRING, null, secure);
153 }
154
155 @Override
156 public void addAttribute(String name, int type) throws PortalException {
157 boolean secure =
158 PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_WRITE_CHECK_BY_DEFAULT;
159
160 if (ImportExportThreadLocal.isImportInProcess()) {
161 secure = false;
162 }
163
164 addAttribute(name, type, null, secure);
165 }
166
167 @Override
168 public void addAttribute(String name, int type, boolean secure)
169 throws PortalException {
170
171 addAttribute(name, type, null, secure);
172 }
173
174 @Override
175 public void addAttribute(String name, int type, Serializable defaultValue)
176 throws PortalException {
177
178 boolean secure =
179 PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_WRITE_CHECK_BY_DEFAULT;
180
181 if (ImportExportThreadLocal.isImportInProcess()) {
182 secure = false;
183 }
184
185 addAttribute(name, type, defaultValue, secure);
186 }
187
188 @Override
189 public void addAttribute(
190 String name, int type, Serializable defaultValue, boolean secure)
191 throws PortalException {
192
193 try {
194 ExpandoTable table = getTable();
195
196 if (secure) {
197 ExpandoColumnServiceUtil.addColumn(
198 table.getTableId(), name, type, defaultValue);
199 }
200 else {
201 ExpandoColumnLocalServiceUtil.addColumn(
202 table.getTableId(), name, type, defaultValue);
203 }
204 }
205 catch (Exception e) {
206 if (e instanceof PortalException) {
207 throw (PortalException)e;
208 }
209 else {
210 _log.error(e, e);
211 }
212 }
213 }
214
215 @Override
216 public boolean equals(Object obj) {
217 if (!(obj instanceof ExpandoBridgeImpl)) {
218 return false;
219 }
220
221 ExpandoBridgeImpl expandoBridgeImpl = (ExpandoBridgeImpl)obj;
222
223 try {
224 long tableId1 = getTable().getTableId();
225 long tableId2 = expandoBridgeImpl.getTable().getTableId();
226
227 if (tableId1 != tableId2) {
228 return false;
229 }
230 }
231 catch (Exception e) {
232 return false;
233 }
234
235 for (ExpandoColumn column : getAttributeColumns()) {
236 String name = column.getName();
237 int type = column.getType();
238
239 Serializable attribute1 = getAttribute(name);
240 Serializable attribute2 = expandoBridgeImpl.getAttribute(name);
241
242 if (!equals(type, attribute1, attribute2)) {
243 return false;
244 }
245 }
246
247 return true;
248 }
249
250 @Override
251 public Serializable getAttribute(String name) {
252 boolean secure =
253 PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_READ_CHECK_BY_DEFAULT;
254
255 if (ImportExportThreadLocal.isExportInProcess()) {
256 secure = false;
257 }
258
259 return getAttribute(name, secure);
260 }
261
262 @Override
263 public Serializable getAttribute(String name, boolean secure) {
264 Serializable data = null;
265
266 try {
267 if (secure) {
268 data = ExpandoValueServiceUtil.getData(
269 _companyId, _className,
270 ExpandoTableConstants.DEFAULT_TABLE_NAME, name, _classPK);
271 }
272 else {
273 data = ExpandoValueLocalServiceUtil.getData(
274 _companyId, _className,
275 ExpandoTableConstants.DEFAULT_TABLE_NAME, name, _classPK);
276 }
277 }
278 catch (Exception e) {
279 if (_log.isDebugEnabled()) {
280 _log.debug(e, e);
281 }
282 }
283
284 return data;
285 }
286
287 @Override
288 public Serializable getAttributeDefault(String name) {
289 try {
290 ExpandoColumn column =
291 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
292 _companyId, _className, name);
293
294 return column.getDefaultValue();
295 }
296 catch (Exception e) {
297 _log.error(e, e);
298
299 return null;
300 }
301 }
302
303 @Override
304 public Enumeration<String> getAttributeNames() {
305 List<String> columnNames = new ArrayList<String>();
306
307 for (ExpandoColumn column : getAttributeColumns()) {
308 columnNames.add(column.getName());
309 }
310
311 return Collections.enumeration(columnNames);
312 }
313
314 @Override
315 public UnicodeProperties getAttributeProperties(String name) {
316 try {
317 ExpandoColumn column =
318 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
319 _companyId, _className, name);
320
321 return column.getTypeSettingsProperties();
322 }
323 catch (Exception e) {
324 if (_log.isDebugEnabled()) {
325 _log.debug("Properties for " + name, e);
326 }
327
328 return new UnicodeProperties(true);
329 }
330 }
331
332 @Override
333 public Map<String, Serializable> getAttributes() {
334 boolean secure =
335 PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_READ_CHECK_BY_DEFAULT;
336
337 if (ImportExportThreadLocal.isExportInProcess()) {
338 secure = false;
339 }
340
341 return getAttributes(secure);
342 }
343
344 @Override
345 public Map<String, Serializable> getAttributes(boolean secure) {
346 Map<String, Serializable> attributes =
347 new HashMap<String, Serializable>();
348
349 for (ExpandoColumn column : getAttributeColumns()) {
350 attributes.put(
351 column.getName(), getAttribute(column.getName(), secure));
352 }
353
354 return attributes;
355 }
356
357 @Override
358 public Map<String, Serializable> getAttributes(Collection<String> names) {
359 boolean secure =
360 PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_READ_CHECK_BY_DEFAULT;
361
362 if (ImportExportThreadLocal.isExportInProcess()) {
363 secure = false;
364 }
365
366 return getAttributes(names, secure);
367 }
368
369 @Override
370 public Map<String, Serializable> getAttributes(
371 Collection<String> names, boolean secure) {
372
373 Map<String, Serializable> attributeValues = null;
374
375 try {
376 if (secure) {
377 attributeValues = ExpandoValueServiceUtil.getData(
378 _companyId, _className,
379 ExpandoTableConstants.DEFAULT_TABLE_NAME, names, _classPK);
380 }
381 else {
382 attributeValues = ExpandoValueLocalServiceUtil.getData(
383 _companyId, _className,
384 ExpandoTableConstants.DEFAULT_TABLE_NAME, names, _classPK);
385 }
386 }
387 catch (Exception e) {
388 if (_log.isDebugEnabled()) {
389 _log.debug(e, e);
390 }
391 }
392
393 return attributeValues;
394 }
395
396 @Override
397 public int getAttributeType(String name) {
398 try {
399 ExpandoColumn column =
400 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
401 _companyId, _className, name);
402
403 return column.getType();
404 }
405 catch (Exception e) {
406 _log.error(e, e);
407
408 return 0;
409 }
410 }
411
412 @Override
413 public String getClassName() {
414 return _className;
415 }
416
417 @Override
418 public long getClassPK() {
419 return _classPK;
420 }
421
422 @Override
423 public long getCompanyId() {
424 return _companyId;
425 }
426
427 @Override
428 public boolean hasAttribute(String name) {
429 ExpandoColumn column = null;
430
431 try {
432 column = ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
433 _companyId, _className, name);
434 }
435 catch (Exception e) {
436 }
437
438 if (column != null) {
439 return true;
440 }
441 else {
442 return false;
443 }
444 }
445
446 @Override
447 public boolean isIndexEnabled() {
448 if (_indexEnabled && (_classPK > 0)) {
449 return true;
450 }
451 else {
452 return false;
453 }
454 }
455
456 public void reindex() {
457 if (!isIndexEnabled()) {
458 return;
459 }
460
461 Indexer indexer = IndexerRegistryUtil.getIndexer(_className);
462
463 if (indexer != null) {
464 try {
465 indexer.reindex(_className, _classPK);
466 }
467 catch (Exception e) {
468 _log.error(e, e);
469 }
470 }
471 }
472
473 @Override
474 public void setAttribute(String name, Serializable value) {
475 boolean secure =
476 PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_WRITE_CHECK_BY_DEFAULT;
477
478 if (ImportExportThreadLocal.isImportInProcess()) {
479 secure = false;
480 }
481
482 setAttribute(name, value, secure);
483 }
484
485 @Override
486 public void setAttribute(String name, Serializable value, boolean secure) {
487 if (_classPK <= 0) {
488 throw new UnsupportedOperationException(
489 "Class primary key is less than 0");
490 }
491
492 try {
493 if (secure) {
494 ExpandoValueServiceUtil.addValue(
495 _companyId, _className,
496 ExpandoTableConstants.DEFAULT_TABLE_NAME, name, _classPK,
497 value);
498 }
499 else {
500 ExpandoValueLocalServiceUtil.addValue(
501 _companyId, _className,
502 ExpandoTableConstants.DEFAULT_TABLE_NAME, name, _classPK,
503 value);
504 }
505 }
506 catch (Exception e) {
507 _log.error(e, e);
508 }
509 }
510
511 @Override
512 public void setAttributeDefault(String name, Serializable defaultValue) {
513 try {
514 ExpandoColumn column =
515 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
516 _companyId, _className, name);
517
518 ExpandoColumnServiceUtil.updateColumn(
519 column.getColumnId(), column.getName(), column.getType(),
520 defaultValue);
521 }
522 catch (Exception e) {
523 _log.error(e, e);
524 }
525 }
526
527 @Override
528 public void setAttributeProperties(
529 String name, UnicodeProperties properties) {
530
531 boolean secure =
532 PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_WRITE_CHECK_BY_DEFAULT;
533
534 if (ImportExportThreadLocal.isImportInProcess()) {
535 secure = false;
536 }
537
538 setAttributeProperties(name, properties, secure);
539 }
540
541 @Override
542 public void setAttributeProperties(
543 String name, UnicodeProperties properties, boolean secure) {
544
545 try {
546 ExpandoColumn column =
547 ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
548 _companyId, _className, name);
549
550 if (secure) {
551 ExpandoColumnServiceUtil.updateTypeSettings(
552 column.getColumnId(), properties.toString());
553 }
554 else {
555 ExpandoColumnLocalServiceUtil.updateTypeSettings(
556 column.getColumnId(), properties.toString());
557 }
558 }
559 catch (Exception e) {
560 _log.error(e, e);
561 }
562 }
563
564 @Override
565 public void setAttributes(Map<String, Serializable> attributes) {
566 boolean secure =
567 PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_WRITE_CHECK_BY_DEFAULT;
568
569 if (ImportExportThreadLocal.isImportInProcess()) {
570 secure = false;
571 }
572
573 setAttributes(attributes, secure);
574 }
575
576 @Override
577 public void setAttributes(
578 Map<String, Serializable> attributes, boolean secure) {
579
580 if (_classPK <= 0) {
581 throw new UnsupportedOperationException(
582 "Class primary key is less than 0");
583 }
584
585 if ((attributes == null) || attributes.isEmpty()) {
586 return;
587 }
588
589 try {
590 if (secure) {
591 ExpandoValueServiceUtil.addValues(
592 _companyId, _className,
593 ExpandoTableConstants.DEFAULT_TABLE_NAME, _classPK,
594 attributes);
595 }
596 else {
597 ExpandoValueLocalServiceUtil.addValues(
598 _companyId, _className,
599 ExpandoTableConstants.DEFAULT_TABLE_NAME, _classPK,
600 attributes);
601 }
602 }
603 catch (Exception e) {
604 _log.error(e, e);
605 }
606 }
607
608 @Override
609 public void setAttributes(ServiceContext serviceContext) {
610 boolean secure =
611 PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_WRITE_CHECK_BY_DEFAULT;
612
613 if (ImportExportThreadLocal.isImportInProcess()) {
614 secure = false;
615 }
616
617 setAttributes(serviceContext, secure);
618 }
619
620 @Override
621 public void setAttributes(ServiceContext serviceContext, boolean secure) {
622 if (serviceContext == null) {
623 return;
624 }
625
626 setAttributes(serviceContext.getExpandoBridgeAttributes(), secure);
627 }
628
629 @Override
630 public void setClassName(String className) {
631 _className = className;
632 }
633
634 @Override
635 public void setClassPK(long classPK) {
636 _classPK = classPK;
637 }
638
639 @Override
640 public void setCompanyId(long companyId) {
641 _companyId = companyId;
642 }
643
644 @Override
645 public void setIndexEnabled(boolean indexEnabled) {
646 _indexEnabled = indexEnabled;
647 }
648
649 protected List<ExpandoColumn> getAttributeColumns() {
650 List<ExpandoColumn> columns = new ArrayList<ExpandoColumn>();
651
652 try {
653 columns = ExpandoColumnLocalServiceUtil.getDefaultTableColumns(
654 _companyId, _className);
655 }
656 catch (Exception e) {
657 if (_log.isDebugEnabled()) {
658 _log.debug(e, e);
659 }
660 }
661
662 return columns;
663 }
664
665 protected ExpandoTable getTable() throws PortalException, SystemException {
666 ExpandoTable table = ExpandoTableLocalServiceUtil.fetchDefaultTable(
667 _companyId, _className);
668
669 if (table == null) {
670 table = ExpandoTableLocalServiceUtil.addDefaultTable(
671 _companyId, _className);
672 }
673
674 return table;
675 }
676
677 private static Log _log = LogFactoryUtil.getLog(ExpandoBridgeImpl.class);
678
679 private String _className;
680 private long _classPK;
681 private long _companyId;
682 private boolean _indexEnabled;
683
684 }