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