001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.expando.model.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.search.Indexer;
019    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
020    import com.liferay.portal.kernel.util.HashUtil;
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    import com.liferay.portlet.exportimport.lar.ExportImportThreadLocal;
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    /**
050     * @author Raymond Aug??
051     */
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<>();
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 = new HashMap<>();
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 int hashCode() {
375                    int hash = 0;
376    
377                    try {
378                            hash = HashUtil.hash(0, getTable());
379                    }
380                    catch (Exception e) {
381                    }
382    
383                    return HashUtil.hash(hash, getAttributeColumns());
384            }
385    
386            @Override
387            public boolean isIndexEnabled() {
388                    if (_indexEnabled && (_classPK > 0)) {
389                            return true;
390                    }
391    
392                    return false;
393            }
394    
395            public void reindex() {
396                    if (!isIndexEnabled()) {
397                            return;
398                    }
399    
400                    Indexer<?> indexer = IndexerRegistryUtil.nullSafeGetIndexer(_className);
401    
402                    try {
403                            indexer.reindex(_className, _classPK);
404                    }
405                    catch (Exception e) {
406                            throw new RuntimeException(e);
407                    }
408            }
409    
410            @Override
411            public void setAttribute(String name, Serializable value) {
412                    boolean secure =
413                            PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_WRITE_CHECK_BY_DEFAULT;
414    
415                    if (ExportImportThreadLocal.isImportInProcess()) {
416                            secure = false;
417                    }
418    
419                    setAttribute(name, value, secure);
420            }
421    
422            @Override
423            public void setAttribute(String name, Serializable value, boolean secure) {
424                    if (_classPK <= 0) {
425                            throw new UnsupportedOperationException(
426                                    "Class primary key is less than 0");
427                    }
428    
429                    try {
430                            if (secure) {
431                                    ExpandoValueServiceUtil.addValue(
432                                            _companyId, _className,
433                                            ExpandoTableConstants.DEFAULT_TABLE_NAME, name, _classPK,
434                                            value);
435                            }
436                            else {
437                                    ExpandoValueLocalServiceUtil.addValue(
438                                            _companyId, _className,
439                                            ExpandoTableConstants.DEFAULT_TABLE_NAME, name, _classPK,
440                                            value);
441                            }
442                    }
443                    catch (Exception e) {
444                            throw new RuntimeException(e);
445                    }
446            }
447    
448            @Override
449            public void setAttributeDefault(String name, Serializable defaultValue) {
450                    try {
451                            ExpandoColumn column =
452                                    ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
453                                            _companyId, _className, name);
454    
455                            ExpandoColumnServiceUtil.updateColumn(
456                                    column.getColumnId(), column.getName(), column.getType(),
457                                    defaultValue);
458                    }
459                    catch (Exception e) {
460                            throw new RuntimeException(e);
461                    }
462            }
463    
464            @Override
465            public void setAttributeProperties(
466                    String name, UnicodeProperties properties) {
467    
468                    boolean secure =
469                            PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_WRITE_CHECK_BY_DEFAULT;
470    
471                    if (ExportImportThreadLocal.isImportInProcess()) {
472                            secure = false;
473                    }
474    
475                    setAttributeProperties(name, properties, secure);
476            }
477    
478            @Override
479            public void setAttributeProperties(
480                    String name, UnicodeProperties properties, boolean secure) {
481    
482                    try {
483                            ExpandoColumn column =
484                                    ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
485                                            _companyId, _className, name);
486    
487                            if (secure) {
488                                    ExpandoColumnServiceUtil.updateTypeSettings(
489                                            column.getColumnId(), properties.toString());
490                            }
491                            else {
492                                    ExpandoColumnLocalServiceUtil.updateTypeSettings(
493                                            column.getColumnId(), properties.toString());
494                            }
495                    }
496                    catch (Exception e) {
497                            throw new RuntimeException(e);
498                    }
499            }
500    
501            @Override
502            public void setAttributes(Map<String, Serializable> attributes) {
503                    boolean secure =
504                            PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_WRITE_CHECK_BY_DEFAULT;
505    
506                    if (ExportImportThreadLocal.isImportInProcess()) {
507                            secure = false;
508                    }
509    
510                    setAttributes(attributes, secure);
511            }
512    
513            @Override
514            public void setAttributes(
515                    Map<String, Serializable> attributes, boolean secure) {
516    
517                    if (_classPK <= 0) {
518                            throw new UnsupportedOperationException(
519                                    "Class primary key is less than 0");
520                    }
521    
522                    if ((attributes == null) || attributes.isEmpty()) {
523                            return;
524                    }
525    
526                    try {
527                            if (secure) {
528                                    ExpandoValueServiceUtil.addValues(
529                                            _companyId, _className,
530                                            ExpandoTableConstants.DEFAULT_TABLE_NAME, _classPK,
531                                            attributes);
532                            }
533                            else {
534                                    ExpandoValueLocalServiceUtil.addValues(
535                                            _companyId, _className,
536                                            ExpandoTableConstants.DEFAULT_TABLE_NAME, _classPK,
537                                            attributes);
538                            }
539                    }
540                    catch (Exception e) {
541                            throw new RuntimeException(e);
542                    }
543            }
544    
545            @Override
546            public void setAttributes(ServiceContext serviceContext) {
547                    boolean secure =
548                            PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_WRITE_CHECK_BY_DEFAULT;
549    
550                    if (ExportImportThreadLocal.isImportInProcess()) {
551                            secure = false;
552                    }
553    
554                    setAttributes(serviceContext, secure);
555            }
556    
557            @Override
558            public void setAttributes(ServiceContext serviceContext, boolean secure) {
559                    if (serviceContext == null) {
560                            return;
561                    }
562    
563                    setAttributes(serviceContext.getExpandoBridgeAttributes(), secure);
564            }
565    
566            @Override
567            public void setClassName(String className) {
568                    _className = className;
569            }
570    
571            @Override
572            public void setClassPK(long classPK) {
573                    _classPK = classPK;
574            }
575    
576            @Override
577            public void setCompanyId(long companyId) {
578                    _companyId = companyId;
579            }
580    
581            @Override
582            public void setIndexEnabled(boolean indexEnabled) {
583                    _indexEnabled = indexEnabled;
584            }
585    
586            protected boolean equals(
587                    int type, Serializable serializable1, Serializable serializable2) {
588    
589                    if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
590                            Boolean[] array1 = (Boolean[])serializable1;
591                            Boolean[] array2 = (Boolean[])serializable2;
592    
593                            return Arrays.equals(array1, array2);
594                    }
595                    else if (type == ExpandoColumnConstants.DATE_ARRAY) {
596                            Date[] array1 = (Date[])serializable1;
597                            Date[] array2 = (Date[])serializable2;
598    
599                            return Arrays.equals(array1, array2);
600                    }
601                    else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
602                            double[] array1 = (double[])serializable1;
603                            double[] array2 = (double[])serializable2;
604    
605                            return Arrays.equals(array1, array2);
606                    }
607                    else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
608                            float[] array1 = (float[])serializable1;
609                            float[] array2 = (float[])serializable2;
610    
611                            return Arrays.equals(array1, array2);
612                    }
613                    else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
614                            int[] array1 = (int[])serializable1;
615                            int[] array2 = (int[])serializable2;
616    
617                            return Arrays.equals(array1, array2);
618                    }
619                    else if (type == ExpandoColumnConstants.LONG_ARRAY) {
620                            long[] array1 = (long[])serializable1;
621                            long[] array2 = (long[])serializable2;
622    
623                            return Arrays.equals(array1, array2);
624                    }
625                    else if (type == ExpandoColumnConstants.NUMBER_ARRAY) {
626                            Number[] array1 = (Number[])serializable1;
627                            Number[] array2 = (Number[])serializable2;
628    
629                            return Arrays.equals(array1, array2);
630                    }
631                    else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
632                            short[] array1 = (short[])serializable1;
633                            short[] array2 = (short[])serializable2;
634    
635                            return Arrays.equals(array1, array2);
636                    }
637                    else if (type == ExpandoColumnConstants.STRING_ARRAY) {
638                            String[] array1 = (String[])serializable1;
639                            String[] array2 = (String[])serializable2;
640    
641                            return Arrays.equals(array1, array2);
642                    }
643    
644                    return serializable1.equals(serializable2);
645            }
646    
647            protected List<ExpandoColumn> getAttributeColumns() {
648                    List<ExpandoColumn> columns = new ArrayList<>();
649    
650                    try {
651                            columns = ExpandoColumnLocalServiceUtil.getDefaultTableColumns(
652                                    _companyId, _className);
653                    }
654                    catch (Exception e) {
655                            throw new RuntimeException(e);
656                    }
657    
658                    return columns;
659            }
660    
661            protected ExpandoTable getTable() throws PortalException {
662                    ExpandoTable table = ExpandoTableLocalServiceUtil.fetchDefaultTable(
663                            _companyId, _className);
664    
665                    if (table == null) {
666                            table = ExpandoTableLocalServiceUtil.addDefaultTable(
667                                    _companyId, _className);
668                    }
669    
670                    return table;
671            }
672    
673            private String _className;
674            private long _classPK;
675            private long _companyId;
676            private boolean _indexEnabled;
677    
678    }