package systems.sdw.baseapps;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;

import systems.sdw.basetech.json.JSONException;
import systems.sdw.basetech.json.JSONObject;


public class Protokoll {
	
	public final static String DRIVER_SAPDB="com.sap.dbtech.jdbc.DriverSapDB";
	public final static String DRIVER_MAXDB="com.sap.dbtech.jdbc.DriverSapDB";
	public final static String DRIVER_ORACLE="oracle.jdbc.driver.OracleDriver";
	public final static String DRIVER_SQLSERVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
	public final static String DRIVER_POSTGRESQL = "org.postgresql.Driver";
	public final static String DRIVER_MSSQLJTDS = "net.sourceforge.jtds.jdbc.Driver";


	private static final String jndiSDW="jdbc/SDW";
	
	public static final String jndiDefault=jndiSDW;

	public static final String encodingUTF8="UTF-8";	
	public static final String encodingISO="ISO-8859-1";
	public static final String encodingDefault=encodingUTF8;
	
	public static final String OptionNotSet="notSet";

	//	 Query
	public static final String muster="muster";
	public static final String stichtag="stichtag";

	//	Frontcontroller
	public static final String tmpDir="tmpdir";	
	public static final String resourcePath="resourcepath";	
	public static final String dmsDir="dmsdir";	
	
	public static final String barrierefrei="bf";
	public static final String opcode="opc";
	public static final String oid="oid";
	public static final String cls="cls";
	public static final String date="date";
	public static final String data="data";
	public static final String event="event";
	public static final String xml="XML";
	public static final String json="JSON";
	public static final String jsonimage="jsonimage";
	public static final String token="token";
	public static final String action="action";
	public static final String magic="magic";

	public static final String name="name";
	public static final String konfig="konfig";
	public static final String modulename="modulename";
	public static final String aktiv="aktiv";
	public static final String uri="uri";
	public static final String reihenfolge="reihenfolge";
	public static final String uuid="uuid";
	public static final String pass="pass";
	public static final String zugriffsrecht="zugriffsrecht";
	public static final String filename="filename";
	
	public static final String command="command";
	public static final String params="params";
	public static final String param="param";
	public static final String value="value";
	public static final String type="type";
	public static final String default_="default";
	public static final String typeNumber="number";
	public static final String typeString="string";
	
	public static final String itemCount="itemCount";
	public static final String resultSetCount="resultSetCount";
	
	public static final String attribut="attribut";
	public static final String wert="wert";
	public static final String titel="titel";
	public static final String beschreibung="beschreibung";
	public static final String exception="exception";

	public static final String zeitraum="zeitraum";
	public static final String von="von";
	public static final String bis="bis";
	public static final String dauer="dauer";
	public static final String datum="datum";
	public static final String unbegrenzt="unbegrenzt";
	public static final String allDay="allDay";

	public static final String filter="filter";

	public static final String html="html";
	public static final String result="result";

	public static final String identifier="identifier";
	public static final String label="label";
	public static final String field="field";
	public static final String timestamp="timestamp";
	public static final String linkLabel="linkLabel";

	public static final String memo="memo";
	public static final String memoNeu="memoneu";
	public static final String memoAlt="memoalt";

	public static final String id="id";
	public static final String desc="desc";	
	public static final String qualifiedName="qualifiedname";
	public static final String schluessel="schluessel";
	public static final String nummer="nummer";
	public static final String positionen="positionen";
	public static final String items="items";
	public static final String itemsEingeklappt="_items_";
	public static final String queryresult="queryresult";
	public static final String item="item";
	public static final String invers="invers";
	public static final String itemlist="itemlist";
	public static final String sortierung="sortierung";
	
	public static final String referenzobjekt="referenzobjekt";
	
	public static final String summe="summe";
	public static final String summeEinnahmen="summe_einnahmen";
	public static final String summeHonorar="summe_honorar";

	
	public static final String status="status";
	public static final String info="info";
	public static final String verfuegbarkeit="verfuegbarkeit";

	public static final String sessionpersonal="systems_sdw_sessionpersonal";
	
	public static final String geodata="systems_sdw_Geodata";
	
	public static final String hasChildren="hasChildren";
	public static final String children="children";
	public static final String reference="_reference";
	public static final String level="level";
	public static final String kurzname="kurzname";
	public static final String systematik="systematik";
	public static final String flag="flag";
	public static final String checked="checked";
	public static final String menue="menue";
	public static final String cssKlasse="cssklasse";
	public static final String klasse="klasse";
	
	public static final String relation="relation";
	
	public static final String locale="locale";
	public static final String chart="chart";
	public static final String container="container";

	public static final String text="text";
	public static final String owner="owner";
	public static final String dirtyFlag="_dirty_";
	public static final String deleteFlag="_deleted_";
	public static final String insertFlag="_inserted_";
	public static final String updateFlag="_updated_";
	public static final String tooltip="tooltip";
	public static final String calendar="calendar";
	public static final String ok="ok";
	public static final String geo="geo";
	
	public static final String before="before";
	public static final String after="after";
	
	//	Vereinheitlichung der Information, ob optionlists und so  das default-Objekt aus sys_defaultsobjects enthalten soll oder nicht
	public static final String defaultObjects="defaultObjects";
	
	
	
	public static final String datentyp="datentyp";
	public static final String datentypspalte="datentypspalte";

	public static final String alphabetSelektor="alphabetselektor";
	public static final String yearSelektor="yearselektor";
	
	
	public static final String contenttypeForm="application/x-www-form-urlencoded";
	public static final String contenttypeMmultipart="multipart/form-data";
	public static final String contenttypeOctetStream="application/octet-stream";
	
	public static final String MIMETYPE_PDF = "application/pdf";
	public static final String MIMETYPE_XML = "text/xml";
	public static final String MIMETYPE_TXT = "text/plain";
	public static final String MIMETYPE_HTML = "text/html";	
	public static final String MIMETYPE_GIF = "image/gif";
	public static final String MIMETYPE_JPG = "image/jpeg";
	public static final String MIMETYPE_PNG = "image/png";
	public static final String MIMETYPE_WORD = "application/msword";
	public static final String MIMETYPE_POWERPOINT = "application/mspowerpoint";
	public static final String MIMETYPE_EXCEL = "application/msexcel";
	public static final String MIMETYPE_PS = "application/postscript";
	
	public static final String response="response";
	
	public static final String sessionNutzer="nutzer";
	public static final String sessionNutzerSession="nutzersession";
	public static final String session_organisation="organisation";

	public static final String sERROR_404_PDF="404_FileNotFound.pdf";
	public static final String cOPEN_WINDOW_PARAMLISTE="menubar=yes,dependent=yes,status=yes,resizable=yes,scrollbars=yes,Width=800,Height=600,Screenx=25,Screeny=25";

	public static final String beschreibungDefault="Zeitstempel:"+getTimestamp();
	public static final String beschreibungAuto="n/a";
	
	
	public static final String rangeKlasseDate="iso_date_range";
	public static final String rangeKlasseTimestamp="iso_timestamp_range";
	public static final String rangeKlasseTime="iso_time_range";

	
	public static final String loremIpsum="Weit hinten, hinter den Wortbergen, fern der Länder Vokalien und Konsonantien leben die Blindtexte. Abgeschieden wohnen sie in Buchstabhausen an der Küste des Semantik, eines großen Sprachozeans. Ein kleines Bächlein namens Duden fließt durch ihren Ort und versorgt sie mit den nötigen Regelialien. Es ist ein paradiesmatisches Land, in dem einem gebratene Satzteile in den Mund fliegen.";
	public static final String readOnly = "readOnly";
	
	public static final String gesperrtStatus = "gesperrtStatus";
	public static final String archivStatus = "archivStatus";

	// REST - Style
	public static final String jsonResponseStyle = "responsStyle";
	public static final String hostPart = "hostPart";
	public static final String styleREST = "rest";
	public static final String styleClassic = "classic";
	public static final String restUrl = "url";


	public static final String authInfo = "authInfo";
	public static final String roleInfo = "roleInfo";

	public static final String gridLabel = "_gridLabel";
	
	
	/**
	 * Quick and dirty, sollte aber für die einfachen Tags in den Labeln reichen
	 * @param input
	 * @return
	 */
	public static String stripHTMLTags(String input) {
		return input.replaceAll("<[^>]*>", "");
	}

	
	/**
	 * Wird insbesonderee bei den Piechart-Labeln verwendet, die können nicht mit UTF-8 umgehen.
	 */
	private static String[][] UMLAUT_REPLACEMENTS = { { "Ä", "Ae" }, { "Ü", "Ue" }, { "Ö", "Oe" }, { "ä", "ae" }, { "ü", "ue" }, { "ö", "oe" }, { "ß", "ss" }, { "-", "" } };
	public static String replaceUmlaute(String orig) {
	    String result = orig;

	    for (int i = 0; i < UMLAUT_REPLACEMENTS.length; i++) {
	        result = result.replaceAll(UMLAUT_REPLACEMENTS[i][0], UMLAUT_REPLACEMENTS[i][1]);
	    }
	    return result;
	}

	/**
	 * Entfernt alle potentiell problematischen Zeichen aus dem String
	 * @param string
	 * @return
	 */
	public static String string2JsonCompatibleString(String string) {
		String result="";
		if (string!=null) result=string.replace("\n", " ").replace("\r", " ").replace("\t", " ").replace("null", "").replace("\"", "'").replace(",", " ").trim();
		return result;
	}
	
	public static String getTimestamp() {
		return new java.text.SimpleDateFormat("dd.MM.yyyy-HH.mm.ss").format(new java.util.Date());
	}

	public static String getTimestampMilliNoDots(java.util.Date date) {
		return new java.text.SimpleDateFormat("dd-MM-yyyy-HH-mm-ss-SSS").format(date);
	}

	public static String getTimestampMilli(java.util.Date date) {
		return new java.text.SimpleDateFormat("dd.MM.yyyy-HH.mm.ss.SSS").format(date);
	}
	public static String getTimestampMilli() {
		return 	getTimestampMilli(new java.util.Date());
	}

	public static Boolean getBoolean(JSONObject jsonData, String jsonTag, Boolean currentValue, boolean defaultValue) {
		return jsonData.getBoolean(jsonTag, currentValue, defaultValue);
	}
	
	public static Double getDouble(JSONObject jsonData, String jsonTag, Double currentValue, Double defaultValue) {
		Double value=currentValue;
		try {value=jsonData.getDouble(jsonTag);}catch (JSONException ignored) {}
		if (value==null) value=defaultValue;
		return value;
	}

	
	public static Integer getInteger(JSONObject jsonData, String jsonTag, Integer currentValue, Integer defaultValue) {
		Integer value=currentValue;
		try {value=jsonData.getInteger(jsonTag);}catch (JSONException ignored) {}
		if (value==null) value=defaultValue;
		return value;
	}

	public static Long getLong(JSONObject jsonData, String jsonTag, Long currentValue, Long defaultValue) {
		Long value=currentValue;
		try {value=jsonData.getLong(jsonTag);}catch (JSONException ignored) {}
		if (value==null) value=defaultValue;
		return value;
	}

	public static String getString(JSONObject jsonData, String jsonTag, String currentValue) {
		return getString(jsonData, jsonTag, currentValue, "");
	}

	/**
	 * Der default-Value wird auch gesetzt wenn der String nicht null aber leer ist.
	 * @param jsonData
	 * @param jsonTag
	 * @param currentValue
	 * @param defaultValue
	 * @return
	 */
	public static String getStringNotEmpty(JSONObject jsonData, String jsonTag, String currentValue, String defaultValue) {
		String value=currentValue; 
		if (currentValue==null || currentValue=="") value=defaultValue;
		try {value=jsonData.getString(jsonTag);}catch (JSONException ignored) {}
		if (value==null || value=="") value=defaultValue;
		return value;
	}

	public static String getString(JSONObject jsonData, String jsonTag, String currentValue, String defaultValue) {
		String value=currentValue; 
		if (currentValue==null) value=defaultValue;
		try {
			value=jsonData.getString2(jsonTag);
		}catch (JSONException e) {
			//e.printStackTrace();
		}
		return value;
	}

	public static String getBeschreibung(JSONObject jsonData, String currentValue) {
		String value=(currentValue!=null && currentValue.length()>0) ? currentValue : null;
		try {value=jsonData.getString(Protokoll.beschreibung);}catch (JSONException ignored) {}
		return value;
	}

	public static String getName(JSONObject jsonData, String defaultValue) {
		String value=(defaultValue!=null) ? defaultValue : "";
		try {value=jsonData.getString(Protokoll.name);}catch (JSONException ignored) {}
		return value;
	}
		
	static public boolean checkParam(String buf) {		
		if (buf==null) return false;
		if (buf.length()==0) return false;
		if (buf.equals("null")) return false;
		if (buf.equals("undefined")) return false;
		return true;
	}
	
	public static String getParameterDecoded( HttpServletRequest request, String buf ) {

		String sValue=new String(); 

		try {
			//	
			String sQueryString=java.net.URLDecoder.decode(request.getQueryString(),Protokoll.encodingDefault);

			int nStartPos=sQueryString.indexOf(buf)+buf.length()+1;			
			int nEndPos=sQueryString.indexOf("&",nStartPos);

			if ( nEndPos<0) {
				//	nach dem gesuchten Wert kommt kein anderer Wert mehr, also den gesamten Reststring zurueckgeben
				nEndPos=sQueryString.length();
			}
			sValue=sQueryString.substring(nStartPos,nEndPos);
		}
		catch (java.io.UnsupportedEncodingException e) {
			System.out.print(" URL fehlerhaft");
		}
		return sValue;
	}

	
	
	public static class TSRange {
		public Timestamp von=null;
		public Timestamp bis=null;
		
		public TSRange() {;}
		public TSRange(Timestamp zeitpunkt) {
			this.von=zeitpunkt;
			this.bis=zeitpunkt;
		}
		public TSRange(Timestamp von, Timestamp bis) {this.von=von; this.bis=bis;}
	}
	
	


}
