/*
 * $Id: IndexInitializer.java,v 1.1 2006/07/17 13:04:01 lawern Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: IndexInitializer.java,v $
 * Revision 1.1  2006/07/17 13:04:01  lawern
 * Neuer typografiegewichteter Index und k-NN-Klassifikation aus meiner Diss.
 *
 */
package vkc.search;

import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;

import vkc.db.serializer.VKCSysParameterSerializer;

import vkc.misc.VKCLogManager;

import vkc.view.WebShare;

import java.io.File;

import java.util.Date;


/**
 * @author lawern
 *
 * Klasse zur Initialisierung der Such- und Klassifikationsindizes.
 *
 */
public final class IndexInitializer implements Job {
    static boolean rebuildIndex = false;

    /**
     * @param create True: Alte Indizes löschen und neu aufbauen.
     */
    public static void initializeIndex(boolean create) {
        rebuildIndex = create;
        WebShare.classifier = new NNClassifier();

        //Initialisierungs-Job erzeugen
        try {
            JobDetail initializationJob = new JobDetail("INDEX_INITIALIZATION",
                    Scheduler.DEFAULT_GROUP, IndexInitializer.class);

            SimpleTrigger initializationTrigger = new SimpleTrigger(initializationJob.getName() +
                    "_TRIGGER", Scheduler.DEFAULT_GROUP,
                    new Date(System.currentTimeMillis() + 500), null, 0, 0);

            WebShare.scheduler.scheduleJob(initializationJob,
                initializationTrigger);
        } catch (SchedulerException e) {
            VKCLogManager.errorLog("vkc.search.IndexInitializer", "1",
                    "Can't schedule initialization job!", e);
        }
    }

    /**
     * Suchindex-Initialisieren
     *
     * @param create True: Alte Indizes löschen und neu aufbauen.
     * @throws Exception
     */
    public static void initSearch(boolean create) throws Exception {
        // Search Initialization 
        String searchIndexDir = VKCSysParameterSerializer.retrieveValue("SYSTEMPATHS",
                "SearchIndexDir", "WEB-INF/searchindex");

        if ((searchIndexDir.charAt(0) == '/') ||
                (searchIndexDir.charAt(1) == ':') ||
                (searchIndexDir.charAt(0) == '\\')) {
            //absoluter Pfad
            searchIndexDir = new File(searchIndexDir).getPath();
        } else {
            //relativer Pfad
            searchIndexDir = new File(WebShare.getWebApplicationDir(),
                    searchIndexDir).getPath();
        }

        VKCLogManager.infoLog(WebShare.class.getName(),
            "SearchIndexDir ==> " + searchIndexDir);
        Search.init(searchIndexDir, create);
    }

    /**
     * Klassifikationsindex initialisieren
     *
     * @param create True: Alte Indizes löschen und neu aufbauen.
     * @throws Exception
     */
    public static void initClassification(boolean create)
        throws Exception {
        // Classification Initialization
        String classifyIndexDir = VKCSysParameterSerializer.retrieveValue("SYSTEMPATHS",
                "ClassifyIndexDir", "WEB-INF/classifyindex");

        if ((classifyIndexDir.charAt(0) == '/') ||
                (classifyIndexDir.charAt(1) == ':') ||
                (classifyIndexDir.charAt(0) == '\\')) {
            //absoluter Pfad
            classifyIndexDir = new File(classifyIndexDir).getPath();
        } else {
            //relativer Pfad
            classifyIndexDir = new File(WebShare.getWebApplicationDir(),
                    classifyIndexDir).getPath();
        }

        VKCLogManager.infoLog(WebShare.class.getName(),
            "ClassifyIndexDir ==> " + classifyIndexDir);

        WebShare.classifier.init(classifyIndexDir, create);
    }

    /* (non-Javadoc)
     * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
     */
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        try {
            initSearch(rebuildIndex);
            initClassification(rebuildIndex);
        } catch (Exception e) {
            VKCLogManager.errorLog("vkc.search.IndexInitializer", "2",
                    "Error in index initialization job!", e);
        }
    }
}
