001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.lenya.cms.ac.usecase.impl;
020:
021: import java.util.Arrays;
022: import java.util.HashMap;
023: import java.util.HashSet;
024: import java.util.Map;
025: import java.util.Set;
026:
027: /**
028: * Class to manage roles for a usecase.
029: *
030: * @version $Id: UsecaseRoles.java 562973 2007-08-05 21:44:42Z nettings $
031: */
032: public class UsecaseRoles {
033:
034: private Map usecaseToRoles = new HashMap();
035:
036: /**
037: * Ctor.
038: */
039: public UsecaseRoles() {
040: // do nothing
041: }
042:
043: /**
044: * Sets the roles for a usecase.
045: * @param usecaseId The usecase ID.
046: * @param roleIds The role IDs.
047: */
048: public void setRoles(String usecaseId, String[] roleIds) {
049: this .usecaseToRoles.put(usecaseId, roleIds);
050: }
051:
052: /**
053: * Returns the roles for a usecase. If no roles are defined for this
054: * usecase, an array of size 0 is returned.
055: * @param usecaseId The usecase ID.
056: * @return A role array.
057: */
058: public String[] getRoles(String usecaseId) {
059: String[] usecaseRoles;
060: if (this .usecaseToRoles.containsKey(usecaseId)) {
061: usecaseRoles = (String[]) this .usecaseToRoles
062: .get(usecaseId);
063: } else {
064: usecaseRoles = new String[0];
065: }
066: return usecaseRoles;
067: }
068:
069: /**
070: * Checks if a usecase has roles.
071: * @param usecaseId The usecase ID.
072: * @return A boolean value.
073: */
074: public boolean hasRoles(String usecaseId) {
075: return this .usecaseToRoles.containsKey(usecaseId);
076: }
077:
078: /**
079: * @return All available usecase names.
080: */
081: public String[] getUsecaseNames() {
082: Set names = this .usecaseToRoles.keySet();
083: return (String[]) names.toArray(new String[names.size()]);
084: }
085:
086: /**
087: * @param usecase The usecase name.
088: * @param role The role ID.
089: */
090: public void addRole(String usecase, String role) {
091: String[] usecaseRoles = getRoles(usecase);
092: Set newRoles = new HashSet();
093: newRoles.addAll(Arrays.asList(usecaseRoles));
094: newRoles.add(role);
095: this .usecaseToRoles.put(usecase, newRoles
096: .toArray(new String[newRoles.size()]));
097: }
098:
099: /**
100: * @param usecase The usecase.
101: * @param role The role.
102: */
103: public void removeRole(String usecase, String role) {
104: String[] usecaseRoles = getRoles(usecase);
105: Set newRoles = new HashSet();
106: newRoles.addAll(Arrays.asList(usecaseRoles));
107:
108: if (!newRoles.contains(role)) {
109: throw new RuntimeException("The role [" + role
110: + "] is not set for usecase [" + usecase + "]");
111: }
112:
113: newRoles.remove(role);
114: this .usecaseToRoles.put(usecase, newRoles
115: .toArray(new String[newRoles.size()]));
116: }
117:
118: }
|