com.hbc.api.trade.order.validator.OrderValidator.java Source code

Java tutorial

Introduction

Here is the source code for com.hbc.api.trade.order.validator.OrderValidator.java

Source

/*
 * Copyright (c) 2015-2016, CCLX.COM. All rights reserved.
 * WANDA GROUP PROPRIETARY/CONFIDENTIAL. 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is private property; you can't redistribute it and/or modify it
 * under the terms of the LICENSE you obtained from
 *
 *    http://www.cclx.com/
 * 
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. 
 *
 * Author: Jongly Ran
 * Revision: 1.0
 */
package com.hbc.api.trade.order.validator;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hbc.api.trade.bdata.common.TradeConstant;
import com.hbc.api.trade.bdata.common.util.ParameterValidator;
import com.hbc.api.trade.order.enums.TradeReturnCodeEnum;
import com.hbc.api.trade.order.exception.TradeException;
import com.hbc.api.trade.order.mapping.gen.bean.OrderBean;

/**
 * @author Jongly Ran
 */
public class OrderValidator extends ParameterValidator {
    private final static Logger log = LoggerFactory.getLogger(OrderValidator.class);

    /**
     * ???-,2-2,[3-3,...]FALSETradeException
     * @param orderBean
     */
    public final static void validateChildSeat(OrderBean orderBean) {
        if (StringUtils.isNotBlank(orderBean.getChildSeat())) {
            for (String childSeatString : orderBean.getChildSeat().split(TradeConstant.SPLITER_COMMA)) {
                if (childSeatString != null && childSeatString.split(TradeConstant.SPLITER_LINE).length != 2) {
                    log.error(
                            "?? -,2-2,[3-3,...]"
                                    + orderBean.getChildSeat());
                    throw new TradeException(TradeReturnCodeEnum.ORDER_PARAM_FAILED, "");
                }
            }
        }

    }

    /**
     * ???cityId-stayDay,cityId2-stayDay2,[cityId3-stayDa3,...]FALSETradeException
     * @param orderBean
     */
    public final static void validatePassCity(OrderBean orderBean) {
        if (StringUtils.isNotBlank(orderBean.getServicePassCity())) {
            for (String passCity : orderBean.getServicePassCity().split(TradeConstant.SPLITER_COMMA)) {
                if (passCity != null && passCity.split(TradeConstant.SPLITER_LINE).length != 2) {
                    log.error(
                            "?? cityId-stayDay,cityId2-stayDay2,[cityId3-stayDa3,...]"
                                    + orderBean.getServicePassCity());
                    throw new TradeException(TradeReturnCodeEnum.ORDER_PARAM_FAILED, "");
                }
            }
        }
    }

    /**
     * @param orderBean
     */
    public static void validateHeadCount(OrderBean orderBean) {
        if (orderBean.getChildNum() == null || orderBean.getChildNum() < 0) {
            log.error("?");
            throw new TradeException(TradeReturnCodeEnum.ORDER_PARAM_FAILED, "?|");
        }

        if (orderBean.getAdultNum() == null || orderBean.getAdultNum() < 0) {
            log.error("??" + orderBean.getUserAreaCode1() + "-"
                    + orderBean.getUserMobile1() + "?" + orderBean.getUserName());
            throw new TradeException(TradeReturnCodeEnum.ORDER_PARAM_FAILED, "?|");
        }

        if (orderBean.getCarSeatNum() == null || orderBean.getCarSeatNum() <= 0) {
            log.error("??");
            throw new TradeException(TradeReturnCodeEnum.ORDER_PARAM_FAILED, "??|");
        }

        Integer customerNum = orderBean.getAdultNum() + orderBean.getChildNum();

        if (customerNum <= 0) {
            log.error("?");
            throw new TradeException(TradeReturnCodeEnum.ORDER_PARAM_FAILED, "?|");
        }

        Integer useableCarSeatNum = orderBean.getCarSeatNum() - 1;
        if (customerNum > useableCarSeatNum) {
            log.error("?" + useableCarSeatNum + "??"
                    + orderBean.getAdultNum() + "" + orderBean.getChildNum());
            throw new TradeException(TradeReturnCodeEnum.ORDER_HEAD_COUNT_ERR);
        }
    }
}