DefaultIBizImporterPresentationControl.java

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

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

  46. /***********************************************************************************************************************
  47.  *
  48.  * @author  Fabrizio Giudici
  49.  *
  50.  **********************************************************************************************************************/
  51. @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.   }