package com.google.enterprise.connector.dctm.dctmdfcwrap;

import java.util.logging.Logger;

import com.documentum.fc.client.DfAuthenticationException;
import com.documentum.fc.client.DfIdentityException;
import com.documentum.fc.client.DfPrincipalException;
import com.documentum.fc.client.DfServiceException;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.client.IDfSessionManager;
import com.documentum.fc.common.DfException;
import com.documentum.fc.common.IDfLoginInfo;
import com.google.enterprise.connector.dctm.dfcwrap.ILoginInfo;
import com.google.enterprise.connector.dctm.dfcwrap.ISession;
import com.google.enterprise.connector.dctm.dfcwrap.ISessionManager;
import com.google.enterprise.connector.spi.RepositoryLoginException;
import com.google.enterprise.connector.spi.RepositoryException;

public class DmSessionManager implements ISessionManager {

	IDfSessionManager dfSessionManager;
	
	IDfSession DfSessionDel;
	IDfSession DfSessionAdd;
	IDfSession DfSessionAuto;

	private String docbaseName;

	private String serverUrl;

	private static Logger logger = Logger.getLogger(DmSessionManager.class
			.getName());
	
	public DmSessionManager(IDfSessionManager DfSessionManager) {

		this.dfSessionManager = DfSessionManager;
	}
	
	
	public void setSessionDel(ISession sess){
		this.DfSessionDel=((DmSession) sess).getDfSession();
		logger.finest("setSessionDel");
		
	}
	
	public void setSessionAdd(ISession sess){
		this.DfSessionAdd=((DmSession) sess).getDfSession();
		logger.finest("setSessionAdd");
	}
	
	public void setSessionAuto(ISession sess){
		this.DfSessionAuto=((DmSession) sess).getDfSession();
		logger.finest("setSessionAuto");
		
	}
	
	public void releaseSessionAdd() {
		logger.finest("before session released");
		this.dfSessionManager.release(this.DfSessionAdd);
		logger.finest("after session released");
	}

	public void releaseSessionDel() {
		logger.finest("before session released");
		this.dfSessionManager.release(this.DfSessionDel);
		logger.finest("after session released");
	}
	
	public void releaseSessionAuto() {
		logger.finest("before session released");
		this.dfSessionManager.release(this.DfSessionAuto);
		logger.finest("after session released");
	}

	public ISession getSession(String docbase) throws RepositoryLoginException,
			RepositoryException {
		IDfSession DfSession = null;
		try {
			DfSession = dfSessionManager.getSession(docbase);
		} catch (DfIdentityException iE) {
			RepositoryLoginException le = new RepositoryLoginException(iE);
			throw le;
		} catch (DfAuthenticationException iE) {
			RepositoryLoginException le = new RepositoryLoginException(iE);
			throw le;
		} catch (DfPrincipalException iE) {
			RepositoryLoginException le = new RepositoryLoginException(iE);
			throw le;
		} catch (DfServiceException iE) {
			RepositoryException re = new RepositoryException(iE);
			throw re;
		}
		return new DmSession(DfSession);
	}

	public void setIdentity(String docbase, ILoginInfo identity)
			throws RepositoryLoginException {
		if (!(identity instanceof DmLoginInfo)) {
			throw new IllegalArgumentException();
		}
		DmLoginInfo dctmLoginInfo = (DmLoginInfo) identity;
		IDfLoginInfo idfLoginInfo = dctmLoginInfo.getIdfLoginInfo();
		try {
			dfSessionManager.setIdentity(docbase, idfLoginInfo);
		} catch (DfServiceException iE) {
			RepositoryLoginException le = new RepositoryLoginException(iE);
			throw le;
		}
	}

	public ILoginInfo getIdentity(String docbase) {
		IDfLoginInfo idfLoginInfo = dfSessionManager.getIdentity(docbase);
		return new DmLoginInfo(idfLoginInfo);
	}

	public ISession newSession(String docbase) throws RepositoryLoginException,
			RepositoryException {
		IDfSession idfSession = null;

		try {
			idfSession = dfSessionManager.newSession(docbase);
		} catch (DfIdentityException iE) {

			throw new RepositoryLoginException(iE);
		} catch (DfAuthenticationException iE) {
			throw new RepositoryLoginException(iE);
		} catch (DfPrincipalException iE) {
			throw new RepositoryLoginException(iE);
		} catch (DfServiceException iE) {
			throw new RepositoryException(iE);
		} catch (NoClassDefFoundError iE) {
			throw new RepositoryException(iE);
		}
		return new DmSession(idfSession);
	}

	public void release(ISession session) {
		logger.finest("session released");
		this.dfSessionManager.release(((DmSession) session).getDfSession());

	}

	public IDfSessionManager getDfSessionManager() {
		return dfSessionManager;
	}

	public void setServerUrl(String serverUrl) {
		this.serverUrl = serverUrl;

	}

	public String getDocbaseName() {

		return docbaseName;
	}

	public void setDocbaseName(String docbaseName) {
		this.docbaseName = docbaseName;
	}

	public String getServerUrl() {
		return serverUrl;
	}

	public boolean authenticate(String docbaseName) {
		boolean authent = false;
		try {
			this.dfSessionManager.authenticate(docbaseName);
			authent = true;
		} catch (DfException e) {
			authent = false;
		}
		return authent;
	}

	public void clearIdentity(String docbase) {
		this.dfSessionManager.clearIdentity(docbase);
	}

	public IDfSessionManager getSessionManager() {
		return this.dfSessionManager;
	}

}

@@ -996,16 +996,45 @@
 ionAuto;
+%0A%09IDfSession DfSessionConfig;
 %0A%0A%09priva
@@ -1702,32 +1702,188 @@
 Auto%22);%0A%09%09%0A%09%7D%0A%09%0A
+%09public void setSessionConfig(ISession sess)%7B%0A%09%09this.DfSessionConfig=((DmSession) sess).getDfSession();%0A%09%09logger.finest(%22setSessionSessionConfig%22);%0A%09%09%0A%09%7D%0A%09%0A
 %09public void rel
@@ -2395,32 +2395,217 @@
 released%22);%0A%09%7D%0A%0A
+%09public void releaseSessionConfig() %7B%0A%09%09logger.finest(%22before session released%22);%0A%09%09this.dfSessionManager.release(this.DfSessionConfig);%0A%09%09logger.finest(%22after session released%22);%0A%09%7D%0A%09%0A
 %09public ISession

Update of the session management for the new DctmConnectorType