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.portal.service.impl;
016    
017    import com.liferay.portal.NoSuchResourceActionException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.spring.aop.Skip;
021    import com.liferay.portal.kernel.transaction.Propagation;
022    import com.liferay.portal.kernel.transaction.Transactional;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.model.ResourceAction;
025    import com.liferay.portal.model.ResourceConstants;
026    import com.liferay.portal.model.RoleConstants;
027    import com.liferay.portal.security.permission.ActionKeys;
028    import com.liferay.portal.security.permission.ResourceActionsUtil;
029    import com.liferay.portal.service.base.ResourceActionLocalServiceBaseImpl;
030    import com.liferay.portal.util.comparator.ResourceActionBitwiseValueComparator;
031    
032    import java.util.ArrayList;
033    import java.util.List;
034    import java.util.Map;
035    import java.util.concurrent.ConcurrentHashMap;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Shuyang Zhou
040     */
041    public class ResourceActionLocalServiceImpl
042            extends ResourceActionLocalServiceBaseImpl {
043    
044            @Override
045            @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
046            public void checkResourceActions() throws SystemException {
047                    List<ResourceAction> resourceActions =
048                            resourceActionPersistence.findAll();
049    
050                    for (ResourceAction resourceAction : resourceActions) {
051                            String key = encodeKey(
052                                    resourceAction.getName(), resourceAction.getActionId());
053    
054                            _resourceActions.put(key, resourceAction);
055                    }
056            }
057    
058            @Override
059            public void checkResourceActions(String name, List<String> actionIds)
060                    throws SystemException {
061    
062                    checkResourceActions(name, actionIds, false);
063            }
064    
065            @Override
066            public void checkResourceActions(
067                            String name, List<String> actionIds, boolean addDefaultActions)
068                    throws SystemException {
069    
070                    long lastBitwiseValue = -1;
071                    List<ResourceAction> newResourceActions = null;
072    
073                    for (String actionId : actionIds) {
074                            String key = encodeKey(name, actionId);
075    
076                            ResourceAction resourceAction = _resourceActions.get(key);
077    
078                            if (resourceAction != null) {
079                                    continue;
080                            }
081    
082                            resourceAction = resourceActionPersistence.fetchByN_A(
083                                    name, actionId);
084    
085                            if (resourceAction == null) {
086                                    long bitwiseValue = 1;
087    
088                                    if (!actionId.equals(ActionKeys.VIEW)) {
089                                            if (lastBitwiseValue < 0) {
090                                                    ResourceAction lastResourceAction =
091                                                            resourceActionPersistence.fetchByName_First(
092                                                                    name,
093                                                                    new ResourceActionBitwiseValueComparator());
094    
095                                                    if (lastResourceAction != null) {
096                                                            lastBitwiseValue =
097                                                                    lastResourceAction.getBitwiseValue();
098                                                    }
099                                                    else {
100                                                            lastBitwiseValue = 1;
101                                                    }
102                                            }
103    
104                                            lastBitwiseValue = lastBitwiseValue << 1;
105    
106                                            bitwiseValue = lastBitwiseValue;
107                                    }
108    
109                                    long resourceActionId = counterLocalService.increment(
110                                            ResourceAction.class.getName());
111    
112                                    resourceAction = resourceActionPersistence.create(
113                                            resourceActionId);
114    
115                                    resourceAction.setName(name);
116                                    resourceAction.setActionId(actionId);
117                                    resourceAction.setBitwiseValue(bitwiseValue);
118    
119                                    resourceActionPersistence.update(resourceAction);
120    
121                                    if (newResourceActions == null) {
122                                            newResourceActions = new ArrayList<ResourceAction>();
123                                    }
124    
125                                    newResourceActions.add(resourceAction);
126                            }
127    
128                            _resourceActions.put(key, resourceAction);
129                    }
130    
131                    if (!addDefaultActions || (newResourceActions == null)) {
132                            return;
133                    }
134    
135                    List<String> groupDefaultActions =
136                            ResourceActionsUtil.getModelResourceGroupDefaultActions(name);
137    
138                    List<String> guestDefaultActions =
139                            ResourceActionsUtil.getModelResourceGuestDefaultActions(name);
140    
141                    long guestBitwiseValue = 0;
142                    long ownerBitwiseValue = 0;
143                    long siteMemberBitwiseValue = 0;
144    
145                    for (ResourceAction resourceAction : newResourceActions) {
146                            String actionId = resourceAction.getActionId();
147    
148                            if (guestDefaultActions.contains(actionId)) {
149                                    guestBitwiseValue |= resourceAction.getBitwiseValue();
150                            }
151    
152                            ownerBitwiseValue |= resourceAction.getBitwiseValue();
153    
154                            if (groupDefaultActions.contains(actionId)) {
155                                    siteMemberBitwiseValue |= resourceAction.getBitwiseValue();
156                            }
157                    }
158    
159                    if (guestBitwiseValue > 0) {
160                            resourcePermissionLocalService.addResourcePermissions(
161                                    name, RoleConstants.GUEST, ResourceConstants.SCOPE_INDIVIDUAL,
162                                    guestBitwiseValue);
163                    }
164    
165                    if (ownerBitwiseValue > 0) {
166                            resourcePermissionLocalService.addResourcePermissions(
167                                    name, RoleConstants.OWNER, ResourceConstants.SCOPE_INDIVIDUAL,
168                                    ownerBitwiseValue);
169                    }
170    
171                    if (siteMemberBitwiseValue > 0) {
172                            resourcePermissionLocalService.addResourcePermissions(
173                                    name, RoleConstants.SITE_MEMBER,
174                                    ResourceConstants.SCOPE_INDIVIDUAL, siteMemberBitwiseValue);
175                    }
176            }
177    
178            @Override
179            public ResourceAction deleteResourceAction(long resourceActionId)
180                    throws PortalException, SystemException {
181    
182                    return deleteResourceAction(
183                            resourceActionPersistence.findByPrimaryKey(resourceActionId));
184            }
185    
186            @Override
187            public ResourceAction deleteResourceAction(ResourceAction resourceAction)
188                    throws SystemException {
189    
190                    _resourceActions.remove(
191                            encodeKey(resourceAction.getName(), resourceAction.getActionId()));
192    
193                    return resourceActionPersistence.remove(resourceAction);
194            }
195    
196            @Override
197            @Skip
198            public ResourceAction fetchResourceAction(String name, String actionId) {
199                    String key = encodeKey(name, actionId);
200    
201                    return _resourceActions.get(key);
202            }
203    
204            @Override
205            @Skip
206            public ResourceAction getResourceAction(String name, String actionId)
207                    throws PortalException {
208    
209                    String key = encodeKey(name, actionId);
210    
211                    ResourceAction resourceAction = _resourceActions.get(key);
212    
213                    if (resourceAction == null) {
214                            throw new NoSuchResourceActionException(key);
215                    }
216    
217                    return resourceAction;
218            }
219    
220            @Override
221            public List<ResourceAction> getResourceActions(String name)
222                    throws SystemException {
223    
224                    return resourceActionPersistence.findByName(name);
225            }
226    
227            protected String encodeKey(String name, String actionId) {
228                    return name.concat(StringPool.POUND).concat(actionId);
229            }
230    
231            private static Map<String, ResourceAction> _resourceActions =
232                    new ConcurrentHashMap<String, ResourceAction>();
233    
234    }