001    /**
002     * Copyright (c) 2000-2013 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.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    /**
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<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.getIndexer(_className);
389    
390                    if (indexer != null) {
391                            try {
392                                    indexer.reindex(_className, _classPK);
393                            }
394                            catch (Exception e) {
395                                    throw new RuntimeException(e);
396                            }
397                    }
398            }
399    
400            @Override
401            public void setAttribute(String name, Serializable value) {
402                    boolean secure =
403                            PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_WRITE_CHECK_BY_DEFAULT;
404    
405                    if (ExportImportThreadLocal.isImportInProcess()) {
406                            secure = false;
407                    }
408    
409                    setAttribute(name, value, secure);
410            }
411    
412            @Override
413            public void setAttribute(String name, Serializable value, boolean secure) {
414                    if (_classPK <= 0) {
415                            throw new UnsupportedOperationException(
416                                    "Class primary key is less than 0");
417                    }
418    
419                    try {
420                            if (secure) {
421                                    ExpandoValueServiceUtil.addValue(
422                                            _companyId, _className,
423                                            ExpandoTableConstants.DEFAULT_TABLE_NAME, name, _classPK,
424                                            value);
425                            }
426                            else {
427                                    ExpandoValueLocalServiceUtil.addValue(
428                                            _companyId, _className,
429                                            ExpandoTableConstants.DEFAULT_TABLE_NAME, name, _classPK,
430                                            value);
431                            }
432                    }
433                    catch (Exception e) {
434                            throw new RuntimeException(e);
435                    }
436            }
437    
438            @Override
439            public void setAttributeDefault(String name, Serializable defaultValue) {
440                    try {
441                            ExpandoColumn column =
442                                    ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
443                                            _companyId, _className, name);
444    
445                            ExpandoColumnServiceUtil.updateColumn(
446                                    column.getColumnId(), column.getName(), column.getType(),
447                                    defaultValue);
448                    }
449                    catch (Exception e) {
450                            throw new RuntimeException(e);
451                    }
452            }
453    
454            @Override
455            public void setAttributeProperties(
456                    String name, UnicodeProperties properties) {
457    
458                    boolean secure =
459                            PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_WRITE_CHECK_BY_DEFAULT;
460    
461                    if (ExportImportThreadLocal.isImportInProcess()) {
462                            secure = false;
463                    }
464    
465                    setAttributeProperties(name, properties, secure);
466            }
467    
468            @Override
469            public void setAttributeProperties(
470                    String name, UnicodeProperties properties, boolean secure) {
471    
472                    try {
473                            ExpandoColumn column =
474                                    ExpandoColumnLocalServiceUtil.getDefaultTableColumn(
475                                            _companyId, _className, name);
476    
477                            if (secure) {
478                                    ExpandoColumnServiceUtil.updateTypeSettings(
479                                            column.getColumnId(), properties.toString());
480                            }
481                            else {
482                                    ExpandoColumnLocalServiceUtil.updateTypeSettings(
483                                            column.getColumnId(), properties.toString());
484                            }
485                    }
486                    catch (Exception e) {
487                            throw new RuntimeException(e);
488                    }
489            }
490    
491            @Override
492            public void setAttributes(Map<String, Serializable> attributes) {
493                    boolean secure =
494                            PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_WRITE_CHECK_BY_DEFAULT;
495    
496                    if (ExportImportThreadLocal.isImportInProcess()) {
497                            secure = false;
498                    }
499    
500                    setAttributes(attributes, secure);
501            }
502    
503            @Override
504            public void setAttributes(
505                    Map<String, Serializable> attributes, boolean secure) {
506    
507                    if (_classPK <= 0) {
508                            throw new UnsupportedOperationException(
509                                    "Class primary key is less than 0");
510                    }
511    
512                    if ((attributes == null) || attributes.isEmpty()) {
513                            return;
514                    }
515    
516                    try {
517                            if (secure) {
518                                    ExpandoValueServiceUtil.addValues(
519                                            _companyId, _className,
520                                            ExpandoTableConstants.DEFAULT_TABLE_NAME, _classPK,
521                                            attributes);
522                            }
523                            else {
524                                    ExpandoValueLocalServiceUtil.addValues(
525                                            _companyId, _className,
526                                            ExpandoTableConstants.DEFAULT_TABLE_NAME, _classPK,
527                                            attributes);
528                            }
529                    }
530                    catch (Exception e) {
531                            throw new RuntimeException(e);
532                    }
533            }
534    
535            @Override
536            public void setAttributes(ServiceContext serviceContext) {
537                    boolean secure =
538                            PropsValues.PERMISSIONS_CUSTOM_ATTRIBUTE_WRITE_CHECK_BY_DEFAULT;
539    
540                    if (ExportImportThreadLocal.isImportInProcess()) {
541                            secure = false;
542                    }
543    
544                    setAttributes(serviceContext, secure);
545            }
546    
547            @Override
548            public void setAttributes(ServiceContext serviceContext, boolean secure) {
549                    if (serviceContext == null) {
550                            return;
551                    }
552    
553                    setAttributes(serviceContext.getExpandoBridgeAttributes(), secure);
554            }
555    
556            @Override
557            public void setClassName(String className) {
558                    _className = className;
559            }
560    
561            @Override
562            public void setClassPK(long classPK) {
563                    _classPK = classPK;
564            }
565    
566            @Override
567            public void setCompanyId(long companyId) {
568                    _companyId = companyId;
569            }
570    
571            @Override
572            public void setIndexEnabled(boolean indexEnabled) {
573                    _indexEnabled = indexEnabled;
574            }
575    
576            protected boolean equals(
577                    int type, Serializable serializable1, Serializable serializable2) {
578    
579                    if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
580                            Boolean[] array1 = (Boolean[])serializable1;
581                            Boolean[] array2 = (Boolean[])serializable2;
582    
583                            return Arrays.equals(array1, array2);
584                    }
585                    else if (type == ExpandoColumnConstants.DATE_ARRAY) {
586                            Date[] array1 = (Date[])serializable1;
587                            Date[] array2 = (Date[])serializable2;
588    
589                            return Arrays.equals(array1, array2);
590                    }
591                    else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
592                            double[] array1 = (double[])serializable1;
593                            double[] array2 = (double[])serializable2;
594    
595                            return Arrays.equals(array1, array2);
596                    }
597                    else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
598                            float[] array1 = (float[])serializable1;
599                            float[] array2 = (float[])serializable2;
600    
601                            return Arrays.equals(array1, array2);
602                    }
603                    else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
604                            int[] array1 = (int[])serializable1;
605                            int[] array2 = (int[])serializable2;
606    
607                            return Arrays.equals(array1, array2);
608                    }
609                    else if (type == ExpandoColumnConstants.LONG_ARRAY) {
610                            long[] array1 = (long[])serializable1;
611                            long[] array2 = (long[])serializable2;
612    
613                            return Arrays.equals(array1, array2);
614                    }
615                    else if (type == ExpandoColumnConstants.NUMBER_ARRAY) {
616                            Number[] array1 = (Number[])serializable1;
617                            Number[] array2 = (Number[])serializable2;
618    
619                            return Arrays.equals(array1, array2);
620                    }
621                    else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
622                            short[] array1 = (short[])serializable1;
623                            short[] array2 = (short[])serializable2;
624    
625                            return Arrays.equals(array1, array2);
626                    }
627                    else if (type == ExpandoColumnConstants.STRING_ARRAY) {
628                            String[] array1 = (String[])serializable1;
629                            String[] array2 = (String[])serializable2;
630    
631                            return Arrays.equals(array1, array2);
632                    }
633    
634                    return serializable1.equals(serializable2);
635            }
636    
637            protected List<ExpandoColumn> getAttributeColumns() {
638                    List<ExpandoColumn> columns = new ArrayList<ExpandoColumn>();
639    
640                    try {
641                            columns = ExpandoColumnLocalServiceUtil.getDefaultTableColumns(
642                                    _companyId, _className);
643                    }
644                    catch (Exception e) {
645                            throw new RuntimeException(e);
646                    }
647    
648                    return columns;
649            }
650    
651            protected ExpandoTable getTable() throws PortalException, SystemException {
652                    ExpandoTable table = ExpandoTableLocalServiceUtil.fetchDefaultTable(
653                            _companyId, _className);
654    
655                    if (table == null) {
656                            table = ExpandoTableLocalServiceUtil.addDefaultTable(
657                                    _companyId, _className);
658                    }
659    
660                    return table;
661            }
662    
663            private String _className;
664            private long _classPK;
665            private long _companyId;
666            private boolean _indexEnabled;
667    
668    }