DefaultIBizImporterPresentationControl.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * blueHour: open source accounting
  5.  * http://tidalwave.it/projects/bluehour
  6.  *
  7.  * Copyright (C) 2013 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *************************************************************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  12.  * You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  17.  * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
  18.  *
  19.  * *************************************************************************************************************************************************************
  20.  *
  21.  * git clone https://bitbucket.org/tidalwave/bluehour-src
  22.  * git clone https://github.com/tidalwave-it/bluehour-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.accounting.ui.importer.ibiz.impl;

  27. import jakarta.annotation.Nonnull;
  28. import java.io.IOException;
  29. import java.nio.file.Path;
  30. import org.springframework.stereotype.Component;
  31. import it.tidalwave.accounting.commons.AccountingOpenRequest;
  32. import it.tidalwave.accounting.commons.ImportRequest;
  33. import it.tidalwave.accounting.importer.ibiz.IBizImporterBuilderFactory;
  34. import it.tidalwave.accounting.ui.importer.ibiz.IBizImporterPresentation;
  35. import it.tidalwave.accounting.ui.importer.ibiz.IBizImporterPresentationControl;
  36. import it.tidalwave.util.annotation.VisibleForTesting;
  37. import it.tidalwave.ui.core.BoundProperty;
  38. import it.tidalwave.dci.annotation.DciContext;
  39. import it.tidalwave.messagebus.MessageBus;
  40. import it.tidalwave.messagebus.annotation.ListensTo;
  41. import it.tidalwave.messagebus.annotation.SimpleMessageSubscriber;
  42. import lombok.RequiredArgsConstructor;
  43. import lombok.extern.slf4j.Slf4j;
  44. import static it.tidalwave.accounting.role.Saveable._Saveable_;
  45. import static it.tidalwave.ui.core.UserNotificationWithFeedback.*;

  46. /***************************************************************************************************************************************************************
  47.  *
  48.  * @author  Fabrizio Giudici
  49.  *
  50.  **************************************************************************************************************************************************************/
  51. @Component @RequiredArgsConstructor @DciContext @SimpleMessageSubscriber @Slf4j
  52. public class DefaultIBizImporterPresentationControl implements IBizImporterPresentationControl
  53.   {
  54.     @Nonnull
  55.     private final MessageBus messageBus;
  56.    
  57.     @Nonnull
  58.     private final IBizImporterBuilderFactory importerBuilderFactory;
  59.    
  60.     private final BoundProperty<Path> iBizFolder = new BoundProperty<>();

  61.     @Nonnull
  62.     private final IBizImporterPresentation presentation;
  63.    
  64.     @VisibleForTesting void onImportRequest (@Nonnull @ListensTo final ImportRequest request)
  65.       throws IOException
  66.       {
  67.         log.info("onImportRequest({})", request);
  68.         presentation.bind(iBizFolder);
  69.         iBizFolder.set(Path.of(System.getProperty("user.home") + "/Settings/iBiz"));
  70.         presentation.chooseFolder(notificationWithFeedback().withFeedback(feedback().withOnConfirm(this::onConfirm)));
  71.       }

  72.     private void onConfirm()
  73.       {
  74.         // TODO: warn for overwriting data, ask for confirmation
  75.         // FIXME: this gets executed in JavaFX thread
  76.         try
  77.           {
  78.             presentation.lock();
  79.             final var accounting = importerBuilderFactory.newBuilder()
  80.                                                          .withPath(iBizFolder.get())
  81.                                                          .create();
  82.             accounting.importAll();
  83.             accounting.as(_Saveable_).save();
  84.             messageBus.publish(new AccountingOpenRequest());

  85.             // TODO: use a progress bar during the import process
  86.           }
  87.         catch (Exception e)
  88.           {
  89.             log.error("", e);
  90.             presentation.notifyError();
  91.           }
  92.         finally
  93.           {
  94.             presentation.unlock();
  95.           }
  96.       }
  97.   }