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.lethe.reports;
18  
19  import java.util.EnumMap;
20  import java.util.Map;
21  
22  import org.w3c.dom.Document;
23  
24  import io.github.tonywasher.joceanus.metis.report.MetisReportBase;
25  import io.github.tonywasher.joceanus.metis.report.MetisReportManager;
26  import io.github.tonywasher.joceanus.moneywise.lethe.data.analysis.data.MoneyWiseAnalysis;
27  import io.github.tonywasher.joceanus.moneywise.lethe.data.analysis.data.MoneyWiseAnalysisDataResource;
28  import io.github.tonywasher.joceanus.moneywise.lethe.data.analysis.data.MoneyWiseAnalysisSecurityBucket;
29  import io.github.tonywasher.joceanus.moneywise.lethe.data.analysis.values.MoneyWiseAnalysisValuesResource;
30  import io.github.tonywasher.joceanus.moneywise.lethe.views.MoneyWiseAnalysisFilter;
31  
32  /**
33   * Report Classes.
34   *
35   * @author Tony Washer
36   */
37  public class MoneyWiseReportBuilder {
38      /**
39       * The Total text.
40       */
41      protected static final String TEXT_TOTAL = MoneyWiseAnalysisDataResource.ANALYSIS_TOTALS.getValue();
42  
43      /**
44       * The Profit text.
45       */
46      protected static final String TEXT_PROFIT = MoneyWiseAnalysisValuesResource.SECURITYATTR_PROFIT.getValue();
47  
48      /**
49       * The Income text.
50       */
51      protected static final String TEXT_INCOME = MoneyWiseAnalysisValuesResource.PAYEEATTR_INCOME.getValue();
52  
53      /**
54       * The Expense text.
55       */
56      protected static final String TEXT_EXPENSE = MoneyWiseAnalysisValuesResource.PAYEEATTR_EXPENSE.getValue();
57  
58      /**
59       * The Report Manager.
60       */
61      private final MetisReportManager<MoneyWiseAnalysisFilter<?, ?>> theManager;
62  
63      /**
64       * Map of allocated reports.
65       */
66      private final Map<MoneyWiseReportType, MetisReportBase<MoneyWiseAnalysis, MoneyWiseAnalysisFilter<?, ?>>> theReportMap;
67  
68      /**
69       * Constructor.
70       *
71       * @param pManager the report manager
72       */
73      public MoneyWiseReportBuilder(final MetisReportManager<MoneyWiseAnalysisFilter<?, ?>> pManager) {
74          /* Record the details */
75          theManager = pManager;
76  
77          /* Allocate map */
78          theReportMap = new EnumMap<>(MoneyWiseReportType.class);
79      }
80  
81      /**
82       * Build a report of the appropriate type.
83       *
84       * @param pAnalysis the analysis
85       * @param pType     the report type
86       * @param pSecurity the security
87       * @return the Web document
88       */
89      public Document createReport(final MoneyWiseAnalysis pAnalysis,
90                                   final MoneyWiseReportType pType,
91                                   final MoneyWiseAnalysisSecurityBucket pSecurity) {
92          /* Access existing report */
93          MetisReportBase<MoneyWiseAnalysis, MoneyWiseAnalysisFilter<?, ?>> myReport = theReportMap.get(pType);
94  
95          /* If we have not previously allocated this report */
96          if (myReport == null) {
97              /* Switch on the report type */
98              switch (pType) {
99                  case NETWORTH:
100                     myReport = new MoneyWiseReportNetWorth(theManager);
101                     break;
102                 case BALANCESHEET:
103                     myReport = new MoneyWiseReportBalanceSheet(theManager);
104                     break;
105                 case CASHFLOW:
106                     myReport = new MoneyWiseReportCashFlow(theManager);
107                     break;
108                 case INCOMEEXPENSE:
109                     myReport = new MoneyWiseReportIncomeExpense(theManager);
110                     break;
111                 case PORTFOLIO:
112                     myReport = new MoneyWiseReportPortfolioView(theManager);
113                     break;
114                 case MARKETGROWTH:
115                     myReport = new MoneyWiseReportMarketGrowth(theManager);
116                     break;
117                 case TAXBASIS:
118                     myReport = new MoneyWiseReportTaxationBasis(theManager);
119                     break;
120                 case TAXCALC:
121                     myReport = new MoneyWiseReportTaxCalculation(theManager);
122                     break;
123                 case ASSETGAINS:
124                     myReport = new MoneyWiseReportAssetGains(theManager);
125                     break;
126                 case CAPITALGAINS:
127                     myReport = new MoneyWiseReportCapitalGains(theManager);
128                     break;
129                 default:
130                     return null;
131             }
132 
133             /* Store allocated report */
134             theReportMap.put(pType, myReport);
135         }
136 
137         /* If the report requires the security */
138         if (myReport instanceof MoneyWiseReportCapitalGains) {
139             ((MoneyWiseReportCapitalGains) myReport).setSecurity(pSecurity);
140         }
141 
142         /* Set up the report */
143         theManager.setReport(myReport);
144 
145         /* Create the report */
146         return myReport.createReport(pAnalysis);
147     }
148 }