lcn.module.batch.core.item.composite.reader.CompositePagingReader.java Source code

Java tutorial

Introduction

Here is the source code for lcn.module.batch.core.item.composite.reader.CompositePagingReader.java

Source

/*
 * 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 lcn.module.batch.core.item.composite.reader;

import java.util.List;

import lcn.module.batch.core.item.composite.ItemsMapper;

import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.ItemStreamReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.batch.item.database.AbstractPagingItemReader;

/**
 * CompositeReader  ItemReaderList? ?? ?? ? 
 * 
 * @author 
 * @since 2012. 07.30
 * @version 1.0
 * @see
 * 
 *      <pre>
 * << ?(Modification Information) >>
 *   
 *   ?               ?               
 *  -------      --------     ---------------------------
 *  2012. 10.20       ?
 * 
 * </pre>
 */
public class CompositePagingReader<T> implements ItemStreamReader<T> {
    private List<AbstractPagingItemReader<?>> itemReaderList;
    private ItemsMapper<T> itemsMapper;
    private String returnType;

    public T read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
        Object[] items = new Object[itemReaderList.size()];

        int count = 0;
        int flagCount = 0;
        if (returnType.toUpperCase().equals("READER")) {

            for (AbstractPagingItemReader<?> itemReader : itemReaderList) {
                items[count] = itemReader;
                count++;
            }
        } else {
            for (AbstractPagingItemReader<?> itemReader : itemReaderList) {
                Object o = null;

                if ((o = itemReader.read()) == null) {
                    flagCount = flagCount + 1;
                } else {
                    items[count] = o;
                }
                count++;

                if (flagCount == itemReaderList.size()) {
                    return null;
                }
            }
        }
        return itemsMapper.mapItems(items);
    }

    /**
     * ? ? ??  Reader? update 
     * 
     * @see org.springframework.batch.item.ItemStream#update(ExecutionContext)
     */
    public void update(ExecutionContext executionContext) {
        for (ItemStream itemStream : itemReaderList) {
            itemStream.update(executionContext);
        }
    }

    /**
     *  Reader? close  
     * 
     * @throws ItemStreamException
     */
    public void close() throws ItemStreamException {
        for (ItemStream itemStream : itemReaderList) {
            itemStream.close();
        }
    }

    /**
     *  Reader? open?  
     * 
     * @throws ItemStreamException
     */
    public void open(ExecutionContext executionContext) throws ItemStreamException {
        for (ItemStream itemStream : itemReaderList) {
            itemStream.open(executionContext);
        }
    }

    /**
     * ??  ? ItemMapper? Setting 
     * 
     * @param mapper
     */
    public void setItemsMapper(ItemsMapper<T> mapper) {
        this.itemsMapper = mapper;
    }

    /**
     * ?? ItemReaderList? ? ?  Setting
     * 
     * @param itemReaderList
     */
    public void setItemReaderList(List<AbstractPagingItemReader<?>> itemReaderList) {
        this.itemReaderList = itemReaderList;
    }

    /**
     * ??  ? returnType? Setting 
     * 
     * @param returnType
     */
    public void setReturnType(String returnType) {
        this.returnType = returnType;
    }

}