X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Fjava%2Forg%2Fmxchange%2Fjcountry%2Fdata%2FJobsCountrySingletonBean.java;h=38275840b1f7859652ffdc48bd874c3a9f82f1c9;hb=fb386c02c2750e0d82d81cd8bae84b81ffb661ce;hp=2e9c313dfef15c2c803a68658d5161ee32c28150;hpb=9329ce9f8a2e61390be7a6228ad3b9b185691994;p=jjobs-ejb.git diff --git a/src/java/org/mxchange/jcountry/data/JobsCountrySingletonBean.java b/src/java/org/mxchange/jcountry/data/JobsCountrySingletonBean.java index 2e9c313..3827584 100644 --- a/src/java/org/mxchange/jcountry/data/JobsCountrySingletonBean.java +++ b/src/java/org/mxchange/jcountry/data/JobsCountrySingletonBean.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Roland Haeder + * Copyright (C) 2016, 2017 Roland Häder * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -16,34 +16,134 @@ */ package org.mxchange.jcountry.data; +import java.text.MessageFormat; +import java.util.GregorianCalendar; import java.util.List; import javax.ejb.Singleton; import javax.ejb.Startup; +import javax.persistence.NoResultException; import javax.persistence.Query; -import org.mxchange.jcoreee.database.BaseDatabaseBean; +import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException; +import org.mxchange.jjobs.database.BaseJobsDatabaseBean; /** - * A singleton bean for country informations + * A singleton EJB for country informations *
- * @author Roland Haeder
+ * @param country Country instance to check
+ *
+ * @return Whether the country was found
+ */
+ private boolean isCountryAdded (final Country country) {
+ if (null == country) {
+ // Throw NPE
+ throw new NullPointerException("country is null"); //NOI18N
+ } else if (country.getCountryCode().isEmpty()) {
+ // Code is not set
+ throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N
+ } else if (country.getCountryI18nKey().isEmpty()) {
+ // I18n key is not set
+ throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N
+ } else if (country.getCountryId() != null) {
+ // Should be null
+ throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N
+ }
+
+ // Default is not found
+ boolean isAdded = false;
+
+ // Get query instance
+ Query query = this.getEntityManager().createNamedQuery("SearchCountryByCodeI18nKey", CountryData.class); //NOI18N
+
+ // Assign all parameters
+ query.setParameter("code", country.getCountryCode()); //NOI18N
+ query.setParameter("key", country.getCountryI18nKey()); //NOI18N
+
+ // Try to get a single result
+ try {
+ // Get single result
+ Country foundCountry = (Country) query.getSingleResult();
+
+ // Found it?
+ isAdded = (foundCountry instanceof Country);
+ } catch (final NoResultException ex) {
+ // Not found, don't log this
+ }
+
+ // Return result
+ return isAdded;
}
}