001    /**
002     * Copyright (c) 2000-present 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.permission;
016    
017    import com.liferay.portal.kernel.util.ListUtil;
018    
019    import java.io.Serializable;
020    
021    import java.util.Collection;
022    import java.util.HashMap;
023    import java.util.HashSet;
024    import java.util.List;
025    import java.util.Map;
026    import java.util.Set;
027    
028    /**
029     * @author Jorge Ferrer
030     */
031    public class ModelPermissions implements Cloneable, Serializable {
032    
033            public ModelPermissions() {
034            }
035    
036            public void addRolePermissions(String roleName, String actionId) {
037                    Set<String> roleNames = _roleNamesMap.get(actionId);
038    
039                    if (roleNames == null) {
040                            roleNames = new HashSet<>();
041    
042                            _roleNamesMap.put(actionId, roleNames);
043                    }
044    
045                    roleNames.add(roleName);
046    
047                    Set<String> actionIds = _actionIdsMap.get(roleName);
048    
049                    if (actionIds == null) {
050                            actionIds = new HashSet<>();
051    
052                            _actionIdsMap.put(roleName, actionIds);
053                    }
054    
055                    actionIds.add(actionId);
056            }
057    
058            public void addRolePermissions(String roleName, String[] actionIds) {
059                    if (actionIds == null) {
060                            return;
061                    }
062    
063                    for (String actionId : actionIds) {
064                            addRolePermissions(roleName, actionId);
065                    }
066            }
067    
068            @Override
069            public Object clone() {
070                    return new ModelPermissions(
071                            new HashMap<String, Set<String>>(_roleNamesMap),
072                            new HashMap<String, Set<String>>(_actionIdsMap));
073            }
074    
075            public String[] getActionIds(String roleName) {
076                    List<String> actionIds = getActionIdsList(roleName);
077    
078                    return actionIds.toArray(new String[actionIds.size()]);
079            }
080    
081            public List<String> getActionIdsList(String roleName) {
082                    Set<String> actionIds = _actionIdsMap.get(roleName);
083    
084                    return ListUtil.fromCollection(actionIds);
085            }
086    
087            public Collection<String> getRoleNames() {
088                    return _actionIdsMap.keySet();
089            }
090    
091            public Collection<String> getRoleNames(String actionId) {
092                    Set<String> roleNames = _roleNamesMap.get(actionId);
093    
094                    if (roleNames == null) {
095                            roleNames = new HashSet<>();
096                    }
097    
098                    return roleNames;
099            }
100    
101            public boolean isEmpty() {
102                    return _actionIdsMap.isEmpty();
103            }
104    
105            protected ModelPermissions(
106                    Map<String, Set<String>> roleNamesMap,
107                    Map<String, Set<String>> actionIdsMap) {
108    
109                    _roleNamesMap.putAll(roleNamesMap);
110                    _actionIdsMap.putAll(actionIdsMap);
111            }
112    
113            private final Map<String, Set<String>> _actionIdsMap = new HashMap<>();
114            private final Map<String, Set<String>> _roleNamesMap = new HashMap<>();
115    
116    }