/*
 * 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.useradmin.service;

import elements.Functions;
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.server.requestprocessor.exception.FormFillException;
import hu.astrid.shared.server.service.BaseOperationsAbstract;
import org.hibernate.Session;

import java.util.List;

/**
 *
 * @author inagy
 */
public class UserAdminService extends BaseOperationsAbstract {

    public void changePassword(DebGenUser userData, String newPassword,
            String newPasswordRetry) throws FormFillException,
            InvalidPasswordException, AuthenticationFailedException, Exception {
        if (newPassword.isEmpty() || newPasswordRetry.isEmpty()) {
            throw new FormFillException("Az új jelszó és új jelszó újra mezők kitöltése kötelező.");  //i18
        }
        if (newPassword.equals(newPasswordRetry) == false) {
            throw new FormFillException("Az új jelszó és új jelszó újra mezők tartalma azonos kell legyen.");  //i18
        }
        DebGenUser.checkData(userData.getUserName(), newPassword);
        userData.setHashedPassword(Functions.hashMD5(newPassword));
        userData.setMustChangePass(false);
        session.update(userData);
    }

    public static BaseOperationsAbstract getInstance(Session session) {
        UserAdminService userAdminService = new UserAdminService();
        userAdminService.session = session;
        return userAdminService;
    }

    public DebGenUser load(Long id) {
        return super.load(DebGenUser.class, id);
    }

    public List<DebGenUser> list(String order) {
        return super.list(DebGenUser.class, order);
    }

    public void delete(DebGenUser user) {
        session.delete(user);
    }

}
