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