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