/*
 * Copyright (c) 2004 N2 Broadband, Inc.  All Rights Reserved.
 *
 * This module contains unpublished, confidential, proprietary
 * material.  The use and dissemination of this material are
 * governed by a license.  The above copyright notice does not
 * evidence any actual or intended publication of this material.
 *
 * Created: Apr 12, 2004
 */
package com.n2bb.sysmonui.alerts;

import com.n2bb.AlertsModule.InUse;
import com.n2bb.AlertsModule.NotFound;
import com.n2bb.AlertsModule.UnspecifiedException;
import com.n2bb.action.AbstractAction;
import com.n2bb.util.ActionConstants;
import com.n2bb.util.N2bbException;
import org.apache.struts.action.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Modifies alert patterns.
 *
 * @author kmatsuoka
 * @version $Id: ModifyAlertPatternsAction.java,v 1.1 2006/08/30 18:34:04 kmehta Exp $
 */
public final class ModifyAlertPatternsAction extends AbstractAction {

  public ActionForward executeAction(ActionMapping mapping, ActionForm form,
          HttpServletRequest request, HttpServletResponse response) {
      ActionErrors errors = new ActionErrors();
      String method = request.getParameter("method");
      if (method == null) {
          method = "";
      }
      String[] ids = request.getParameterValues("ids");
      if (ids != null) {
          if (method.equals(ActionConstants.delete)) {
              delete(ids, errors);
          }
      }
      if (!errors.isEmpty()) {
          saveErrors(request, errors);
      }

      // success and failure go to same page
      return (mapping.findForward("success"));
  }

    private void delete(String[] ids, ActionErrors errors) {
        for (int i = 0; i < ids.length; i++) {
            String name = ids[i];
            try {
                AlertManager.deleteAlertPattern(name);
            }
            catch (N2bbException e) {
                errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(e.getErrorCode()));
            }
            catch (InUse e) {
                errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("sysmonui.error.alertPattern.inUse", name));
            }
            catch (NotFound e) {
                errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("sysmonui.error.alertPattern.notFound", name));
            }
            catch (UnspecifiedException e) {
                errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("sysmonui.error.alertPattern.delete", name));
            }
        }
    }

}





