package org.orienteer.etl.component;

import org.orienteer.core.component.BootstrapType;
import org.orienteer.core.component.FAIconType;
import org.orienteer.core.component.property.DisplayMode;
import org.orienteer.core.method.ClassOMethod;
import org.orienteer.core.method.IMethodEnvironmentData;
import org.orienteer.core.method.OFilter;
import org.orienteer.core.method.filters.PlaceFilter;
import org.orienteer.core.method.filters.WidgetTypeFilter;
import org.orienteer.core.tasks.OTask;
import org.orienteer.core.tasks.OTaskSessionRuntime;
import org.orienteer.core.web.AbstractWidgetDisplayModeAwarePage;
import org.orienteer.core.web.ODocumentPage;
import org.orienteer.etl.tasks.OETLTaskSession;
import org.orienteer.etl.tasks.OETLTaskSessionCallback;

import com.orientechnologies.orient.core.record.impl.ODocument;

import ru.ydn.wicket.wicketorientdb.model.ODocumentModel;
/**
 * 
 * OrientDB ETL config object
 *
 */
public class OETLConfig extends OTask {
	private static final long serialVersionUID = 1L;
	private static final String ETL_CONFIG_FIELD="config";

	public OETLConfig(ODocument iDocument) {
		super(iDocument);
	}
	
	@ClassOMethod(
		order=10,bootstrap=BootstrapType.SUCCESS,icon = FAIconType.play,
		permission="EXECUTE",
		filters={
				@OFilter(fClass = PlaceFilter.class, fData = "STRUCTURE_TABLE"),
				@OFilter(fClass = WidgetTypeFilter.class, fData = "parameters"),
//					@OFilter(fClass = PlaceFilter.class, fData = "STRUCTURE_TABLE|DATA_TABLE"),
		}
	)
	public void run(IMethodEnvironmentData data){
		OTaskSessionRuntime newSession = startNewSession();
		AbstractWidgetDisplayModeAwarePage<ODocument> page = new ODocumentPage(new ODocumentModel(newSession.getOTaskSessionPersisted().getDocument())).setModeObject(DisplayMode.VIEW);
		
		data.getCurrentWidget().setResponsePage(page);
	}

	@Override
	public OTaskSessionRuntime startNewSession() {
		
		final OETLTaskSession session = new OETLTaskSession();
		session.start();

		final String configuration = getDocument().field(ETL_CONFIG_FIELD);
		final OrienteerETLProcessor processor = OrienteerETLProcessor.parseConfigRecord(session,configuration);

		Thread thread = new Thread(new Runnable(){
			@Override
			public void run() {
				processor.doExecute();
				session.finish();
			}
		});

		session.setCallback(new OETLTaskSessionCallback(thread,processor));
		session.setDeleteOnFinish((Boolean) document.field(OTask.Field.AUTODELETE_SESSIONS.fieldName()));
		session.setOTask(this);

		thread.start();
		
		return session;
	}

}

@@ -27,24 +27,70 @@
 component;%0A%0A
+import org.apache.wicket.util.string.Strings;%0A
 import org.o
@@ -2005,16 +2005,25 @@
 art();%0A%0A
+%09%09try %7B%0A%09
 %09%09final 
@@ -2086,16 +2086,17 @@
 ELD);%0A%09%09
+%09
 final Or
@@ -2194,16 +2194,17 @@
 on);%0A%0A%09%09
+%09
 Thread t
@@ -2241,16 +2241,17 @@
 e()%7B%0A%09%09%09
+%09
 @Overrid
@@ -2252,16 +2252,17 @@
 verride%0A
+%09
 %09%09%09publi
@@ -2276,16 +2276,29 @@
 run() %7B%0A
+%09%09%09%09%09try %7B%0A%09%09
 %09%09%09%09proc
@@ -2316,16 +2316,149 @@
 cute();%0A
+%09%09%09%09%09%7D catch (Exception e) %7B%0A%09%09%09%09%09%09session.appendOut(%22ETL Processor runtime error!%22);%0A%09%09%09%09%09%09printCause(e, session);%0A%09%09%09%09%09%7Dfinally%7B%0A%09%09
 %09%09%09%09sess
@@ -2475,20 +2475,30 @@
 ();%0A
+%09%09
 %09%09%09%7D%0A
+%09%09%09%09%7D%0A%09
 %09%09%7D);%0A%0A
+%09
 %09%09se
@@ -2559,24 +2559,25 @@
 rocessor));%0A
+%09
 %09%09session.se
@@ -2662,24 +2662,25 @@
 Name()));%0A%09%09
+%09
 session.setO
@@ -2694,16 +2694,17 @@
 is);%0A%0A%09%09
+%09
 thread.s
@@ -2717,26 +2717,380 @@
 ;%0A%09%09
-%0A%09%09return session;
+%7D catch (Exception e) %7B%0A%09%09%09session.appendOut(%22ETL Processor execute error!%22);%0A%09%09%09printCause(e, session);%0A%09%09%09session.finish();%0A%09%09%7D%0A%09%09%0A%09%09%0A%09%09return session;%0A%09%7D%0A%0A%09private void printCause(Throwable e,OETLTaskSession session)%7B%0A%09%09if (!Strings.isEmpty(e.getMessage()))%7B%0A%09%09%09session.appendOut(e.getMessage());%0A%09%09%7D%0A%09%09if (e.getCause()!=null)%7B%0A%09%09%09printCause(e.getCause(), session);%0A%09%09%7D
 %0A%09%7D%0A

Fix ETL unthrowed exception