/*
 * $Id: ConversionsMaintenanceAction.java,v 1.5 2007/08/14 14:49:57 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * Created on 04.11.2004
 *
 * $Log: ConversionsMaintenanceAction.java,v $
 * Revision 1.5  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.4  2004/12/17 19:08:24  jonyp
 * deleted unneccessary import
 *
 * Revision 1.3  2004/12/17 00:55:34  koppy
 * Checkboxen statt Radio-Buttons
 *
 * Revision 1.2  2004/12/10 14:57:06  koppy
 * +Action für die Veranlassung der Konvertierung
 * aller noch nicht konvertierten Dokumente
 *
 * Revision 1.1  2004/11/08 12:28:14  koppy
 * initiale version
 *
 */
package vkc.administration.action;

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

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

import vkc.administration.form.ConversionsMaintenanceForm;
import vkc.db.common.ConversionStatus;
import vkc.db.entities.VKCUser;
import vkc.exception.DBException;
import vkc.view.WebShare;

/**
 * This class represents
 * 
 * @author Andreas Koop
 */
public class ConversionsMaintenanceAction extends SystemMaintenanceAction {
    public ActionForward execute(ActionMapping mapping, 
                                 ActionForm f, 
                                 HttpServletRequest request, 
                                 HttpServletResponse response) 
    throws ServletException {
        try {
            ConversionsMaintenanceForm form = (ConversionsMaintenanceForm) f;
            VKCUser user = form.getUser();

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

                return getInfoForward();
            }

            if (request.getParameter("action.trigger_reconversion") != null) {
                this.triggerConversionsForDoctypeAction(form, request);
                form.reset(mapping, request);
            } else if (request.getParameter("action.trigger_notconverted") != null) {
                this.executeTriggerConversionsForNotYetConvertedAction(form, request);
                form.reset(mapping, request);
            } else if (request.getParameter("action.delete_status") != null) {
                this.executeDeleteStatusAction(form, request);
                form.reset(mapping, request);
            } else if (request.getParameter("action.reset_status") != null) {
                this.executeResetStatusAction(form, request);
                form.reset(mapping, request);
            }

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

    /**
     * @param form
     * @param request
     */
    private void executeTriggerConversionsForNotYetConvertedAction(ConversionsMaintenanceForm form, HttpServletRequest request) throws DBException {
        int affectedDocs = ConversionStatus.createConversionsForUnconverted();
        request.setAttribute("succeeded", form.getResources().getMessage(form.getLocale(), "vkc.administration.systemmaintenance.conversions.succeededNotConverted", affectedDocs + ""));
    }

    /**
     * Löscht Einträge aus der CONVERSIONSTATI-Tabelle
     * 
     * @param form
     * @param request
     */
    private void executeDeleteStatusAction(ConversionsMaintenanceForm form, HttpServletRequest request) throws DBException {
        Integer[] docIds = form.getSelectedIds();
        
        if (docIds == null || docIds.length == 0) {
            ActionErrors errors = new ActionErrors();
            errors.add("noStatusSelected", new ActionError("vkc.administration.systemmaintenance.conversions.noStatusSelected"));
            this.saveErrors(request, errors);
            
            return;
        }

        ConversionStatus.deleteStatusForDocumentIds(docIds);
        request.setAttribute("succeeded", form.getResources().getMessage(form.getLocale(), "vkc.administration.systemmaintenance.conversions.deleteStatusSucceeded"));
    }

    /**
     * Setzt die Einträge aus der CONVERSIONSTATI-Tabelle zurück
     * 
     * @param form
     * @param request
     */
    private void executeResetStatusAction(ConversionsMaintenanceForm form, HttpServletRequest request) throws DBException {
        Integer[] docIds = form.getSelectedIds();

        if (docIds == null || docIds.length == 0) {
            ActionErrors errors = new ActionErrors();
            errors.add("noStatusSelected", new ActionError("vkc.administration.systemmaintenance.conversions.noStatusSelected"));
            this.saveErrors(request, errors);
            
            return;
        }

        ConversionStatus.resetStatusForDocumentIds(docIds);
        request.setAttribute("succeeded", form.getResources().getMessage(form.getLocale(), "vkc.administration.systemmaintenance.conversions.resetStatusSucceeded"));
    }

    /**
     * Veranlässt Konvertierungen für einen bestimmten Dokumenttyp
     * 
     * @param form
     * @param request
     */
    private void triggerConversionsForDoctypeAction(ConversionsMaintenanceForm form, HttpServletRequest request) throws DBException {
        if (form.getSelectedDocTypeIds() == null || form.getSelectedDocTypeIds().length == 0) {
            ActionErrors errors = new ActionErrors();
            errors.add("docTypeNotSelected", new ActionError("vkc.administration.systemmaintenance.conversions.docType_notSelected"));
            this.saveErrors(request, errors);
        
            return;
        }

        int affectedDocs = 0;

        for (int i = 0; i < form.getSelectedDocTypeIds().length; i++) {
            affectedDocs += ConversionStatus.recreateConversionsForDocTypeId(form.getSelectedDocTypeIds()[i]);
        }

        if (affectedDocs > 0) {
            request.setAttribute("succeeded", form.getResources().getMessage(form.getLocale(), "vkc.administration.systemmaintenance.conversions.succeeded", affectedDocs + ""));
        } else {
            ActionErrors errors = new ActionErrors();
            errors.add("noDocsForDoctype", new ActionError("vkc.administration.systemmaintenance.conversions.noDocsForDoctype"));
            this.saveErrors(request, errors);
        }

    }
}
