/*
 * $Id: NotificationMail.java,v 1.20 2007/08/08 12:14:40 marbobo Exp $
 * 
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: NotificationMail.java,v $
 * Revision 1.20  2007/08/08 12:14:40  marbobo
 * + License-Funktionalität komplett umgeschrieben
 * + Bugfix bzgl. Beschreibungsfelder von Entitäten
 * + Bugfix mailing
 *
 * Revision 1.19  2006/10/04 03:15:15  marbobo
 * + Vollständige Unterstützung für Templates implementiert
 * + Alles von Virtoweb auf ProCoBa umgestellt
 *
 * Revision 1.18  2006/03/06 15:12:03  jonyp
 * + Bugfix (wg. Oracle)
 *
 * Revision 1.17  2005/12/15 15:28:51  marbobo
 * + Benutzername und Passwort für Mailserver implementiert
 *
 * Revision 1.16  2005/01/20 15:25:49  marbobo
 * + Anpassungen an den Methoden getRootURL() und getContextName()
 *
 * Revision 1.15  2004/11/10 15:02:48  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.14  2004/08/20 12:48:26  koppy
 * Etwas überarbeitet + Behandlung von registrierten Queries
 *
 * Revision 1.13  2004/07/20 08:28:18  jonyp
 * added copyright info + cvs log
 *
 */
package vkc.messenger.mail;

import java.util.HashMap;

import vkc.db.entities.VKCLanguage;
import vkc.db.serializer.VKCSysParameterSerializer;
import vkc.exception.DBException;
import vkc.misc.MailUtils;
import vkc.misc.VKCLogManager;
import vkc.view.WebShare;
import vkc.view.form.SysLinkForm;
import vkc.misc.TemplateParser;

/**
 * Eine abstrakte Klasse fuer Email-Dienst.
 * 
 * @author teng
 */
public abstract class NotificationMail extends Thread {
    boolean debug = true;

    private boolean enabled = true;

    public MailManager mailManager = null;

    public MailUtils mailUtils = null;

    /**
     * the real system vkc root path
     */
    public String rootPath = null;

    public String vkc_rootURL = null;

    public String catURL = "/sysLink.do?linkType=" + SysLinkForm.CATEGORY_LINK + "&objectId=";

    public String docURL = "/sysLink.do?linkType=" + SysLinkForm.DOCUMENT_LINK + "&objectId=";

    public String smtpServer;

    public String smtpUsername;

    public String smtpPassword;

    public String senderAddr;

    public String recepient = null;

    public TemplateParser plainTemplateParser = null;

    public TemplateParser htmlTemplateParser = null;

    public HashMap valueMap = null;

    public VKCLanguage language;

    public NotificationMail() {
        this.setPriority(Thread.MIN_PRIORITY);
        this.setDaemon(true);
    }

    /**
     * Initializes this notification service
     * 
     * @return true if initialization is successful
     */
    public boolean init() throws DBException {
        if (debug) {
            VKCLogManager.infoLog(this, "Initializes NotificationMail " + this.getClass().getName() + "...");
        }

        mailManager = MailManager.getMailManager();
        rootPath = WebShare.VKC_ROOT_PATH;
        senderAddr = VKCSysParameterSerializer.retrieveValue("MAIL", "AdminEmail", "");
        smtpServer = VKCSysParameterSerializer.retrieveValue("MAIL", "MailServer", "");
        smtpUsername = VKCSysParameterSerializer.retrieveValue("MAIL", "MailUsername", "");
        smtpPassword = VKCSysParameterSerializer.retrieveValue("MAIL", "MailPassword", "");
        vkc_rootURL = WebShare.getRootURL() + WebShare.getContextPath();

        String enableMail = VKCSysParameterSerializer.retrieveValue("MAIL", "EnableMail");

        if (rootPath == null) {
            throw new IllegalArgumentException("rootPath must not be null");
        }

        if (senderAddr == null) {
            throw new IllegalArgumentException("senderAddr must not be null");
        }

        if (smtpServer == null) {
            throw new IllegalArgumentException("smtpServer must not be null");
        }

        if (vkc_rootURL == null) {
            throw new IllegalArgumentException("vkc_rootURL must not be null");
        }

        if (enableMail == null) {
            throw new IllegalArgumentException("enableMail must not be null");
        }

        enabled = Integer.parseInt(enableMail) == 1;
        mailUtils = new MailUtils(null);

        return true;
    }

    /**
     * Use this method to update SysParameters if the setting changed
     */
    public void updateSysParameters() {
        try {
            senderAddr = VKCSysParameterSerializer.retrieveValue("MAIL", "AdminEmail", "");
            smtpServer = VKCSysParameterSerializer.retrieveValue("MAIL", "MailServer", "");
            smtpUsername = VKCSysParameterSerializer.retrieveValue("MAIL", "MailUsername", "");
            smtpPassword = VKCSysParameterSerializer.retrieveValue("MAIL", "MailPassword", "");
            vkc_rootURL = WebShare.getRootURL() + WebShare.getContextPath();

            if (Integer.parseInt(VKCSysParameterSerializer.retrieveValue("MAIL", "EnableMail")) == 0) {
                enabled = false;
            } else {
                enabled = true;
            }

        } catch (Exception ex) {
            VKCLogManager.errorLog(this, "1", "updateSysParameters failed : " + ex.getMessage(), ex);
        }
    }

    /**
     * Enable or disable this notification service
     * 
     * @param bool
     *            true enable, false disable
     */
    public void setEnabled(boolean bool) {
        enabled = bool;
    }

    /**
     * check if this service enabled
     * 
     * @return
     */
    public boolean isEnabled() {
        return enabled;
    }

    /**
     * Call this method to create new parser one for html and one another for
     * plain part every time for sending a new email The variables
     * plainTemplateParser and htmlTemplateParser are initiated with new
     * <code>TemplateParser</code> object
     * 
     */
    public void createTemplateParser() {
        try {
            plainTemplateParser = TemplateParser.getTemplateParser();
        } catch (Exception ex) {
            if (debug) {
                VKCLogManager.infoLog(this, "Get TemplateParser for Email plain part failed!");
            }
        }

        try {
            htmlTemplateParser = TemplateParser.getTemplateParser();
        } catch (Exception ex) {
            if (debug) {
                VKCLogManager.infoLog(this, "Get TemplateParser for Email html part failed!");
            }
        }
    }
}