head	1.3;
access;
symbols
	TIGRIS_NOV_12_2000:1.2
	OLDHELM:1.2.0.2
	TIGRIS_NOV_11_2000:1.2;
locks; strict;
comment	@# @;


1.3
date	2000.11.12.23.10.06;	author dlr;	state dead;
branches;
next	1.2;

1.2
date	2000.10.03.02.22.01;	author dlr;	state Exp;
branches;
next	1.1;

1.1
date	2000.09.29.23.37.45;	author kmaples;	state Exp;
branches;
next	;


desc
@@


1.3
log
@Does not exist--possibly only WRT this location--in newhelm.
@
text
@/* ================================================================
 * Copyright (c) 2000 CollabNet.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. The end-user documentation included with the redistribution, if
 * any, must include the following acknowlegement: "This product includes
 * software developed by CollabNet (http://www.Collab.Net/)."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 * 
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 * 
 * 5. Products derived from this software may not use the "Tigris" name
 * nor may "Tigris" appear in their names without prior written
 * permission of CollabNet.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by many
 * individuals on behalf of CollabNet.
 */


package org.tigris.helm.admin;

import org.webmacro.util.*;
import org.webmacro.servlet.*;
import org.webmacro.engine.*;
import org.webmacro.broker.*;

import javax.servlet.*;
import javax.servlet.http.*;

import java.sql.*;
import java.io.IOException;
import java.util.StringTokenizer;
import com.sun.java.util.collections.ArrayList;
import com.sun.java.util.collections.Iterator;
import gnu.regexp.RE;

import org.joist.security.PermissionSet;
import org.joist.security.RedirectException;
import org.joist.security.RedirectURLException;
import org.joist.security.UnsecureServlet;

import org.tigris.helm.database.ProjectManager;
import org.tigris.helm.tool.UpdateException;

import org.tigris.helm.tool.ProjectCreator;

/**
 * Present a form for the addition of a new project
 */
public class SelfServeProjectAdd extends UnsecureServlet {

    static public Log log = new Log("sspAdd", "Self Serve Project Add");
    static private boolean debug = true;

    private boolean add_failed = false;

    static private RE projectRE = null;

    static final int succesfulError = -1;
    static final int invalidCouponError = 0;
    static final int missingFieldError = 1;
    static final int validationProblemError = 2;
    static final int creationFailedError = 3;
    static final int badProjectNameError = 4;
    static final int badProjectIDError = 5;
    static final int projectAlreadyExistsError = 6;
    static final int updateFailedError = 7;
    static final int databaseError = 8;
    static final int noSuchUserError = 9;

    public void start() throws ServletException {
	super.start();
	try {
	    projectRE= new RE("^[a-zA-Z0-9]{1,64}$");
	}
	catch (gnu.regexp.REException e) {
	    log.exception(e);
	}
    }

    String vendorCoupon;
    String bugs_password;
    int projectID;

    String successURL;
    String failureURL;

    static final String defaultFailureURL = "http://www.tigris.org";
    static final String defaultSuccessURL = "http://www.sourcexchange.com";

    protected boolean validateCoupon(Connection connection, Integer projectID, String coupon)
    throws SQLException {
	/*String query = "select coupon from project where project=" + projectID.toString() + " and coupon=" + coupon;
	Statement s = connection.createStatement();
	ResultSet r = s.executeQuery(query);
	return (r.next());*/
	return true;
    }

    protected void failTransaction(WebContext context, int errorCode)
	throws RedirectURLException, RedirectException {
	add_failed = true;
	String pid = new Integer(projectID).toString();
	String ec = new Integer(errorCode).toString();
	if (debug) log.debug("Project creation failed: error " + ec);

	String URL;
	if (failureURL == null) URL = defaultFailureURL;
	else URL = failureURL;

	URL = URL + "?projectID=" + pid + "&vendorCoupon=" + vendorCoupon 
	    + "&errorCode=" + ec;
	redirectToURL(context, URL);
    }

    protected void succeedTransaction(WebContext context, String projectURL)
    throws RedirectURLException, RedirectException {
	String pid = new Integer(projectID).toString();
	if (debug) log.debug("Project creation succeeded: project ID " + pid);

	String URL;
	if (successURL == null) URL = defaultSuccessURL;
	else URL = successURL;
	URL = URL + "?projectId=" + pid + "&coupon=" + vendorCoupon + 
	    "&projectURL=" + projectURL + "&bugs=" + bugs_password;
	redirectToURL(context, URL);
    }

    protected String processRequest(Connection connection, WebContext context)
	throws RedirectException {
    	
	//	String templateName = super.processRequest(connection, context);
	// super sets $UserName, Cancel -- but is this relevant?

	HttpServletRequest request = context.getRequest();
	String domain = request.getServerName();

	if (debug) log.debug("SelfServeProjectAdd: entered");

	successURL = (String) context.getForm("successURL");
	failureURL = (String) context.getForm("failureURL");

	Integer projectID= new Integer(-1);
	try {
	    projectID = new Integer(context.getForm("projectID"));
	} catch (NumberFormatException e) {
	    failTransaction(context, badProjectIDError);	   
	}

	// Before doing anything, check the vendor coupon.
	try {
	    if (!validateCoupon(connection, projectID, context.getForm("vendorCoupon"))) {
		failTransaction(context, invalidCouponError);
	    }
	} catch (SQLException e) {
	    failTransaction(context, databaseError);
	}

	//Check project name for validity (giving a default if not provided)
	String projectName = (String) context.getForm("projectName");
	if (projectName == null || projectName.equals("")) {
	    projectName = new String("project" + projectID.toString());
	}
	
	if (!projectRE.isMatch(projectName)) {
	    failTransaction(context, badProjectNameError);	    
	}
	
	String projectDescription = (String) context.getForm("projectDescription");

	//Turn the list of owners into an actual list.
	String owners = (String) context.getForm("owners");	
	if (owners == null) owners = "";
	ArrayList ownerList = new ArrayList();
        StringTokenizer tok = new StringTokenizer(owners, ";");
        while (tok.hasMoreTokens()) ownerList.add(tok.nextToken());
	
	//Create the project.
	try {
	    bugs_password = ProjectCreator.createProject(getDataManager(context), 
							 connection, domain, projectName, 
							 projectDescription, ownerList);
	} catch (ProjectManager.AlreadyExists e) {
	    failTransaction(context, projectAlreadyExistsError);
	} catch (UpdateException e) {
	    failTransaction(context, updateFailedError);
	} catch (Exception e) {
	    if (debug) log.debug("Caught exception of type " + e.toString());
	    failTransaction(context, creationFailedError);
	}

	if (add_failed==false) {
	    String projectURL = "http://" + domain + "/projects/" 
		+ projectName;
	    succeedTransaction(context, projectURL);
	}
	return "";	
    }
  
} /* end class SelfServeProjectAdd */

@


1.2
log
@No such exception throwable here.
@
text
@@


1.1
log
@Initial commit of self serve project add functionality.
@
text
@a212 2
	} catch (org.tigris.helm.database.TUserManager$NoSuchUserException e) {
	    failTransaction(context, noSuchUserError);
@

