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.atlas.reports;
18  
19  import io.github.tonywasher.joceanus.metis.report.MetisReportBase;
20  import io.github.tonywasher.joceanus.metis.report.MetisReportManager;
21  import io.github.tonywasher.joceanus.moneywise.atlas.data.analysis.buckets.MoneyWiseXAnalysis;
22  import io.github.tonywasher.joceanus.moneywise.atlas.data.analysis.buckets.MoneyWiseXAnalysisSecurityBucket;
23  import io.github.tonywasher.joceanus.moneywise.atlas.views.MoneyWiseXAnalysisFilter;
24  import org.w3c.dom.Document;
25  
26  import java.util.EnumMap;
27  import java.util.Map;
28  
29  /**
30   * Report Classes.
31   *
32   * @author Tony Washer
33   */
34  public class MoneyWiseXReportBuilder {
35      /**
36       * The Report Manager.
37       */
38      private final MetisReportManager<MoneyWiseXAnalysisFilter<?, ?>> theManager;
39  
40      /**
41       * Map of allocated reports.
42       */
43      private final Map<MoneyWiseXReportType, MetisReportBase<MoneyWiseXAnalysis, MoneyWiseXAnalysisFilter<?, ?>>> theReportMap;
44  
45      /**
46       * Constructor.
47       *
48       * @param pManager the report manager
49       */
50      public MoneyWiseXReportBuilder(final MetisReportManager<MoneyWiseXAnalysisFilter<?, ?>> pManager) {
51          /* Record the details */
52          theManager = pManager;
53  
54          /* Allocate map */
55          theReportMap = new EnumMap<>(MoneyWiseXReportType.class);
56      }
57  
58      /**
59       * Build a report of the appropriate type.
60       *
61       * @param pAnalysis the analysis
62       * @param pType     the report type
63       * @param pSecurity the security
64       * @return the Web document
65       */
66      public Document createReport(final MoneyWiseXAnalysis pAnalysis,
67                                   final MoneyWiseXReportType pType,
68                                   final MoneyWiseXAnalysisSecurityBucket pSecurity) {
69          /* Access existing report */
70          MetisReportBase<MoneyWiseXAnalysis, MoneyWiseXAnalysisFilter<?, ?>> myReport = theReportMap.get(pType);
71  
72          /* If we have not previously allocated this report */
73          if (myReport == null) {
74              /* Switch on the report type */
75              myReport = switch (pType) {
76                  case NETWORTH -> new MoneyWiseXReportNetWorth(theManager);
77                  case BALANCESHEET -> new MoneyWiseXReportBalanceSheet(theManager);
78                  case CASHFLOW -> new MoneyWiseXReportCashFlow(theManager);
79                  case INCOMEEXPENSE -> new MoneyWiseXReportIncomeExpense(theManager);
80                  case PORTFOLIO -> new MoneyWiseXReportPortfolioView(theManager);
81                  case MARKETGROWTH -> new MoneyWiseXReportMarketGrowth(theManager);
82                  case TAXBASIS -> new MoneyWiseXReportTaxationBasis(theManager);
83                  case TAXCALC -> new MoneyWiseXReportTaxCalculation(theManager);
84                  case ASSETGAINS -> new MoneyWiseXReportAssetGains(theManager);
85                  case CAPITALGAINS -> new MoneyWiseXReportCapitalGains(theManager);
86                  default -> null;
87              };
88  
89              /* Return null if no report found */
90              if (myReport == null) {
91                  return null;
92              }
93  
94              /* Store allocated report */
95              theReportMap.put(pType, myReport);
96          }
97  
98          /* If the report requires the security */
99          if (myReport instanceof MoneyWiseXReportCapitalGains myCapGains) {
100             myCapGains.setSecurity(pSecurity);
101         }
102 
103         /* Set up the report */
104         theManager.setReport(myReport);
105 
106         /* Create the report */
107         return myReport.createReport(pAnalysis);
108     }
109 }