001    /**
002     * Copyright (c) 2000-2012 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.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.security.permission.ActionKeys;
024    import com.liferay.portlet.expando.model.ExpandoColumn;
025    import com.liferay.portlet.expando.model.ExpandoValue;
026    import com.liferay.portlet.expando.service.base.ExpandoValueServiceBaseImpl;
027    import com.liferay.portlet.expando.service.permission.ExpandoColumnPermissionUtil;
028    
029    import java.io.Serializable;
030    
031    import java.util.Collection;
032    import java.util.Map;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class ExpandoValueServiceImpl extends ExpandoValueServiceBaseImpl {
038    
039            public ExpandoValue addValue(
040                            long companyId, String className, String tableName,
041                            String columnName, long classPK, Object data)
042                    throws PortalException, SystemException {
043    
044                    ExpandoColumn column = expandoColumnLocalService.getColumn(
045                            companyId, className, tableName, columnName);
046    
047                    ExpandoColumnPermissionUtil.check(
048                            getPermissionChecker(), column, ActionKeys.UPDATE);
049    
050                    return expandoValueLocalService.addValue(
051                            companyId, className, tableName, columnName, classPK, data);
052            }
053    
054            public ExpandoValue addValue(
055                            long companyId, String className, String tableName,
056                            String columnName, long classPK, String data)
057                    throws PortalException, SystemException {
058    
059                    ExpandoColumn column = expandoColumnLocalService.getColumn(
060                            companyId, className, tableName, columnName);
061    
062                    ExpandoColumnPermissionUtil.check(
063                            getPermissionChecker(), column, ActionKeys.UPDATE);
064    
065                    return expandoValueLocalService.addValue(
066                            companyId, className, tableName, columnName, classPK, data);
067            }
068    
069            public void addValues(
070                            long companyId, String className, String tableName, long classPK,
071                            Map<String, Serializable> attributeValues)
072                    throws PortalException, SystemException {
073    
074                    for (Map.Entry<String, Serializable> entry :
075                                    attributeValues.entrySet()) {
076    
077                            addValue(
078                                    companyId, className, tableName, entry.getKey(), classPK,
079                                    entry.getValue());
080                    }
081            }
082    
083            public Map<String, Serializable> getData(
084                            long companyId, String className, String tableName,
085                            Collection<String> columnNames, long classPK)
086                    throws PortalException, SystemException {
087    
088                    Map<String, Serializable> attributeValues =
089                            expandoValueLocalService.getData(
090                                    companyId, className, tableName, columnNames, classPK);
091    
092                    for (String columnName : columnNames) {
093                            ExpandoColumn column = expandoColumnLocalService.getColumn(
094                                    companyId, className, tableName, columnName);
095    
096                            if (!ExpandoColumnPermissionUtil.contains(
097                                            getPermissionChecker(), column, ActionKeys.VIEW)) {
098    
099                                    attributeValues.remove(columnName);
100                            }
101                    }
102    
103                    return attributeValues;
104            }
105    
106            public Serializable getData(
107                            long companyId, String className, String tableName,
108                            String columnName, long classPK)
109                    throws PortalException, SystemException {
110    
111                    ExpandoColumn column = expandoColumnLocalService.getColumn(
112                            companyId, className, tableName, columnName);
113    
114                    if (ExpandoColumnPermissionUtil.contains(
115                                    getPermissionChecker(), column, ActionKeys.VIEW)) {
116    
117                            return expandoValueLocalService.getData(
118                                    companyId, className, tableName, columnName, classPK);
119                    }
120                    else {
121                            return null;
122                    }
123            }
124    
125            public JSONObject getJSONData(
126                            long companyId, String className, String tableName,
127                            String columnName, long classPK)
128                    throws PortalException, SystemException {
129    
130                    ExpandoColumn column = expandoColumnLocalService.getColumn(
131                            companyId, className, tableName, columnName);
132    
133                    if (ExpandoColumnPermissionUtil.contains(
134                                    getPermissionChecker(), column, ActionKeys.VIEW)) {
135    
136                            String data = expandoValueLocalService.getData(
137                                    companyId, className, tableName, columnName, classPK,
138                                    StringPool.BLANK);
139    
140                            if (Validator.isNotNull(data)) {
141                                    if (!data.startsWith(StringPool.OPEN_CURLY_BRACE)) {
142                                            data = "{data:".concat(data).concat("}");
143                                    }
144    
145                                    return JSONFactoryUtil.createJSONObject(data);
146                            }
147                            else {
148                                    return null;
149                            }
150                    }
151                    else {
152                            return null;
153                    }
154            }
155    
156    }