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