package com.capgemini.framework.teamcenter.authent;

import com.teamcenter.schemas.soa._2006_03.exceptions.InvalidCredentialsException;
import com.teamcenter.schemas.soa._2006_03.exceptions.InvalidUserException;
import com.teamcenter.services.strong.core.SessionService;
import com.teamcenter.soa.client.CredentialManager;
import com.teamcenter.soa.client.model.strong.GroupMember;

/**
 * The CredentialManager is used by the Teamcenter Services framework to findAll the user's credentials when challenged by the server. This can occur after a period of inactivity and the server has timed-out the user's session, at which time the client application will need to re-authenticate. The
 * framework will call one of the getCredentials methods (depending on circumstances) and will send the SessionService.login service request. Upon successful completion of the login service request. The last service request (one that caused the challenge) will be resent.
 * <p>
 * The framework will also call the setUserPassword setGroupRole methods when ever these credentials change, thus allowing this implementation of the CredentialManager to cache these values so prompting of the user is not required for re-authentication.
 */
public class TcCredentialManager implements CredentialManager {

    private String name;
    private String password;
    private String discriminator;
    private String group = "";
    private String role = "";

    // always connect same user to same instance of server

    /**
     * Return the type of credentials this implementation provides, standard (user/password) or Single-Sign-On. In this case Standard credentials are returned.
     *
     * @see com.teamcenter.soa.client.CredentialManager#getCredentialType()
     */
    @Override
    public int getCredentialType() {
        return CredentialManager.CLIENT_CREDENTIAL_TYPE_STD;
    }

    /**
     * Prompts the user for credentials. This method will only be called by the framework when a login attempt has failed.
     *
     * @see com.teamcenter.soa.client.CredentialManager#getCredentials(com.teamcenter.schemas.soa._2006_03.exceptions.InvalidCredentialsException)
     */
    @Override
    public String[] getCredentials(final InvalidCredentialsException anException) {
        return new String[]{name, password, group, role, discriminator};
    }

    /**
     * Return the cached credentials. This method will be called when a service request is sent without a valid session ( session has expired on the server).
     *
     * @see com.teamcenter.soa.client.CredentialManager#getCredentials(com.teamcenter.schemas.soa._2006_03.exceptions.InvalidUserException)
     */
    @Override
    public String[] getCredentials(final InvalidUserException anException) {
        return new String[]{name, password, group, role, discriminator};
    }

    /**
     * Cache the group and role. This is called after the {@link SessionService#setSessionGroupMember(GroupMember)} service operation is called.
     *
     * @see com.teamcenter.soa.client.CredentialManager#setGroupRole(java.lang.String, java.lang.String)
     */
    @Override
    public void setGroupRole(final String group, final String role) {
        this.group = group;
        this.role = role;
    }

    /**
     * Cache the User and Password This is called after the SessionService.login service operation is called.
     *
     * @see com.teamcenter.soa.client.CredentialManager#setUserPassword(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public void setUserPassword(final String user, final String password, final String discriminator) {
        this.name = user;
        this.password = password;
        this.discriminator = discriminator;
    }
}
