001    /**
002     * Copyright (c) 2000-2010 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.annotation.Propagation;
019    import com.liferay.portal.kernel.annotation.Transactional;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.util.ListUtil;
023    import com.liferay.portal.kernel.util.MathUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.model.ResourceAction;
026    import com.liferay.portal.security.permission.ActionKeys;
027    import com.liferay.portal.service.base.ResourceActionLocalServiceBaseImpl;
028    import com.liferay.portal.util.PropsValues;
029    
030    import java.util.HashMap;
031    import java.util.List;
032    import java.util.Map;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class ResourceActionLocalServiceImpl
038            extends ResourceActionLocalServiceBaseImpl {
039    
040            @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
041            public void checkResourceActions() throws SystemException {
042                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM != 6) {
043                            return;
044                    }
045    
046                    List<ResourceAction> resourceActions =
047                            resourceActionPersistence.findAll();
048    
049                    for (ResourceAction resourceAction : resourceActions) {
050                            String key = encodeKey(
051                                    resourceAction.getName(), resourceAction.getActionId());
052    
053                            _resourceActions.put(key, resourceAction);
054                    }
055            }
056    
057            public void checkResourceActions(String name, List<String> actionIds)
058                    throws SystemException {
059    
060                    if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM != 6) {
061                            return;
062                    }
063    
064                    List<ResourceAction> resourceActions =
065                            resourceActionPersistence.findByName(name);
066    
067                    resourceActions = ListUtil.copy(resourceActions);
068    
069                    checkResourceActions(name, actionIds, resourceActions);
070            }
071    
072            public ResourceAction getResourceAction(String name, String actionId)
073                    throws PortalException {
074    
075                    String key = encodeKey(name, actionId);
076    
077                    ResourceAction resourceAction = _resourceActions.get(key);
078    
079                    if (resourceAction == null) {
080                            throw new NoSuchResourceActionException(key);
081                    }
082    
083                    return resourceAction;
084            }
085    
086            public List<ResourceAction> getResourceActions(String name)
087                    throws SystemException {
088    
089                    return resourceActionPersistence.findByName(name);
090            }
091    
092            protected void checkResourceActions(
093                            String name, List<String> actionIds,
094                            List<ResourceAction> resourceActions)
095                    throws SystemException {
096    
097                    long lastBitwiseValue = 1;
098    
099                    if (!resourceActions.isEmpty()) {
100                            ResourceAction resourceAction = resourceActions.get(
101                                    resourceActions.size() - 1);
102    
103                            lastBitwiseValue = resourceAction.getBitwiseValue();
104                    }
105    
106                    int lastBitwiseLogValue = MathUtil.base2Log(lastBitwiseValue);
107    
108                    for (String actionId : actionIds) {
109                            String key = encodeKey(name, actionId);
110    
111                            ResourceAction resourceAction = _resourceActions.get(key);
112    
113                            if (resourceAction == null) {
114                                    resourceAction = resourceActionPersistence.fetchByN_A(
115                                            name, actionId);
116                            }
117    
118                            if (resourceAction == null) {
119                                    long bitwiseValue = 1;
120    
121                                    if (!actionId.equals(ActionKeys.VIEW)) {
122                                            bitwiseValue = MathUtil.base2Pow(++lastBitwiseLogValue);
123                                    }
124    
125                                    long resourceActionId = counterLocalService.increment(
126                                            ResourceAction.class.getName());
127    
128                                    resourceAction = resourceActionPersistence.create(
129                                            resourceActionId);
130    
131                                    resourceAction.setName(name);
132                                    resourceAction.setActionId(actionId);
133                                    resourceAction.setBitwiseValue(bitwiseValue);
134    
135                                    resourceActionPersistence.update(resourceAction, false);
136    
137                                    _resourceActions.put(key, resourceAction);
138                            }
139                    }
140            }
141    
142            protected String encodeKey(String name, String actionId) {
143                    return name.concat(StringPool.POUND).concat(actionId);
144            }
145    
146            private static Map<String, ResourceAction> _resourceActions =
147                    new HashMap<String, ResourceAction>();
148    
149    }