BATCH-2110: Updated batch to support Spring 4

* Refactored iBatis based readers and writers to not utilize
 SqlMapClientTemplate.
* Depricated all iBatis based readers and writers in favor of MyBatis's
 native Spring support.
* Updated XStream support to 1.4.4 and Jettison to 1.2 to be in
 alignment with Spring 4.
* Added the PooledEmbeddedDataSource to address the issue outlined in
 SPR-11372.
This commit is contained in:
Michael Minella
2014-01-29 22:21:00 -06:00
parent 3bdbfac0d9
commit b278239751
51 changed files with 1130 additions and 1685 deletions

View File

@@ -1,55 +0,0 @@
/*
* Copyright 2006-2007 the original author or authors.
*
* 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 org.springframework.batch.sample.domain.order.internal.valang;
import java.util.Date;
import org.springmodules.validation.valang.functions.AbstractFunction;
import org.springmodules.validation.valang.functions.Function;
/**
* Returns Boolean.TRUE if given value is future date, else it returns Boolean.FALSE
* @author peter.zozom
*/
public class FutureDateFunction extends AbstractFunction {
public FutureDateFunction(Function[] arguments, int line, int column) {
super(arguments, line, column);
definedExactNumberOfArguments(1);
}
/**
* @see org.springmodules.validation.valang.functions.AbstractFunction#doGetResult(java.lang.Object)
*/
protected Object doGetResult(final Object target) throws Exception {
//get argument
final Object value = getArguments()[0].getResult(target);
Boolean result;
if (value instanceof Date) {
final Date now = new Date(System.currentTimeMillis());
final Date date = (Date) value;
result = (now.compareTo(date) < 0) ? Boolean.TRUE : Boolean.FALSE;
} else {
throw new Exception("No Date value for validation");
}
return result;
}
}

View File

@@ -1,63 +0,0 @@
/*
* Copyright 2006-2007 the original author or authors.
*
* 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 org.springframework.batch.sample.domain.order.internal.valang;
import java.util.List;
import org.springframework.batch.sample.domain.order.LineItem;
import org.springmodules.validation.valang.functions.AbstractFunction;
import org.springmodules.validation.valang.functions.Function;
/**
* Validates total items count in Order.
*
* @author peter.zozom
*/
public class TotalOrderItemsFunction extends AbstractFunction {
public TotalOrderItemsFunction(Function[] arguments, int line, int column) {
super(arguments, line, column);
definedExactNumberOfArguments(2);
}
/**
* @see org.springmodules.validation.valang.functions.AbstractFunction#doGetResult(java.lang.Object)
*/
@SuppressWarnings("unchecked")
protected Object doGetResult(Object target) throws Exception {
//get arguments
int count = (Integer) getArguments()[0].getResult(target);
Object value = getArguments()[1].getResult(target);
Boolean result;
//count items in list of order lines
if (value instanceof List) {
int totalItems = 0;
for (LineItem lineItem : ((List<LineItem>) value)) {
totalItems += lineItem.getQuantity();
}
result = (totalItems == count) ? Boolean.TRUE : Boolean.FALSE;
} else {
throw new Exception("No list for validation");
}
return result;
}
}

View File

@@ -1,68 +0,0 @@
/*
* Copyright 2006-2007 the original author or authors.
*
* 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 org.springframework.batch.sample.domain.order.internal.valang;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.batch.sample.domain.order.LineItem;
import org.springmodules.validation.valang.functions.AbstractFunction;
import org.springmodules.validation.valang.functions.Function;
/**
* @author peter.zozom
*
*/
public class ValidateDiscountsFunction extends AbstractFunction {
private static final BigDecimal BD_0 = new BigDecimal(0.0);
private static final BigDecimal BD_PERC_MAX = new BigDecimal(100.0);
public ValidateDiscountsFunction(Function[] arguments, int line, int column) {
super(arguments, line, column);
definedExactNumberOfArguments(1);
}
/**
* @see org.springmodules.validation.valang.functions.AbstractFunction#doGetResult(java.lang.Object)
*/
@SuppressWarnings("unchecked")
protected Object doGetResult(Object target) throws Exception {
List<LineItem> lineItems = (List<LineItem>) getArguments()[0].getResult(target);
for (LineItem item : lineItems) {
if (BD_0.compareTo(item.getDiscountPerc()) != 0) {
//DiscountPerc must be between 0.0 and 100.0
if ((BD_0.compareTo(item.getDiscountPerc()) > 0)
|| (BD_PERC_MAX.compareTo(item.getDiscountPerc()) < 0)
|| (BD_0.compareTo(item.getDiscountAmount()) != 0)) { //only one of DiscountAmount and DiscountPerc should be non-zero
return Boolean.FALSE;
}
} else {
//DiscountAmount must be between 0.0 and item.price
if ((BD_0.compareTo(item.getDiscountAmount()) > 0)
|| (item.getPrice().compareTo(item.getDiscountAmount()) < 0)) {
return Boolean.FALSE;
}
}
}
return Boolean.TRUE;
}
}

View File

@@ -1,57 +0,0 @@
/*
* Copyright 2006-2007 the original author or authors.
*
* 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 org.springframework.batch.sample.domain.order.internal.valang;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.batch.sample.domain.order.LineItem;
import org.springmodules.validation.valang.functions.AbstractFunction;
import org.springmodules.validation.valang.functions.Function;
/**
* @author peter.zozom
*
*/
public class ValidateHandlingPricesFunction extends AbstractFunction {
private static final BigDecimal BD_MIN = new BigDecimal(0.0);
private static final BigDecimal BD_MAX = new BigDecimal(99999999.99);
public ValidateHandlingPricesFunction(Function[] arguments, int line, int column) {
super(arguments, line, column);
definedExactNumberOfArguments(1);
}
/**
* @see org.springmodules.validation.valang.functions.AbstractFunction#doGetResult(java.lang.Object)
*/
@SuppressWarnings("unchecked")
protected Object doGetResult(Object target) throws Exception {
List<LineItem> lineItems = (List<LineItem>) getArguments()[0].getResult(target);
for (LineItem item : lineItems) {
if ((BD_MIN.compareTo(item.getHandlingPrice()) > 0)
|| (BD_MAX.compareTo(item.getHandlingPrice()) < 0)) {
return Boolean.FALSE;
}
}
return Boolean.TRUE;
}
}

View File

@@ -1,54 +0,0 @@
/*
* Copyright 2006-2007 the original author or authors.
*
* 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 org.springframework.batch.sample.domain.order.internal.valang;
import java.util.List;
import org.springframework.batch.sample.domain.order.LineItem;
import org.springmodules.validation.valang.functions.AbstractFunction;
import org.springmodules.validation.valang.functions.Function;
/**
* @author peter.zozom
*
*/
public class ValidateIdsFunction extends AbstractFunction {
private static final long MAX_ID = 9999999999L;
public ValidateIdsFunction(Function[] arguments, int line, int column) {
super(arguments, line, column);
definedExactNumberOfArguments(1);
}
/**
* @see org.springmodules.validation.valang.functions.AbstractFunction#doGetResult(java.lang.Object)
*/
@SuppressWarnings("unchecked")
protected Object doGetResult(Object target) throws Exception {
List<LineItem> lineItems = (List<LineItem>) getArguments()[0].getResult(target);
for (LineItem item : lineItems) {
if ((item.getItemId() <= 0) || (item.getItemId() > MAX_ID)) {
return Boolean.FALSE;
}
}
return Boolean.TRUE;
}
}

View File

@@ -1,56 +0,0 @@
/*
* Copyright 2006-2007 the original author or authors.
*
* 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 org.springframework.batch.sample.domain.order.internal.valang;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.batch.sample.domain.order.LineItem;
import org.springmodules.validation.valang.functions.AbstractFunction;
import org.springmodules.validation.valang.functions.Function;
/**
* @author peter.zozom
*
*/
public class ValidatePricesFunction extends AbstractFunction {
private static final BigDecimal BD_MIN = new BigDecimal(0.0);
private static final BigDecimal BD_MAX = new BigDecimal(99999999.99);
public ValidatePricesFunction(Function[] arguments, int line, int column) {
super(arguments, line, column);
definedExactNumberOfArguments(1);
}
/**
* @see org.springmodules.validation.valang.functions.AbstractFunction#doGetResult(java.lang.Object)
*/
@SuppressWarnings("unchecked")
protected Object doGetResult(Object target) throws Exception {
List<LineItem> lineItems = (List<LineItem>) getArguments()[0].getResult(target);
for (LineItem item : lineItems) {
if ((BD_MIN.compareTo(item.getPrice()) > 0) || (BD_MAX.compareTo(item.getPrice()) < 0)) {
return Boolean.FALSE;
}
}
return Boolean.TRUE;
}
}

View File

@@ -1,54 +0,0 @@
/*
* Copyright 2006-2007 the original author or authors.
*
* 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 org.springframework.batch.sample.domain.order.internal.valang;
import java.util.List;
import org.springframework.batch.sample.domain.order.LineItem;
import org.springmodules.validation.valang.functions.AbstractFunction;
import org.springmodules.validation.valang.functions.Function;
/**
* @author peter.zozom
*
*/
public class ValidateQuantitiesFunction extends AbstractFunction {
private static final int MAX_QUANTITY = 9999;
public ValidateQuantitiesFunction(Function[] arguments, int line, int column) {
super(arguments, line, column);
definedExactNumberOfArguments(1);
}
/**
* @see org.springmodules.validation.valang.functions.AbstractFunction#doGetResult(java.lang.Object)
*/
@SuppressWarnings("unchecked")
protected Object doGetResult(Object target) throws Exception {
List<LineItem> lineItems = (List<LineItem>) getArguments()[0].getResult(target);
for (LineItem item : lineItems) {
if ((item.getQuantity() <= 0) || (item.getQuantity() > MAX_QUANTITY)) {
return Boolean.FALSE;
}
}
return Boolean.TRUE;
}
}

View File

@@ -1,57 +0,0 @@
/*
* Copyright 2006-2007 the original author or authors.
*
* 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 org.springframework.batch.sample.domain.order.internal.valang;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.batch.sample.domain.order.LineItem;
import org.springmodules.validation.valang.functions.AbstractFunction;
import org.springmodules.validation.valang.functions.Function;
/**
* @author peter.zozom
*
*/
public class ValidateShippingPricesFunction extends AbstractFunction {
private static final BigDecimal BD_MIN = new BigDecimal(0.0);
private static final BigDecimal BD_MAX = new BigDecimal(99999999.99);
public ValidateShippingPricesFunction(Function[] arguments, int line, int column) {
super(arguments, line, column);
definedExactNumberOfArguments(1);
}
/**
* @see org.springmodules.validation.valang.functions.AbstractFunction#doGetResult(java.lang.Object)
*/
@SuppressWarnings("unchecked")
protected Object doGetResult(Object target) throws Exception {
List<LineItem> lineItems = (List<LineItem>) getArguments()[0].getResult(target);
for (LineItem item : lineItems) {
if ((BD_MIN.compareTo(item.getShippingPrice()) > 0)
|| (BD_MAX.compareTo(item.getShippingPrice()) < 0)) {
return Boolean.FALSE;
}
}
return Boolean.TRUE;
}
}

View File

@@ -1,83 +0,0 @@
/*
* Copyright 2006-2007 the original author or authors.
*
* 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 org.springframework.batch.sample.domain.order.internal.valang;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.batch.sample.domain.order.LineItem;
import org.springmodules.validation.valang.functions.AbstractFunction;
import org.springmodules.validation.valang.functions.Function;
/**
* @author peter.zozom
*
*/
public class ValidateTotalPricesFunction extends AbstractFunction {
private static final BigDecimal BD_MIN = new BigDecimal(0.0);
private static final BigDecimal BD_MAX = new BigDecimal(99999999.99);
private static final BigDecimal BD_100 = new BigDecimal(100.00);
public ValidateTotalPricesFunction(Function[] arguments, int line, int column) {
super(arguments, line, column);
definedExactNumberOfArguments(1);
}
/**
* @see org.springmodules.validation.valang.functions.AbstractFunction#doGetResult(java.lang.Object)
*/
@SuppressWarnings("unchecked")
protected Object doGetResult(Object target) throws Exception {
List<LineItem> lineItems = (List<LineItem>) getArguments()[0].getResult(target);
for (LineItem item : lineItems) {
if ((BD_MIN.compareTo(item.getTotalPrice()) > 0)
|| (BD_MAX.compareTo(item.getTotalPrice()) < 0)) {
return Boolean.FALSE;
}
//calculate total price
//discount coeficient = (100.00 - discountPerc) / 100.00
BigDecimal coef = BD_100.subtract(item.getDiscountPerc())
.divide(BD_100, 4, BigDecimal.ROUND_HALF_UP);
//discountedPrice = (price * coef) - discountAmount
//at least one of discountPerc and discountAmount is 0 - this is validated by ValidateDiscountsFunction
BigDecimal discountedPrice = item.getPrice().multiply(coef)
.subtract(item.getDiscountAmount());
//price for single item = discountedPrice + shipping + handling
BigDecimal singleItemPrice = discountedPrice.add(item.getShippingPrice())
.add(item.getHandlingPrice());
//total price = singleItemPrice * quantity
BigDecimal quantity = new BigDecimal(item.getQuantity());
BigDecimal totalPrice = singleItemPrice.multiply(quantity)
.setScale(2, BigDecimal.ROUND_HALF_UP);
//calculatedPrice should equal to item.totalPrice
if (totalPrice.compareTo(item.getTotalPrice()) != 0) {
return Boolean.FALSE;
}
}
return Boolean.TRUE;
}
}

View File

@@ -0,0 +1,272 @@
package org.springframework.batch.sample.domain.order.internal.validator;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.batch.sample.domain.order.Address;
import org.springframework.batch.sample.domain.order.BillingInfo;
import org.springframework.batch.sample.domain.order.Customer;
import org.springframework.batch.sample.domain.order.LineItem;
import org.springframework.batch.sample.domain.order.Order;
import org.springframework.batch.sample.domain.order.ShippingInfo;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
public class OrderValidator implements Validator {
private static final List<String> CARD_TYPES = new ArrayList<String>();
private static final List<String> SHIPPER_IDS = new ArrayList<String>();
private static final List<String> SHIPPER_TYPES = new ArrayList<String>();
private static final long MAX_ID = 9999999999L;
private static final BigDecimal BD_MIN = new BigDecimal(0.0);
private static final BigDecimal BD_MAX = new BigDecimal(99999999.99);
private static final BigDecimal BD_PERC_MAX = new BigDecimal(100.0);
private static final int MAX_QUANTITY = 9999;
private static final BigDecimal BD_100 = new BigDecimal(100.00);
static {
CARD_TYPES.add("VISA");
CARD_TYPES.add("AMEX");
CARD_TYPES.add("ECMC");
CARD_TYPES.add("DCIN");
CARD_TYPES.add("PAYP");
SHIPPER_IDS.add("FEDX");
SHIPPER_IDS.add("UPS");
SHIPPER_IDS.add("DHL");
SHIPPER_IDS.add("DPD");
SHIPPER_TYPES.add("STD");
SHIPPER_TYPES.add("EXP");
SHIPPER_TYPES.add("AMS");
SHIPPER_TYPES.add("AME");
}
@Override
public boolean supports(Class<?> arg0) {
return arg0.isAssignableFrom(Order.class);
}
@Override
public void validate(Object arg0, Errors errors) {
Order item = null;
try {
item = (Order) arg0;
} catch (ClassCastException cce) {
errors.reject("Incorrect type");
}
if(item != null) {
validateOrder(item, errors);
validateCustomer(item.getCustomer(), errors);
validateAddress(item.getBillingAddress(), errors, "billingAddress");
validateAddress(item.getShippingAddress(), errors, "shippingAddress");
validatePayment(item.getBilling(), errors);
validateShipping(item.getShipping(), errors);
validateLineItems(item.getLineItems(), errors);
}
}
protected void validateLineItems(List<LineItem> lineItems, Errors errors) {
boolean ids = true;
boolean prices = true;
boolean discounts = true;
boolean shippingPrices = true;
boolean handlingPrices = true;
boolean quantities = true;
boolean totalPrices = true;
for (LineItem lineItem : lineItems) {
if(lineItem.getItemId() <= 0 || lineItem.getItemId() > MAX_ID) {
ids = false;
}
if((BD_MIN.compareTo(lineItem.getPrice()) > 0) || (BD_MAX.compareTo(lineItem.getPrice()) < 0)) {
prices = false;
}
if (BD_MIN.compareTo(lineItem.getDiscountPerc()) != 0) {
//DiscountPerc must be between 0.0 and 100.0
if ((BD_MIN.compareTo(lineItem.getDiscountPerc()) > 0)
|| (BD_PERC_MAX.compareTo(lineItem.getDiscountPerc()) < 0)
|| (BD_MIN.compareTo(lineItem.getDiscountAmount()) != 0)) { //only one of DiscountAmount and DiscountPerc should be non-zero
discounts = false;
}
} else {
//DiscountAmount must be between 0.0 and item.price
if ((BD_MIN.compareTo(lineItem.getDiscountAmount()) > 0)
|| (lineItem.getPrice().compareTo(lineItem.getDiscountAmount()) < 0)) {
discounts = false;
}
}
if ((BD_MIN.compareTo(lineItem.getShippingPrice()) > 0) || (BD_MAX.compareTo(lineItem.getShippingPrice()) < 0)) {
shippingPrices = false;
}
if ((BD_MIN.compareTo(lineItem.getHandlingPrice()) > 0) || (BD_MAX.compareTo(lineItem.getHandlingPrice()) < 0)) {
handlingPrices = false;
}
if ((lineItem.getQuantity() <= 0) || (lineItem.getQuantity() > MAX_QUANTITY)) {
quantities = false;
}
if ((BD_MIN.compareTo(lineItem.getTotalPrice()) > 0)
|| (BD_MAX.compareTo(lineItem.getTotalPrice()) < 0)) {
totalPrices = false;
}
//calculate total price
//discount coeficient = (100.00 - discountPerc) / 100.00
BigDecimal coef = BD_100.subtract(lineItem.getDiscountPerc())
.divide(BD_100, 4, BigDecimal.ROUND_HALF_UP);
//discountedPrice = (price * coef) - discountAmount
//at least one of discountPerc and discountAmount is 0 - this is validated by ValidateDiscountsFunction
BigDecimal discountedPrice = lineItem.getPrice().multiply(coef)
.subtract(lineItem.getDiscountAmount());
//price for single item = discountedPrice + shipping + handling
BigDecimal singleItemPrice = discountedPrice.add(lineItem.getShippingPrice())
.add(lineItem.getHandlingPrice());
//total price = singleItemPrice * quantity
BigDecimal quantity = new BigDecimal(lineItem.getQuantity());
BigDecimal totalPrice = singleItemPrice.multiply(quantity)
.setScale(2, BigDecimal.ROUND_HALF_UP);
//calculatedPrice should equal to item.totalPrice
if (totalPrice.compareTo(lineItem.getTotalPrice()) != 0) {
totalPrices = false;
}
}
if(!ids) {
errors.rejectValue("lineItems", "error.lineitems.id");
}
if(!prices) {
errors.rejectValue("lineItems", "error.lineitems.price");
}
if(!discounts) {
errors.rejectValue("lineItems", "error.lineitems.discount");
}
if(!shippingPrices) {
errors.rejectValue("lineItems", "error.lineitems.shipping");
}
if(!handlingPrices) {
errors.rejectValue("lineItems", "error.lineitems.handling");
}
if(!quantities) {
errors.rejectValue("lineItems", "error.lineitems.quantity");
}
if(!totalPrices) {
errors.rejectValue("lineItems", "error.lineitems.totalprice");
}
}
protected void validateShipping(ShippingInfo shipping, Errors errors) {
if(!SHIPPER_IDS.contains(shipping.getShipperId())) {
errors.rejectValue("shipping.shipperId", "error.shipping.shipper");
}
if(!SHIPPER_TYPES.contains(shipping.getShippingTypeId())) {
errors.rejectValue("shipping.shippingTypeId", "error.shipping.type");
}
if(StringUtils.hasText(shipping.getShippingInfo())) {
validateStringLength(shipping.getShippingInfo(), errors, "shipping.shippingInfo", "error.shipping.shippinginfo.length", 100);
}
}
protected void validatePayment(BillingInfo billing, Errors errors) {
if(!CARD_TYPES.contains(billing.getPaymentId())) {
errors.rejectValue("billing.paymentId", "error.billing.type");
}
if(!billing.getPaymentDesc().matches("[A-Z]{4}-[0-9]{10,11}")) {
errors.rejectValue("billing.paymentDesc", "error.billing.desc");
}
}
protected void validateAddress(Address address, Errors errors,
String prefix) {
if(address != null) {
if(StringUtils.hasText(address.getAddressee())) {
validateStringLength(address.getAddressee(), errors, prefix + ".addressee", "error.baddress.addresse.length", 60);
}
validateStringLength(address.getAddrLine1(), errors, prefix + ".addrLine1", "error.baddress.addrline1.length", 50);
if(StringUtils.hasText(address.getAddrLine2())) {
validateStringLength(address.getAddrLine2(), errors, prefix + ".addrLine2", "error.baddress.addrline2.length", 50);
}
validateStringLength(address.getCity(), errors, prefix + ".city", "error.baddress.city.length", 30);
validateStringLength(address.getZipCode(), errors, prefix + ".zipCode", "error.baddress.zipcode.length", 5);
if(StringUtils.hasText(address.getZipCode()) && !address.getZipCode().matches("[0-9]{5}")) {
errors.rejectValue(prefix + ".zipCode", "error.baddress.zipcode.format");
}
if((!StringUtils.hasText(address.getState()) && ("United States".equals(address.getCountry())) || StringUtils.hasText(address.getState()) && address.getState().length() != 2)) {
errors.rejectValue(prefix + ".state", "error.baddress.state.length");
}
validateStringLength(address.getCountry(), errors, prefix + ".country", "error.baddress.country.length", 50);
}
}
protected void validateStringLength(String string, Errors errors,
String field, String message, int length) {
if(!StringUtils.hasText(string) || string.length() > length) {
errors.rejectValue(field, message);
}
}
protected void validateCustomer(Customer customer, Errors errors) {
if(!customer.isRegistered() && customer.isBusinessCustomer()) {
errors.rejectValue("customer.registered", "error.customer.registration");
}
if(!StringUtils.hasText(customer.getCompanyName()) && customer.isBusinessCustomer()) {
errors.rejectValue("customer.companyName", "error.customer.companyname");
}
if(!StringUtils.hasText(customer.getFirstName()) && !customer.isBusinessCustomer()) {
errors.rejectValue("customer.firstName", "error.customer.firstname");
}
if(!StringUtils.hasText(customer.getLastName()) && !customer.isBusinessCustomer()) {
errors.rejectValue("customer.lastName", "error.customer.lastname");
}
if(customer.isRegistered() && (customer.getRegistrationId() < 0 || customer.getRegistrationId() >= 99999999l)) {
errors.rejectValue("customer.registrationId", "error.customer.registrationid");
}
}
protected void validateOrder(Order item, Errors errors) {
if(item.getOrderId() < 0 || item.getOrderId() > 9999999999l) {
errors.rejectValue("orderId", "error.order.id");
}
if(new Date().compareTo(item.getOrderDate()) < 0) {
errors.rejectValue("orderDate", "error.order.date.future");
}
if(item.getLineItems() != null && item.getTotalLines() != item.getLineItems().size()) {
errors.rejectValue("totalLines", "error.order.lines.badcount");
}
}
}

View File

@@ -15,25 +15,35 @@
*/
package org.springframework.batch.sample.domain.trade.internal;
import java.sql.SQLException;
import org.springframework.batch.sample.domain.trade.CustomerCredit;
import org.springframework.batch.sample.domain.trade.CustomerCreditDao;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator;
import com.ibatis.sqlmap.client.SqlMapClient;
/**
* @author Lucas Ward
*
*/
public class IbatisCustomerCreditDao extends SqlMapClientDaoSupport
implements CustomerCreditDao {
public class IbatisCustomerCreditDao implements CustomerCreditDao {
SqlMapClient sqlMapClient;
String statementId;
/* (non-Javadoc)
* @see org.springframework.batch.sample.domain.trade.internal.CustomerCreditWriter#write(org.springframework.batch.sample.domain.CustomerCredit)
*/
@Override
public void writeCredit(CustomerCredit customerCredit) {
getSqlMapClientTemplate().update(statementId, customerCredit);
try {
sqlMapClient.update(statementId, customerCredit);
} catch (SQLException e) {
throw new SQLStateSQLExceptionTranslator().translate("SqlMapClient operation", null, e);
}
}
/* (non-Javadoc)

View File

@@ -8,17 +8,18 @@
class="org.springframework.batch.item.database.IbatisPagingItemReader">
<property name="queryId" value="getAllCustomerCredits" />
<property name="sqlMapClient" ref="sqlMapClient" />
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="itemWriter"
class="org.springframework.batch.item.database.IbatisBatchItemWriter">
<property name="statementId" value="updateCredit" />
<property name="sqlMapClient" ref="sqlMapClient" />
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="ibatis-config.xml" />
<bean id="sqlMapClient" class="com.ibatis.sqlmap.client.SqlMapClientBuilder" factory-method="buildSqlMapClient">
<constructor-arg value="ibatis-config.xml"/>
</bean>
</beans>

View File

@@ -4,7 +4,7 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
http://www.springframework.org/schema/batch/spring-batch.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
@@ -56,6 +56,7 @@
<bean id="processor" class="org.springframework.batch.item.validator.ValidatingItemProcessor">
<constructor-arg ref="validator" />
<property name="filter" value="true"/>
</bean>
<bean id="fileItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
@@ -67,4 +68,4 @@
</property>
</bean>
</beans>
</beans>

View File

@@ -1,76 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="validator" class="org.springframework.batch.item.validator.SpringValidator">
<property name="validator">
<bean id="orderValidator" class="org.springmodules.validation.valang.ValangValidator">
<property name="valang">
<value>
<!-- "{" <key> : <rule> : <message> : [ <error_code> [ : <error_parameters> ] ] "}" -->
<![CDATA[
{ orderId : ? > 0 AND ? <= 9999999999 : 'Incorrect order ID' : 'error.order.id' }
{ orderDate : isFutureDate(?) = FALSE : 'Future date is not allowed' : 'error.order.date.future' }
{ totalLines : ? = size(lineItems) : 'Bad count of order lines' : 'error.order.lines.badcount'}
{ customer.registered : customer.businessCustomer = FALSE OR ? = TRUE : 'Business customer must be registered' : 'error.customer.registration'}
{ customer.companyName : customer.businessCustomer = FALSE OR ? HAS TEXT : 'Company name for business customer is mandatory' : 'error.customer.companyname'}
{ customer.firstName : customer.businessCustomer = TRUE OR ? HAS TEXT : 'Firstname for non-business customer is mandatory' : 'error.customer.firstname'}
{ customer.lastName : customer.businessCustomer = TRUE OR ? HAS TEXT : 'Lastname name for non-business customer is mandatory' : 'error.customer.lastname'}
{ customer.registrationId : customer.registered = FALSE OR (? > 0 AND ? < 99999999) : 'Incorrect registration ID' : 'error.customer.registrationid'}
{ billingAddress.addressee : ? HAS NO TEXT OR length(?) <= 60 : 'Maximum length for Addressee is 60 characters' : 'error.baddress.addresse.length'}
{ billingAddress.addrLine1 : ? HAS TEXT AND length(?) <= 50 : 'Address line1 is mandatory and maximum length for address line1 is 50 characters' : 'error.baddress.addrline1.length'}
{ billingAddress.addrLine2 : ? HAS NO TEXT OR length(?) <= 50 : 'Maximum length for address line2 is 50 characters' : 'error.baddress.addrline2.length'}
{ billingAddress.city : ? HAS TEXT AND length(?) <= 30 : 'City is mandatory and maximum length for city is 30 characters' : 'error.baddress.city.length'}
{ billingAddress.zipCode : ? HAS TEXT AND length(?) <= 50 : 'Zipcode is mandatory and maximum length for zipcode is 5 characters' : 'error.baddress.zipcode.length'}
{ billingAddress.zipCode : match('[0-9]{5}',?) = TRUE : 'ZipCode must contain exactly 5 digits' : 'error.baddress.zipcode.format'}
{ billingAddress.state : (? HAS NO TEXT AND billingAddress.country != 'United States') OR (? HAS TEXT AND length(?) <= 2) : 'Maximum length for state is 2 characters' : 'error.baddress.state.length'}
{ billingAddress.country : ? HAS TEXT AND length(?) <= 50 : 'Country is mandatory and maximum length for country is 50 characters' : 'error.baddress.country.length'}
{ shippingAddress.addressee : shippingAddress IS NULL OR (? HAS TEXT AND length(?) <= 60) : 'Addressee is mandatory and maximum length for addressee is 60 characters' : 'error.saddress.addresse.length'}
{ shippingAddress.addrLine1 : shippingAddress IS NULL OR (? HAS TEXT AND length(?) <= 50) : 'Address line1 is mandatory and maximum length for address line1 is 50 characters' : 'error.baddress.addrline1.length'}
{ shippingAddress.addrLine2 : shippingAddress IS NULL OR (? HAS NO TEXT OR length(?) <= 50) : 'Maximum length for address line2 is 50 characters' : 'error.baddress.addrline2.length'}
{ shippingAddress.city : shippingAddress IS NULL OR (? HAS TEXT AND length(?) <= 30) : 'City is mandatory and maximum length for city is 30 characters' : 'error.baddress.city.length'}
{ shippingAddress.zipCode : shippingAddress IS NULL OR (? HAS TEXT AND length(?) <= 50) : 'Zipcode is mandatory and maximum length for zipcode is 5 characters' : 'error.baddress.zipcode.length'}
{ shippingAddress.zipCode : shippingAddress IS NULL OR (match('[0-9]{5}',?) = TRUE) : 'Zipcode must contain exactly 5 digits' : 'error.baddress.zipcode.format'}
{ shippingAddress.state : shippingAddress IS NULL OR ((? HAS NO TEXT AND billingAddress.country != 'United States') OR (? HAS TEXT AND length(?) <= 2)) : 'Maximum length for state is 2 characters' : 'error.baddress.state.length'}
{ shippingAddress.country : shippingAddress IS NULL OR (? HAS TEXT AND length(?) <= 50) : 'Country is mandatory and maximum length for country is 50 characters' : 'error.baddress.country.length'}
{ billing.paymentId : ? IN 'VISA','AMEX','ECMC','DCIN','PAYP' : 'Invalid payment type' : 'error.billing.type' }
{ billing.paymentDesc : match('[A-Z]{4}-[0-9]{10,11}',?) = TRUE : 'Invalid format of payment description' : 'error.billing.desc' }
{ shipping.shipperId : ? IN 'FEDX', 'UPS', 'DHL', 'DPD' : 'Invalid shipper ID' : 'error.shipping.shipper'}
{ shipping.shippingTypeId : ? IN 'STD', 'EXP', 'AMS', 'AME' : 'Invalid shipping type' : 'error.shipping.type' }
{ shipping.shippingInfo : ? HAS NO TEXT OR length(?) <= 100 : 'Maximum length for additional shipping info is 100 characters' }
{ lineItems : validateTotalItemsCount(totalItems,?) = TRUE : 'Bad count of total line items' : 'error.lineitems.badcount' }
{ lineItems : validateIds(?) = TRUE : 'One or more invalid item IDs' : 'error.lineitems.id' }
{ lineItems : validatePrices(?) = TRUE : 'One or more invalid item prices' : 'error.lineitems.price' }
{ lineItems : validateDiscounts(?) = TRUE : 'One or more invalid item discounts' : 'error.lineitems.discount' }
{ lineItems : validateShippingPrices(?) = TRUE : 'One or more invalid item shipping prices' : 'error.lineitems.shipping' }
{ lineItems : validateHandlingPrices(?) = TRUE : 'One or more invalid item handling prices' : 'error.lineitems.handling' }
{ lineItems : validateQuantities(?) = TRUE : 'One or more invalid item quantities' : 'error.lineitems.quantity' }
{ lineItems : validateTotalPrices(?) = TRUE : 'One or more invalid item total prices' : 'error.lineitems.totalprice' }
]]>
</value>
</property>
<property name="customFunctions">
<map>
<entry key="isFutureDate" value="org.springframework.batch.sample.domain.order.internal.valang.FutureDateFunction" />
<entry key="validateTotalItemsCount" value="org.springframework.batch.sample.domain.order.internal.valang.TotalOrderItemsFunction" />
<entry key="validateIds" value="org.springframework.batch.sample.domain.order.internal.valang.ValidateIdsFunction" />
<entry key="validatePrices" value="org.springframework.batch.sample.domain.order.internal.valang.ValidatePricesFunction" />
<entry key="validateDiscounts" value="org.springframework.batch.sample.domain.order.internal.valang.ValidateDiscountsFunction" />
<entry key="validateShippingPrices" value="org.springframework.batch.sample.domain.order.internal.valang.ValidateShippingPricesFunction" />
<entry key="validateHandlingPrices" value="org.springframework.batch.sample.domain.order.internal.valang.ValidateHandlingPricesFunction" />
<entry key="validateQuantities" value="org.springframework.batch.sample.domain.order.internal.valang.ValidateQuantitiesFunction" />
<entry key="validateTotalPrices" value="org.springframework.batch.sample.domain.order.internal.valang.ValidateTotalPricesFunction" />
</map>
</property>
</bean>
<bean id="orderValidator" class="org.springframework.batch.sample.domain.order.internal.validator.OrderValidator"/>
</property>
</bean>
</beans>
</beans>

View File

@@ -3,9 +3,9 @@
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<import resource="data-source-context.xml" />
<import
resource="classpath:/org/springframework/batch/sample/config/common-context.xml" />
@@ -19,7 +19,7 @@
</bean>
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"
p:isolationLevelForCreate = "${batch.isolationlevel}"
p:dataSource-ref="dataSource" p:transactionManager-ref="transactionManager" p:lobHandler-ref="lobHandler"/>
@@ -48,4 +48,4 @@
<bean id="eventAdvice"
class="org.springframework.batch.sample.jmx.StepExecutionApplicationEventAdvice" />
</beans>
</beans>