001    /**
002     * Copyright (c) 2000-2012 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.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    }