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.portal.model;
016    
017    import java.util.SortedMap;
018    import java.util.TreeMap;
019    
020    /**
021     * Manages a list of the roles with permission to access a resource block and
022     * the actions they can perform.
023     *
024     * @author Connor McKay
025     */
026    public class ResourceBlockPermissionsContainer {
027    
028            public void addPermission(long roleId, long actionIdsLong) {
029                    actionIdsLong |= getActionIds(roleId);
030    
031                    setPermissions(roleId, actionIdsLong);
032            }
033    
034            public long getActionIds(long roleId) {
035                    Long actionIdsLong = _permissions.get(roleId);
036    
037                    if (actionIdsLong == null) {
038                            actionIdsLong = 0L;
039                    }
040    
041                    return actionIdsLong;
042            }
043    
044            public SortedMap<Long, Long> getPermissions() {
045                    return _permissions;
046            }
047    
048            public void removePermission(long roleId, long actionIdsLong) {
049                    actionIdsLong = getActionIds(roleId) & (~actionIdsLong);
050    
051                    setPermissions(roleId, actionIdsLong);
052            }
053    
054            public void setPermissions(long roleId, long actionIdsLong) {
055                    if (actionIdsLong == 0) {
056                            _permissions.remove(roleId);
057                    }
058                    else {
059                            _permissions.put(roleId, actionIdsLong);
060                    }
061            }
062    
063            private SortedMap<Long, Long> _permissions = new TreeMap<Long, Long>();
064    
065    }