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 io.github.tonywasher.joceanus.oceanus.date.OceanusDate;
20  import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
21  import io.github.tonywasher.joceanus.metis.report.MetisReportBase;
22  import io.github.tonywasher.joceanus.metis.report.MetisReportHTMLBuilder;
23  import io.github.tonywasher.joceanus.metis.report.MetisReportHTMLBuilder.MetisHTMLTable;
24  import io.github.tonywasher.joceanus.metis.report.MetisReportManager;
25  import io.github.tonywasher.joceanus.metis.report.MetisReportReferenceManager.DelayedTable;
26  import io.github.tonywasher.joceanus.moneywise.lethe.data.analysis.data.MoneyWiseAnalysis;
27  import io.github.tonywasher.joceanus.moneywise.lethe.data.analysis.data.MoneyWiseAnalysisPortfolioBucket;
28  import io.github.tonywasher.joceanus.moneywise.lethe.data.analysis.data.MoneyWiseAnalysisPortfolioBucket.MoneyWiseAnalysisPortfolioBucketList;
29  import io.github.tonywasher.joceanus.moneywise.lethe.data.analysis.data.MoneyWiseAnalysisSecurityBucket;
30  import io.github.tonywasher.joceanus.moneywise.lethe.data.analysis.data.MoneyWiseAnalysisSecurityBucket.MoneyWiseAnalysisSecurityBucketList;
31  import io.github.tonywasher.joceanus.moneywise.lethe.data.analysis.values.MoneyWiseAnalysisSecurityAttr;
32  import io.github.tonywasher.joceanus.moneywise.lethe.data.analysis.values.MoneyWiseAnalysisSecurityValues;
33  import io.github.tonywasher.joceanus.moneywise.lethe.data.analysis.values.MoneyWiseAnalysisValuesResource;
34  import io.github.tonywasher.joceanus.moneywise.lethe.views.MoneyWiseAnalysisFilter;
35  import io.github.tonywasher.joceanus.moneywise.lethe.views.MoneyWiseAnalysisFilter.MoneyWiseAnalysisSecurityFilter;
36  import org.w3c.dom.Document;
37  import org.w3c.dom.Element;
38  
39  import java.util.Iterator;
40  
41  /**
42   * AssetGains report builder.
43   */
44  public class MoneyWiseReportAssetGains
45          extends MetisReportBase<MoneyWiseAnalysis, MoneyWiseAnalysisFilter<?, ?>> {
46      /**
47       * The Title text.
48       */
49      private static final String TEXT_TITLE = MoneyWiseReportResource.ASSETGAINS_TITLE.getValue();
50  
51      /**
52       * The Cost text.
53       */
54      private static final String TEXT_COST = MoneyWiseAnalysisValuesResource.SECURITYATTR_RESIDUALCOST.getValue();
55  
56      /**
57       * The Value text.
58       */
59      private static final String TEXT_VALUE = MoneyWiseAnalysisValuesResource.ACCOUNTATTR_VALUATION.getValue();
60  
61      /**
62       * The Gains text.
63       */
64      private static final String TEXT_GAINS = MoneyWiseAnalysisValuesResource.SECURITYATTR_REALISEDGAINS.getValue();
65  
66      /**
67       * HTML builder.
68       */
69      private final MetisReportHTMLBuilder theBuilder;
70  
71      /**
72       * The Formatter.
73       */
74      private final OceanusDataFormatter theFormatter;
75  
76      /**
77       * Constructor.
78       *
79       * @param pManager the Report Manager
80       */
81      protected MoneyWiseReportAssetGains(final MetisReportManager<MoneyWiseAnalysisFilter<?, ?>> pManager) {
82          /* Access underlying utilities */
83          theBuilder = pManager.getBuilder();
84          theFormatter = theBuilder.getDataFormatter();
85      }
86  
87      @Override
88      public Document createReport(final MoneyWiseAnalysis pAnalysis) {
89          /* Access the bucket lists */
90          final MoneyWiseAnalysisPortfolioBucketList myPortfolios = pAnalysis.getPortfolios();
91          final OceanusDate myDate = pAnalysis.getDateRange().getEnd();
92  
93          /* Access the totals */
94          final MoneyWiseAnalysisPortfolioBucket myTotals = myPortfolios.getTotals();
95  
96          /* Start the report */
97          final Element myBody = theBuilder.startReport();
98          theBuilder.makeTitle(myBody, TEXT_TITLE, theFormatter.formatObject(myDate));
99  
100         /* Initialise the table */
101         final MetisHTMLTable myTable = theBuilder.startTable(myBody);
102         theBuilder.startHdrRow(myTable);
103         theBuilder.makeTitleCell(myTable);
104         theBuilder.makeTitleCell(myTable, TEXT_VALUE);
105         theBuilder.makeTitleCell(myTable, TEXT_COST);
106         theBuilder.makeTitleCell(myTable, TEXT_GAINS);
107 
108         /* Loop through the Portfolio Buckets */
109         final Iterator<MoneyWiseAnalysisPortfolioBucket> myIterator = myPortfolios.iterator();
110         while (myIterator.hasNext()) {
111             final MoneyWiseAnalysisPortfolioBucket myBucket = myIterator.next();
112 
113             /* Access bucket name */
114             final String myName = myBucket.getName();
115 
116             /* Access values */
117             final MoneyWiseAnalysisSecurityValues myValues = myBucket.getValues();
118 
119             /* Format the Asset */
120             theBuilder.startRow(myTable);
121             theBuilder.makeDelayLinkCell(myTable, myName);
122 
123             /* Handle values bucket value */
124             theBuilder.makeValueCell(myTable, myBucket.getNonCashValue(false));
125             theBuilder.makeValueCell(myTable, myValues.getMoneyValue(MoneyWiseAnalysisSecurityAttr.RESIDUALCOST));
126             theBuilder.makeValueCell(myTable, myValues.getMoneyValue(MoneyWiseAnalysisSecurityAttr.REALISEDGAINS));
127 
128             /* Note the delayed subTable */
129             setDelayedTable(myName, myTable, myBucket);
130         }
131 
132         /* Access values */
133         final MoneyWiseAnalysisSecurityValues myValues = myTotals.getValues();
134 
135         /* Create the total row */
136         theBuilder.startTotalRow(myTable);
137         theBuilder.makeTitleCell(myTable, MoneyWiseReportBuilder.TEXT_TOTAL);
138         theBuilder.makeTotalCell(myTable, myTotals.getNonCashValue(false));
139         theBuilder.makeTotalCell(myTable, myValues.getMoneyValue(MoneyWiseAnalysisSecurityAttr.RESIDUALCOST));
140         theBuilder.makeTotalCell(myTable, myValues.getMoneyValue(MoneyWiseAnalysisSecurityAttr.REALISEDGAINS));
141 
142         /* Return the document */
143         return theBuilder.getDocument();
144     }
145 
146     @Override
147     public MetisHTMLTable createDelayedTable(final DelayedTable pTable) {
148         /* Access the source */
149         final Object mySource = pTable.getSource();
150         if (mySource instanceof MoneyWiseAnalysisPortfolioBucket mySourceBucket) {
151             return createDelayedPortfolio(pTable.getParent(), mySourceBucket);
152         }
153 
154         /* Return the null table */
155         return null;
156     }
157 
158     /**
159      * Create a delayed portfolio table.
160      *
161      * @param pParent the parent table
162      * @param pSource the source bucket
163      * @return the new document fragment
164      */
165     private MetisHTMLTable createDelayedPortfolio(final MetisHTMLTable pParent,
166                                                   final MoneyWiseAnalysisPortfolioBucket pSource) {
167         /* Access the securities and portfolio */
168         final MoneyWiseAnalysisSecurityBucketList mySecurities = pSource.getSecurities();
169 
170         /* Create a new table */
171         final MetisHTMLTable myTable = theBuilder.createEmbeddedTable(pParent);
172 
173         /* Loop through the Security Buckets */
174         final Iterator<MoneyWiseAnalysisSecurityBucket> myIterator = mySecurities.iterator();
175         while (myIterator.hasNext()) {
176             final MoneyWiseAnalysisSecurityBucket myBucket = myIterator.next();
177 
178             /* Access bucket name */
179             final String myName = myBucket.getSecurityName();
180             String myFullName = myBucket.getDecoratedName();
181             myFullName = myFullName.replace(':', '-');
182 
183             /* Access values */
184             final MoneyWiseAnalysisSecurityValues myValues = myBucket.getValues();
185 
186             /* Create the detail row */
187             theBuilder.startRow(myTable);
188             theBuilder.makeFilterLinkCell(myTable, myFullName, myName);
189             theBuilder.makeValueCell(myTable, myValues.getMoneyValue(MoneyWiseAnalysisSecurityAttr.VALUATION));
190             theBuilder.makeValueCell(myTable, myValues.getMoneyValue(MoneyWiseAnalysisSecurityAttr.RESIDUALCOST));
191             theBuilder.makeValueCell(myTable, myValues.getMoneyValue(MoneyWiseAnalysisSecurityAttr.REALISEDGAINS));
192 
193             /* Record the filter */
194             setFilterForId(myFullName, myBucket);
195         }
196 
197         /* Return the table */
198         return myTable;
199     }
200 
201     @Override
202     public MoneyWiseAnalysisFilter<?, ?> processFilter(final Object pSource) {
203         if (pSource instanceof MoneyWiseAnalysisSecurityBucket mySource) {
204             /* Create the new filter */
205             return new MoneyWiseAnalysisSecurityFilter(mySource);
206         }
207         return null;
208     }
209 }