LoadableSaveableAccounting.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.role;

  27. import jakarta.annotation.Nonnull;
  28. import java.io.IOException;
  29. import java.nio.file.Files;
  30. import java.nio.file.Path;
  31. import it.tidalwave.accounting.model.Accounting;
  32. import it.tidalwave.util.PreferencesHandler;
  33. import it.tidalwave.dci.annotation.DciRole;
  34. import lombok.RequiredArgsConstructor;
  35. import lombok.extern.slf4j.Slf4j;
  36. import static it.tidalwave.role.io.Marshallable._Marshallable_;
  37. import static it.tidalwave.role.io.Unmarshallable._Unmarshallable_;

  38. /***************************************************************************************************************************************************************
  39.  *
  40.  * The implementation of {@link Loadable} and {@link Saveable} for {@link LoadableSaveableAccounting}.
  41.  *
  42.  * @stereotype role
  43.  *
  44.  * @author  Fabrizio Giudici
  45.  *
  46.  **************************************************************************************************************************************************************/
  47. @RequiredArgsConstructor @DciRole(datumType = Accounting.class) @Slf4j
  48. public class LoadableSaveableAccounting implements Loadable, Saveable
  49.   {
  50.     public static final String BLUEHOUR_FILE_NAME = "blueHour.xml";

  51.     @Nonnull
  52.     private final Accounting accounting;
  53.    
  54.     @Nonnull
  55.     private final PreferencesHandler preferencesHandler;

  56.     /***********************************************************************************************************************************************************
  57.      * {@inheritDoc}
  58.      **********************************************************************************************************************************************************/
  59.     @Override
  60.     public Accounting load()
  61.       throws IOException
  62.       {
  63.         final var dataFile = getDataFile();
  64.         log.info(">>>> loading data from {}...", dataFile);

  65.         try (final var is = Files.newInputStream(dataFile))
  66.           {
  67.             return accounting.as(_Unmarshallable_).unmarshal(is);
  68.           }
  69.       }

  70.     /***********************************************************************************************************************************************************
  71.      * {@inheritDoc}
  72.      **********************************************************************************************************************************************************/
  73.     @Override
  74.     public void save()
  75.       throws IOException
  76.       {
  77.         final var dataFile = getDataFile();
  78.         log.info(">>>> saving data to {}...", dataFile);

  79.         try (final var os = Files.newOutputStream(dataFile))
  80.           {
  81.             accounting.as(_Marshallable_).marshal(os);
  82.           }
  83.       }

  84.     /***********************************************************************************************************************************************************
  85.      *  
  86.      **********************************************************************************************************************************************************/
  87.     @Nonnull
  88.     protected Path getDataFile()
  89.       {
  90.         return preferencesHandler.getAppFolder().resolve(BLUEHOUR_FILE_NAME);
  91.       }
  92.   }