View Javadoc

1   /*
2    * Copyright 2006-2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.batch.sample.domain.person;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.springframework.batch.item.ItemReader;
23  import org.springframework.batch.item.ItemWriter;
24  import org.springframework.batch.sample.domain.order.Address;
25  
26  /**
27   * Custom class that contains logic that would normally be be contained in
28   * {@link ItemReader} and {@link ItemWriter}.
29   * 
30   * @author tomas.slanina
31   * @author Robert Kasanicky
32   */
33  public class PersonService {
34  
35  	private static final int GENERATION_LIMIT = 10;
36  
37  	private int generatedCounter = 0;
38  
39  	private int processedCounter = 0;
40  
41  	public Person getData() {
42  		if (generatedCounter >= GENERATION_LIMIT)
43  			return null;
44  
45  		Person person = new Person();
46  		Address address = new Address();
47  		Child child = new Child();
48  		List<Child> children = new ArrayList<Child>(1);
49  
50  		children.add(child);
51  
52  		person.setFirstName("John" + generatedCounter);
53  		person.setAge(20 + generatedCounter);
54  		address.setCity("Johnsville" + generatedCounter);
55  		child.setName("Little Johny" + generatedCounter);
56  
57  		person.setAddress(address);
58  		person.setChildren(children);
59  
60  		generatedCounter++;
61  
62  		return person;
63  	}
64  
65  	/*
66  	 * Badly designed method signature which accepts multiple implicitly related
67  	 * arguments instead of a single Person argument.
68  	 */
69  	public void processPerson(String name, String city) {
70  		processedCounter++;
71  	}
72  
73  	public int getReturnedCount() {
74  		return generatedCounter;
75  	}
76  
77  	public int getReceivedCount() {
78  		return processedCounter;
79  	}
80  }