/*
 * $Id: LoginChangePasswordForm.java,v 1.9 2005/09/01 13:11:52 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: LoginChangePasswordForm.java,v $
 * Revision 1.9  2005/09/01 13:11:52  marbobo
 * + Einige Anpassung zur SysLink
 *
 * Revision 1.8  2004/11/10 15:02:47  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.7  2004/08/08 01:40:49  marbobo
 * + Refactorings und Behandlung von Fehlermeldungen
 *
 * Revision 1.6  2004/08/02 12:37:25  marbobo
 * + Bugfix
 *
 * Revision 1.5  2004/07/22 22:13:32  marbobo
 * + News beim Einlogen anzeigen ( ist zur Zeit deaktiviert )
 *
 * Revision 1.4  2003/12/24 10:11:54  bigmac
 * Projektweite Import-Optimierung
 *
 * Revision 1.3  2003/09/09 00:33:07  marbobo
 * ungenutzte Imports entfernt und Bugfixes
 *
 * Revision 1.2  2003/08/07 10:36:50  marbobo
 * Kommentar korrigiert
 *
 * Revision 1.1  2003/08/04 13:54:54  marbobo
 * Initiale Version
 *
 */

package vkc.view.form;


import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import vkc.db.entities.VKCUser;
import vkc.misc.VKCLogManager;
import vkc.util.StringUtils;
import vkc.util.VKCCoder;
import vkc.view.WebShare;

/**
 * Form bean für die Login-Seite um den Benutzerpasswort
 * zu Ändern
 *
 * @author marbobo
 */

public final class LoginChangePasswordForm extends ActionForm {
	private String oldpassword = "";
	private String newpassword = "";
	private String repassword = "";
    private String linkType = null;
    private String objectId = null;
    private String loginType = null;

    private VKCUser user = null;

    /**
	 * Reset der Eingabefelder auf Defaultwerte
	 *
	 * @param mapping The mapping used to select this instance
	 * @param request The servlet request we are processing
	 */
	public void reset(ActionMapping mapping, HttpServletRequest request) {
	    linkType = null;
	    objectId = null;
	    loginType = null;

   		user = (VKCUser) request.getSession().getAttribute(WebShare.TEMP_USER_KEY);
	}

	/**
	 * Validierung der Eingabefelder
	 *
	 * @param mapping The mapping used to select this instance
	 * @param request The servlet request we are processing
	 */
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
        
		if (request.getParameter("action.change.password") != null) {
			//Passwort ändern
			if (StringUtils.isEmpty(getOldpassword())) {
				errors.add("oldpassword.mismatch", new ActionError("error.oldpassword.mismatch"));
			}
	        
			if (getNewpassword().length() < 6 || getRepassword().length() < 6) {
				errors.add("shortpassword", new ActionError("vkc.profile.error.password.tooshort"));
			}

			if (!getNewpassword().equals(getRepassword())) {
				errors.add("mismatchpassword", new ActionError("vkc.profile.error.password.mismatch"));
			}
			
			VKCUser user = getUser();
			String currentPassword = user.getPassword();
			
			if (!StringUtils.isEmpty(currentPassword) && !StringUtils.isEmpty(getOldpassword())) {
				String userName = user.getUser();
				byte[] md5Bytes = null;
				
				try {
					md5Bytes = (userName + getOldpassword()).getBytes("ISO-8859-1");
				} catch (UnsupportedEncodingException e) {
					VKCLogManager.errorLog(this, "", e.getMessage(), e);
				}
		
				MessageDigest digest = null;
				
				try {
					digest = MessageDigest.getInstance("MD5");
				} catch (NoSuchAlgorithmException e1) {
					VKCLogManager.errorLog(this, "", e1.getMessage(), e1);
				}
				
				digest.reset();
				digest.update(md5Bytes);
				byte[] fingerPrint = digest.digest();
				fingerPrint = VKCCoder.base64Encode(fingerPrint);
				String md5Password = null;
				
				try {
					md5Password = new String(fingerPrint, "ISO-8859-1");
				} catch (UnsupportedEncodingException e2) {
					VKCLogManager.errorLog(this, "", e2.getMessage(), e2);
				}
				
				//Passwort stimmt nicht überein
				if (!currentPassword.trim().equals(md5Password)) {
					errors.add("oldpassword.mismatch", new ActionError("error.oldpassword.mismatch"));
				}
			}
		}

		return errors;
	}

	public String getNewpassword() {
		return newpassword;
	}

	public String getOldpassword() {
		return oldpassword;
	}

	public String getRepassword() {
		return repassword;
	}

	public String getLinkType() {
		return linkType;
	}

	public String getObjectId() {
		return objectId;
	}

	public String getSysLink() {
		return "/sysLink.do?linkType=" + getLinkType() + "&objectId=" + getObjectId() + (getLoginType() != null ? "&loginType=" + getLoginType() : "");
	}

    public VKCUser getUser() {
        return user;
    }

	public boolean isSysLink() {	
		return getLinkType() != null && getObjectId() != null;
	}

	public void setNewpassword(String string) {
		newpassword = string;
	}

	public void setOldpassword(String string) {
		oldpassword = string;
	}

	public void setRepassword(String string) {
		repassword = string;
	}

	public void setLinkType(String string) {
		linkType = string;
	}

	public void setObjectId(String string) {
		objectId = string;
	}

    public void setUser(VKCUser u) {
        user = u;
    }

	public String getLoginType() {
		return loginType;
	}

	public void setLoginType(String loginType) {
		this.loginType = loginType;
	}
}