Developing Your Own ExchangeRateProvider for CurrencyField Type
Join the DZone community and get the full member experience.
Join For Freein the previous entry showing how to develop custom solr functionalities we are showing how to implement your own solr filter. today i’ll show you how to implement your own exchange rate provider, so we will implement the exchangerateprovider for the currencyfield type introduced in solr 3.6 .
assumptions
in order to show how to implement your own provider for the currencyfield type, i assume that our application which uses solr is able to provide exchange rates in the proper form. so, i assume we have a library which provided implementation for the following interface:
package pl.solr; import java.util.list; public interface uptodatecurrencyprovider { map<string, list<currency>> getexchangerates(); void refresh(); void initialize(); }
and the currency class looks like this:
package pl.solr; public class currency { private string from; private string to; private double rate; public currency(string from, string to, double rate) { this.from = from; this.to = to; this.rate = rate; } public string getfrom() { return from; } public string getto() { return to; } public double getrate() { return rate; } }
similar to the previous development entry i’ll omit the details about ide and jar building. we will focus on the most interesting stuff – the implementation of solrplcurrencyexchangeprovider .
solrplexchangerateprovider implementation
package pl.solr; import java.util.list; import java.util.map; import java.util.set; import org.apache.solr.common.resourceloader; import org.apache.solr.common.solrexception; import org.apache.solr.schema.exchangerateprovider; public class solrplcurrencyexchangeprovider implements exchangerateprovider { private uptodatecurrencyprovider provider; @override public double getexchangerate(string sourcecurrencycode, string targetcurrencycode) throws solrexception { if (sourcecurrencycode == null || targetcurrencycode == null) { throw new solrexception(solrexception.errorcode.bad_request, "cannot get exchange rate - source or target currency was null."); } list<currency> exchangesrates = provider.getexchangerates().get(sourcecurrencycode); if (exchangesrates == null) { throw new solrexception(solrexception.errorcode.bad_request, "cannot get exchange rate - source currency exchange rates not found"); } currency exchange = null; for (currency currency : exchangesrates) { if (currency.getto().compareto(targetcurrencycode) == 0) { exchange = currency; break; } } if (exchange == null) { throw new solrexception(solrexception.errorcode.bad_request, "cannot get exchange rate - target currency exchange rates not found"); } return exchange.getrate(); } @override public void inform(resourceloader loader) throws solrexception { this.provider = new solrpluptodatecurrencyprovider(); this.provider.initialize(); reload(); } @override public void init(map<string, string> args) { } @override public set<string> listavailablecurrencies() { return provider.getexchangerates().keyset(); } @override public boolean reload() throws solrexception { provider.refresh(); return true; } }
some explanations
some explanations regarding the above code:
- linia 11 – we create a class that implements exchangerateprovider interface from org.apache.solr.schema package.
- linia 12 – our service, which provides exchange rates.
- linia 15 – getexchangerate method implementation which gets the data from the provided service and returns double value.
- linia 19 – get the data regarding the exchange rates. in the production environment we would like to cache this data from some period of time. we don’t do it in the example to simplify the code.
- linie 24 – 29 – loop in which we choose the correct currency object.
- linie 30 – 32 – we throw an exception, when proper currency object wasn’t found.
- linie 37 – 41 – we create an instance of our service in the inform method and we initialize it.
- linie 48 – 50 – method returning all the available exchange rates.
- linie 53 – 56 – method refreshing the provider.
configuration
after compilation and jar file preparation, we copy the jar file to a directory where solr will see it. for example, we can create a lib directory in solr home directory and then add the following entry to solrconfig.xml file:
<lib dir="../lib/" regex="*.jar" />
we also need to change the schema.xml file and add a type based on solr.currencyfield , for example:
<fieldtype class="solr.currencyfield" name="currencyfield" defaultcurrency="usd" providerclass="pl.solr.solrplexchangerateprovider" />
summary
the main problem with implementing your own
exchangerateprovider
is the data, not the actual implementation as you can see in the above
example. i hope that this entry will help some of you and let you save
some precious time
of course, you should remember that this is only an simplified example
and we omit the exchange rates cache for example, which should be
present in a code you want to use in the production system.
Published at DZone with permission of Rafał Kuć, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments