org.ojbc.web.portal.validators.ArrestSubscriptionAddValidator.java Source code

Java tutorial

Introduction

Here is the source code for org.ojbc.web.portal.validators.ArrestSubscriptionAddValidator.java

Source

/*
 * Unless explicitly acquired and licensed from Licensor under another license, the contents of
 * this file are subject to the Reciprocal Public License ("RPL") Version 1.5, or subsequent
 * versions as allowed by the RPL, and You may not copy or use this file in either source code
 * or executable form, except in compliance with the terms and conditions of the RPL
 *
 * All software distributed under the RPL is provided strictly on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, AND LICENSOR HEREBY DISCLAIMS ALL SUCH
 * WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, QUIET ENJOYMENT, OR NON-INFRINGEMENT. See the RPL for specific language
 * governing rights and limitations under the RPL.
 *
 * http://opensource.org/licenses/RPL-1.5
 *
 * Copyright 2012-2015 Open Justice Broker Consortium
 */
package org.ojbc.web.portal.validators;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import org.apache.commons.lang.StringUtils;
import org.ojbc.web.model.subscription.Subscription;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

@Service
public class ArrestSubscriptionAddValidator {

    private Logger logger = Logger.getLogger(ArrestSubscriptionAddValidator.class.getName());

    public void validate(Subscription subscription, BindingResult errors) {

        logger.info("* * * inside validate()");

        if (subscription == null) {
            return;
        }

        Map<String, String> fieldToErrorMap = getValidationErrorsList(subscription);

        if (fieldToErrorMap == null) {
            return;
        }

        for (String iField : fieldToErrorMap.keySet()) {

            String errorMessage = fieldToErrorMap.get(iField);

            errors.rejectValue(iField, errorMessage);
        }

    }

    Map<String, String> getValidationErrorsList(Subscription subscription) {

        if (subscription == null) {
            return null;
        }

        Map<String, String> fieldToErrorMap = new HashMap<String, String>();

        String topic = subscription.getSubscriptionType();
        if (StringUtils.isBlank(topic)) {
            fieldToErrorMap.put("subscriptionType", "Subscription type must be specified");
        }

        String sid = subscription.getStateId();
        if (StringUtils.isBlank(sid)) {
            fieldToErrorMap.put("stateId", "SID must be specified");
        }

        String name = subscription.getFullName();
        if (StringUtils.isBlank(name)) {
            fieldToErrorMap.put("fullName", "Name must be specified");
        }

        Date subStartDate = subscription.getSubscriptionStartDate();
        if (subStartDate == null) {
            fieldToErrorMap.put("subscriptionStartDate", "Start date must be specified");
        }

        Date subEndDate = subscription.getSubscriptionEndDate();
        if (subEndDate != null && subStartDate != null) {
            if (subEndDate.before(subStartDate)) {
                fieldToErrorMap.put("subscriptionEndDate", "End date may not occur before start date");
            }
        }

        boolean hasEmail = false;

        for (String iEmail : subscription.getEmailList()) {
            if (StringUtils.isNotBlank(iEmail)) {
                hasEmail = true;
            }
        }

        if (!hasEmail) {
            fieldToErrorMap.put("emailList", "Email Address must be specified");
        }

        return fieldToErrorMap;
    }

}