1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.moneywise.views;
18
19 import io.github.tonywasher.joceanus.oceanus.date.OceanusDate;
20 import io.github.tonywasher.joceanus.oceanus.decimal.OceanusRatio;
21 import io.github.tonywasher.joceanus.metis.data.MetisDataEditState;
22 import io.github.tonywasher.joceanus.metis.data.MetisDataState;
23 import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
24 import io.github.tonywasher.joceanus.metis.field.MetisFieldVersionValues;
25 import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseBasicDataType;
26 import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseBasicResource;
27 import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseDataSet;
28 import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseExchangeRate;
29 import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseCurrency;
30 import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseCurrency.MoneyWiseCurrencyList;
31 import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseStaticDataType;
32 import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
33 import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataValues;
34
35 import java.util.Iterator;
36 import java.util.ListIterator;
37
38
39
40
41 public final class MoneyWiseSpotExchangeRate
42 extends MoneyWiseExchangeRate {
43
44
45
46 public static final String OBJECT_NAME = MoneyWiseSpotExchangeRate.class.getSimpleName();
47
48
49
50
51 public static final String LIST_NAME = OBJECT_NAME + "s";
52
53
54
55
56 @SuppressWarnings("rawtypes")
57 private static final MetisFieldSet<MoneyWiseSpotExchangeRate> FIELD_DEFS = MetisFieldSet.newFieldSet(MoneyWiseSpotExchangeRate.class);
58
59
60
61
62 static {
63 FIELD_DEFS.declareLocalField(MoneyWiseViewResource.SPOTEVENT_PREVDATE, MoneyWiseSpotExchangeRate::getPrevDate);
64 FIELD_DEFS.declareLocalField(MoneyWiseViewResource.SPOTRATE_PREVRATE, MoneyWiseSpotExchangeRate::getPrevRate);
65 }
66
67
68
69
70 private OceanusDate thePrevDate;
71
72
73
74
75 private OceanusRatio thePrevRate;
76
77
78
79
80
81
82
83 private MoneyWiseSpotExchangeRate(final MoneyWiseSpotExchangeList pList,
84 final MoneyWiseCurrency pCurrency) {
85 super(pList);
86
87
88 setDate(pList.theDate);
89 setFromCurrency(pList.getCurrency());
90 setToCurrency(pCurrency);
91 }
92
93 @Override
94 public MetisFieldSetDef getDataFieldSet() {
95 return FIELD_DEFS;
96 }
97
98
99
100
101
102
103 public OceanusRatio getPrevRate() {
104 return thePrevRate;
105 }
106
107
108
109
110
111
112 public OceanusDate getPrevDate() {
113 return thePrevDate;
114 }
115
116
117
118
119 @Override
120 public void validate() {
121 setValidEdit();
122 }
123
124
125 @Override
126 public boolean isLocked() {
127 return isDeleted();
128 }
129
130
131
132
133 @Override
134 public void setValidEdit() {
135 setEditState(hasHistory()
136 ? MetisDataEditState.VALID
137 : MetisDataEditState.CLEAN);
138 }
139
140 @Override
141 public OceanusRatio getExchangeRate() {
142
143 switch (getState()) {
144 case NEW:
145 case CHANGED:
146 case RECOVERED:
147 case CLEAN:
148 return super.getExchangeRate();
149 default:
150 return null;
151 }
152 }
153
154 @Override
155 public MetisDataState getState() {
156 final MetisFieldVersionValues myCurr = getValues();
157 final MetisFieldVersionValues myBase = getOriginalValues();
158
159
160 if (myCurr.getVersion() == 0) {
161 return MetisDataState.CLEAN;
162 }
163
164
165 if (myBase.getValue(MoneyWiseBasicResource.XCHGRATE_RATE) == null) {
166
167 return myCurr.getValue(MoneyWiseBasicResource.XCHGRATE_RATE) == null
168 ? MetisDataState.DELNEW
169 : MetisDataState.NEW;
170 }
171
172
173 return myCurr.getValue(MoneyWiseBasicResource.XCHGRATE_RATE) == null
174 ? MetisDataState.DELETED
175 : MetisDataState.CHANGED;
176 }
177
178
179
180
181 public static class MoneyWiseSpotExchangeList
182 extends MoneyWiseExchangeRateBaseList<MoneyWiseSpotExchangeRate> {
183
184
185
186 @SuppressWarnings("rawtypes")
187 private static final MetisFieldSet<MoneyWiseSpotExchangeList> FIELD_DEFS = MetisFieldSet.newFieldSet(MoneyWiseSpotExchangeList.class);
188
189
190
191
192 static {
193 FIELD_DEFS.declareLocalField(MoneyWiseStaticDataType.CURRENCY, MoneyWiseSpotExchangeList::getCurrency);
194 FIELD_DEFS.declareLocalField(MoneyWiseBasicResource.MONEYWISEDATA_FIELD_DATE, MoneyWiseSpotExchangeList::getDate);
195 FIELD_DEFS.declareLocalField(MoneyWiseViewResource.SPOTEVENT_NEXTDATE, MoneyWiseSpotExchangeList::getNext);
196 FIELD_DEFS.declareLocalField(MoneyWiseViewResource.SPOTEVENT_PREVDATE, MoneyWiseSpotExchangeList::getPrev);
197 }
198
199
200
201
202 private final OceanusDate theDate;
203
204
205
206
207 private final MoneyWiseView theView;
208
209
210
211
212 private final MoneyWiseCurrency theCurrency;
213
214
215
216
217 private OceanusDate theNext;
218
219
220
221
222 private OceanusDate thePrev;
223
224
225
226
227
228
229
230 public MoneyWiseSpotExchangeList(final MoneyWiseView pView,
231 final OceanusDate pDate) {
232
233 super(pView.getData(), MoneyWiseSpotExchangeRate.class, MoneyWiseBasicDataType.SECURITYPRICE);
234 setStyle(PrometheusListStyle.EDIT);
235 ensureMap();
236
237
238 theDate = pDate;
239 theView = pView;
240
241
242 final MoneyWiseDataSet myData = theView.getData();
243 theCurrency = myData.getReportingCurrency();
244 final MoneyWiseCurrencyList myCurrencies = myData.getAccountCurrencies();
245
246
247 final OceanusDate myDate = new OceanusDate(theDate);
248 final Iterator<MoneyWiseCurrency> myCurIterator = myCurrencies.iterator();
249 while (myCurIterator.hasNext()) {
250 final MoneyWiseCurrency myCurrency = myCurIterator.next();
251
252
253 boolean bIgnore = myCurrency.isDeleted() || myCurrency.isDisabled();
254 bIgnore |= myCurrency.equals(theCurrency);
255 if (bIgnore) {
256 continue;
257 }
258
259
260 final MoneyWiseSpotExchangeRate mySpot = new MoneyWiseSpotExchangeRate(this, myCurrency);
261 mySpot.setIndexedId(myCurrency.getIndexedId());
262 mySpot.setDate(myDate);
263 add(mySpot);
264 }
265
266
267 final MoneyWiseExchangeRateList myRates = myData.getExchangeRates();
268 setBase(myRates);
269
270
271 final ListIterator<MoneyWiseExchangeRate> myIterator = myRates.listIterator(myRates.size());
272 while (myIterator.hasPrevious()) {
273 final MoneyWiseExchangeRate myRate = myIterator.previous();
274
275
276 if (myRate.isDeleted()) {
277 continue;
278 }
279
280
281 final int iDiff = theDate.compareTo(myRate.getDate());
282
283
284 if (iDiff < 0) {
285
286 theNext = myRate.getDate();
287 break;
288 }
289
290
291 final MoneyWiseCurrency myCurrency = myRate.getToCurrency();
292 final MoneyWiseSpotExchangeRate mySpot = findItemById(myCurrency.getIndexedId());
293 if (mySpot == null) {
294 continue;
295 }
296
297
298 if (iDiff == 0) {
299
300 mySpot.setValueExchangeRate(myRate.getExchangeRate());
301
302
303 mySpot.setBase(myRate);
304
305
306 } else {
307
308 mySpot.thePrevDate = myRate.getDate();
309 mySpot.thePrevRate = myRate.getExchangeRate();
310
311
312 thePrev = myRate.getDate();
313 }
314 }
315 }
316
317 @SuppressWarnings("rawtypes")
318 @Override
319 public MetisFieldSet<MoneyWiseSpotExchangeList> getDataFieldSet() {
320 return FIELD_DEFS;
321 }
322
323 @Override
324 public String listName() {
325 return MoneyWiseSpotExchangeList.class.getSimpleName();
326 }
327
328 @Override
329 public MetisFieldSetDef getItemFields() {
330 return MoneyWiseSpotExchangeRate.FIELD_DEFS;
331 }
332
333 @Override
334 protected MoneyWiseSpotExchangeList getEmptyList(final PrometheusListStyle pStyle) {
335 throw new UnsupportedOperationException();
336 }
337
338 @Override
339 public MoneyWiseDataSet getDataSet() {
340 return (MoneyWiseDataSet) super.getDataSet();
341 }
342
343
344
345
346
347
348 private MoneyWiseCurrency getCurrency() {
349 return theCurrency;
350 }
351
352
353
354
355
356
357 private OceanusDate getDate() {
358 return theDate;
359 }
360
361
362
363
364
365
366 public OceanusDate getNext() {
367 return theNext;
368 }
369
370
371
372
373
374
375 public OceanusDate getPrev() {
376 return thePrev;
377 }
378
379
380 @Override
381 public boolean isLocked() {
382 return false;
383 }
384
385
386 @Override
387 public MoneyWiseSpotExchangeRate addCopyItem(final PrometheusDataItem pElement) {
388 throw new UnsupportedOperationException();
389 }
390
391 @Override
392 public MoneyWiseSpotExchangeRate addNewItem() {
393 throw new UnsupportedOperationException();
394 }
395
396 @Override
397 public MoneyWiseSpotExchangeRate addValuesItem(final PrometheusDataValues pValues) {
398 throw new UnsupportedOperationException();
399 }
400 }
401 }