001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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,
071                            long classPK, 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 Serializable getData(
084                            long companyId, String className, String tableName,
085                            String columnName, long classPK)
086                    throws PortalException, SystemException {
087    
088                    ExpandoColumn column = expandoColumnLocalService.getColumn(
089                            companyId, className, tableName, columnName);
090    
091                    if (ExpandoColumnPermissionUtil.contains(
092                                    getPermissionChecker(), column, ActionKeys.VIEW)) {
093    
094                            return expandoValueLocalService.getData(
095                                    companyId, className, tableName, columnName, classPK);
096                    }
097                    else {
098                            return null;
099                    }
100            }
101    
102            public Map<String, Serializable> getData(
103                            long companyId, String className, String tableName,
104                            Collection<String> columnNames, long classPK)
105                    throws PortalException, SystemException {
106    
107                    Map<String, Serializable> attributeValues =
108                            expandoValueLocalService.getData(
109                                    companyId, className, tableName, columnNames, classPK);
110    
111                    for (String columnName : columnNames) {
112                            ExpandoColumn column = expandoColumnLocalService.getColumn(
113                                    companyId, className, tableName, columnName);
114    
115                            if (!ExpandoColumnPermissionUtil.contains(
116                                            getPermissionChecker(), column, ActionKeys.VIEW)) {
117    
118                                    attributeValues.remove(columnName);
119                            }
120                    }
121    
122                    return attributeValues;
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    }