package com.transformuk.bdt.validator; import java.util.Map; import javax.servlet.http.HttpSession; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.validation.Errors; import org.springframework.validation.ValidationUtils; import org.springframework.validation.Validator; import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpStatusCodeException; import com.transformuk.bdt.domain.Page; import com.transformuk.bdt.domain.Questions; import com.transformuk.bdt.model.ProfileQuestions; import com.transformuk.bdt.service.MapItServiceImpl; import com.transformuk.bdt.util.StaticReferences; /** * This class validates the question in the Profile Journey. Errors are put back * in the error object and returned to the view. */ @Component public class ProfileValidator implements Validator { private static final String POSTCODE_REGEX = "^(([gG][iI][rR] {0,}0[aA]{2})|((([a-pr-uwyzA-PR-UWYZ][a-hk-yA-HK-Y]?[0-9][0-9]?)|(([a-pr-uwyzA-PR-UWYZ][0-9][a-hjkstuwA-HJKSTUW])|([a-pr-uwyzA-PR-UWYZ][a-hk-yA-HK-Y][0-9][abehmnprv-yABEHMNPRV-Y]))) {0,}[0-9][abd-hjlnp-uw-zABD-HJLNP-UW-Z]{2}))$"; private static final Logger LOGGER = Logger .getLogger(ProfileValidator.class); @Autowired MapItServiceImpl mapitService; @Override public boolean supports(Class<?> clazz) { return ProfileQuestions.class.equals(clazz); } @Override public void validate(Object target, Errors errors) { ProfileQuestions profileQuestions = (ProfileQuestions) target; } public void validateBusinessStage(Object target, Errors errors) { LOGGER.info("validating step one"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "profileQuestions.businessStage", "answer.required"); } public void validate(Object target, Errors e, Page page, HttpSession session) { try { LOGGER.info("validating profile page: " + page.getPageName()); // get user selections Map<String, String> userSelectedData = ((ProfileQuestions) target).getValues(); if (userSelectedData != null) { for (Questions currentQuestion : page.getQuestions()) { if (currentQuestion.isMandatory()) { String qCode = currentQuestion.getQuestionCode(); String userAnswer = userSelectedData.get(qCode); if (StringUtils.isBlank(userAnswer)) { e.rejectValue("profileQuestions.values['" + qCode + "']", "answer.required"); page.setPageErrors(true); } else if (StringUtils.equalsIgnoreCase(StaticReferences.POSTCODE_FIELD, qCode) && StringUtils.equalsIgnoreCase("input", currentQuestion.getType())) { // validate postcode validatePostCode(e, qCode, userAnswer, session); } } } } } catch (Exception exception) { LOGGER.error("Exception while validating: "+ exception.getMessage()); e.reject("general exception occured while validating"); } } private void validatePostCode(Errors e, String qCode, String userAnswer, HttpSession session) { // valid post code format /* * boolean isValidPostCodeFormat = StringUtils.trim(userAnswer).matches(POSTCODE_REGEX); * if (!isValidPostCodeFormat) { * e.rejectValue("profileQuestions.values['"+qCode+"']", "invalid.postcode"); * } else { */ // valid post code from actual MapIt. try { // we're doing it here as the user need to be stopped at this point mapitService.getMapItDataForFullPostCode(userAnswer); session.setAttribute(StaticReferences.IS_PARTIAL_POSTCODE, false); } catch (Exception ex) { LOGGER.error("MapIt Full postcode lookup returned HttpResponse error code: "+ ex.getMessage() + " for postcode: "+ userAnswer); // try partial post code try { mapitService.getMapItDataForPartialPostCode(userAnswer); session.setAttribute(StaticReferences.IS_PARTIAL_POSTCODE, true); } catch (Exception pex) { LOGGER.error("MapIt Partial postcode lookup returned HttpResponse error code: "+ pex.getMessage()); if (pex instanceof HttpClientErrorException && (((HttpStatusCodeException) pex).getStatusCode() == HttpStatus.NOT_FOUND) || (((HttpStatusCodeException) pex).getStatusCode() == HttpStatus.BAD_REQUEST)) { e.rejectValue("profileQuestions.values['" + qCode + "']", "unknown.postcode"); return; } // general error incl non 200 responses e.rejectValue("profileQuestions.values['" + qCode + "']", "nonok.postcode.response"); } } } }