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: package org.apache.lenya.cms.ac.usecases;
019:
020: import java.util.ArrayList;
021: import java.util.Arrays;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.apache.lenya.ac.Group;
026: import org.apache.lenya.ac.IPRange;
027: import org.apache.lenya.cms.usecase.UsecaseException;
028:
029: /**
030: * Usecase to edit a IP range's group affiliation.
031: */
032: public class IPRangeGroups extends AccessControlUsecase {
033:
034: protected static final String IP_RANGE_GROUPS = "ipRangeGroups";
035: protected static final String OTHER_GROUPS = "otherGroups";
036: protected static final String ADD = "add";
037: protected static final String REMOVE = "remove";
038: protected static final String IP_RANGE_GROUP = "ipRangeGroup";
039: protected static final String OTHER_GROUP = "otherGroup";
040:
041: /**
042: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
043: */
044: protected void doExecute() throws Exception {
045: super .doExecute();
046:
047: IPRange ipRange = getIpRange();
048: ipRange.removeFromAllGroups();
049:
050: List ipRangeGroups = (List) getParameter(IP_RANGE_GROUPS);
051: for (Iterator i = ipRangeGroups.iterator(); i.hasNext();) {
052: Group group = (Group) i.next();
053: group.add(ipRange);
054: }
055: ipRange.save();
056: }
057:
058: protected IPRange getIpRange() {
059: String ipRangeId = getParameterAsString(IPRangeProfile.ID);
060: IPRange ipRange = getIpRangeManager().getIPRange(ipRangeId);
061: if (ipRange == null) {
062: throw new RuntimeException("IP range [" + ipRangeId
063: + "] not found.");
064: }
065: return ipRange;
066: }
067:
068: /**
069: * @see org.apache.lenya.cms.usecase.Usecase#advance()
070: */
071: public void advance() throws UsecaseException {
072: super .advance();
073:
074: String add = getParameterAsString(ADD);
075: String remove = getParameterAsString(REMOVE);
076: if (add != null || remove != null) {
077:
078: List ipRangeGroups = (List) getParameter(IP_RANGE_GROUPS);
079: List otherGroups = (List) getParameter(OTHER_GROUPS);
080:
081: if (add != null) {
082: String groupId = getParameterAsString(OTHER_GROUP);
083: if (groupId != null) {
084: if (getLogger().isDebugEnabled()) {
085: getLogger()
086: .debug("add group [" + groupId + "]");
087: }
088: Group group = getGroupManager().getGroup(groupId);
089: ipRangeGroups.add(group);
090: otherGroups.remove(group);
091: }
092: }
093:
094: if (remove != null) {
095: String groupId = getParameterAsString(IP_RANGE_GROUP);
096: if (groupId != null) {
097: if (getLogger().isDebugEnabled()) {
098: getLogger().debug(
099: "remove group [" + groupId + "]");
100: }
101: Group group = getGroupManager().getGroup(groupId);
102: otherGroups.add(group);
103: ipRangeGroups.remove(group);
104: }
105: }
106:
107: deleteParameter(ADD);
108: deleteParameter(REMOVE);
109: deleteParameter(IP_RANGE_GROUP);
110: deleteParameter(OTHER_GROUP);
111: }
112:
113: }
114:
115: protected void initParameters() {
116: super .initParameters();
117: Group[] ipRangeGroupArray = getIpRange().getGroups();
118:
119: List ipRangeGroups = new ArrayList(Arrays
120: .asList(ipRangeGroupArray));
121: setParameter(IP_RANGE_GROUPS, ipRangeGroups);
122:
123: Group[] allGroups = getGroupManager().getGroups();
124: List otherGroups = new ArrayList();
125: for (int i = 0; i < allGroups.length; i++) {
126: if (!ipRangeGroups.contains(allGroups[i])) {
127: otherGroups.add(allGroups[i]);
128: }
129: }
130: setParameter(OTHER_GROUPS, otherGroups);
131: }
132:
133: }
|