package de.finetech.utils;

import static de.abas.eks.jfop.remote.EKS.Uvar;
import static de.abas.eks.jfop.remote.EKS.art;
import static de.abas.eks.jfop.remote.EKS.datei;
import static de.abas.eks.jfop.remote.EKS.hinweis;
import static de.abas.eks.jfop.remote.EKS.println;
import static de.abas.eks.jfop.remote.EKS.system;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Vector;
/**
 * führt einen Infosystem aufruf mit edpinfosys.sh
 * aus
 * 
 * @author MKürbis
 *
 */
public class Infosystemcall {

	protected String infosystem = null;
	protected String outputFile = null;
	protected String resultFilter = null;

	protected Vector<String> headOutputFields = new Vector<String>();
	protected Vector<String> tableOutputFields = new Vector<String>();
	protected LinkedHashMap<String, String> headParameter = new LinkedHashMap<String, String>();

	protected boolean showAllTableFields = false;
	protected boolean showAllHeadFields = false;

	public static Infosystemcall build(String infosystem,
			String outputFile) {
		return new Infosystemcall(infosystem, outputFile);
	}

	public static Infosystemcall build(String infosystem) {
		String varName = new RandomString(5).nextString();
		art("text " + varName);
		datei("-FOP -tempname U|" + varName);
		String outputFile = Uvar(varName);
		return new Infosystemcall(infosystem, outputFile);
	}

	// edpinfosys.sh
	private Infosystemcall(String infosystem, String outputFile) {
		this.infosystem = infosystem;
		this.outputFile = outputFile;
	}

	public Infosystemcall setHeadParameter(String name, String value) {
		this.headParameter.put(name,
				value == null ? "" : value);
		return this;
	}
	
	public Infosystemcall setTableParameter(String name, int row, String value) {
		this.headParameter.put(row+":"+name,
				value == null ? "" : value);
		return this;
	}

	public Infosystemcall addHeadOutputField(String name) {
		this.headOutputFields.add(name);
		return this;
	}

	public Infosystemcall addTableOutputField(String name) {
		this.tableOutputFields.add(name);
		return this;
	}

	public Infosystemcall setShowAllTableFields(
			boolean showAllTableFields) {
		this.showAllTableFields = showAllTableFields;
		return this;
	}

	public Infosystemcall setShowAllHeadFields(boolean showAllHeadFields) {
		this.showAllHeadFields = showAllHeadFields;
		return this;
	}

	public Infosystemcall setResultFilter(String filter) {
		this.resultFilter = filter;
		return this;
	}

	private String buildCommand() {
		String cmd = "edpinfosys.sh -P -F -N " + this.infosystem;
		String foo = "";
		if (this.headParameter.size() > 0) {
			cmd += " -s ";
			for (String key : this.headParameter.keySet()) {
				foo += (key + "=" + this.headParameter.get(key) + ",");
			}
			foo = foo.substring(0, foo.length() - 1);
			cmd += foo;
			foo = "";
		}
		if (this.showAllHeadFields) {
			cmd += " -k +";
		} else {
			if (this.headOutputFields.size() > 0) {
				cmd += " -k ";
				for (String value : this.headOutputFields) {
					foo += value + ",";
				}
				foo = foo.substring(0, foo.length() - 1);
				cmd += foo;
				foo = "";
			}
		}
		if (this.showAllTableFields) {
			// cmd +=" -f +";
		} else {
			if (this.tableOutputFields.size() > 0) {
				cmd += " -f ";
				for (String value : this.tableOutputFields) {
					foo += value + ",";
				}
				foo = foo.substring(0, foo.length() - 1);
				cmd += foo;
				foo = "";
			} else {
				cmd += " -f -";
			}
		}
		if (this.resultFilter != null && !this.resultFilter.isEmpty()) {
			cmd += " -T '" + this.resultFilter + "'";
		}
		cmd += " > " + this.outputFile;
		return cmd;
	}
	
	public String getCommand(){
		String foo = "";
		for (String key : this.headParameter.keySet()) {
			foo += (key + "=" + this.headParameter.get(key) + "|");
		}
		foo = foo.substring(0, foo.length() - 1);
		//FIXME sprachunabhängigkeit
		return "*<Infosystem>"+this.infosystem+"<hole>?"+foo;
	}

	public InfosystemcallResult execute() throws IOException {
		hinweis("-SOFORT hole Daten von Infosystem " + this.infosystem);
		if (system("\"" + this.buildCommand() + "\" PASSWORT hinter")) {
			hinweis("-SOFORT Datenbeschaffung von Infosystem "
					+ this.infosystem + " erfolgreich");
			return new InfosystemcallResult(this);
		}
		hinweis("-SOFORT Datenbeschaffung von Infosystem " + this.infosystem
				+ " fehlgeschlagen");
		try {
			BufferedReader br = new BufferedReader(new FileReader(
					this.outputFile));
			for (String line; (line = br.readLine()) != null;) {
				println(line);
			}
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}

Create Infosystemcall.java