/*
 * $Id: DisplayAclAction.java,v 1.10 2006/10/04 03:14:31 marbobo Exp $
 * 
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * $Log: DisplayAclAction.java,v $
 * Revision 1.10  2006/10/04 03:14:31  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.9  2004/11/30 09:58:52  jonyp
 * added support for ldapGroups
 *
 * Revision 1.8  2004/11/10 15:02:44  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.7  2004/07/20 18:39:00  marbobo
 * - Unnötige Anweisungen entfernt
 *
 * Revision 1.6  2004/07/20 15:16:54  jonyp
 * added some functionality to display dialog in mainframe or popup-window
 *
 * Revision 1.5  2004/06/24 23:35:16  marbobo
 * Quellcode maintenance
 *
 * Revision 1.4  2004/03/13 19:15:12  marbobo
 * Unbenutze lokale Variablen projektweit entsorgt
 *
 * Revision 1.3  2004/02/24 13:32:27  vchris
 * Bugfix
 *
 * Revision 1.2  2004/02/23 16:18:55  vchris
 * DisplayACL Manager fertiggestellt
 *
 * Revision 1.1  2004/02/17 17:06:12  vchris
 * Initiale Version
 *
 * 
 * Created on 16.02.2004
 */
package vkc.view.action;

import java.util.Vector;
import java.util.Iterator;

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.db.entities.VKCAclEntry;
import vkc.db.entities.VKCCategory;
import vkc.misc.VKCLogManager;
import vkc.procoba.project.ProjectViewForm;
import vkc.util.VKCStringUtils;
import vkc.security.AccessorInfo;
import vkc.view.form.DisplayAclForm;
import vkc.view.PageId;
import vkc.view.WebShare;


/**
 * DOCUMENT ME!!!
 * 
 * @author vchris
 */
public class DisplayAclAction extends Action {

    private boolean ldapEnabled = Integer.parseInt( WebShare.getSysParameterValue( "LDAP", "EnableLdapSupport", "0" ) ) != 0 ? true : false;
    private ActionForward defaultInputForward = null;

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

		// defaultInputForward setzen
		defaultInputForward = mapping.getInputForward();
		
		// ACL in die Form laden
		loadAclToForm( form, request ); 

		// Verschiedene Actions durchführen
		if ( form.getCtrlAction() == DisplayAclForm.ACTION_INIT ) {
			return performInit(form);
		} else if ( form.getCtrlAction() == DisplayAclForm.ACTION_CHANGE_SELECT ) {
			return performChangeSelect( form );
		} else if ( form.getCtrlAction() == DisplayAclForm.ACTION_CLOSE ) {
			return performExitAction( form );
		}
		
		return defaultInputForward;
	}

	/**
	 * Lädt eine ACL in die Form
	 * 
	 * @param form
	 * @param request
	 */
	private void loadAclToForm(DisplayAclForm form, HttpServletRequest request) {
		form.initPermissions(request);		
		form.loadAcl(request, form.getCategoryId(), form.isDefaultAcl());
	}

	/**
	 * Init-Action
	 * 
	 * @param form
	 * @return
	 */
	private ActionForward performInit(DisplayAclForm form) {
		form.setAccessorSelect(getFirstAccessorInList(form));
		return performChangeSelect(form);
	}

	/**
	 * Change-Select-Action
	 * 
	 * @param form
	 * @return
	 */
	private ActionForward performChangeSelect(DisplayAclForm form) {
		setCheckboxValues(form);
		return defaultInputForward;
	}

	/** 
	 * Liefert den ersten Accessor als ID-String zurück
	 * 
	 * @param form
	 * @return
	 */
	private String getFirstAccessorInList(DisplayAclForm form) {
		
		String accessorSelect = null;
		AccessorInfo ai = null;
		
		if (form.getAnyothers().size() != 0) {
			// anyother selektieren
			ai = (AccessorInfo)form.getAnyothers().get(0);
		}
		else if (form.getUsers().size() != 0) {
			// ersten user selektieren
			ai = (AccessorInfo)form.getUsers().get(0);
		}
		else if (form.getGroups().size() != 0) {
			// ersten Group-Eintrag selektieren
			ai = (AccessorInfo)form.getGroups().get(0);
		}
		else if (form.getLdapGroups().size() != 0 && ldapEnabled) {
			// ersten ldapGroup-Eintrag selektieren
			ai = (AccessorInfo)form.getLdapGroups().get(0);
		}
		
		if (ai != null) accessorSelect = ai.getSelectIdString();
		
		return accessorSelect;				
	}

	/**
	 * Setzt die Checkbox-Werte anhand des aktuell selektierten Accessors
	 * 
	 * @param form
	 */
	public void setCheckboxValues(DisplayAclForm form) {
		if (!(form.getAccessorSelect() == null || "".equals(form.getAccessorSelect()))) {
			Vector v = VKCStringUtils.stringToVector(form.getAccessorSelect(), "_");
			String type;
			Integer id;
			try {
				type = (String)v.elementAt(0);
				id = new Integer ( Integer.parseInt( (String)v.elementAt(1) ) );
			}
			catch (Exception ex) {
				VKCLogManager.errorLog(this,"","Could not resolve Accessor Type oder ID from ID-String.",ex);
				return;
			}
			// die Checkboxen entsprechend setzen
			form.setAllCheckboxesToValue(false);
			VKCAclEntry c = null;
			boolean gefunden = false;
			Iterator it = form.getAcl().getAclEntries().iterator();
			while (it.hasNext()) {
				c = (VKCAclEntry)it.next();
				if (c.getAccessorId().equals(id) && c.getAclType().getName().equals(type)) {
					gefunden = true;
					form.setCheckboxValue(c);
				}
			}
			if (!gefunden) VKCLogManager.errorLog(this,"","ACL Entry not found.");
		}
		else {
			form.setAllCheckboxesToValue(false);
		}
	}
	
	public ActionForward performExitAction(DisplayAclForm form) {
    	
		String forwardURL = "";

		if ( form.getSourcePageId().equals( PageId.PROJECT_VIEW ) ) {
			forwardURL = "/projectView.do?viewMode=" + ProjectViewForm.VIEW_MODE_FOLDER + "&projectId=" + form.getTargetId() + "&subfolderId=" + form.getSubTargetId();
			
			return new ActionForward( forwardURL, true );
		} else if ( form.getSourcePageId().equals( PageId.FUNCTIONAL_VIEW ) ) {
			forwardURL = "/functionalView.do?functionalViewId=" + form.getTargetId() + "&subfolderId=" + form.getSubTargetId();

			return new ActionForward( forwardURL, true );
		} else if ( form.getSourcePageId().equals( PageId.CATEGORY_VIEW ) ) {
			if ( form.getTargetId() != null ) {
				forwardURL = "/showCategory.do?categoryId=" + form.getTargetId();
			} else {
				forwardURL = "/showCategory.do?categoryId=" + form.getCategoryId();
			}

			return new ActionForward( forwardURL, true );
		} else {
			return new ActionForward( "/showCategory.do?categoryId=" + VKCCategory.ROOT_CATEGORY_ID, true );
		}
	}
}