Package org.cyclos.impl.banking
Class BaseRateTypeHandlerImpl<T,R extends RateParameters,D extends RateParametersDTO>
- java.lang.Object
-
- org.cyclos.impl.AbstractServerComponent
-
- org.cyclos.impl.AbstractNetworkedServerComponent
-
- org.cyclos.impl.BaseNetworkedHandlerImpl
-
- org.cyclos.impl.banking.BaseRateTypeHandlerImpl<T,R,D>
-
- All Implemented Interfaces:
RateTypeHandler<T,R,D>
- Direct Known Subclasses:
TimeBasedRateTypeHandlerImpl
public abstract class BaseRateTypeHandlerImpl<T,R extends RateParameters,D extends RateParametersDTO> extends BaseNetworkedHandlerImpl implements RateTypeHandler<T,R,D>
Implementation for RateTypeHandler; each rate type has it's own child of this one.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classBaseRateTypeHandlerImpl.RateParametersChangeActionThe action status for the RateParameters of the currency when saving the currency.
-
Field Summary
-
Fields inherited from class org.cyclos.impl.AbstractNetworkedServerComponent
authHandler, conversionHandler, customFieldValueHandler, entityManagerHandler, groupsHandler, productsHandler, restBeanPropertyMapping, userLocatorHandler
-
Fields inherited from class org.cyclos.impl.AbstractServerComponent
accountHandler, configurationHandler, dataTranslationHandler, notificationHandler, profileFieldHandler, transactionHandler, translationHandler
-
Fields inherited from interface org.cyclos.impl.banking.RateTypeHandler
RATE_SCALE
-
-
Constructor Summary
Constructors Constructor Description BaseRateTypeHandlerImpl()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract TadjustIncomingRawRateInTransfer(T incomingRawRate, Transfer transfer)adjusts a rate as a result of the transaction.voidapplyIncomingTransfer(RatesData accountRates, Transfer transfer)Applies the incoming transfer.protected abstract voidassignEntityToCurrency(R entity, Currency currency)Assigns the RateParameters entity to the currency, so that the RateParameters entity becomes the new active RateParameters for this currency.protected abstract voidassignValidator(Validator currencyValidator, Validator rateParametersValidator)sets specific checks to the RateParametersValidator, and assigns it as nested Validator to the currencyValidator.voidcalculateFromRates(FromRatesData rates, Account fromAccount, TransferType transferType, BigDecimal amount)Adapts the from account rates after it has been retrieved from the account, and adapts the fields to specific additional rate rules.protected <C> Cconvert(Class<C> toType, Object object)quick link to convertionHandler.convertprotected abstract com.querydsl.core.types.PathgetAccountRatesFieldPath()gets the path for the relevant field of the QAccountRates query.BigDecimalgetCreationValue(Currency currency, Date date)Gets the creation value of the rate, that is, the value which is assigned for this rate at unit creation.protected abstract RgetCurrentRateParameters(Currency currency)DategetEnableDate(Currency currency, Date requestedDate)Gets the earliest date of first enabling the rate.protected abstract Class<R>getEntityClass()gets the RateParameters classprotected abstract List<R>getParametersList()protected abstract QRateParametersgetQuery()gets the QXRateParameters.xRateParameters of QueryDSL as a view of itself as the parent type.RgetRateParameters(Currency currency, Date date)Gets the RateParameters at a certain time by querying the db.protected booleanhasEqualProperties(R entity, D dto)checks if the RateParameters entity has equal property values compared to the dto, in order to determine if it has been updated.protected voidimmediateDisableActions(Currency currency)does immediate disable actions needed when the rate is disabled.TinverseMerge(RatedCurrencyAmount<T> previousSetOfUnits, RatedCurrencyAmount<T> transferSetOfUnits, Date date)does an inverse merge, that is: when a set of units is distracted from an account, and when the distracted set has a rate different from the account rate, then the remaining sum on the account must do an inverseMerge to calculate the rate of the remainder.booleanisEnabled(Currency currency, Date date)Checks if the rate is enabled on the given datebooleanisVisible(Account account, Date date, RateVisibility visibility)Determines if the rate is visible, depending on the visibility param.booleanisVisibleToUser(Currency currency, Date date)Returns true if the rate is visible to the user on the given date.Tmerge(RatedCurrencyAmount<T> setOfUnits1, RatedCurrencyAmount<T> setOfUnits2, Date date)Does the actual calculation of the rate from a merge (where a set of units enters an account).longnullAllAccountRates(Currency currency)Sets all rates on all accounts explicitly to null (for the actual rate type belonging to this RateTypeHandler).voidresolveValidator(Validator validator, CurrencyDTO dto)Resolves the validator for the RateParameters as nested validator for CurrencyDTO.voidtoEntity(Currency currency, D dto)Sets the rateParameters from the dto to the entity.-
Methods inherited from class org.cyclos.impl.AbstractNetworkedServerComponent
canManage, checkManagesUser, checkPermission, checkRelatesToUser, checkValue, clearAlreadyValidated, getBaseEntityManagerHandler, getConfiguration, getLoggedBasicUser, getLoggedUser, getProducts, getTranslatedName, getTranslatedValue, hasPermission, hasValue, inSameNetwork, inSameNetworkOrGlobal, isAdmin, isAlreadyValidated, isBroker, isGlobalAdmin, isGlobalAdminInNetwork, isGuest, isLoggedIn, isMember, isMemberOnly, isNetworkAdmin, isOperator, isRelatedToUser, isSystem, isUserManager, isUserManagerOf, message, message, permission, permission, permissionOptionalValue, permissionOptionalValue, toDate, toDateTime, validate
-
Methods inherited from class org.cyclos.impl.AbstractServerComponent
dataTranslationProxy, delete, detach, find, flush, from, getApplicationContext, getFormatter, getFormatter, getFormatter, getLogger, getRemoteAddress, getSessionData, mailContentBuilder, message, message, persist, processBatch, processBatch, refresh, remove, selectFrom, subQuery, update
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.cyclos.impl.banking.RateTypeHandler
complete, convert, convert, copy, getNullSafeRawRate, getRateParameters, getRateParametersDTO, getRateType, setRate, setRawRate
-
-
-
-
Method Detail
-
applyIncomingTransfer
public void applyIncomingTransfer(RatesData accountRates, Transfer transfer)
Description copied from interface:RateTypeHandlerApplies the incoming transfer. Chargebacks are treated as any other normal transfer, just according to the rules as described in the wiki. It does the following:
* checks if enabled and if not unlimited account
* gets the incoming rate from the transfer
* gets the account rate from the accountRates Param
* merges those two, where the accountRates param's virtualRatedBalance field is the amount of the account
* sets the new raw rate evolving from this merge to the accountRates parameter.
* Note that only the raw rate is adapted; the normal (BigDecimal) rate is not updated, because it is not used when persisting it.- Specified by:
applyIncomingTransferin interfaceRateTypeHandler<T,R extends RateParameters,D extends RateParametersDTO>- Parameters:
accountRates- a RatesVO with the present rates of the account on which the transfer is received. Also the virtualRatedBalance must be set on this. This parameter will be updated with the new merged rate value.transfer- The transfer which should be merged.
-
calculateFromRates
public void calculateFromRates(FromRatesData rates, Account fromAccount, TransferType transferType, BigDecimal amount)
Description copied from interface:RateTypeHandlerAdapts the from account rates after it has been retrieved from the account, and adapts the fields to specific additional rate rules. The rate balance correction and virtual rated balance are NOT adapted. The following rules are applied:- in case of D-rate: if the from account is unlimited and there is a creation value D-rate set on the transferType, this is used as the D-rate.
- in case of insufficient balance: the total amount is considered to consist of two sets: one set coming from
the from account, being the
complete balance and having the rates of the from accounts, and another set which is newly created against the
rate creation values, and being
the transaction amount minus the balance. These sets are merged in order to get the rates to be used for the
transfer.
If there was insufficient balance, the FromRatesData.changedDueToCreation field is set to true.
- Specified by:
calculateFromRatesin interfaceRateTypeHandler<T,R extends RateParameters,D extends RateParametersDTO>- Parameters:
rates- - the rates as retrieved by requesting the rates from the from account, without considering the balance or the transfer type.
-
getCreationValue
public BigDecimal getCreationValue(Currency currency, Date date)
Description copied from interface:RateTypeHandlerGets the creation value of the rate, that is, the value which is assigned for this rate at unit creation.- Specified by:
getCreationValuein interfaceRateTypeHandler<T,R extends RateParameters,D extends RateParametersDTO>- Returns:
- the rate creation value as BigDecimal, but returns null if the rate is not enabled at the given date.
-
getEnableDate
public Date getEnableDate(Currency currency, Date requestedDate)
Description copied from interface:RateTypeHandlerGets the earliest date of first enabling the rate. The result is the start of the earliest rate enabling period to which the requestedDate also belongs.
Example: if A-rate was enabled for two months in 2003, and then disabled, and again enabled in 2012, and the requestedDate falls in the 2012 period, the start of the 2012 period is returned.- Specified by:
getEnableDatein interfaceRateTypeHandler<T,R extends RateParameters,D extends RateParametersDTO>- Parameters:
currency- - rates are set on this currencyrequestedDate- - the returned date and the requestedDate belong to the same rate enabling period without interrupt. However, rateParameters may have changed during that period.
-
getRateParameters
public R getRateParameters(Currency currency, Date date)
Description copied from interface:RateTypeHandlerGets the RateParameters at a certain time by querying the db. If the rate was not enabled at that time, it returns null.- Specified by:
getRateParametersin interfaceRateTypeHandler<T,R extends RateParameters,D extends RateParametersDTO>date- if null, present datetime is used.
-
inverseMerge
public T inverseMerge(RatedCurrencyAmount<T> previousSetOfUnits, RatedCurrencyAmount<T> transferSetOfUnits, Date date)
does an inverse merge, that is: when a set of units is distracted from an account, and when the distracted set has a rate different from the account rate, then the remaining sum on the account must do an inverseMerge to calculate the rate of the remainder.- Parameters:
previousSetOfUnits-transferSetOfUnits-date-- Returns:
- The result of this method is as such, that when the result is merged with the transferSetOfUnits, it gives again the previousSetOfUnits.
-
isEnabled
public boolean isEnabled(Currency currency, Date date)
Description copied from interface:RateTypeHandlerChecks if the rate is enabled on the given date- Specified by:
isEnabledin interfaceRateTypeHandler<T,R extends RateParameters,D extends RateParametersDTO>
-
isVisible
public boolean isVisible(Account account, Date date, RateVisibility visibility)
Description copied from interface:RateTypeHandlerDetermines if the rate is visible, depending on the visibility param. If the rate is not enabled it is considered not visible, so then it returns false.- Specified by:
isVisiblein interfaceRateTypeHandler<T,R extends RateParameters,D extends RateParametersDTO>
-
isVisibleToUser
public boolean isVisibleToUser(Currency currency, Date date)
Description copied from interface:RateTypeHandlerReturns true if the rate is visible to the user on the given date. It is visible if and only if: the rate is enabled the showToUser flag on the params is true.- Specified by:
isVisibleToUserin interfaceRateTypeHandler<T,R extends RateParameters,D extends RateParametersDTO>
-
merge
public T merge(RatedCurrencyAmount<T> setOfUnits1, RatedCurrencyAmount<T> setOfUnits2, Date date) throws IllegalArgumentException
Does the actual calculation of the rate from a merge (where a set of units enters an account).- Parameters:
setOfUnits1- a RatesDTO object, which must have set the following fields:- currency
- amount
- anyRawRate or anyRate, depending on the type T.
setOfUnits2- as setOfUnits1.date- the date on which the transaction takes place. May be null.- Returns:
- the new raw rate resulting from the merge. returns null if rate not enabled.
- Throws:
IllegalArgumentException- if the two sets of units belong to different currencies.
-
nullAllAccountRates
public long nullAllAccountRates(Currency currency)
Description copied from interface:RateTypeHandlerSets all rates on all accounts explicitly to null (for the actual rate type belonging to this RateTypeHandler).- Specified by:
nullAllAccountRatesin interfaceRateTypeHandler<T,R extends RateParameters,D extends RateParametersDTO>
-
resolveValidator
public void resolveValidator(Validator validator, CurrencyDTO dto)
Description copied from interface:RateTypeHandlerResolves the validator for the RateParameters as nested validator for CurrencyDTO.- Specified by:
resolveValidatorin interfaceRateTypeHandler<T,R extends RateParameters,D extends RateParametersDTO>- Parameters:
validator- the resolved validator is assigned as nested validator to this parameters.
-
toEntity
public void toEntity(Currency currency, D dto)
Description copied from interface:RateTypeHandlerSets the rateParameters from the dto to the entity. method does the following:- checks if the rate parameters have been changed
- Specified by:
toEntityin interfaceRateTypeHandler<T,R extends RateParameters,D extends RateParametersDTO>
-
adjustIncomingRawRateInTransfer
protected abstract T adjustIncomingRawRateInTransfer(T incomingRawRate, Transfer transfer)
adjusts a rate as a result of the transaction. For date based rates, this is meaningless, so the method does nothing. For transfer based rates (I-rate, future C-rate) this method adds 1 to the raw rate of the from account - except when the from-account is unlimited; in that case it does nothing.- Parameters:
incomingRawRate- the from account's raw ratetransfer- this is only relevant for iRate, so in other cases this may as well be null, because it is not used then.- Returns:
- the incoming rate, but increased by 1 for transfer based rates. For date based rates, it returns just the unchanged input.
-
assignEntityToCurrency
protected abstract void assignEntityToCurrency(R entity, Currency currency)
Assigns the RateParameters entity to the currency, so that the RateParameters entity becomes the new active RateParameters for this currency. Note that the method only calls the currency's setter. SeeappendToRateParametersHistory(D,org.cyclos.entities.banking.Currency)for other actions to be taken.- Parameters:
entity-currency-- Throws:
IllegalArgumentException- - only new entities may be assigned to the currency. An IllegalArgumentException is thrown if the entity has a non-null id.
-
assignValidator
protected abstract void assignValidator(Validator currencyValidator, Validator rateParametersValidator)
sets specific checks to the RateParametersValidator, and assigns it as nested Validator to the currencyValidator.
-
convert
protected <C> C convert(Class<C> toType, Object object)
quick link to convertionHandler.convert
-
getAccountRatesFieldPath
protected abstract com.querydsl.core.types.Path getAccountRatesFieldPath()
gets the path for the relevant field of the QAccountRates query. For example, returns for A-rate the QAccountRates.emissionDate field path.- Parameters:
ar-- Returns:
-
getQuery
protected abstract QRateParameters getQuery()
gets the QXRateParameters.xRateParameters of QueryDSL as a view of itself as the parent type. So for A-Rate, it gets QARateParametes.aRateParameters as QRateParameters. This is useful for queries on RateParameters for fields which are general for all rate types.
-
hasEqualProperties
protected boolean hasEqualProperties(R entity, D dto)
checks if the RateParameters entity has equal property values compared to the dto, in order to determine if it has been updated. Supposed to be overridden.
-
immediateDisableActions
protected void immediateDisableActions(Currency currency)
does immediate disable actions needed when the rate is disabled.
-
-