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.sheets;
18  
19  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseBasicResource;
20  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseDataSet;
21  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseExchangeRate;
22  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
23  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataValues;
24  import io.github.tonywasher.joceanus.prometheus.sheets.PrometheusSheetDataItem;
25  import io.github.tonywasher.joceanus.prometheus.sheets.PrometheusSheetReader;
26  import io.github.tonywasher.joceanus.prometheus.sheets.PrometheusSheetWriter;
27  
28  /**
29   * SheetDataItem extension for ExchangeRate.
30   *
31   * @author Tony Washer
32   */
33  public final class MoneyWiseSheetExchangeRate
34          extends PrometheusSheetDataItem<MoneyWiseExchangeRate> {
35      /**
36       * NamedArea for Rates.
37       */
38      private static final String AREA_XCHGRATES = MoneyWiseExchangeRate.LIST_NAME;
39  
40      /**
41       * Date column.
42       */
43      private static final int COL_DATE = COL_ID + 1;
44  
45      /**
46       * From column.
47       */
48      private static final int COL_FROM = COL_DATE + 1;
49  
50      /**
51       * To column.
52       */
53      private static final int COL_TO = COL_FROM + 1;
54  
55      /**
56       * Rate column.
57       */
58      private static final int COL_RATE = COL_TO + 1;
59  
60      /**
61       * Constructor for loading a spreadsheet.
62       *
63       * @param pReader the spreadsheet reader
64       */
65      MoneyWiseSheetExchangeRate(final PrometheusSheetReader pReader) {
66          /* Call super constructor */
67          super(pReader, AREA_XCHGRATES);
68  
69          /* Access the Rates list */
70          final MoneyWiseDataSet myData = (MoneyWiseDataSet) pReader.getData();
71          setDataList(myData.getExchangeRates());
72      }
73  
74      /**
75       * Constructor for creating a spreadsheet.
76       *
77       * @param pWriter the spreadsheet writer
78       */
79      MoneyWiseSheetExchangeRate(final PrometheusSheetWriter pWriter) {
80          /* Call super constructor */
81          super(pWriter, AREA_XCHGRATES);
82  
83          /* Access the Rates list */
84          final MoneyWiseDataSet myData = (MoneyWiseDataSet) pWriter.getData();
85          setDataList(myData.getExchangeRates());
86      }
87  
88      @Override
89      protected PrometheusDataValues loadSecureValues() throws OceanusException {
90          /* Build data values */
91          final PrometheusDataValues myValues = getRowValues(MoneyWiseExchangeRate.OBJECT_NAME);
92          myValues.addValue(MoneyWiseBasicResource.MONEYWISEDATA_FIELD_DATE, loadDate(COL_DATE));
93          myValues.addValue(MoneyWiseBasicResource.XCHGRATE_FROM, loadInteger(COL_FROM));
94          myValues.addValue(MoneyWiseBasicResource.XCHGRATE_TO, loadInteger(COL_TO));
95          myValues.addValue(MoneyWiseBasicResource.XCHGRATE_RATE, loadRatio(COL_RATE));
96  
97          /* Return the values */
98          return myValues;
99      }
100 
101     @Override
102     protected void insertSecureItem(final MoneyWiseExchangeRate pItem) throws OceanusException {
103         /* Set the fields */
104         super.insertSecureItem(pItem);
105         writeInteger(COL_FROM, pItem.getFromCurrencyId());
106         writeInteger(COL_TO, pItem.getToCurrencyId());
107         writeDate(COL_DATE, pItem.getDate());
108         writeDecimal(COL_RATE, pItem.getExchangeRate());
109     }
110 
111     @Override
112     protected int getLastColumn() {
113         /* Return the last column */
114         return COL_RATE;
115     }
116 }