DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Data
  4. Developing Your Own ExchangeRateProvider for CurrencyField Type

Developing Your Own ExchangeRateProvider for CurrencyField Type

Rafał Kuć user avatar by
Rafał Kuć
·
Jun. 28, 12 · Interview
Like (0)
Save
Tweet
Share
5.42K Views

Join the DZone community and get the full member experience.

Join For Free

in 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.

Implementation Data (computing) Home directory Directory JAR (file format) Cache (computing) Production (computer science)

Published at DZone with permission of Rafał Kuć, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Choose the Right Streaming Database
  • Introduction to Spring Cloud Kubernetes
  • REST vs. Messaging for Microservices
  • Solving the Kubernetes Security Puzzle

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: