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