*/
package org.mxchange.addressbook.beans.country;
-import fish.payara.cdi.jsr107.impl.NamedCache;
-import java.text.MessageFormat;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-import javax.annotation.PostConstruct;
-import javax.cache.Cache;
-import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
-import javax.enterprise.event.Observes;
-import javax.inject.Inject;
import javax.inject.Named;
import org.mxchange.addressbook.beans.BaseAddressbookBean;
-import org.mxchange.jcountry.events.ObservableAdminAddedCountryEvent;
-import org.mxchange.jcountry.model.data.Country;
-import org.mxchange.jcountry.model.data.CountrySingletonBeanRemote;
-import org.mxchange.jcountry.events.ObservableAdminAddedCountryEvent;
/**
* A country bean
*/
private static final long serialVersionUID = 176_985_298_681_742_960L;
- /**
- * Remote country EJB
- */
- @EJB (lookup = "java:global/addressbook-ejb/country!org.mxchange.jcountry.model.data.CountrySingletonBeanRemote")
- private CountrySingletonBeanRemote countryBean;
-
- /**
- * List of all countries
- */
- @Inject
- @NamedCache (cacheName = "countryCache")
- private Cache<Long, Country> countryCache;
-
/**
* Default constructor
*/
super();
}
- /**
- * Observing method when the event is fired that an administrator added a
- * new country
- * <p>
- * @param event Event instance
- */
- public void afterAdminAddedCountryEvent (@Observes final ObservableAdminAddedCountryEvent event) {
- // Is all valid?
- if (null == event) {
- // Throw NPE
- throw new NullPointerException("event is null"); //NOI18N
- } else if (event.getAddedCountry() == null) {
- // Throw again ...
- throw new NullPointerException("event.addedCountry is null"); //NOI18N
- } else if (event.getAddedCountry().getCountryId() == null) {
- // And again ...
- throw new NullPointerException("event.addedCountry.countryId is null"); //NOI18N
- } else if (event.getAddedCountry().getCountryId() < 1) {
- // Id is invalid
- throw new IllegalArgumentException(MessageFormat.format("event.addedCountry.countryId={0} is not valid.", event.getAddedCountry().getCountryId())); //NOI18N
- }
-
- // Add the event
- this.countryCache.put(event.getAddedCountry().getCountryId(), event.getAddedCountry());
- }
-
- @Override
- @SuppressWarnings ("ReturnOfCollectionOrArrayField")
- public List<Country> allCountries () {
- // Init list
- final List<Country> list = new LinkedList<>();
-
- // Get iterator
- final Iterator<Cache.Entry<Long, Country>> iterator = this.countryCache.iterator();
-
- // Loop over all
- while (iterator.hasNext()) {
- // Get next entry
- final Cache.Entry<Long, Country> next = iterator.next();
-
- // Add value to list
- list.add(next.getValue());
- }
-
- // Return it
- return list;
- }
-
- /**
- * Post-construction method
- */
- @PostConstruct
- public void init () {
- // Is cache there?
- if (!this.countryCache.iterator().hasNext()) {
- // Get whole list
- final List<Country> list = this.countryBean.allCountries();
-
- // Add all
- for (final Iterator<Country> iterator = list.iterator(); iterator.hasNext();) {
- // Get next element
- final Country next = iterator.next();
-
- // Add it to cache
- this.countryCache.put(next.getCountryId(), next);
- }
- }
- }
-
}