package org.jetel.component;

import java.util.Properties;

import org.jetel.data.formatter.Formatter;
import org.jetel.exception.ComponentNotReadyException;
import org.jetel.graph.Node;
import org.jetel.util.exec.DataConsumer;
import org.jetel.util.string.StringUtils;

/**
 * It is a base class for all kinds of bulkloaders.
 * 
 * @author Miroslav Haupt (mirek.haupt@javlin.cz)<br>
 *         (c) Javlin Consulting (www.javlin.cz)
 * @see org.jetel.graph.TransformationGraph
 * @see org.jetel.graph.Node
 * @see org.jetel.graph.Edge
 * @since 5.2.2009
 *
 */
public abstract class BulkLoader extends Node {

	protected final static String EQUAL_CHAR = "=";
	
	// variables for load utility's command
	protected String loadUtilityPath = null; // format data to load utility format and write them to dataFileName
	protected String dataURL = null; // fileUrl from XML - data file that is used when no input port is connected or for log
	protected String table = null;
	protected String user = null;
	protected String password = null;
	protected String columnDelimiter = null;
	protected String database = null;
	
	protected String parameters = null;
	protected Properties properties = null;
	
	protected DataConsumer consumer = null; // consume data from out stream of load utility
	protected DataConsumer errConsumer = null; // consume data from err stream of utility - write them to by logger
	protected Formatter formatter = null; // format data to load utility format and write them to dataFileName
	
	public BulkLoader(String id, String loadUtilityPath, String database) {
		super(id);
		this.loadUtilityPath = loadUtilityPath;
		this.database = database;
	}

	@Override
	public void init() throws ComponentNotReadyException {
		super.init();
		
		properties = parseParameters(parameters);
	}

	/**
	 * Create instance of Properties from String. 
	 * Parse parameters from string "parameters" and fill properties by them.
	 * 
	 * @param parameters string that contains parameters
	 * @return instance of Properties created by parsing string
	 */
	private static Properties parseParameters(String parameters) {
		Properties properties = new Properties();

		if (parameters != null) {
			for (String param : StringUtils.split(parameters)) {
				String[] par = param.split(EQUAL_CHAR);
				properties.setProperty(par[0], par.length > 1 ? StringUtils.unquote(par[1]) : "true");
			}
		}

		return properties;
	}
}

@@ -655,16 +655,144 @@
  %22=%22;%0A%09%0A
+%09protected final static int READ_FROM_PORT = 0;%0A    protected final static int WRITE_TO_PORT = 0;%09//port for write bad record%0A%09%0A
 %09// vari
@@ -1626,16 +1626,338 @@
 eName%0A%09%0A
+%09/**%0A     * true - data is read from in port;%0A     * false - data is read from file directly by load utility%0A     */%0A    protected boolean isDataReadFromPort;%0A    %0A    /**%0A     * true - bad rows is written to out port;%0A     * false - bad rows isn't written to anywhere%0A     */%0A    protected boolean isDataWrittenToPort;%0A%09%0A
 %09public 
@@ -2194,16 +2194,123 @@
 t();%0A%09%09%0A
+%09%09isDataReadFromPort = !getInPorts().isEmpty();%0A        isDataWrittenToPort = !getOutPorts().isEmpty();%0A%09%09%0A
 %09%09proper

add isDataReadFromPort and isDataWriteToPort attribute and their init