001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.web.controllers.subversion;
034:
035: import org.libresource.Libresource;
036: import org.libresource.LibresourceResourceValue;
037:
038: import org.libresource.kernel.KernelConstants;
039: import org.libresource.kernel.LibresourceSecurityException;
040: import org.libresource.kernel.interfaces.KernelService;
041:
042: import org.libresource.subversion.SubversionConstants;
043: import org.libresource.subversion.ejb.model.SubversionRepositoryResourceValue;
044: import org.libresource.subversion.interfaces.LibresourceSubversionService;
045:
046: import org.libresource.web.Controller;
047:
048: import java.net.URI;
049:
050: import java.util.ArrayList;
051: import java.util.Iterator;
052: import java.util.TreeSet;
053:
054: import javax.servlet.http.HttpServletRequest;
055: import javax.servlet.http.HttpServletResponse;
056:
057: public class EditRepositoryController implements Controller {
058: public Object process(URI uri, HttpServletRequest request,
059: HttpServletResponse response) throws Exception {
060: if (request.getParameter("cancel") != null) {
061: return uri;
062: }
063:
064: KernelService kernelService = (KernelService) Libresource
065: .getService(KernelConstants.SERVICE);
066:
067: if (!kernelService.checkSecurity(uri,
068: KernelConstants.SECURITY_UPDATE)) {
069: throw new LibresourceSecurityException(uri,
070: KernelConstants.SECURITY_UPDATE);
071: }
072:
073: LibresourceSubversionService libresourceSubversionService = (LibresourceSubversionService) Libresource
074: .getService(SubversionConstants.SERVICE);
075: SubversionRepositoryResourceValue repository = libresourceSubversionService
076: .getRepository(uri);
077:
078: if (request.getParameter("addReader") != null) {
079: String newReader = request.getParameter("newReader");
080:
081: if (newReader.indexOf("/users/") != -1) {
082: newReader = newReader.substring("/users/".length());
083: }
084:
085: libresourceSubversionService.editRepository(uri, repository
086: .getName(), repository.getSvnName(), repository
087: .getDescription(), repository.getCommiters(),
088: addUser(newReader, repository.getReaders(), ","));
089: }
090:
091: if (request.getParameter("removeReader") != null) {
092: String removeUser = request.getParameter("user").substring(
093: request.getParameter("user").indexOf("/users/")
094: + "/users/".length()).trim();
095: libresourceSubversionService
096: .editRepository(uri, repository.getName(),
097: repository.getSvnName(), repository
098: .getDescription(), repository
099: .getCommiters(), removeUser(
100: removeUser,
101: repository.getReaders(), ","));
102: }
103:
104: if (request.getParameter("addCommiter") != null) {
105: String newCommiter = request.getParameter("newCommiter");
106:
107: if (newCommiter.indexOf("/users/") != -1) {
108: newCommiter = newCommiter.substring("/users/".length());
109: }
110:
111: libresourceSubversionService.editRepository(uri, repository
112: .getName(), repository.getSvnName(), repository
113: .getDescription(), addUser(newCommiter, repository
114: .getCommiters(), ","), repository.getReaders());
115: }
116:
117: if (request.getParameter("removeCommiter") != null) {
118: String removeUser = request.getParameter("user").substring(
119: request.getParameter("user").indexOf("/users/")
120: + "/users/".length()).trim();
121: libresourceSubversionService.editRepository(uri, repository
122: .getName(), repository.getSvnName(), repository
123: .getDescription(), removeUser(removeUser,
124: repository.getCommiters(), ","), repository
125: .getReaders());
126: }
127:
128: // show form or change data
129: if (request.getParameter("save") == null) {
130: request.setAttribute("repository", repository);
131:
132: LibresourceResourceValue[] readers = libresourceSubversionService
133: .listReaders(uri);
134: LibresourceResourceValue[] commiters = libresourceSubversionService
135: .listCommiters(uri);
136: request.setAttribute("commitersUser", commiters);
137: request.setAttribute("readersUser", readers);
138:
139: return "/pages/modules/subversion/editRepository.jsp";
140: }
141:
142: if (request.getParameter("cancel") == null) {
143: libresourceSubversionService.editRepository(uri, request
144: .getParameter("name"), request
145: .getParameter("svnname"), request
146: .getParameter("description"), repository
147: .getCommiters(), repository.getReaders());
148: }
149:
150: return uri;
151: }
152:
153: private String removeUser(String user, String users, String split) {
154: String[] list = users.split(split);
155: ArrayList userList = new ArrayList();
156: String currentUser = null;
157:
158: if (user.equals("guest")) {
159: user = "*";
160: }
161:
162: for (int i = 0; i < list.length; i++) {
163: currentUser = list[i].trim();
164:
165: if ((currentUser.length() > 0) && !currentUser.equals(user)) {
166: if (currentUser.equals("guest")) {
167: userList.add("*");
168: } else {
169: userList.add(currentUser);
170: }
171: }
172: }
173:
174: StringBuffer result = new StringBuffer();
175: Iterator i = userList.iterator();
176:
177: while (i.hasNext()) {
178: result.append(i.next());
179:
180: if (i.hasNext()) {
181: result.append(",");
182: }
183: }
184:
185: return result.toString();
186: }
187:
188: private String addUser(String user, String users, String split) {
189: String[] list = users.split(split);
190: TreeSet userList = new TreeSet();
191:
192: if (user.equals("guest")) {
193: userList.add("*");
194: } else if (user.trim().length() > 0) {
195: userList.add(user);
196: }
197:
198: String currentUser = null;
199:
200: for (int i = 0; i < list.length; i++) {
201: currentUser = list[i].trim();
202:
203: if (currentUser.length() > 0) {
204: if (currentUser.equals("guest")) {
205: userList.add("*");
206: } else {
207: userList.add(currentUser);
208: }
209: }
210: }
211:
212: StringBuffer result = new StringBuffer();
213: Iterator i = userList.iterator();
214:
215: while (i.hasNext()) {
216: result.append(i.next());
217:
218: if (i.hasNext()) {
219: result.append(",");
220: }
221: }
222:
223: return result.toString();
224: }
225: }
|