/*
 * $Id: SystemMaintenanceAction.java,v 1.7 2007/08/14 14:49:57 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: SystemMaintenanceAction.java,v $
 * Revision 1.7  2007/08/14 14:49:57  marbobo
 * + Licenseverwaltung komplett umgeschrieben
 * + Kategoriesuche und verlorene Kategorien anzeigen komplett umgeschrieben
 * + Fehler beim Speichern von Beschreibenungsfeldern für Entitäten beseitigt.
 *
 * Revision 1.6  2006/07/17 13:04:02  lawern
 * Neuer typografiegewichteter Index und k-NN-Klassifikation aus meiner Diss.
 *
 */
package vkc.administration.action;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import vkc.administration.form.SystemMaintenanceForm;
import vkc.db.entities.VKCUser;
import vkc.exception.DBException;
import vkc.search.IndexInitializer;
import vkc.view.WebShare;

public class SystemMaintenanceAction extends Action {
    private ActionForward DEFAULT_FORWARD = new ActionForward("/templates/${template.name}/administration/systemMaintenance.jsp", false);
    private ActionForward INFO_FORWARD = new ActionForward("/templates/${template.name}/common/InfoPage.jsp");

    public ActionForward execute(ActionMapping mapping, 
                                 ActionForm f, 
                                 HttpServletRequest request, 
                                 HttpServletResponse response) 
    throws ServletException {
        try {
            SystemMaintenanceForm form = (SystemMaintenanceForm) f;
            VKCUser user = form.getUser();

            if (!user.hasAdminPrivileges()) {
                request.setAttribute(WebShare.INFO_KEY, "vkc.info.access_denied");

                return INFO_FORWARD;
            }

            if (request.getParameter("action.start_rebuild") != null) {
                return handleRebuildSearchClassifyIndexAction(form, request);
            }

            return handleInitializeAction(form, request);
        } catch (Exception ex) {
            throw new ServletException(ex);
        }
    }

    public ActionForward getInfoForward() {
        return INFO_FORWARD;
    }

    protected ActionForward handleInitializeAction(SystemMaintenanceForm form, HttpServletRequest request) {
        form.setSelectedContext(request.getParameter("selectedContext"));

        return DEFAULT_FORWARD;
    }

    /**
     * Action für die Neuerstellung des Suchindex
     * 
     * @param form
     *            systemmaintnance form
     * @param request
     *            http request
     * @return action forward
     * @throws DBException
     */
    private ActionForward handleRebuildSearchClassifyIndexAction(SystemMaintenanceForm form, HttpServletRequest request) throws DBException {
        try {
            if (Integer.parseInt(form.getIndexMethod()) == SystemMaintenanceForm.INDEX_METHOD_OPTIMIZE.intValue()) {
                // just optimize indizes
                IndexInitializer.initSearch(false);
                IndexInitializer.initClassification(false);
            } else {
                // rebuild completely
                IndexInitializer.initSearch(true);
                IndexInitializer.initClassification(true);
            }
        } catch (Exception ex) {
            throw new DBException(ex);
        }

        request.setAttribute("rebuild_succeeded", "succeeded");

        return DEFAULT_FORWARD;
    }
}