/*
 * $Id: VKCUserGroupManager.java,v 1.3 2004/11/10 15:02:51 koppy Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: VKCUserGroupManager.java,v $
 * Revision 1.3  2004/11/10 15:02:51  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.2  2004/09/20 10:26:14  koppy
 * +javadoc Dokumentation
 *
 * Revision 1.1  2004/08/30 14:49:58  koppy
 * Initiale Version der Workflow-Komponente
 *
 */
package vkc.workflow.shark;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.enhydra.shark.api.RootException;
import org.enhydra.shark.api.UserTransaction;
import org.enhydra.shark.api.internal.usergroup.UserGroupManager;
import org.enhydra.shark.api.internal.working.CallbackUtilities;

import vkc.db.entities.VKCUser;
import vkc.db.serializer.VKCGroupSerializer;
import vkc.db.serializer.VKCUserSerializer;
import vkc.view.bean.GroupInfo;
import vkc.workflow.util.SharkUtils;

/**
 * This class represents a custom user group manager
 * for shark.
 * <p>
 * This way shark can work with the VKC users and groups. 
 * 
 * @author <a href="mailto:andreas.koop@c-lab.de">Andreas Koop</a>
 */
public class VKCUserGroupManager implements UserGroupManager {

    protected CallbackUtilities cus;

	/**
	 * Method configure is called at Shark start up, to configure
	 * implementation of VKC UserGroupManager.
	 *
	 * @param cus an instance of CallbackUtilities used to get
	 *            properties for configuring usergroup manager in Shark.
	 *
	 * @exception RootException thrown if configuring doesn't succeed.
	 */
	public void configure(CallbackUtilities cus) throws RootException {
		this.cus = cus;
		SharkUtils.initVKC(cus);
	}
	
	/**
		* Returns Ids of all user groups.
		*
		* @return   an array of user group Ids.
		*
		* @throws   RootException If something unexpected happens.
		*
		*/
	public List getAllGroupnames(UserTransaction t) throws RootException {
		try {
			// TODO Optimierungspotenzial: Nur die Gruppennamen aus DB holen
			HashMap groups = VKCGroupSerializer.getAllGroups();
			List groupNames = new ArrayList();
			for (Iterator iter = groups.keySet().iterator(); iter.hasNext();) {
				groupNames.add( iter.next() );
			}
			return groupNames;
			
		} catch (Throwable ex) {
			throw new RootException(ex);
		}
	}

	/**
		* Returns Ids of all users.
		*
		* @return   an array of user Ids.
		*
		* @throws   RootException If something unexpected happens.
		*
		*/
	public List getAllUsers(UserTransaction t) throws RootException {
		try {
			// TODO Optimierungspotenzial: Nur die Benutzernamen aus DB holen
			HashMap users = VKCUserSerializer.getAllUsers();
			List userNames = new ArrayList();
			for (Iterator iter = users.keySet().iterator(); iter.hasNext();) {
				userNames.add( iter.next() );
			}
			return userNames;
		} catch (Throwable ex) {
			throw new RootException(ex);
		}
	}

	/**
		* Returns all usernames that belong to given group.
		*
		* @param    t user transaction.
		* @param    groupName group name.
		*
		* @return   a List of all usernames that belong to given group.
		*
		* @throws   RootException If something unexpected happens.
		*
		*/
	public List getAllUsers(UserTransaction t, String groupName)
		throws RootException {
		try {
			// TODO Optimierungspotenzial: Über SQL-Anweisung nur das nötigste holen
			HashMap users = VKCUserSerializer.getAllUsers();
			List userNames = new ArrayList();
			for (Iterator iter = users.values().iterator(); iter.hasNext();) {
				VKCUser user = (VKCUser)iter.next();
				if( user.getGroupsString().indexOf( groupName ) > -1 )
					userNames.add( user.getUser() );
			}
			
			return userNames;
		} catch (Throwable ex) {
			throw new RootException(ex);
		}
	}

	/**
		* Returns all users that belong to given groups.
		*
		* @param    t user transaction.
		* @param    groupNames names of the given groups.
		*
		* @return   a List of all users that belong to given groups.
		*
		* @throws   RootException If something unexpected happens.
		*
		*/
	public List getAllUsers(UserTransaction t, List groupNames)
		throws RootException {
		try {
			Iterator it = groupNames.iterator();
			List ret = new ArrayList();
			while (it.hasNext()) {
				String groupName = (String) it.next();
				ret.addAll( this.getAllUsers( t, groupName ));
			}
			return ret;
		} catch (Throwable ex) {
			throw new RootException(ex);
		}
	}

	/**
		* Returns all groups that belong to given group.
		*
		* @param    t user transaction.
		* @param    groupName group name.
		*
		* @return   a List of all groups that belong to given group.
		*
		* @throws   RootException If something unexpected happens.
		*
		*/
	public List getAllSubgroups(UserTransaction t, String groupName)
		throws RootException {
		throw new RootException("Not implemented");
		/*
		//	   FIX ME: 
		//	  method getAllSubOrganizationalUnitEntries returns and OU groupName, not only its subOrganizationalUnits
			 try {
				 return ldapClient.getAllSubOrganizationalUnitEntries(groupName);
			  } catch (Throwable ex) {
				 throw new RootException(ex);
			  }
		*/
	}

	/**
		* Returns all groups that belong to given groups.
		*
		* @param    t user transaction.
		* @param    groupNames names of the given groups.
		*
		* @return   a List of all groups that belong to given group.
		*
		* @throws   RootException If something unexpected happens.
		*
		*/
	public List getAllSubgroups(UserTransaction t, List groupNames)
		throws RootException {
		throw new RootException("Not implemented");
		/*
		//	   FIX ME: 
		//	  method getAllSubOrganizationalUnitEntries returns and OU groupName, not only its subOrganizationalUnits
			  try{
				 Iterator it=groupNames.iterator();
				 List ret=new ArrayList();
				 while (it.hasNext()){
					String groupName=(String)it.next();
					ret.addAll(ldapClient.getAllSubOrganizationalUnitEntries(groupName));
				 }
				 return ret;
			  } catch (Throwable ex) {
				 throw new RootException(ex);
			  }
		*/
	}

	/**
		* Creates a new user group.
		*
		* @param    t user transaction.
		* @param    groupName group name.
		* @param    description group description.
		*
		* @throws   RootException If something unexpected happens.
		*
		*/
	public void createGroup(
		UserTransaction t,
		String groupName,
		String description)
		throws RootException {
		throw new RootException("Not implemented");
	}

	/**
		* Removes user group.
		*
		* @param    t user transaction.
		* @param    groupName group name.
		*
		* @throws   RootException If something unexpected happens.
		*
		*/
	public void removeGroup(UserTransaction t, String groupName)
		throws RootException {
		throw new RootException("Not implemented");
	}

	/**
		* Returns true if user group with given name exists.
		*
		* @param    t user transaction.
		* @param    groupName group name.
		* @return   true if user group exists, otherwise false.
		*
		* @throws   RootException If something unexpected happens.
		*/
	public boolean doesGroupExist(UserTransaction t, String groupName)
		throws RootException {
		try {
			return VKCGroupSerializer.isGroupExistent( groupName );
		} catch (Throwable ex) {
			throw new RootException(ex);
		}
	}

	/**
		* Returns true if group <i>subgroupName</i> is subgroup of group <i>groupName</i>.
		*
		* @param    t user transaction.
		* @param    groupName group name.
		* @param    subgroupName subgroup name.  
		*
		* @return   true if group <i>subgroupName</i> is subgroup of group <i>groupName</i>,
		* otherwise false.
		* @throws   RootException If something unexpected happens.
		*/
	public boolean doesGroupBelongToGroup(
		UserTransaction t,
		String groupName,
		String subgroupName)
		throws RootException {
		
		return false;
	}

	/**
		* Allows administrator to update data about group.
		*
		* @param    t user transaction.
		* @param    groupName group name.
		* @param    description group description.
		*
		* @throws   RootException If something unexpected happens.
		*
		*/
	public void updateGroup(
		UserTransaction t,
		String groupName,
		String description)
		throws RootException {
		throw new RootException("Not implemented");
	}

	/**
		* Adds an existing group <i>subgroupName</i> to a group <i>groupName</i>.
		*
		* @param    t user transaction.
		* @param    groupName group name.
		* @param    subgroupName subgroup name.
		*
		* @throws   RootException If something unexpected happens.
		*
		*/
	public void addGroupToGroup(
		UserTransaction t,
		String groupName,
		String subgroupName)
		throws RootException {
		throw new RootException("Not implemented");
	}

	/**
		* Removes group <i>subgroupName</i> from the group <i>groupName</i>.
		*
		* @param    t user transaction.
		* @param    groupName group name.
		* @param    subgroupName subgroup name.
		*
		* @throws   RootException If something unexpected happens.
		*
		*/
	public void removeGroupFromGroup(
		UserTransaction t,
		String groupName,
		String subgroupName)
		throws RootException {
		throw new RootException("Not implemented");
	}

	/**
		* Returns a group description.
		*
		* @param    t user transaction.
		* @param    groupName group name.
		* @return   group description.
		*
		* @throws   RootException If something unexpected happens.
		*/
	public String getGroupDescription(UserTransaction t, String groupName)
		throws RootException {
		try {
			GroupInfo group = VKCGroupSerializer.retrieveGroupInfo( groupName );
			return group.getDescription();
		} catch (Throwable ex) {
			throw new RootException(ex);
		}
	}

	/**
		* Adds an existing user with a given username to a given group.
		*
		* @param    t user transaction.
		* @param    groupName group name.
		* @param    username username used to uniquely identify shark user.
		*
		* @throws   RootException If something unexpected happens.
		*
		*/
	public void addUserToGroup(
		UserTransaction t,
		String groupName,
		String username)
		throws RootException {
		throw new RootException("Not implemented");
	}

	/**
		* Removes a user from a group.
		*
		* @param    t user transaction.
		* @param    groupName group name.
		* @param    username username used to uniquely identify shark user.
		*
		* @throws   RootException If something unexpected happens.
		*
		*/
	public void removeUserFromGroup(
		UserTransaction t,
		String groupName,
		String username)
		throws RootException {
		throw new RootException("Not implemented");
	}

	/**
		* Returns true if given user belongs to a given group.
		*
		* @param    t user transaction.
		* @param    groupName group name.
		* @param    username      username used to uniquely identify shark's user.
		* @return   true if given user belongs to a given group, otherwise false.
		*
		* @throws   RootException If something unexpected happens.
		*/
	public boolean doesUserBelongToGroup(
		UserTransaction t,
		String groupName,
		String username)
		throws RootException {
		
			VKCUser user = this.getVKCUser(username);
			return user.getGroupsString().indexOf( groupName ) > -1; 
		
	}

	/**
		* Allows administrator to remove user.
		*
		* @param    t user transaction.
		* @param    username      username used to uniquely identify user.
		*
		* @throws   RootException If something unexpected happens (i.e the user with
		* given username does not exist, or this is a user that can't be removed).
		*
		*/
	public void removeUser(UserTransaction t, String username)
		throws RootException {
		throw new RootException("Not implemented");
	}

	/**
		* Returns true if user with given username exists.
		*
		* @param    t user transaction.
		* @param    username   username of shark user.
		* @return   true if user with given username exists, otherwise false.
		*
		* @throws   RootException If something unexpected happens.
		*/
	public boolean doesUserExist(UserTransaction t, String username)
		throws RootException {
		try {
			return VKCUserSerializer.isUserExistent( username );
		} catch (Throwable ex) {
			throw new RootException(ex);
		}
	}

	/**
		* Sets user password.
		*
		* @param    t user transaction.
		* @param    username   username of shark user.
		* @param    password   new password of shark user.
		*
		* @throws   RootException If something unexpected happens.
		*
		*/
	public void setPassword(
		UserTransaction t,
		String username,
		String password)
		throws RootException {
		throw new RootException("Not implemented");
	}

	/**
		* Returns string representing the real name for the shark user with a given
		* username.
		*
		* @param    t user transaction.
		* @param    username   The username of shark user.
		* @return   real name of shark user.
		*
		* @throws   RootException If something unexpected happens.
		*
		*/
	public String getUserRealName(UserTransaction t, String username)
		throws RootException {
	    
			VKCUser user = this.getVKCUser(username);
			return user.getFirstname() + " " + user.getLastname();
	}

	/**
		* Returns string representing email address for the user with a given
		* username.
		*
		* @param    t user transaction.
		* @param    username   The username of shark user.
		* @return   email of shark user.
		*
		* @throws   RootException If something unexpected happens.
		*
		*/
	public String getUserEMailAddress(UserTransaction t, String username)
		throws RootException {
		return this.getVKCUser(username).getEmail();
	}

    /** 
     * (non-Javadoc)
     * @see org.enhydra.shark.api.internal.usergroup.UserGroupManager#moveGroup(org.enhydra.shark.api.UserTransaction, java.lang.String, java.lang.String, java.lang.String)
     */
    public void moveGroup(UserTransaction arg0, String arg1, String arg2, String arg3) throws RootException {
        throw new RootException( "Not yet implemented!" );
    }

    /** 
     * (non-Javadoc)
     * @see org.enhydra.shark.api.internal.usergroup.UserGroupManager#moveUser(org.enhydra.shark.api.UserTransaction, java.lang.String, java.lang.String, java.lang.String)
     */
    public void moveUser(UserTransaction arg0, String arg1, String arg2, String arg3) throws RootException {
        throw new RootException( "Not yet implemented!" );
    }

    /** 
     * (non-Javadoc)
     * @see org.enhydra.shark.api.internal.usergroup.UserGroupManager#createUser(org.enhydra.shark.api.UserTransaction, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
     */
    public void createUser(UserTransaction arg0, String arg1, String arg2, String arg3, String arg4, String arg5, String arg6) throws RootException {
        throw new RootException( "Not yet implemented!" );
    }

    /** 
     * (non-Javadoc)
     * @see org.enhydra.shark.api.internal.usergroup.UserGroupManager#updateUser(org.enhydra.shark.api.UserTransaction, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
     */
    public void updateUser(UserTransaction arg0, String arg1, String arg2, String arg3, String arg4) throws RootException {
        throw new RootException( "Not yet implemented!" );
    }

    /** 
     * (non-Javadoc)
     * @see org.enhydra.shark.api.internal.usergroup.UserGroupManager#getUserFirstName(org.enhydra.shark.api.UserTransaction, java.lang.String)
     */
    public String getUserFirstName(UserTransaction arg0, String username) throws RootException {
        return this.getVKCUser(username).getFirstname();
    }

    /** 
     * (non-Javadoc)
     * @see org.enhydra.shark.api.internal.usergroup.UserGroupManager#getUserLastName(org.enhydra.shark.api.UserTransaction, java.lang.String)
     */
    public String getUserLastName(UserTransaction arg0, String username) throws RootException {
        return this.getVKCUser(username).getLastname();
    }
    
    /**
     * Returns the VKCUser for the given username
     * @param username
     * @return
     * @throws RootException
     */
    private VKCUser getVKCUser( String username ) throws RootException{
        try {
			return VKCUserSerializer.retrieveUser( username );
			
		} catch (Throwable ex) {
			throw new RootException(ex);
		}
    }

    /** 
     * (non-Javadoc)
     * @see org.enhydra.shark.api.internal.usergroup.UserGroupManager#getAllImmediateSubgroups(org.enhydra.shark.api.UserTransaction, java.lang.String)
     */
    public List getAllImmediateSubgroups(UserTransaction arg0, String arg1) throws RootException {
       
        throw new RootException( "Not yet implemented!" );
    }

    /** 
     * (non-Javadoc)
     * @see org.enhydra.shark.api.internal.usergroup.UserGroupManager#getAllImmediateUsers(org.enhydra.shark.api.UserTransaction, java.lang.String)
     */
    public List getAllImmediateUsers(UserTransaction arg0, String arg1) throws RootException {
        throw new RootException("Not implemented");
    }

    /** 
     * (non-Javadoc)
     * @see org.enhydra.shark.api.internal.usergroup.UserGroupManager#removeGroupTree(org.enhydra.shark.api.UserTransaction, java.lang.String)
     */
    public void removeGroupTree(UserTransaction arg0, String arg1) throws RootException {
        throw new RootException("Not implemented");
    }

    /** 
     * (non-Javadoc)
     * @see org.enhydra.shark.api.internal.usergroup.UserGroupManager#removeUsersFromGroupTree(org.enhydra.shark.api.UserTransaction, java.lang.String)
     */
    public void removeUsersFromGroupTree(UserTransaction arg0, String arg1) throws RootException {
        throw new RootException("Not implemented");
    }
}
