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