/*
 * $Id: EditCatAclForm.java,v 1.12 2005/01/19 16:26:51 jonyp Exp $
 * 
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * $Log: EditCatAclForm.java,v $
 * Revision 1.12  2005/01/19 16:26:51  jonyp
 * enabled display of created/creatorID
 *
 * Revision 1.11  2005/01/18 16:17:31  jonyp
 * added methods to get infos of creator/created of each individual acl enty
 *
 * Revision 1.10  2004/07/08 15:13:57  marbobo
 * + Mergen von Acls
 *
 * Revision 1.9  2004/05/25 21:08:52  marbobo
 * + getTargetId() setTargetId() getSubTargetId() setSubTargetId()
 *
 * Revision 1.8  2004/04/27 15:10:36  jonyp
 * added few lines of code to add new permissions (a/q)
 *
 * Revision 1.7  2004/03/24 13:10:37  bigmac
 * Handling für Rücksprung in Projektsicht eingefügt
 *
 * Revision 1.6  2003/12/24 10:11:54  bigmac
 * Projektweite Import-Optimierung
 *
 * Revision 1.5  2003/11/04 16:52:01  vchris
 * *** empty log message ***
 *
 * Revision 1.4  2003/11/04 16:19:44  vchris
 * *** empty log message ***
 *
 * Revision 1.3  2003/10/27 17:00:09  vchris
 * *** empty log message ***
 *
 * 
 * Created on 15.10.2003
 */
package vkc.view.form;

import java.util.Map;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionMapping;

import vkc.db.entities.VKCAclEntry;
import vkc.db.entities.VKCPermission;
import vkc.db.entities.VKCUser;

/**
 * DOCUMENT ME!!!
 * 
 * @author vchris
 */
public class EditCatAclForm extends EditAclForm {

	protected boolean changeAcl;				// c
	protected boolean readCategory;				// r
	protected boolean writeCategory;			// w
	protected boolean deleteDocuments;			// d
	protected boolean deleteCategory;			// D
	protected boolean createSubcategory;		// C
	protected boolean createLinkToCategory; 	// L
	protected boolean followLinkToCategory;		// F
	protected boolean followLinkToDocument;		// f
	protected boolean createLinkToDocument;		// l
	protected boolean archiveCategory;		    // a
	protected boolean editQueryVirtualDocs;	    // q
    
    protected boolean changeAclEnabled;    
    protected boolean readCategoryEnabled;
	protected boolean writeCategoryEnabled;
	protected boolean deleteDocumentsEnabled;
	protected boolean deleteCategoryEnabled;
	protected boolean createSubcategoryEnabled;
	protected boolean createLinkToCategoryEnabled;
	protected boolean followLinkToCategoryEnabled;
	protected boolean followLinkToDocumentEnabled;
	protected boolean createLinkToDocumentEnabled;
	protected boolean archiveCategoryEnabled;	
	protected boolean editQueryVirtualDocsEnabled;	    

	protected String changeAclCreator;
	protected Timestamp changeAclCreated;
	protected String readCategoryCreator;
	protected Timestamp readCategoryCreated;
	protected String writeCategoryCreator;
	protected Timestamp writeCategoryCreated;
	protected String deleteDocumentsCreator;
	protected Timestamp deleteDocumentsCreated;
	protected String createSubcategoriesCreator;
	protected Timestamp createSubcategoriesCreated;
	protected String deleteCategoryCreator;
	protected Timestamp deleteCategoryCreated;
	protected String createSubcategoryCreator;
	protected Timestamp createSubcategoryCreated;
	
	/**
	 * Hier im Default-Konstruktor müssen einige wichtige Variablen aus der 
	 * abstrakten Klasse EditAclForm überschrieben werden.
	 *
	 */
	public EditCatAclForm() {
		permissionsContext = "category";
		permTokens = new char[] {'c','r','w','d','C','D','L','F','l','f', 'a', 'q'};
	}
	
	protected void initEnabledPermissions(Map permissions) {
		
		if (permissions.get("c") == null) {
			changeAclEnabled = false;
		} else {
			changeAclEnabled = ((VKCPermission)(permissions.get("c"))).getEnabled().intValue() != 0;
		}

		if (permissions.get("r") == null) {
			readCategoryEnabled = false;
		} else {
			readCategoryEnabled = ((VKCPermission)(permissions.get("r"))).getEnabled().intValue() != 0;
		}

		if (permissions.get("w") == null) {
			writeCategoryEnabled = false;
		} else {
			writeCategoryEnabled = ((VKCPermission)(permissions.get("w"))).getEnabled().intValue() != 0;
		}

		if (permissions.get("d") == null) {
			deleteDocumentsEnabled = false;
		} else {
			deleteDocumentsEnabled = ((VKCPermission)(permissions.get("d"))).getEnabled().intValue() != 0;
		}
		
		if (permissions.get("a") == null) {
			archiveCategoryEnabled = false;
		} else {
			archiveCategoryEnabled = ((VKCPermission)(permissions.get("a"))).getEnabled().intValue() != 0;
		}

		if (permissions.get("q") == null) {
			editQueryVirtualDocsEnabled = false;
		} else {
			editQueryVirtualDocsEnabled = ((VKCPermission)(permissions.get("q"))).getEnabled().intValue() != 0;
		}

		if (permissions.get("D") == null) {
			deleteCategoryEnabled = false;
		} else {
			deleteCategoryEnabled = ((VKCPermission)(permissions.get("D"))).getEnabled().intValue() != 0;
		}

		if (permissions.get("C") == null) {
			createSubcategoryEnabled = false;
		} else {
			createSubcategoryEnabled = ((VKCPermission)(permissions.get("C"))).getEnabled().intValue() != 0;
		}

		if (permissions.get("L") == null) {
			createLinkToCategoryEnabled = false;
		} else {
			createLinkToCategoryEnabled = ((VKCPermission)(permissions.get("L"))).getEnabled().intValue() != 0;
		}

		if (permissions.get("F") == null) {
			followLinkToCategoryEnabled = false;
		} else {
			followLinkToCategoryEnabled = ((VKCPermission)(permissions.get("F"))).getEnabled().intValue() != 0;
		}

		if (permissions.get("l") == null) {
			createLinkToDocumentEnabled = false;
		} else {
			createLinkToDocumentEnabled = ((VKCPermission)(permissions.get("l"))).getEnabled().intValue() != 0;
		}

		if (permissions.get("f") == null) {
			followLinkToDocumentEnabled = false;
		} else {
			followLinkToDocumentEnabled = ((VKCPermission)(permissions.get("f"))).getEnabled().intValue() != 0;	
		}
	}

	public void setAllCheckboxesToValue(boolean value) {
		setChangeAcl(value);
		setReadCategory(value);
		setWriteCategory(value);
		setDeleteDocuments(value);
		setDeleteCategory(value);
		setCreateSubcategory(value);
		setCreateLinkToCategory(value);
		setFollowLinkToCategory(value);
		setCreateLinkToDocument(value);
		setFollowLinkToDocument(value);		
		setArchiveCategory(value);
		setEditQueryVirtualDocs(value);
	}
	
	protected boolean[] getAllCheckboxValues() {
		boolean[] values = {changeAcl,
							readCategory,
							writeCategory,
							deleteDocuments,
							createSubcategory,
							deleteCategory,
							createLinkToCategory,
							followLinkToCategory,
							createLinkToDocument,
							followLinkToDocument,
			                archiveCategory,
			                editQueryVirtualDocs};
		return values;
	}
	
	protected void setCheckboxValue(VKCAclEntry c) {
		// neue Berechtigung setzen
	    VKCUser user = c.getCreator();
	    
	    char token = c.getPermission().getToken();
		if (token == 'c') {

		    setChangeAcl(true);
		    setChangeAclCreated(c.getCreated());
		    setChangeAclCreator(user.getUser() + " (" + user.getUserId() +")");
		}

		if (token == 'r') {
		    setReadCategory(true);
		    setReadCategoryCreated(c.getCreated());
		    setReadCategoryCreator(user.getUser() + " (" + user.getUserId() +")");
		}

		if (token == 'w') {
		    setWriteCategory(true);
		    setWriteCategoryCreated(c.getCreated());
		    setWriteCategoryCreator(user.getUser() + " (" + user.getUserId() +")");
		}

		if (token == 'd') {
		    setDeleteDocuments(true);
		    setDeleteDocumentsCreated(c.getCreated());
		    setDeleteDocumentsCreator(user.getUser() + " (" + user.getUserId() +")");
		}
		
		if (token == 'D') {
		    setDeleteCategory(true);
		    setDeleteCategoryCreated(c.getCreated());
		    setDeleteCategoryCreator(user.getUser() + " (" + user.getUserId() +")");
		}
		
		if (token == 'C') {
		    setCreateSubcategory(true);
		    setCreateSubcategoryCreated(c.getCreated());
		    setCreateSubcategoryCreator(user.getUser() + " (" + user.getUserId() +")");
		}
		
		if (token == 'L') setCreateLinkToCategory(true);
		if (token == 'F') setFollowLinkToCategory(true);
		if (token == 'l') setCreateLinkToDocument(true);
		if (token == 'f') setFollowLinkToDocument(true);
		if (token == 'a') setArchiveCategory(true);
		if (token == 'q') setEditQueryVirtualDocs(true);
	}

	/**
	 * @param mapping
	 * @param request
	 */
	public void reset(ActionMapping mapping, HttpServletRequest request)
	{
		super.reset( mapping, request );
		
		// Checkboxen resetten
		if ((request.getParameter("changeAcl") == null) && isChangeAcl()) setChangeAcl(false) ;
		if ((request.getParameter("readCategory") == null) && isReadCategory()) setReadCategory(false) ;
		if ((request.getParameter("writeCategory") == null) && isWriteCategory()) setWriteCategory(false) ;
		if ((request.getParameter("deleteDocuments") == null) && isDeleteDocuments()) setDeleteDocuments(false) ;
		if ((request.getParameter("deleteCategory") == null) && isDeleteCategory()) setDeleteCategory(false) ;
		if ((request.getParameter("createSubategory") == null) && isCreateSubcategory()) setCreateSubcategory(false) ;
		if ((request.getParameter("followLinkToDocument") == null) && isFollowLinkToDocument()) setFollowLinkToDocument(false) ;
		if ((request.getParameter("createLinkToDocument") == null) && isCreateLinkToDocument()) setCreateLinkToDocument(false) ;
		if ((request.getParameter("followLinkToCategory") == null) && isFollowLinkToCategory()) setFollowLinkToCategory(false) ;
		if ((request.getParameter("createLinkToCategory") == null) && isCreateLinkToCategory()) setCreateLinkToCategory(false) ;
		if ((request.getParameter("archiveCategory") == null) && isArchiveCategory()) setArchiveCategory(false) ;
		if ((request.getParameter("editQueryVirtualDocs") == null) && isEditQueryVirtualDocs()) setEditQueryVirtualDocs(false) ;
		request.getSession().setAttribute("EditCatAclForm",this);
	}

	/**
	 * @return
	 */
	public boolean isChangeAcl() {
		return changeAcl;
	}

	/**
	 * @return
	 */
	public boolean isChangeAclEnabled() {
		return changeAclEnabled;
	}

	/**
	 * @return
	 */
	public boolean isCreateLinkToCategory() {
		return createLinkToCategory;
	}

	/**
	 * @return
	 */
	public boolean isCreateLinkToCategoryEnabled() {
		return createLinkToCategoryEnabled;
	}

	/**
	 * @return
	 */
	public boolean isCreateLinkToDocument() {
		return createLinkToDocument;
	}

	/**
	 * @return
	 */
	public boolean isCreateLinkToDocumentEnabled() {
		return createLinkToDocumentEnabled;
	}

	/**
	 * @return
	 */
	public boolean isCreateSubcategory() {
		return createSubcategory;
	}

	/**
	 * @return
	 */
	public boolean isCreateSubcategoryEnabled() {
		return createSubcategoryEnabled;
	}

	/**
	 * @return
	 */
	public boolean isDeleteCategory() {
		return deleteCategory;
	}

	/**
	 * @return
	 */
	public boolean isDeleteCategoryEnabled() {
		return deleteCategoryEnabled;
	}

	/**
	 * @return
	 */
	public boolean isDeleteDocuments() {
		return deleteDocuments;
	}

	/**
	 * @return
	 */
	public boolean isDeleteDocumentsEnabled() {
		return deleteDocumentsEnabled;
	}

	/**
	 * @return
	 */
	public boolean isFollowLinkToCategory() {
		return followLinkToCategory;
	}

	/**
	 * @return
	 */
	public boolean isFollowLinkToCategoryEnabled() {
		return followLinkToCategoryEnabled;
	}

	/**
	 * @return
	 */
	public boolean isFollowLinkToDocument() {
		return followLinkToDocument;
	}

	/**
	 * @return
	 */
	public boolean isFollowLinkToDocumentEnabled() {
		return followLinkToDocumentEnabled;
	}

	/**
	 * @return
	 */
	public boolean isReadCategory() {
		return readCategory;
	}

	/**
	 * @return
	 */
	public boolean isReadCategoryEnabled() {
		return readCategoryEnabled;
	}

	/**
	 * @return
	 */
	public boolean isWriteCategory() {
		return writeCategory;
	}

	/**
	 * @return
	 */
	public boolean isWriteCategoryEnabled() {
		return writeCategoryEnabled;
	}

	/**
	 * @param b
	 */
	public void setChangeAcl(boolean b) {
		changeAcl = b;
	}

	/**
	 * @param b
	 */
	public void setCreateLinkToCategory(boolean b) {
		createLinkToCategory = b;
	}

	/**
	 * @param b
	 */
	public void setCreateLinkToDocument(boolean b) {
		createLinkToDocument = b;
	}

	/**
	 * @param b
	 */
	public void setCreateSubcategory(boolean b) {
		createSubcategory = b;
	}

	/**
	 * @param b
	 */
	public void setDeleteCategory(boolean b) {
		deleteCategory = b;
	}

	/**
	 * @param b
	 */
	public void setDeleteDocuments(boolean b) {
		deleteDocuments = b;
	}

	/**
	 * @param b
	 */
	public void setFollowLinkToCategory(boolean b) {
		followLinkToCategory = b;
	}

	/**
	 * @param b
	 */
	public void setFollowLinkToDocument(boolean b) {
		followLinkToDocument = b;
	}

	/**
	 * @param b
	 */
	public void setReadCategory(boolean b) {
		readCategory = b;
	}

	/**
	 * @param b
	 */
	public void setWriteCategory(boolean b) {
		writeCategory = b;
	}
	
	public String getEditDefaultAclLink() {
		Integer targetId = getTargetId();
		Integer subTargetId = getSubTargetId();
		
		if ( targetId == null && subTargetId == null ) {
			return "editDefaultCatAcl.do" + 
				"?categoryId=" + getCategoryId() + 
				"&init=true" + 
				"&sourcePageId=" + getSourcePageId();
		}
		
		if ( targetId != null && subTargetId != null ) {
			return "editDefaultCatAcl.do" + 
				"?categoryId=" + getCategoryId() + 
				"&init=true" + 
				"&targetId=" + targetId + 
				"&subTargetId=" + subTargetId + 
				"&sourcePageId=" + getSourcePageId();
		}
		
		if ( targetId != null && subTargetId == null ) {
			return "editDefaultCatAcl.do" + 
				"?categoryId=" + getCategoryId() + 
				"&init=true" + 
				"&targetId=" + targetId + 
				"&sourcePageId=" + getSourcePageId();
		}
		
		if ( targetId == null && subTargetId != null ) {
			return "editDefaultCatAcl.do" + 
				"?categoryId=" + getCategoryId() + 
				"&init=true" + 
				"&subTargetId=" + subTargetId + 
				"&sourcePageId=" + getSourcePageId();
		}

		return "editDefaultCatAcl.do" + 
			"?categoryId=" + getCategoryId() + 
			"&init=true" + 
			"&sourcePageId=" + getSourcePageId();
	}
	
	public boolean isArchiveCategory() {
		return archiveCategory;
	}

	public boolean isArchiveCategoryEnabled() {
		return archiveCategoryEnabled;
	}

	public boolean isEditQueryVirtualDocs() {
		return editQueryVirtualDocs;
	}

	public boolean isEditQueryVirtualDocsEnabled() {
		return editQueryVirtualDocsEnabled;
	}

	public void setArchiveCategory(boolean b) {
		archiveCategory = b;
	}

	public void setArchiveCategoryEnabled(boolean b) {
		archiveCategoryEnabled = b;
	}

	public void setEditQueryVirtualDocs(boolean b) {
		editQueryVirtualDocs = b;
	}

	public void setEditQueryVirtualDocsEnabled(boolean b) {
		editQueryVirtualDocsEnabled = b;
	}
	
    public Timestamp getChangeAclCreated() {
        return changeAclCreated;
    }
    public void setChangeAclCreated(Timestamp changeAclCreated) {
        this.changeAclCreated = changeAclCreated;
    }
    public String getChangeAclCreator() {
        return changeAclCreator;
    }
    public void setChangeAclCreator(String changeAclCreator) {
        this.changeAclCreator = changeAclCreator;
    }
	
    public Timestamp getCreateSubcategoriesCreated() {
        return createSubcategoriesCreated;
    }
    public void setCreateSubcategoriesCreated(
            Timestamp createSubcategoriesCreated) {
        this.createSubcategoriesCreated = createSubcategoriesCreated;
    }
    public String getCreateSubcategoriesCreator() {
        return createSubcategoriesCreator;
    }
    public void setCreateSubcategoriesCreator(String createSubcategoriesCreator) {
        this.createSubcategoriesCreator = createSubcategoriesCreator;
    }
    public Timestamp getDeleteDocumentsCreated() {
        return deleteDocumentsCreated;
    }
    public void setDeleteDocumentsCreated(Timestamp deleteDocumentsCreated) {
        this.deleteDocumentsCreated = deleteDocumentsCreated;
    }
    public String getDeleteDocumentsCreator() {
        return deleteDocumentsCreator;
    }
    public void setDeleteDocumentsCreator(String deleteDocumentsCreator) {
        this.deleteDocumentsCreator = deleteDocumentsCreator;
    }
    public Timestamp getReadCategoryCreated() {
        return readCategoryCreated;
    }
    public void setReadCategoryCreated(Timestamp readCategoryCreated) {
        this.readCategoryCreated = readCategoryCreated;
    }
    public String getReadCategoryCreator() {
        return readCategoryCreator;
    }
    public void setReadCategoryCreator(String readCategoryCreator) {
        this.readCategoryCreator = readCategoryCreator;
    }
    public Timestamp getWriteCategoryCreated() {
        return writeCategoryCreated;
    }
    public void setWriteCategoryCreated(Timestamp writeCategoryCreated) {
        this.writeCategoryCreated = writeCategoryCreated;
    }
    public String getWriteCategoryCreator() {
        return writeCategoryCreator;
    }
    public void setWriteCategoryCreator(String writeCategoryCreator) {
        this.writeCategoryCreator = writeCategoryCreator;
    }
    public String getDeleteCategoryCreator() {
        return deleteCategoryCreator;
    }
    public void setDeleteCategoryCreator(String deleteCategoryCreator) {
        this.deleteCategoryCreator = deleteCategoryCreator;
    }
    public Timestamp getDeleteCategoryCreated() {
        return deleteCategoryCreated;
    }
    public void setDeleteCategoryCreated(Timestamp deleteCategoryCreated) {
        this.deleteCategoryCreated = deleteCategoryCreated;
    }
    public String getCreateSubcategoryCreator() {
        return createSubcategoryCreator;
    }
    public void setCreateSubcategoryCreator(String createSubcategoryCreator) {
        this.createSubcategoryCreator = createSubcategoryCreator;
    }
    public Timestamp getCreateSubcategoryCreated() {
        return createSubcategoryCreated;
    }
    public void setCreateSubcategoryCreated(Timestamp createSubcategoryCreated) {
        this.createSubcategoryCreated = createSubcategoryCreated;
    }
}
