/*
* Copyright (C) 2010 Alexander Kolosov
*
* This file is part of FlowBrook.
*
* FlowBrook is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* FlowBrook is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FlowBrook. If not, see <http://www.gnu.org/licenses/>
*/
package ru.petrsu.akolosov.flowbrook.processing;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import ru.petrsu.akolosov.flowbrook.FlowRecord;
import ru.petrsu.akolosov.flowbrook.FlowRecordExtractionException;
import ru.petrsu.akolosov.flowbrook.FlowRecordIterator;
import ru.petrsu.akolosov.flowbrook.FlowSource;
import ru.petrsu.akolosov.flowbrook.FlowSourceException;
/**
*
* @author Alexander Kolosov
*/
public final class FlowSourceHandler {
private final List<FlowDataProcessingRule> processingRules =
new LinkedList<FlowDataProcessingRule>();
private final static DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
public FlowSourceHandler() {
}
public FlowSourceHandler(Collection<FlowDataProcessingRule> processingRules) {
this.processingRules.addAll(processingRules);
}
public void addFlowProcessingRule(FlowDataProcessingRule rule) {
processingRules.add(rule);
}
public Collection<FlowDataProcessingRule> getFlowProcessingRules() {
return processingRules;
}
public void process(FlowSource flowSource) throws FlowProcessingException {
try {
process(flowSource.getRecordIterator(),
flowSource.getFirstFlowTime(), flowSource.getLastFlowTime());
} catch (FlowSourceException ex) {
throw new FlowProcessingException(ex);
}
}
public List<FlowRecordHandler> process(FlowRecordIterator recordIterator, Date startDate, Date endDate) throws FlowProcessingException {
List<FlowRecordHandler> recordHandlers =
new ArrayList<FlowRecordHandler>(processingRules.size());
FlowRecord rec = null;
for (FlowDataProcessingRule rule : processingRules) {
FlowRecordHandler h = rule.getRecordHandlerInstance();
h.setStartDate(startDate);
h.setEndDate(endDate);
recordHandlers.add(h);
}
while (recordIterator.hasNextRecord()) {
try {
rec = recordIterator.getNextRecord(rec);
} catch (FlowRecordExtractionException ex) {
throw new FlowProcessingException(ex);
}
for (int i = 0; i < recordHandlers.size(); i++) {
recordHandlers.get(i).process(rec);
}
//recordIterator.restoreFlowRecord(rec);
}
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Flow source from " + df.format(startDate) + " to " + df.format(endDate) + ": processing finished");
return recordHandlers;
}
}