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.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            @Override
040            public ExpandoValue addValue(
041                            long companyId, String className, String tableName,
042                            String columnName, long classPK, Object data)
043                    throws PortalException, SystemException {
044    
045                    ExpandoColumn column = expandoColumnLocalService.getColumn(
046                            companyId, className, tableName, columnName);
047    
048                    ExpandoColumnPermissionUtil.check(
049                            getPermissionChecker(), column, ActionKeys.UPDATE);
050    
051                    return expandoValueLocalService.addValue(
052                            companyId, className, tableName, columnName, classPK, data);
053            }
054    
055            @Override
056            public ExpandoValue addValue(
057                            long companyId, String className, String tableName,
058                            String columnName, long classPK, String data)
059                    throws PortalException, SystemException {
060    
061                    ExpandoColumn column = expandoColumnLocalService.getColumn(
062                            companyId, className, tableName, columnName);
063    
064                    ExpandoColumnPermissionUtil.check(
065                            getPermissionChecker(), column, ActionKeys.UPDATE);
066    
067                    return expandoValueLocalService.addValue(
068                            companyId, className, tableName, columnName, classPK, data);
069            }
070    
071            @Override
072            public void addValues(
073                            long companyId, String className, String tableName, long classPK,
074                            Map<String, Serializable> attributeValues)
075                    throws PortalException, SystemException {
076    
077                    for (Map.Entry<String, Serializable> entry :
078                                    attributeValues.entrySet()) {
079    
080                            addValue(
081                                    companyId, className, tableName, entry.getKey(), classPK,
082                                    entry.getValue());
083                    }
084            }
085    
086            @Override
087            public Map<String, Serializable> getData(
088                            long companyId, String className, String tableName,
089                            Collection<String> columnNames, long classPK)
090                    throws PortalException, SystemException {
091    
092                    Map<String, Serializable> attributeValues =
093                            expandoValueLocalService.getData(
094                                    companyId, className, tableName, columnNames, classPK);
095    
096                    for (String columnName : columnNames) {
097                            ExpandoColumn column = expandoColumnLocalService.getColumn(
098                                    companyId, className, tableName, columnName);
099    
100                            if (!ExpandoColumnPermissionUtil.contains(
101                                            getPermissionChecker(), column, ActionKeys.VIEW)) {
102    
103                                    attributeValues.remove(columnName);
104                            }
105                    }
106    
107                    return attributeValues;
108            }
109    
110            @Override
111            public Serializable getData(
112                            long companyId, String className, String tableName,
113                            String columnName, long classPK)
114                    throws PortalException, SystemException {
115    
116                    ExpandoColumn column = expandoColumnLocalService.getColumn(
117                            companyId, className, tableName, columnName);
118    
119                    if (ExpandoColumnPermissionUtil.contains(
120                                    getPermissionChecker(), column, ActionKeys.VIEW)) {
121    
122                            return expandoValueLocalService.getData(
123                                    companyId, className, tableName, columnName, classPK);
124                    }
125                    else {
126                            return null;
127                    }
128            }
129    
130            @Override
131            public JSONObject getJSONData(
132                            long companyId, String className, String tableName,
133                            String columnName, long classPK)
134                    throws PortalException, SystemException {
135    
136                    ExpandoColumn column = expandoColumnLocalService.getColumn(
137                            companyId, className, tableName, columnName);
138    
139                    if (ExpandoColumnPermissionUtil.contains(
140                                    getPermissionChecker(), column, ActionKeys.VIEW)) {
141    
142                            String data = expandoValueLocalService.getData(
143                                    companyId, className, tableName, columnName, classPK,
144                                    StringPool.BLANK);
145    
146                            if (Validator.isNotNull(data)) {
147                                    if (!data.startsWith(StringPool.OPEN_CURLY_BRACE)) {
148                                            data = "{data:".concat(data).concat("}");
149                                    }
150    
151                                    return JSONFactoryUtil.createJSONObject(data);
152                            }
153                            else {
154                                    return null;
155                            }
156                    }
157                    else {
158                            return null;
159                    }
160            }
161    
162    }