/**
 * Copyright (C) 2013 – 2017 SLUB Dresden & Avantgarde Labs GmbH (<code@dswarm.org>)
 *
 * 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 org.dswarm.converter.flow;

import java.io.Reader;
import java.util.Collection;

import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import org.culturegraph.mf.framework.ObjectPipe;
import org.culturegraph.mf.framework.ObjectReceiver;
import org.culturegraph.mf.types.Triple;
import rx.Observable;
import rx.Subscriber;

import org.dswarm.converter.DMPConverterException;
import org.dswarm.converter.mf.stream.converter.StreamToRecordTriples;
import org.dswarm.converter.mf.stream.reader.CsvReader;
import org.dswarm.converter.pipe.timing.ObjectTimer;
import org.dswarm.converter.pipe.timing.StreamTimer;
import org.dswarm.converter.pipe.timing.TimerBasedFactory;
import org.dswarm.persistence.model.resource.DataModel;

/**
 * @author phorn
 */
public class MonitoringCSVSourceResourceTriplesFlow extends CSVSourceResourceTriplesFlow {

	private final TimerBasedFactory timerBasedFactory;

	@Inject
	private MonitoringCSVSourceResourceTriplesFlow(
			final TimerBasedFactory timerBasedFactory,
			@Assisted final DataModel dataModel) throws DMPConverterException {
		super(dataModel);
		this.timerBasedFactory = timerBasedFactory;
	}

	@Override
	protected Observable<Collection<Triple>> process(final ObjectPipe<String, ObjectReceiver<Reader>> opener, final String obj,
			final CsvReader pipe) {

		final ObservableRecordTriplesReceiver tripleReceiver = new ObservableRecordTriplesReceiver();
		final ObjectTimer<Reader> csvReaderTimer = timerBasedFactory.forObject(MonitoringFlowStatics.INPUT_RESOURCE_FILES);
		final StreamTimer csvInputTimer = timerBasedFactory.forStream(MonitoringFlowStatics.CSV_RECORDS);
		final ObjectTimer<Collection<Triple>> csvTriplesTimer = timerBasedFactory.forObject(MonitoringFlowStatics.CSV_TRIPLES);

		pipe
				.setReceiver(csvInputTimer)
				.setReceiver(new StreamToRecordTriples())
				.setReceiver(csvTriplesTimer)
				.setReceiver(tripleReceiver);

		opener.setReceiver(csvReaderTimer).setReceiver(pipe);

		return Observable.create(new Observable.OnSubscribe<Collection<Triple>>() {

			@Override public void call(final Subscriber<? super Collection<Triple>> subscriber) {

				tripleReceiver.getObservable().subscribe(subscriber);

				opener.process(obj);
				opener.closeStream();
			}
		});
	}
}

@@ -688,61 +688,8 @@
 w;%0A%0A
-import java.io.Reader;%0Aimport java.util.Collection;%0A%0A
 impo
@@ -914,53 +914,8 @@
 le;%0A
-import rx.Observable;%0Aimport rx.Subscriber;%0A%0A
 impo
@@ -1308,16 +1308,110 @@
 taModel;
+%0Aimport rx.Emitter;%0Aimport rx.Observable;%0A%0Aimport java.io.Reader;%0Aimport java.util.Collection;
 %0A%0A/**%0A *
@@ -2662,151 +2662,25 @@
 ate(
-new Observable.OnSubscribe%3CCollection%3CTriple%3E%3E() %7B%0A%0A%09%09%09@Override public void call(final Subscriber%3C? super Collection%3CTriple%3E%3E subscriber)
+subscriber -%3E
  %7B%0A%0A
-%09
 %09%09%09t
@@ -2732,25 +2732,24 @@
 riber);%0A%0A%09%09%09
-%09
 opener.proce
@@ -2757,17 +2757,16 @@
 s(obj);%0A
-%09
 %09%09%09opene
@@ -2788,14 +2788,42 @@
 ;%0A%09%09
-%09%7D%0A%09%09%7D
+%7D, Emitter.BackpressureMode.BUFFER
 );%0A%09

make use BackPressureMode.BUFFER @ Observable creation @ MonitoringCSVSourceResourceTriplesFlow