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