/*
 * $Id: NewGroupAction.java,v 1.17 2006/10/04 03:15:01 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: NewGroupAction.java,v $
 * Revision 1.17  2006/10/04 03:15:01  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.16  2005/02/20 16:56:23  marbobo
 * + Beim Speichern der Entities in die Datenbank wird sichergestellt
 *    dass Unicode-Zeichen in Kodierterform (als &#xxxxx;) gespeichert werden
 *
 * Revision 1.15  2005/02/14 14:01:46  bigmac
 * Applikationsweites CSS konfigurierbar gemacht + Refactorings
 *
 * Revision 1.14  2004/09/16 13:10:55  marbobo
 * + Auf VKCRequestProcessor umgestellt
 *
 * Revision 1.13  2004/09/01 13:53:14  marbobo
 * + Redirectadresse angepasst
 *
 * Revision 1.12  2004/08/07 00:34:43  marbobo
 * + Bugfix für Zeitangaben und Refactorings
 *
 * Revision 1.11  2004/06/27 17:01:06  marbobo
 * + Quellcode maintenance
 *
 * Revision 1.10  2004/03/12 18:03:00  marbobo
 * sinnloser eintrag entfernt
 *
 * Revision 1.9  2004/03/02 17:46:55  bigmac
 * Anpassungen an VKCGroupSerializer
 *
 * Revision 1.8  2004/01/26 13:55:25  marbobo
 * +Benutzer- und Gruppenverwaltung für dedizierte Administratoren
 *
 * Revision 1.7  2003/12/24 10:12:09  bigmac
 * Projektweite Import-Optimierung
 *
 * Revision 1.6  2003/12/16 11:27:19  bigmac
 * Formular auf request-scope umgebaut
 *
 * Revision 1.5  2003/11/03 11:41:49  marbobo
 * - CreateGroups entfernt
 *
 * Revision 1.4  2003/10/14 21:33:51  marbobo
 * Bugfix
 *
 * Revision 1.3  2003/10/05 01:40:57  marbobo
 * +accountValid bei Gruppen
 * +Sperren und Aktivieren von Gruppen
 * +Verbesserungen
 *
 * Revision 1.2  2003/09/08 16:59:02  marbobo
 * *** empty log message ***
 *
 * Revision 1.1  2003/08/18 21:11:46  marbobo
 * Package verschoben
 *
 * Revision 1.1  2003/08/13 12:51:06  marbobo
 * Initial Version
 *
 */
package vkc.administration.action;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import vkc.administration.form.NewGroupForm;
import vkc.db.entities.VKCGroup;
import vkc.db.entities.VKCUser;
import vkc.db.serializer.VKCGroupSerializer;
import vkc.util.DateUtils;
import vkc.util.StringUtils;
import vkc.util.VKCStringUtils;
import vkc.view.WebShare;


/**
 * Action-Klasse zum Anlegen einer neuen Gruppe
 *
 * @version $Revision: 1.17 $
 * @author marbobo
 */
public final class NewGroupAction extends Action {
	private ActionForward DEFAULT_FORWARD = new ActionForward( "/templates/${template.name}/administration/newGroup.jsp" );

	private ActionForward INFO_FORWARD = 
		new ActionForward( "/templates/${template.name}/common/InfoPage.jsp" );

	public ActionForward execute( ActionMapping mapping, 
								  ActionForm f,
								  HttpServletRequest request, 
								  HttpServletResponse response )
	throws ServletException {
		NewGroupForm form = (NewGroupForm)f;

		VKCUser user = form.getUser();

		if ( !user.hasAdminPrivileges() && !user.getManageOwnGroups() ) {
			request.setAttribute( WebShare.INFO_KEY, "vkc.info.access_denied" );
			
			return INFO_FORWARD;
		}

		if ( request.getParameter( "action.create.group" ) != null ) {
			return handleCreateAction( form, request );
		} 

		return DEFAULT_FORWARD;
	}
	
	private ActionForward handleCreateAction( NewGroupForm form, HttpServletRequest request ) throws ServletException {
		try{
			VKCUser user = form.getUser();

			// Hole neue Gruppendaten aus der Form
			String  groupname       = VKCStringUtils.filterUnicodeChars( form.getName() );
			String  description     = VKCStringUtils.filterUnicodeChars( form.getDescription() );
			Boolean adminprivileges = new Boolean( form.isAdminprivileges() );
			//Boolean creategroups  = new Boolean( form.isCreategroups() );
			Boolean accountValid    = new Boolean( form.isGroupActive() );

			//neue Gruppe anlegen und Gruppendaten setzen
			VKCGroup newGroup = new VKCGroup();
			newGroup.setName( groupname );
			newGroup.setDescription( description );
			newGroup.setAdminPrivileges( adminprivileges );
			//newGroup.setCreateGroups( creategroups );
			newGroup.setAccountValid( accountValid );

			if ( newGroup.getAccountValid().booleanValue() ) {
				String from = form.getValidFrom();
				String until = form.getValidUntil();
				
				if ( !StringUtils.isEmpty( from ) ) {
					Date d = DateUtils.parseDate( from );
					newGroup.setValidFrom( new Timestamp( d.getTime() ) );
				} else {
				    newGroup.setValidFrom( null );
				}
				
				if ( !StringUtils.isEmpty( until ) ) {
					Date d = DateUtils.parseDate( until );
					Calendar c = Calendar.getInstance();
					c.setTime( d );
					c.set( Calendar.HOUR_OF_DAY, 23 );
					c.set( Calendar.MINUTE, 59 );
					c.set( Calendar.SECOND, 59 );
					newGroup.setValidUntil( new Timestamp( c.getTimeInMillis() ) );
				} else {
				    newGroup.setValidUntil( null );
				}
			} else {
				newGroup.setValidFrom( null );
				newGroup.setValidUntil( null );
			}

			newGroup.setCreated( new Timestamp( System.currentTimeMillis() ) );
			newGroup.setLastChanged( new Timestamp( System.currentTimeMillis() ) );
			newGroup.setCreatorId( user.getUserId() );
			newGroup.setLastChangerId( user.getUserId() );

			// Neue Gruppe in die Datenbank speichern
			VKCGroupSerializer.saveGroup( newGroup );

			request.setAttribute( WebShare.INFO_KEY, "vkc.administration.newgroup.created" );

			return new ActionForward( "/editGroup.do?initialize=true&groupId=" + newGroup.getGroupId(), false );
		} catch( Exception ex ) {
			throw new ServletException( ex );
		}
	}
}