1 /*
2 * *************************************************************************************************************************************************************
3 *
4 * blueHour: open source accounting
5 * http://tidalwave.it/projects/bluehour
6 *
7 * Copyright (C) 2013 - 2024 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
28 import javax.annotation.Nonnull;
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import it.tidalwave.accounting.model.Accounting;
33 import it.tidalwave.util.PreferencesHandler;
34 import it.tidalwave.dci.annotation.DciRole;
35 import lombok.RequiredArgsConstructor;
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 *
42 * The implementation of {@link Loadable} and {@link Saveable} for {@link LoadableSaveableAccounting}.
43 *
44 * @stereotype role
45 *
46 * @author Fabrizio Giudici
47 *
48 **************************************************************************************************************************************************************/
49 @RequiredArgsConstructor @DciRole(datumType = Accounting.class) @Slf4j
50 public class LoadableSaveableAccounting implements Loadable, Saveable
51 {
52 public static final String BLUEHOUR_FILE_NAME = "blueHour.xml";
53
54 @Nonnull
55 private final Accounting accounting;
56
57 @Nonnull
58 private final PreferencesHandler preferencesHandler;
59
60 /***********************************************************************************************************************************************************
61 * {@inheritDoc}
62 **********************************************************************************************************************************************************/
63 @Override
64 public Accounting load()
65 throws IOException
66 {
67 final var dataFile = getDataFile();
68 log.info(">>>> loading data from {}...", dataFile);
69
70 try (final var is = Files.newInputStream(dataFile))
71 {
72 return accounting.as(_Unmarshallable_).unmarshal(is);
73 }
74 }
75
76 /***********************************************************************************************************************************************************
77 * {@inheritDoc}
78 **********************************************************************************************************************************************************/
79 @Override
80 public void save()
81 throws IOException
82 {
83 final var dataFile = getDataFile();
84 log.info(">>>> saving data to {}...", dataFile);
85
86 try (final var os = Files.newOutputStream(dataFile))
87 {
88 accounting.as(_Marshallable_).marshal(os);
89 }
90 }
91
92 /***********************************************************************************************************************************************************
93 *
94 **********************************************************************************************************************************************************/
95 @Nonnull
96 protected Path getDataFile()
97 {
98 return preferencesHandler.getAppFolder().resolve(BLUEHOUR_FILE_NAME);
99 }
100 }