jp.co.ntt.atrs.app.c0.MemberValidator.java Source code

Java tutorial

Introduction

Here is the source code for jp.co.ntt.atrs.app.c0.MemberValidator.java

Source

/*
 * Copyright 2014-2018 NTT Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package jp.co.ntt.atrs.app.c0;

import javax.inject.Inject;

import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.terasoluna.gfw.common.date.jodatime.JodaTimeDateFactory;

import jp.co.ntt.atrs.domain.common.util.DateTimeUtil;
import jp.co.ntt.atrs.domain.common.validate.ValidationUtil;
import jp.co.ntt.atrs.domain.service.c0.MemberErrorCode;

/**
 * ????
 * 
 * @author NTT ?
 */
@Component
public class MemberValidator implements Validator {

    /**
     * ??
     */
    @Inject
    JodaTimeDateFactory dateFactory;

    /**
     * ???
     */
    @Value("${atrs.dateOfBirthMinDate}")
    private String dateOfBirthMinDate;

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean supports(Class<?> clazz) {
        return IMemberForm.class.isAssignableFrom(clazz);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void validate(Object target, Errors errors) {

        IMemberForm form = (IMemberForm) target;

        // ?
        if (!errors.hasFieldErrors("mail") && !errors.hasFieldErrors("reEnterMail")) {
            String mail = form.getMail();
            String reEnterMail = form.getReEnterMail();
            if (!mail.equals(reEnterMail)) {
                // ??????????
                errors.reject(MemberErrorCode.E_AR_C0_5001.code());
            }
        }

        // ??
        if (!errors.hasFieldErrors("tel1") && !errors.hasFieldErrors("tel2")) {

            if (!ValidationUtil.isValidTelNum(form.getTel1(), form.getTel2())) {
                // ?????67?????
                errors.reject(MemberErrorCode.E_AR_C0_5002.code());
            }
        }

        // ?
        if (!errors.hasFieldErrors("dateOfBirth")) {

            DateTime dateOfBirthMin = DateTimeUtil.toDateTime(dateOfBirthMinDate);
            DateTime dateOfBirthMax = dateFactory.newDateTime();
            DateTime dateOfBirth = new DateTime(form.getDateOfBirth());

            Interval interval = new Interval(dateOfBirthMin, dateOfBirthMax);
            if (!interval.contains(dateOfBirth)) {
                // ?(190011????)????
                errors.reject(MemberErrorCode.E_AR_C0_5003.code(),
                        new Object[] { dateOfBirthMinDate, DateTimeUtil.toFormatDateString(dateOfBirthMax) }, "");
            }
        }

    }
}