View Javadoc
1   /*
2    * MoneyWise: Finance Application
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.moneywise.views;
18  
19  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20  import io.github.tonywasher.joceanus.oceanus.date.OceanusDateRange;
21  import io.github.tonywasher.joceanus.oceanus.profile.OceanusProfile;
22  import io.github.tonywasher.joceanus.metis.viewer.MetisViewerEntry;
23  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseDataSet;
24  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseTax.MoneyWiseTaxFactory;
25  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseCurrency;
26  import io.github.tonywasher.joceanus.moneywise.data.validate.MoneyWiseValidatorFactory;
27  import io.github.tonywasher.joceanus.moneywise.database.MoneyWiseDataStore;
28  import io.github.tonywasher.joceanus.moneywise.lethe.data.analysis.analyse.MoneyWiseAnalysisTransAnalyser;
29  import io.github.tonywasher.joceanus.moneywise.lethe.data.analysis.data.MoneyWiseAnalysis;
30  import io.github.tonywasher.joceanus.moneywise.lethe.data.analysis.data.MoneyWiseAnalysisManager;
31  import io.github.tonywasher.joceanus.moneywise.sheets.MoneyWiseSheet;
32  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataSet;
33  import io.github.tonywasher.joceanus.prometheus.database.PrometheusDBConfig;
34  import io.github.tonywasher.joceanus.prometheus.database.PrometheusDataStore;
35  import io.github.tonywasher.joceanus.prometheus.database.PrometheusDatabase.PrometheusDatabasePreferenceKey;
36  import io.github.tonywasher.joceanus.prometheus.database.PrometheusDatabase.PrometheusDatabasePreferences;
37  import io.github.tonywasher.joceanus.prometheus.sheets.PrometheusSpreadSheet;
38  import io.github.tonywasher.joceanus.prometheus.toolkit.PrometheusToolkit;
39  import io.github.tonywasher.joceanus.prometheus.views.PrometheusDataControl;
40  import io.github.tonywasher.joceanus.prometheus.views.PrometheusEditSet;
41  import io.github.tonywasher.joceanus.prometheus.views.PrometheusViewerEntryId;
42  
43  /**
44   * Data Control for MoneyWiseApp.
45   */
46  public class MoneyWiseView
47          extends PrometheusDataControl {
48      /**
49       * The TaxFactory.
50       */
51      private final MoneyWiseTaxFactory theTaxFactory;
52  
53      /**
54       * The Maps.
55       */
56      private final MoneyWiseMaps theMaps;
57  
58      /**
59       * The DataSet.
60       */
61      private MoneyWiseDataSet theData;
62  
63      /**
64       * The analysis manager.
65       */
66      private MoneyWiseAnalysisManager theAnalysisMgr;
67  
68      /**
69       * Do we have security buckets?.
70       */
71      private boolean hasActiveSecurities;
72  
73      /**
74       * Do we have multiple currencies?
75       */
76      private boolean hasMultiCurrency;
77  
78      /**
79       * Constructor.
80       *
81       * @param pUtilitySet the utility set
82       * @param pTaxFactory the tax factory
83       */
84      public MoneyWiseView(final PrometheusToolkit pUtilitySet,
85                           final MoneyWiseTaxFactory pTaxFactory) {
86          /* Call super-constructor */
87          super(pUtilitySet);
88  
89          /* Record the tax factory */
90          theTaxFactory = pTaxFactory;
91  
92          /* Create the validator factory */
93          setValidatorFactory(new MoneyWiseValidatorFactory());
94  
95          /* Create an empty data set */
96          setData(getNewData());
97  
98          /* Create the maps */
99          theMaps = new MoneyWiseMaps(this);
100     }
101 
102     /**
103      * Obtain the date range.
104      *
105      * @return the date range
106      */
107     public OceanusDateRange getRange() {
108         return theTaxFactory.getDateRange();
109     }
110 
111     /**
112      * Obtain the analysis manager.
113      *
114      * @return the analyser.
115      */
116     public MoneyWiseAnalysisManager getAnalysisManager() {
117         return theAnalysisMgr;
118     }
119 
120     /**
121      * Do we have active securities?
122      *
123      * @return true/false.
124      */
125     public boolean hasActiveSecurities() {
126         return hasActiveSecurities;
127     }
128 
129     /**
130      * Do we have multiple currencies?
131      *
132      * @return true/false.
133      */
134     public boolean hasMultipleCurrencies() {
135         return hasMultiCurrency;
136     }
137 
138     /**
139      * Obtain a new DataSet.
140      *
141      * @return new DataSet
142      */
143     @Override
144     public final MoneyWiseDataSet getNewData() {
145         final MoneyWiseDataSet myDataSet = new MoneyWiseDataSet(getToolkit(), theTaxFactory);
146         myDataSet.setValidatorFactory(getValidatorFactory());
147         return myDataSet;
148     }
149 
150     @Override
151     public String getDatabaseName() {
152         final PrometheusDatabasePreferences myPrefs = getPreferenceManager().getPreferenceSet(PrometheusDatabasePreferences.class);
153         return myPrefs.getStringValue(PrometheusDatabasePreferenceKey.DBNAME);
154     }
155 
156     @Override
157     public PrometheusDataStore getDatabase() throws OceanusException {
158         final PrometheusDatabasePreferences myPrefs = getPreferenceManager().getPreferenceSet(PrometheusDatabasePreferences.class);
159         final PrometheusDBConfig myConfig = PrometheusDBConfig.fromPrefs(myPrefs);
160         return new MoneyWiseDataStore(myPrefs.getStringValue(PrometheusDatabasePreferenceKey.DBNAME), myConfig);
161     }
162 
163     @Override
164     public PrometheusDataStore getNullDatabase() throws OceanusException {
165         final PrometheusDatabasePreferences myPrefs = getPreferenceManager().getPreferenceSet(PrometheusDatabasePreferences.class);
166         final PrometheusDBConfig myConfig = PrometheusDBConfig.fromPrefs(myPrefs);
167         return new MoneyWiseDataStore(myConfig);
168     }
169 
170     /**
171      * Obtain a Database interface.
172      *
173      * @return new DataSet
174      */
175     @Override
176     public PrometheusSpreadSheet getSpreadSheet() {
177         return new MoneyWiseSheet(getGuiFactory());
178     }
179 
180     /**
181      * Update the data for a view.
182      *
183      * @param pData the new data set
184      */
185     @Override
186     public final void setData(final PrometheusDataSet pData) {
187         /* Record the data */
188         theData = (MoneyWiseDataSet) pData;
189         super.setData(pData);
190 
191         if (theMaps != null) {
192             theMaps.adjustForDataSet(pData);
193         }
194     }
195 
196     @Override
197     public MoneyWiseDataSet getData() {
198         return theData;
199     }
200 
201     /**
202      * Analyse the data.
203      *
204      * @param pData the data
205      * @return the analysis
206      * @throws OceanusException on error
207      */
208     public final MoneyWiseAnalysisTransAnalyser analyseData(final MoneyWiseDataSet pData) throws OceanusException {
209         /* Obtain the active profile */
210         OceanusProfile myTask = getActiveTask();
211         myTask = myTask.startTask("analyseData");
212 
213         /* Update the maps */
214         myTask.startTask("updateMaps");
215         pData.updateMaps();
216 
217         /* Create the analysis */
218         final PrometheusEditSet myEditSet = new PrometheusEditSet(this);
219         final MoneyWiseAnalysisTransAnalyser myAnalyser = new MoneyWiseAnalysisTransAnalyser(myTask, myEditSet, getPreferenceManager());
220 
221         /* Post process the analysis */
222         myAnalyser.postProcessAnalysis();
223 
224         /* Complete the task */
225         myTask.end();
226 
227         /* Return the analyser */
228         return myAnalyser;
229     }
230 
231     @Override
232     protected boolean analyseData(final boolean bPreserve) {
233         /* Obtain the active profile */
234         OceanusProfile myTask = getActiveTask();
235         myTask = myTask.startTask("analyseTheData");
236 
237         /* Clear the errors */
238         if (!bPreserve) {
239             clearErrors();
240         }
241 
242         /* Protect against exceptions */
243         try {
244             /* Analyse the data */
245             final MoneyWiseAnalysisTransAnalyser myAnalyser = analyseData(theData);
246             final MoneyWiseAnalysis myAnalysis = myAnalyser.getAnalysis();
247             theAnalysisMgr = new MoneyWiseAnalysisManager(myAnalysis);
248 
249             /* Analyse the basic ranged analysis */
250             myTask.startTask("AnalyseBase");
251             theAnalysisMgr.analyseBase();
252             hasMultiCurrency = theAnalysisMgr.haveForeignCurrency();
253             hasActiveSecurities = theAnalysisMgr.haveActiveSecurities();
254 
255             /* Update the Data entry */
256             final MetisViewerEntry myData = getViewerEntry(PrometheusViewerEntryId.ANALYSIS);
257             myData.setTreeObject(theAnalysisMgr);
258 
259             /* Derive the update Set */
260             myTask.startTask("deriveUpdates");
261             deriveUpdates();
262 
263             /* Catch any exceptions */
264         } catch (OceanusException e) {
265             if (!bPreserve) {
266                 addError(e);
267             }
268         }
269 
270         /* Complete the task */
271         myTask.end();
272 
273         /* Return whether there was success */
274         return getErrors().isEmpty();
275     }
276 
277     /**
278      * Set the reporting currency.
279      *
280      * @param pCurrency the new reporting currency
281      */
282     public void setReportingCurrency(final MoneyWiseCurrency pCurrency) {
283         /* Set default currency in AccountCurrencies */
284         theData.getAccountCurrencies().setReportingCurrency(pCurrency);
285 
286         /* Set default currency in ExchangeRates */
287         theData.getExchangeRates().setReportingCurrency(pCurrency);
288 
289         /* Register the changes */
290         incrementVersion();
291     }
292 }