/**********************************************************************
 * Copyright (c) by Heiner Jostkleigrewe
 * This program is free software: you can redistribute it and/or modify it under the terms of the 
 * GNU General Public License as published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,  but WITHOUT ANY WARRANTY; without 
 *  even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See 
 *  the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program.  If not, 
 * see <http://www.gnu.org/licenses/>.
 * 
 * heiner@jverein.de
 * www.jverein.de
 **********************************************************************/

package de.jost_net.JVerein.gui.action;

import de.jost_net.JVerein.gui.control.BuchungsControl;
import de.jost_net.JVerein.gui.dialogs.BuchungsartZuordnungDialog;
import de.jost_net.JVerein.rmi.Buchung;
import de.jost_net.JVerein.rmi.Buchungsart;
import de.jost_net.JVerein.rmi.Buchungsklasse;
import de.willuhn.jameica.gui.Action;
import de.willuhn.jameica.gui.GUI;
import de.willuhn.jameica.system.OperationCanceledException;
import de.willuhn.logging.Logger;
import de.willuhn.util.ApplicationException;

/**
 * Buchungsart zuordnen.
 */
public class BuchungBuchungsartZuordnungAction implements Action
{
  private BuchungsControl control;

  public BuchungBuchungsartZuordnungAction(BuchungsControl control)
  {
    this.control = control;
  }

  @Override
  public void handleAction(Object context) throws ApplicationException
  {
    if (context == null
        || (!(context instanceof Buchung) && !(context instanceof Buchung[])))
    {
      throw new ApplicationException("Keine Buchung(en) ausgewählt");
    }
    try
    {
      Buchung[] b = null;
      if (context instanceof Buchung)
      {
        b = new Buchung[1];
        b[0] = (Buchung) context;
      }
      if (context instanceof Buchung[])
      {
        b = (Buchung[]) context;
      }
      if (b == null)
      {
        return;
      }
      if (b.length == 0)
      {
        return;
      }
      if (b[0].isNewObject())
      {
        return;
      }
      
      BuchungsartZuordnungDialog baz = new BuchungsartZuordnungDialog(
          BuchungsartZuordnungDialog.POSITION_MOUSE);
      baz.open();
      if (!baz.getAbort())
      {
        Buchungsart ba = baz.getBuchungsart();
        Buchungsklasse bk = baz.getBuchungsklasse();
        int counter = 0;
        if (ba == null)
        {
          for (Buchung buchung : b)
          {
            buchung.setBuchungsart(null);
            buchung.setBuchungsklasseId(null);
            buchung.store();
          }
        }
        else
        {
          for (Buchung buchung : b)
          {
            boolean protect = buchung.getBuchungsart() != null
                && !baz.getOverride();
            if (protect)
            {
              counter++;
            }
            else
            {
              buchung.setBuchungsart(Long.valueOf(ba.getID()));
              if (bk != null)
               buchung.setBuchungsklasseId(Long.valueOf(bk.getID()));
              else
                buchung.setBuchungsklasseId(null);
              buchung.store();
            }
          }
        }
        control.getBuchungsList();
        if (ba == null)
        {
          GUI.getStatusBar().setSuccessText("Buchungsarten gelöscht");
        }
        else
        {
          String protecttext = "";
          if (counter > 0)
          {
            protecttext = String
                .format(", %d Buchungen wurden nicht überschrieben. ", counter);
          }
          GUI.getStatusBar()
          .setSuccessText("Buchungsarten zugeordnet" + protecttext);
        }
      }
    }
    catch (OperationCanceledException oce)
    {
      throw oce;
    }
    catch (Exception e)
    {
      Logger.error("Fehler", e);
      GUI.getStatusBar().setErrorText("Fehler bei der Zuordnung der Buchungsart");
    }
  }
}
