/*
 * Copyright 2010 PC-NG Inc. All rights reserved.
 * PC-NG PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nexiwave.search.client.sample;

import org.apache.axis2.AxisFault;

import com.nexiwave.search.client.GroupAdminServiceStub;
import com.nexiwave.search.client.SearchService2Stub;
import com.nexiwave.search.client.GroupAdminServiceStub.NewUserData;

/**
 * This sample client will demonstrate the groupAdmin service of nexiwave Web Service.
 * 
 * nexiwave GroupAdmin service lets you to add account, delete account and query account information.
 * 
 * To do this, you MUST obtain the group administrator status. You must contact support@nexiwave.com to set up the group administrator status.
 * You will need to supply these information to nexiwave to set this up:
 * 				group administrator email:
 * 				group administrator password
 * 				desired group name
 * 
 * Upon completion, nexiwave will send you these information to you:
 * 				the unique groupId
 * 
 * Note:
 * 1. groupId is mandatory in any GroupAdmin services calls. This is different from the Audio Search sample, where groupId is optional when sending request to nexiwave, 
 * 2. 
 * 
 * 
 * 
 * You may use nexiwave GroupAdmin web service to manipulate nexiwave accounts. 
 * 
 * @author bjiang
 *
 */
public class GroupAdminServiceSampleClient {
	private TestData data;
	private GroupAdminServiceStub stub;
	
	public static void main(String[] args)throws Exception{
		GroupAdminServiceSampleClient client=new GroupAdminServiceSampleClient("http://search.nexiwave.com", 16, "your_userId", "your_pwd");
		client.testUserManagement();
	}
	
	public GroupAdminServiceSampleClient(String server_loc, long groupId, String groupAdmin_UserId, String groupAdmin_Passwd) throws AxisFault{
		this.data=new TestData();
		this.data.setMasterNexiwaveAccountEmail(groupAdmin_UserId);
		this.data.setMasterNexiwavePassword(groupAdmin_Passwd);
		this.data.setChildAccountGroupId(groupId);
		
		this.stub = new GroupAdminServiceStub(server_loc+"/AudioSearch/services/GroupAdminService");
	}

	/**
	 * Test all Web Service calls
	 * @throws Exception
	 */
	public void testUserManagement()throws Exception{
		System.out.println("Testing user management...");
		
		String childAcctEmail="test321@yourdomain.com";
		
		long oldUserId=queryUserByEmail(childAcctEmail);
		if(oldUserId>0){
			deleteUser(oldUserId);
		}
		
		// create a new user with email and password
		long newNexiwaveUserId=testAddUser(childAcctEmail);
		System.out.println("Added user "+newNexiwaveUserId+". Deletng it...");		
		
		// query nexiwave UserId from email 
		long queriedUID=queryUserByEmail(childAcctEmail);
		if(queriedUID!=newNexiwaveUserId){
			throw new RuntimeException("Expected same userId come back. Expect "+newNexiwaveUserId+", received "+queriedUID);
		}
		
		// query user infor from a nexiwave UserId
		queryUserByNexiwaveUserId(newNexiwaveUserId);

		// delete this user from nexiwave.
		deleteUser(newNexiwaveUserId);
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Individual test methods below:
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	/**
	 * Demonstrate how to add a user. 
	 * Note: 
	 * 1. This new user's email MUST be unique in your group.
	 */
	private long testAddUser(String childAcctEmail)throws Exception{		
		GroupAdminServiceStub.AddUser req=new GroupAdminServiceStub.AddUser();
		GroupAdminServiceStub.AuthData authData=new GroupAdminServiceStub.AuthData();
		req.setAuthData(authData);
		authData.setEmail(this.data.getMasterNexiwaveAccountEmail());
		authData.setPasswd(this.data.getMasterNexiwavePassword());
		authData.setGroupId(this.data.getChildAccountGroupId());
		
		GroupAdminServiceStub.NewUserData newUserData=new GroupAdminServiceStub.NewUserData();
		req.setNewUserData(newUserData);
		newUserData.setEmail(childAcctEmail);
		newUserData.setPassword("test1233");
	
		GroupAdminServiceStub.AddUserResponse resp=stub.addUser(req);
		long newNexiwaveUserId=resp.get_return();
		if(newNexiwaveUserId<=0){
			throw new RuntimeException("Expected a positive number for adding new user. Received new user id as "+newNexiwaveUserId+" instead.");
		}
		return newNexiwaveUserId;
	}
	
	/**
	 * Query user info in your group by an email address
	 * 
	 * It returns null if such user does not exist in your group.
	 * 
	 * @param email
	 * @throws Exception
	 */
	private long queryUserByEmail(String email)throws Exception{
		GroupAdminServiceStub.ListUserByEmail req=new GroupAdminServiceStub.ListUserByEmail();
		GroupAdminServiceStub.AuthData authData=new GroupAdminServiceStub.AuthData();
		req.setAuthData(authData);
		authData.setEmail(this.data.getMasterNexiwaveAccountEmail());
		authData.setPasswd(this.data.getMasterNexiwavePassword());
		authData.setGroupId(this.data.getChildAccountGroupId());
		
		req.setChildUserEmail(email);
		GroupAdminServiceStub.ListUserByEmailResponse resp=stub.listUserByEmail(req);
		GroupAdminServiceStub.UserData ud=resp.get_return();
		
		if(ud==null)
			return 0;
		
		System.out.println("Query result for user "+email+": nexiwave userId "+ud.getUserId());
		return ud.getUserId();
	}
	
	/**
	 * Query user info in your group by nexiwave userId.
	 * 
	 * It returns null if such user does not exist in your group.
	 * 
	 * @param userId
	 * @throws Exception
	 */
	private void queryUserByNexiwaveUserId(long userId)throws Exception{
		GroupAdminServiceStub.ListUserByUserId req=new GroupAdminServiceStub.ListUserByUserId();
		GroupAdminServiceStub.AuthData authData=new GroupAdminServiceStub.AuthData();
		req.setAuthData(authData);
		authData.setEmail(this.data.getMasterNexiwaveAccountEmail());
		authData.setPasswd(this.data.getMasterNexiwavePassword());
		authData.setGroupId(this.data.getChildAccountGroupId());
		
		req.setChildUserId(userId);
		GroupAdminServiceStub.ListUserByUserIdResponse resp=stub.listUserByUserId(req);
		GroupAdminServiceStub.UserData ud=resp.get_return();
		
		System.out.println("Query result for user "+userId+": nexiwave userId "+ud.getEmail());
	}
	
	/**
	 * Delete a user from your group.
	 * 
	 * @param nexiwaveUserId
	 * @throws Exception
	 */
	private void deleteUser(long nexiwaveUserId)throws Exception{		
		GroupAdminServiceStub.DeleteUser req=new GroupAdminServiceStub.DeleteUser();
		GroupAdminServiceStub.AuthData authData=new GroupAdminServiceStub.AuthData();
		req.setAuthData(authData);
		authData.setEmail(this.data.getMasterNexiwaveAccountEmail());
		authData.setPasswd(this.data.getMasterNexiwavePassword());
		authData.setGroupId(this.data.getChildAccountGroupId());

		req.setChildUserId(nexiwaveUserId);
		GroupAdminServiceStub.DeleteUserResponse resp=stub.deleteUser(req);
		if(!resp.get_return())
			throw new RuntimeException("failed to delete a user from nexiwave.");
	}

}
