/*
 * This file is part of DebGen Biobank Software developed by ASTRID Research.
 * All rights reserved. Reproduction, modification, use or disclosure
 * to third parties without express authority is forbidden.
 *
 * Copyright ASTRID Research Inc., Hungary, 2009-2011.
 *
 * ASTRID Research
 * Headquaters: Csapo utca 42. Debrecen H-4029 HUNGARY
 * Phone: +36-52-533-173        Fax: +36-52-533-174
 * Web: http://www.astrid.hu    E-mail: info@astrid.hu
 *
 * Project name: DebGen Biobank Software project.
 * URLS:
 * Developers: Istvan Zoltan Nagy (istvan.nagy@astrid.hu)
 */
package hu.astrid.debgen.authentication.facade;

import hu.astrid.debgen.authentication.domain.DebGenUser;
import hu.astrid.debgen.authentication.exception.AuthenticationFailedException;
import hu.astrid.debgen.authentication.exception.InvalidPasswordException;
import hu.astrid.debgen.authentication.service.AuthenticationService;
import hu.astrid.debgen.server.requestprocessor.exception.FormFillException;
import hu.astrid.toucan.control.persistence.DatabaseServiceClass;
import org.hibernate.Session;

import java.security.InvalidParameterException;

/**
 * The facade of the authentication mechanism
 * @author inagy
 */
public class AuthenticationFacade extends DatabaseServiceClass {

    /**
     * Creates a session and authenticates the user
     * @param userData The userData which should be checked
     * @return The user object if, the data is valid.
     * @throws AuthenticationFailedException When the data is not valid.
     * @throws Exception When some DB related error occurs.
     */
    public DebGenUser authenticate(DebGenUser userData) throws AuthenticationFailedException, Exception {
        DebGenUser user = null;
        Session session = null;
        try {
            session = startSession();
            session.beginTransaction();
            AuthenticationService service = (AuthenticationService) AuthenticationService.getInstance(session);
            user = service.authenticate(userData);
            session.getTransaction().commit();
            return user;
        } catch (Exception ex) {
            rollback();
            throw ex;
        } finally {
            closeSession();
        }
    }

    /**
     * Creates a session to change the password of the given user, and changes the password
     * @param userData The user
     * @param oldPassword The old password
     * @param newPassword The new password
     * @param newPasswordRetry The new password again
     * @throws FormFillException When the form is not filled properly.
     * @throws InvalidPasswordException When the password is invalid.
     * @throws AuthenticationFailedException When the authentication fails.
     * @throws Exception When some DB related error occurs.
     */
    public void changePassword(DebGenUser userData, String oldPassword, String newPassword, String newPasswordRetry, String newFullName, String newEmail)
            throws FormFillException, InvalidPasswordException, AuthenticationFailedException, Exception {
        if (userData==null || oldPassword==null || newPassword == null || newPasswordRetry == null || newFullName == null || newEmail == null){
        	throw new InvalidParameterException();
        }
        Session session = null;
        try {
            session = startSession();
            session.beginTransaction();
            AuthenticationService service = (AuthenticationService) AuthenticationService.getInstance(session);
            service.changePassword(userData, oldPassword, newPassword, newPasswordRetry, newFullName, newEmail);
            session.getTransaction().commit();
        } catch (Exception ex) {
            rollback();
            throw ex;
        } finally {
            closeSession();
        }
    }

    public void changeLanguage(DebGenUser userData) throws Exception {
        Session session = null;
        try {
            session = startSession();
            session.beginTransaction();
            AuthenticationService service = (AuthenticationService) AuthenticationService.getInstance(session);
            service.create(userData);
            session.getTransaction().commit();
        } catch (Exception ex) {
            rollback();
            throw ex;
        } finally {
            closeSession();
        }
    }
}
