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.security.permission;
016    
017    import java.io.Serializable;
018    
019    import java.util.ArrayList;
020    import java.util.HashMap;
021    import java.util.List;
022    import java.util.Map;
023    
024    /**
025     * Manages a list resource block IDs and the actions that can be performed on
026     * the resources in each.
027     *
028     * @author Connor McKay
029     */
030    public class ResourceBlockIdsBag implements Serializable {
031    
032            public void addResourceBlockId(long resourceBlockId, long actionIdsLong) {
033                    actionIdsLong |= getActionIds(resourceBlockId);
034    
035                    _permissions.put(resourceBlockId, actionIdsLong);
036            }
037    
038            public long getActionIds(long resourceBlockId) {
039                    Long oldActionIdsLong = _permissions.get(resourceBlockId);
040    
041                    if (oldActionIdsLong == null) {
042                            oldActionIdsLong = 0L;
043                    }
044    
045                    return oldActionIdsLong;
046            }
047    
048            public List<Long> getResourceBlockIds(long actionIdsLong) {
049                    List<Long> resourceBlockIds = new ArrayList<Long>();
050    
051                    for (Map.Entry<Long, Long> permission : _permissions.entrySet()) {
052                            if ((permission.getValue() & actionIdsLong) == actionIdsLong) {
053                                    resourceBlockIds.add(permission.getKey());
054                            }
055                    }
056    
057                    return resourceBlockIds;
058            }
059    
060            public boolean hasResourceBlockId(
061                    long resourceBlockId, long actionIdsLong) {
062    
063                    if ((getActionIds(resourceBlockId) & actionIdsLong) == actionIdsLong) {
064                            return true;
065                    }
066                    else {
067                            return false;
068                    }
069            }
070    
071            private Map<Long, Long> _permissions = new HashMap<Long, Long>();
072    
073    }