package uk.org.taverna.scufl2.translator.t2flow.defaultactivities;

import java.net.URI;

import uk.org.taverna.scufl2.api.activity.Activity;
import uk.org.taverna.scufl2.api.configurations.Configuration;
import uk.org.taverna.scufl2.api.io.ReaderException;
import uk.org.taverna.scufl2.api.port.OutputActivityPort;
import uk.org.taverna.scufl2.translator.t2flow.ParserState;
import uk.org.taverna.scufl2.translator.t2flow.T2FlowParser;
import uk.org.taverna.scufl2.xml.t2flow.jaxb.ConfigBean;
import uk.org.taverna.scufl2.xml.t2flow.jaxb.StringConstantConfig;

public class StringConstantActivityParser extends AbstractActivityParser {

	private static final String VALUE = "value";

	private static URI activityRavenURI =
			T2FlowParser.ravenURI.resolve("net.sf.taverna.t2.activities/stringconstant-activity/");

	private static String activityClassName = "net.sf.taverna.t2.activities.stringconstant.StringConstantActivity";

	public static URI CONSTANT = URI
			.create("http://ns.taverna.org.uk/2010/activity/constant");

	@Override
	public boolean canHandlePlugin(URI activityURI) {
		String activityUriStr = activityURI.toASCIIString();
		return activityUriStr.startsWith(activityRavenURI.toASCIIString())
				&& activityUriStr.endsWith(activityClassName);
	}

	@Override
	public URI mapT2flowRavenIdToScufl2URI(URI t2flowActivity) {
		return CONSTANT;
	}

	@Override
	public Configuration parseConfiguration(T2FlowParser t2FlowParser,
			ConfigBean configBean, ParserState parserState) throws ReaderException {
		StringConstantConfig strConfig = unmarshallConfig(t2FlowParser,
				configBean, "xstream", StringConstantConfig.class);
		String value = strConfig.getValue();
		if (value == null) {
			throw new ReaderException("String constant configuration has no value set");
		}
		Configuration configuration = new Configuration();
		configuration.getJson().setTypeURI(
				CONSTANT.resolve("#Config"));
		configuration.getJson().addPropertyAsString(
				CONSTANT.resolve("#string"), value);
		
		Activity activity = parserState.getCurrentActivity();
		OutputActivityPort valuePort = new OutputActivityPort(activity, VALUE);
		valuePort.setDepth(0);
		valuePort.setGranularDepth(0);
		return configuration;
	}


}

@@ -555,16 +555,72 @@
 onfig;%0A%0A
+import com.fasterxml.jackson.databind.node.ObjectNode;%0A%0A
 public c
@@ -1888,24 +1888,88 @@
 ration();%0A%09%09
+ObjectNode json = (ObjectNode) configuration.getJson();%0A        
 configuratio
@@ -1973,26 +1973,16 @@
 tion
-.getJson()
 .setType
 URI(
@@ -1977,19 +1977,16 @@
 .setType
-URI
 (%0A%09%09%09%09CO
@@ -2019,84 +2019,25 @@
 ;%0A%09%09
-configuration.getJson().addPropertyAsString(%0A%09%09%09%09CONSTANT.resolve
+json.put
 (%22
-#
 string%22
-)
 , va

String constant as JSON