com.univocity.parsers.common.processor.MasterDetailProcessor Maven / Gradle / Ivy
Show all versions of univocity-parsers Show documentation
/*******************************************************************************
* Copyright 2014 uniVocity Software Pty Ltd
*
* 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 com.univocity.parsers.common.processor;
import java.util.*;
import com.univocity.parsers.common.*;
import com.univocity.parsers.conversions.*;
/**
*
* A {@link RowProcessor} implementation for associating rows extracted from any implementation of {@link AbstractParser} into {@link MasterDetailRecord} instances.
*
* For each row processed, a call to {@link MasterDetailProcessor#isMasterRecord(String[], ParsingContext)} will be made to identify whether or not it is a master row.
*
The detail rows are automatically associated with the master record in an instance of {@link MasterDetailRecord}.
*
When the master record is fully processed (i.e. {@link MasterDetailRecord} contains a master row and all associated detail rows), it is sent to the user for processing in {@link MasterDetailProcessor#masterDetailRecordProcessed(MasterDetailRecord, ParsingContext)}.
*
*
Note this class extends {@link ObjectRowProcessor} and value conversions provided by {@link Conversion} instances are fully supported.
*
* @see MasterDetailRecord
* @see RowPlacement
* @see AbstractParser
* @see ObjectRowListProcessor
* @see RowProcessor
*
* @author uniVocity Software Pty Ltd - [email protected]
*
*/
public abstract class MasterDetailProcessor extends ObjectRowProcessor {
private ObjectRowListProcessor detailProcessor;
private MasterDetailRecord record;
private boolean isMasterRowAboveDetail;
/**
* Creates a MasterDetailProcessor
*
* @param rowPlacement indication whether the master records are placed in relation its detail records in the input.
*
*
*
* Master record (Totals) Master record (Totals)
* above detail records under detail records
*
* Totals | 100 Item | 60
* Item | 60 Item | 40
* Item | 40 Totals | 100
*
* @param detailProcessor the {@link ObjectRowListProcessor} that processes detail rows.
*/
public MasterDetailProcessor(RowPlacement rowPlacement, ObjectRowListProcessor detailProcessor) {
ArgumentUtils.noNulls("Row processor for reading detail rows", detailProcessor);
this.detailProcessor = detailProcessor;
this.isMasterRowAboveDetail = rowPlacement == RowPlacement.TOP;
}
/**
* Creates a MasterDetailProcessor assuming master records are positioned above its detail records in the input.
*
* @param detailProcessor the {@link ObjectRowListProcessor} that processes detail rows.
*/
public MasterDetailProcessor(ObjectRowListProcessor detailProcessor) {
this(RowPlacement.TOP, detailProcessor);
}
/**
* {@inheritDoc}
*/
@Override
public void processStarted(ParsingContext context) {
detailProcessor.processStarted(context);
}
/**
* Invoked by the parser after all values of a valid record have been processed.
*
* This method will then try to identify whether the given record is a master record.
*
If it is, any conversions applied to the fields of the master record will be executed;
*
Otherwise, the parsed row will be delegated to the {@link MasterDetailProcessor#detailProcessor} given in the constructor, and a detail record will be associated with the current {@link MasterDetailRecord}
*
*
* @param row the data extracted by the parser for an individual record.
* @param context A contextual object with information and controls over the current state of the parsing process
*/
@Override
public final void rowProcessed(String[] row, ParsingContext context) {
if (isMasterRecord(row, context)) {
super.rowProcessed(row, context);
} else {
detailProcessor.rowProcessed(row, context);
}
}
/**
* Invoked by the parser after all values of a valid record have been processed and any conversions have been executed.
*
* @param row the data extracted by the parser for an individual record.
* @param context A contextual object with information and controls over the current state of the parsing process
*/
@Override
public final void rowProcessed(Object[] row, ParsingContext context) {
if (record == null) {
record = new MasterDetailRecord();
record.setMasterRow(row);
if (isMasterRowAboveDetail) {
return;
}
}
processRecord(row, context);
}
/**
* Associates individual rows to a {@link MasterDetailRecord} and invokes {@link MasterDetailProcessor#masterDetailRecordProcessed(MasterDetailRecord, ParsingContext)} when it is fully populated.
* @param row a record extracted from the parser that had all (if any) conversions executed and is ready to be sent to the user.
* @param context A contextual object with information and controls over the current state of the parsing process
*/
private void processRecord(Object[] row, ParsingContext context) {
List