/**********************************************************************
 * 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.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.jost_net.JVerein.rmi.Steuer;
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
{

  @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())
      {
        int counter = 0;
        if (baz.getDelete())
        {
          for (Buchung buchung : b)
          {
            buchung.setBuchungsartId(null);
            buchung.setBuchungsklasseId(null);
            buchung.setSteuer(null);
            buchung.store();
          }
        }
        else
        {
          Buchungsart ba = baz.getBuchungsart();
          Buchungsklasse bk = baz.getBuchungsklasse();
          Steuer st = baz.getSteuer();
          for (Buchung buchung : b)
          {
            boolean store = false;
            if (ba != null)
            {
              if (buchung.getBuchungsart() != null && !baz.getOverride())
              {
                counter++;
              }
              else
              {
                buchung.setBuchungsartId(Long.valueOf(ba.getID()));
                store = true;
              }
            }
            if (bk != null)
            {
              if (buchung.getBuchungsklasse() != null && !baz.getOverride())
              {
                counter++;
              }
              else
              {
                buchung.setBuchungsklasseId(Long.valueOf(bk.getID()));
                store = true;
              }
            }
            if (st != null)
            {
              boolean istLoeschen = st.getName()
                  .equalsIgnoreCase(BuchungsartZuordnungDialog.KEINE_STEUER);
              if (buchung.getSteuer() != null && !istLoeschen
                  && !baz.getOverride())
              {
                counter++;
              }
              else
              {
                if (istLoeschen)
                {
                  buchung.setSteuer(null);
                }
                else
                {
                  buchung.setSteuerId(Long.valueOf(st.getID()));
                }
                store = true;
              }
            }
            if (store)
            {
              buchung.store();
            }
          }
        }
        if (baz.getDelete())
        {
          GUI.getStatusBar().setSuccessText("Buchungsarten gelöscht");
        }
        else
        {
          String protecttext = "";
          if (counter > 0)
          {
            protecttext = String
                .format(", %d Attribute wurden nicht überschrieben. ", counter);
          }
          GUI.getStatusBar()
              .setSuccessText("Buchungsarten zugeordnet" + protecttext);
        }
      }
    }
    catch (OperationCanceledException oce)
    {
      throw oce;
    }
    catch (ApplicationException e)
    {
      GUI.getStatusBar().setErrorText(e.getLocalizedMessage());
    }
    catch (Exception e)
    {
      Logger.error("Fehler", e);
      GUI.getStatusBar()
          .setErrorText("Fehler bei der Zuordnung der Buchungsart: "
              + e.getLocalizedMessage());
    }
  }
}
