/**
*
*/
protected MarketArea evaluateMarketPlace(final HttpServletRequest request) throws Exception {
EngineEcoSession engineEcoSession = getCurrentEcoSession(request);
MarketPlace marketPlace = null;
Market market = null;
MarketArea marketArea = null;
if(engineEcoSession == null){
initEcoSession(request);
}
// STEP 1 - CHECK THE URL PARAMETERS
UrlParameterMapping urlParameterMapping = handleUrlParameters(request);
String marketPlaceCode = urlParameterMapping.getMarketPlaceCode();
if(StringUtils.isNotEmpty(marketPlaceCode)){
marketPlace = marketService.getMarketPlaceByCode(marketPlaceCode);
if(marketPlace != null){
String marketCode = urlParameterMapping.getMarketCode();
market = marketPlace.getMarket(marketCode);
if(market != null){
String marketAreaCode = urlParameterMapping.getMarketAreaCode();
marketArea = market.getMarketArea(marketAreaCode);
return marketArea;
}
}
}
// STEP 2 - TRY TO GEOLOC THE CUSTOMER AND SET THE RIGHT MARKET AREA
final GeolocData geolocData = engineEcoSession.getGeolocData();
MarketArea marketAreaGeoloc = null;
if(geolocData != null){
final Country country = geolocData.getCountry();
if(country != null && StringUtils.isNotEmpty(country.getIsoCode())){
List<MarketArea> marketAreas = marketService.getMarketAreaByGeolocCountryCode(country.getIsoCode());
if(marketAreas != null && marketAreas.size() == 1){
marketAreaGeoloc = marketAreas.get(0);
} else {
// WE HAVE MANY MARKET AREA FOR THE CURRENT COUNTRY CODE - WE SELECT THE DEFAULT MARKET PLACE ASSOCIATE
for (Iterator<MarketArea> iterator = marketAreas.iterator(); iterator.hasNext();) {
MarketArea marketAreaIt = (MarketArea) iterator.next();
if(marketAreaIt.getMarket().getMarketPlace().isDefault()){
marketAreaGeoloc = marketAreaIt;
}
}
}
}
}
if (marketAreaGeoloc != null) {
marketPlace = marketService.getMarketPlaceByCode(marketAreaGeoloc.getMarket().getMarketPlace().getCode());
market = marketAreaGeoloc.getMarket();
marketArea = marketAreaGeoloc;
return marketArea;
}
// STEP 3 - DEFAULT MARTKETPLACE
marketPlace = marketService.getDefaultMarketPlace();
market = marketPlace.getDefaultMarket();
marketArea = market.getDefaultMarketArea();
return marketArea;
}