001/*
002 * Copyright 2006-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.batch.sample.domain.person;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.springframework.batch.sample.domain.order.Address;
023
024/**
025 * Custom class that contains logic that would normally be be contained in
026 * {@link org.springframework.batch.item.ItemReader} and
027 * {@link javax.batch.api.chunk.ItemWriter}.
028 * 
029 * @author tomas.slanina
030 * @author Robert Kasanicky
031 */
032public class PersonService {
033        private static final int GENERATION_LIMIT = 10;
034
035        private int generatedCounter = 0;
036        private int processedCounter = 0;
037
038        public Person getData() {
039                if (generatedCounter >= GENERATION_LIMIT) {
040                        return null;
041                }
042
043                Person person = new Person();
044                Address address = new Address();
045                Child child = new Child();
046                List<Child> children = new ArrayList<Child>(1);
047
048                children.add(child);
049
050                person.setFirstName("John" + generatedCounter);
051                person.setAge(20 + generatedCounter);
052                address.setCity("Johnsville" + generatedCounter);
053                child.setName("Little Johny" + generatedCounter);
054
055                person.setAddress(address);
056                person.setChildren(children);
057
058                generatedCounter++;
059
060                return person;
061        }
062
063        /*
064         * Badly designed method signature which accepts multiple implicitly related
065         * arguments instead of a single Person argument.
066         */
067        public void processPerson(String name, String city) {
068                processedCounter++;
069        }
070
071        public int getReturnedCount() {
072                return generatedCounter;
073        }
074
075        public int getReceivedCount() {
076                return processedCounter;
077        }
078}