/*
 * $Id: EditCatAclAction.java,v 1.17 2006/10/04 03:14:33 marbobo Exp $
 * 
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * $Log: EditCatAclAction.java,v $
 * Revision 1.17  2006/10/04 03:14:33  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.16  2004/11/15 12:35:39  vchris
 * + Cleanup-Job für obsolete LDAP-Group Einträge
 *
 * Revision 1.15  2004/11/10 15:02:45  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.14  2004/10/26 13:40:38  vchris
 * *** empty log message ***
 *
 * Revision 1.13  2004/06/24 23:35:16  marbobo
 * Quellcode maintenance
 *
 * Revision 1.12  2004/05/25 21:08:51  marbobo
 * + getTargetId() setTargetId() getSubTargetId() setSubTargetId()
 *
 * Revision 1.11  2004/03/24 13:07:31  bigmac
 * Handling für Rücksprung in Projektsicht eingefügt
 *
 * Revision 1.10  2004/03/07 16:53:20  bigmac
 * Anpassungen an VKCAclSerializer
 *
 * Revision 1.9  2004/03/02 23:05:45  bigmac
 * Anpassungen an VKCDocumentSerializer und VKCCategorySerializer
 *
 * Revision 1.8  2004/02/09 13:24:14  bigmac
 * Anpassungen an /showCategory.do
 *
 * Revision 1.7  2004/01/29 15:34:30  jonyp
 * load category/categoryId from request
 *
 * Revision 1.6  2003/12/24 10:11:52  bigmac
 * Projektweite Import-Optimierung
 *
 * Revision 1.5  2003/11/19 11:34:09  vchris
 * Änderungen für geändertes Verhalten des DefaultACL Editors
 *
 * Revision 1.4  2003/11/12 11:15:14  vchris
 * (Änderugnen für) Default-ACL Editor
 *
 * Revision 1.3  2003/10/28 16:40:07  vchris
 * *** empty log message ***
 *
 * Revision 1.2  2003/10/27 17:00:10  vchris
 * *** empty log message ***
 *
 * 
 * Created on 20.10.2003
 */
package vkc.view.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionForward;

import vkc.db.entities.VKCCategory;
import vkc.db.serializer.VKCAclSerializer;
import vkc.db.serializer.VKCCategorySerializer;
import vkc.exception.DBException;
import vkc.ldap.db.serializer.VKCLdapGroupSerializer;
import vkc.misc.VKCLogManager;
import vkc.procoba.project.ProjectViewForm;
import vkc.security.VKCAcl;
import vkc.view.PageId;
import vkc.view.WebShare;
import vkc.view.form.EditAclForm;

/**
 * DOCUMENT ME!!!
 * 
 * @author vchris
 */
public class EditCatAclAction extends EditAclAction {

	private boolean ldapEnabled = Integer.parseInt( WebShare.getSysParameterValue( "LDAP", "EnableLdapSupport", "0" ) ) != 0 ? true : false;
	
	// Defaultkonstruktor
	public EditCatAclAction() {
		requiredPermission = 'c';
		permissionsContext = "category";
	}
	
	protected VKCCategory retrieveCategory(HttpServletRequest request) throws DBException {
		return VKCCategorySerializer.retrieveCategory(new Integer(request.getParameter("categoryId")));
	}
	
	protected VKCAcl retrieveParentAcl(HttpServletRequest request, VKCCategory category) throws DBException {
		// nicht implementiert
		return null;
	}

	protected VKCAcl retrieveAcl(HttpServletRequest request, VKCCategory category) throws DBException {
		return VKCAclSerializer.getAclByCategoryId(category.getCategoryId());
	}
	
    protected ActionForward exitActionForward(EditAclForm 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 );
		}
    }
    
	/**
	 * Speichert die geänderte ACL in der Datenbank
	 * 
	 * @param request Der aus der Execute Methode übergebene Request
	 * @param aclForm Die ACLForm, auf dessen Eigenschaften zugegriffen wird 
	 * @param categoryAcl Die zu speichernde ACL
	 */
	protected void performSaveAcl(HttpServletRequest request, EditAclForm aclForm, VKCAcl acl) throws DBException {		 

		try {
			aclForm.updateAcl(request, acl);
            VKCAclSerializer.saveAcl(acl);
			// überflüssige LDAP Gruppeneinträge löschen
            if (ldapEnabled) VKCLdapGroupSerializer.deleteObsoleteEntries();
 			request.setAttribute("vkc.acl.succeeded","vkc.acl.saved");
		} catch (Exception ex) {
			VKCLogManager.errorLog(this, "", ex.getMessage(),ex);
			request.setAttribute("vkc.acl.failed","vkc.acl.notSaved");
		}
	}

}
