package ch.elexis.core.logback.rocketchat;

import java.io.IOException;
import java.net.URL;

import ch.elexis.core.logback.rocketchat.internal.IntegrationPostHandler;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;

public class RocketchatAppender extends AppenderBase<ILoggingEvent> {

	@Override
	public void start() {
		if (getContext().getProperty("identification") == null) {
			getContext().putProperty("identification", "logback");
			addInfo("No <identification> parameter defined, defaulting to logback.");
		}
		super.start();
	}

	@Override
	protected void append(ILoggingEvent eventObject) {
		try {
			String integrationUrl = getContext().getProperty("integrationUrl");
			if (integrationUrl == null || integrationUrl.length() == 0) {
				return;
			}
			
			URL realIntegrationUrl = new URL(integrationUrl);
			String identification = getContext().getProperty("identification");
			boolean attachmentBased = (getContext().getProperty("attachmentBased") == null) ? true
					: Boolean.valueOf(getContext().getProperty("attachmentBased"));
			new IntegrationPostHandler(eventObject, identification, attachmentBased).post(realIntegrationUrl);
		} catch (IOException ex) {
			addError("Error posting to integrationUrl [" + getContext().getProperty("integrationUrl") + "]", ex);
		}
	}

	public String getEvent() {
		return getContext().getProperty("event");
	}

	public void setEvent(String event) {
		getContext().putProperty("event", event);
	}

	public String getIntegrationUrl() {
		return getContext().getProperty("integrationUrl");
	}

	public void setIntegrationUrl(String integrationUrl) {
		addInfo("Changing integration url ...");
		getContext().putProperty("integrationUrl", integrationUrl);
	}

	public String getIdentification() {
		return getContext().getProperty("identification");
	}

	public void setIdentification(String identification) {
		getContext().putProperty("identification", identification);
	}

	public String getAttachmentBased() {
		return getContext().getProperty("attachmentBased");
	}

	public void setAttachmentBased(String attachmentBased) {
		getContext().putProperty("attachmentBased", attachmentBased);
	}
}

@@ -87,16 +87,58 @@
 t.URL;%0A%0A
+import org.eclipse.core.runtime.IStatus;%0A%0A
 import c
@@ -364,16 +364,17 @@
 vent%3E %7B%0A
+%09
 %0A%09@Overr
@@ -397,17 +397,16 @@
  start()
- 
 %7B%0A%09%09if (
@@ -618,16 +618,17 @@
 t();%0A%09%7D%0A
+%09
 %0A%09@Overr
@@ -680,17 +680,16 @@
 tObject)
- 
 %7B%0A%09%09try 
@@ -1130,16 +1130,41 @@
 sed%22));%0A
+%09%09%09IStatus postStatus =%0A%09
 %09%09%09new I
@@ -1230,16 +1230,22 @@
 ntBased)
+%0A%09%09%09%09%09
 .post(re
@@ -1263,16 +1263,185 @@
 onUrl);%0A
+%09%09%09if (!postStatus.isOK()) %7B%0A%09%09%09%09addError(%22Error posting to integrationUrl %5B%22%0A%09%09%09%09%09+ getContext().getProperty(%22integrationUrl%22) + %22%5D: %22 + postStatus.getMessage());%0A%09%09%09%7D%0A
 %09%09%7D catc
@@ -1508,17 +1508,21 @@
 onUrl %5B%22
- 
+%0A%09%09%09%09
 + getCon
@@ -1573,24 +1573,25 @@
 ex);%0A%09%09%7D%0A%09%7D%0A
+%09
 %0A%09public Str
@@ -1604,17 +1604,16 @@
 tEvent()
- 
 %7B%0A%09%09retu
@@ -1649,24 +1649,25 @@
 event%22);%0A%09%7D%0A
+%09
 %0A%09public voi
@@ -1690,17 +1690,16 @@
 g event)
- 
 %7B%0A%09%09getC
@@ -1735,24 +1735,25 @@
  event);%0A%09%7D%0A
+%09
 %0A%09public Str
@@ -1775,17 +1775,16 @@
 ionUrl()
- 
 %7B%0A%09%09retu
@@ -1829,24 +1829,25 @@
 onUrl%22);%0A%09%7D%0A
+%09
 %0A%09public voi
@@ -1888,17 +1888,16 @@
 tionUrl)
- 
 %7B%0A%09%09addI
@@ -1994,24 +1994,25 @@
 ionUrl);%0A%09%7D%0A
+%09
 %0A%09public Str
@@ -2034,17 +2034,16 @@
 cation()
- 
 %7B%0A%09%09retu
@@ -2088,24 +2088,25 @@
 ation%22);%0A%09%7D%0A
+%09
 %0A%09public voi
@@ -2147,17 +2147,16 @@
 ication)
- 
 %7B%0A%09%09getC
@@ -2210,24 +2210,25 @@
 cation);%0A%09%7D%0A
+%09
 %0A%09public Str
@@ -2251,17 +2251,16 @@
 tBased()
- 
 %7B%0A%09%09retu
@@ -2310,16 +2310,17 @@
 d%22);%0A%09%7D%0A
+%09
 %0A%09public
@@ -2367,17 +2367,16 @@
 ntBased)
- 
 %7B%0A%09%09getC

Check status on RocketchatAppender#append