<commit_before>package fi.csc.microarray.client.workflow;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;

import bsh.EvalError;
import bsh.Interpreter;
import fi.csc.microarray.client.AtEndListener;
import fi.csc.microarray.client.ClientApplication;
import fi.csc.microarray.client.Session;
import fi.csc.microarray.client.dialog.ChipsterDialog.DetailsVisibility;
import fi.csc.microarray.client.dialog.DialogInfo.Severity;
import fi.csc.microarray.client.workflow.api.WfApplication;
import fi.csc.microarray.config.DirectoryLayout;
import fi.csc.microarray.util.Exceptions;
import fi.csc.microarray.util.GeneralFileFilter;


/**
 * 
 * @author <NAME>
 *
 */
public class WorkflowManager {
	
	static final Logger logger = Logger.getLogger(WorkflowManager.class);

	public static final String WORKFLOW_VERSION = "BSH/2";

	public static void checkVersionHeaderLine(String line) throws IllegalArgumentException {
		if (!line.contains(WORKFLOW_VERSION + " ")) {
			throw new IllegalArgumentException("Script version not supported. Supported version is " + WORKFLOW_VERSION + ", but script begins with " + line);
		}
	}

	public static final String SCRIPT_EXTENSION = "bsh";
	String[] extensions = { WorkflowManager.SCRIPT_EXTENSION };
	public static final GeneralFileFilter FILE_FILTER = 
		new GeneralFileFilter("Workflow in BeanShell format", new String[]{ WorkflowManager.SCRIPT_EXTENSION });
	
	public File scriptDirectory;

	private ClientApplication application;


	public WorkflowManager(ClientApplication application) throws IOException {
		this.application = application;
		this.scriptDirectory = DirectoryLayout.getInstance().getUserDataDir();
	}

	public List<File> getWorkflows() {
		LinkedList<File> workflows = new LinkedList<File>();
		File[] scripts = scriptDirectory.listFiles(new FilenameFilter() {
			public boolean accept(File dir, String name) {
				return name.endsWith(SCRIPT_EXTENSION);
			}				
		});
		if(scripts != null){ //May be null if user's home folder isn't found
			for (File script : scripts) {
				workflows.add(script);
			}
		}
		return workflows;
	}

	public void runScript(final File file, final AtEndListener listener) {
		try {
			runScript(file.toURL(), listener);			
		} catch (MalformedURLException e) {			
			throw new IllegalArgumentException(e);
		}
	}
	
	public void runScript(final URL workflowUrl, final AtEndListener listener) {

		Runnable runnable = new Runnable() {
			public void run() {
				BufferedReader in = null;
				boolean success = false;
				try {
					
					in = new BufferedReader(new InputStreamReader(workflowUrl.openConnection().getInputStream()));
					String line = in.readLine();
					checkVersionHeaderLine(line);
					in.close();
					Interpreter i = initialiseBshEnvironment();
					i.eval(new InputStreamReader(workflowUrl.openConnection().getInputStream()));
					success = true;
				} catch (Throwable e) {
					logger.warn("running workflow failed", e);
					String workflowName = "";
					try {
						workflowName = " " + workflowUrl.getPath().substring(workflowUrl.getPath().lastIndexOf('/') + 1);
					} catch (Exception we) {
					}
					application.showDialog("Running workflow" + workflowName + " failed.", 
							"The most common reason for a workflow failure is that the data used as an input for the worklfow " + 
							" is not compatible with the tools in the workflow. This causes one of tools to fail and aborting the rest " +
							" of the workflow.\n\n" +
							"To get an idea of why a tool has failed, please see the tool specific failure window.",
							Exceptions.getStackTrace(e), Severity.WARNING, false, DetailsVisibility.DETAILS_ALWAYS_HIDDEN);
				} finally {
					if (listener != null) {
						listener.atEnd(success);
					}
					if (in != null) {
						try {
							in.close(); // might be closed twice but that is legal<commit_msg>Fixed typo in error dialog.<commit_after>package fi.csc.microarray.client.workflow;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;

import bsh.EvalError;
import bsh.Interpreter;
import fi.csc.microarray.client.AtEndListener;
import fi.csc.microarray.client.ClientApplication;
import fi.csc.microarray.client.Session;
import fi.csc.microarray.client.dialog.ChipsterDialog.DetailsVisibility;
import fi.csc.microarray.client.dialog.DialogInfo.Severity;
import fi.csc.microarray.client.workflow.api.WfApplication;
import fi.csc.microarray.config.DirectoryLayout;
import fi.csc.microarray.util.Exceptions;
import fi.csc.microarray.util.GeneralFileFilter;


/**
 * 
 * @author <NAME>
 *
 */
public class WorkflowManager {
	
	static final Logger logger = Logger.getLogger(WorkflowManager.class);

	public static final String WORKFLOW_VERSION = "BSH/2";

	public static void checkVersionHeaderLine(String line) throws IllegalArgumentException {
		if (!line.contains(WORKFLOW_VERSION + " ")) {
			throw new IllegalArgumentException("Script version not supported. Supported version is " + WORKFLOW_VERSION + ", but script begins with " + line);
		}
	}

	public static final String SCRIPT_EXTENSION = "bsh";
	String[] extensions = { WorkflowManager.SCRIPT_EXTENSION };
	public static final GeneralFileFilter FILE_FILTER = 
		new GeneralFileFilter("Workflow in BeanShell format", new String[]{ WorkflowManager.SCRIPT_EXTENSION });
	
	public File scriptDirectory;

	private ClientApplication application;


	public WorkflowManager(ClientApplication application) throws IOException {
		this.application = application;
		this.scriptDirectory = DirectoryLayout.getInstance().getUserDataDir();
	}

	public List<File> getWorkflows() {
		LinkedList<File> workflows = new LinkedList<File>();
		File[] scripts = scriptDirectory.listFiles(new FilenameFilter() {
			public boolean accept(File dir, String name) {
				return name.endsWith(SCRIPT_EXTENSION);
			}				
		});
		if(scripts != null){ //May be null if user's home folder isn't found
			for (File script : scripts) {
				workflows.add(script);
			}
		}
		return workflows;
	}

	public void runScript(final File file, final AtEndListener listener) {
		try {
			runScript(file.toURL(), listener);			
		} catch (MalformedURLException e) {			
			throw new IllegalArgumentException(e);
		}
	}
	
	public void runScript(final URL workflowUrl, final AtEndListener listener) {

		Runnable runnable = new Runnable() {
			public void run() {
				BufferedReader in = null;
				boolean success = false;
				try {
					
					in = new BufferedReader(new InputStreamReader(workflowUrl.openConnection().getInputStream()));
					String line = in.readLine();
					checkVersionHeaderLine(line);
					in.close();
					Interpreter i = initialiseBshEnvironment();
					i.eval(new InputStreamReader(workflowUrl.openConnection().getInputStream()));
					success = true;
				} catch (Throwable e) {
					logger.warn("running workflow failed", e);
					String workflowName = "";
					try {
						workflowName = " " + workflowUrl.getPath().substring(workflowUrl.getPath().lastIndexOf('/') + 1);
					} catch (Exception we) {
					}
					application.showDialog("Running workflow" + workflowName + " failed.", 
							"The most common reason for a workflow failure is that the data used as an input for the workflow" + 
							" is not compatible with the tools in the workflow. This causes one of tools to fail and aborting the rest" +
							" of the workflow.\n\n" +
							"To get an idea of why a tool has failed, please see the tool specific failure window.",
							Exceptions.getStackTrace(e), Severity.WARNING, false, DetailsVisibility.DETAILS_ALWAYS_HIDDEN);
				} finally {
					if (listener != null) {
						listener.atEnd(success);
					}
					if (in != null) {
						try {
							in.close(); // might be closed twice but that is legal
{'fraction_non_alphanumeric': 0.10303247373447946, 'fraction_numerical': 0.0007163323782234957, 'mean_word_length': 4.126682986536108, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5712544', 'n_tokens_mistral': 2866, 'n_tokens_neox': 2395, 'n_words': 643}