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

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

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

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

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

  68.         try (final var is = Files.newInputStream(dataFile))
  69.           {
  70.             return accounting.as(_Unmarshallable_).unmarshal(is);
  71.           }
  72.       }

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

  84.         try (final var os = Files.newOutputStream(dataFile))
  85.           {
  86.             accounting.as(_Marshallable_).marshal(os);
  87.           }
  88.       }

  89.     /*******************************************************************************************************************
  90.      *
  91.      *  
  92.      *
  93.      ******************************************************************************************************************/
  94.     @Nonnull
  95.     protected Path getDataFile()
  96.       {
  97.         return preferencesHandler.getAppFolder().resolve(BLUEHOUR_FILE_NAME);
  98.       }
  99.   }