EntityManager em = JpaConnection.getInstance().getCurrentEntityManager();
Query query = em.createQuery("from ShippingRegion " +
"where siteId = :siteId ");
query.setParameter("siteId", contentSessionKey.getSiteId());
Iterator<?> iterator = query.getResultList().iterator();
ShippingRegion defaultRegion = null;
while (iterator.hasNext()) {
ShippingRegion shippingRegion = (ShippingRegion) iterator.next();
if (shippingRegion.getPublished() == Constants.VALUE_NO) {
continue;
}
if (shippingRegion.getSystemRecord() == Constants.VALUE_YES) {
if (shippingRegion.getPublished() == Constants.VALUE_YES) {
defaultRegion = shippingRegion;
continue;
}
}
boolean isFound = false;
Iterator<?> countries = shippingRegion.getCountries().iterator();
while (countries.hasNext()) {
Country country = (Country) countries.next();
if (country.getCountryId().equals(countryId)) {
isFound = true;
}
}
if (!isFound) {
Iterator<?> states = shippingRegion.getStates().iterator();
while (states.hasNext()) {
State state = (State) states.next();
if (state.getStateId().equals(stateId)) {
isFound = true;
}
}
}
String zipCode = address.getCustZipCode();
if (!isFound && !Format.isNullOrEmpty(zipCode)) {
for (ShippingRegionZip shippingRegionZip : shippingRegion.getZipCodes()) {
if (shippingRegionZip.getZipCodeExpression() == Constants.VALUE_YES) {
if (zipCode.matches(shippingRegionZip.getZipCodeStart())) {
isFound = true;
break;
}
}
else {
if (zipCode.compareTo(shippingRegionZip.getZipCodeStart()) >= 0 &&
zipCode.compareTo(shippingRegionZip.getZipCodeEnd()) <= 0) {
isFound = true;
break;
}
}
}
}
if (isFound) {
matchedRegion.add(shippingRegion);
}
}
if (matchedRegion.size() == 0 && defaultRegion == null) {
logger.debug("No shipping region (including default) can be found");
return shippingMethods;
}
if (matchedRegion.size() == 0) {
logger.debug("Use default shipping region");
if (defaultRegion == null) {
logger.debug("Default shipping region does not exist or turned off.");
}
matchedRegion.add(defaultRegion);
}
query = em.createQuery("from ShippingMethod " +
"where siteId = :siteId " +
"and published = :published " +
"order by seqNum");
query.setParameter("siteId", contentSessionKey.getSiteId());
query.setParameter("published", String.valueOf(Constants.PUBLISHED_YES));
iterator = query.getResultList().iterator();
while (iterator.hasNext()) {
ShippingMethod shippingMethod = (ShippingMethod) iterator.next();
Iterator<?> shippingMethodRegions = shippingMethod.getShippingMethodRegions().iterator();
boolean found = false;
boolean doNotShip = false;
while (shippingMethodRegions.hasNext()) {
ShippingMethodRegion shippingMethodRegion = (ShippingMethodRegion) shippingMethodRegions.next();
Long shippingRegionId = shippingMethodRegion.getShippingRegion().getShippingRegionId();
Iterator<ShippingRegion> matchedRegionIterator = matchedRegion.iterator();
while (matchedRegionIterator.hasNext()) {
ShippingRegion matchedShippingRegion = (ShippingRegion) matchedRegionIterator.next();
if (!matchedShippingRegion.getShippingRegionId().equals(shippingRegionId)) {
continue;
}
found = true;
if (shippingMethodRegion.getPublished() == Constants.VALUE_NO) {
doNotShip = true;