/**
 * 
 */
package org.javarosa.core.model;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Hashtable;

import org.javarosa.core.util.externalizable.DeserializationException;
import org.javarosa.core.util.externalizable.ExtUtil;
import org.javarosa.core.util.externalizable.ExtWrapMap;
import org.javarosa.core.util.externalizable.ExtWrapTagged;
import org.javarosa.core.util.externalizable.Externalizable;
import org.javarosa.core.util.externalizable.PrototypeFactory;

/**
 * A Submission Profile is a class which is responsible for
 * holding and processing the details of how a submission
 * should be handled. 
 * 
 * @author ctsims
 *
 */
public class SubmissionProfile implements Externalizable {
	
	IDataReference ref;
	String method;
	String action;
	String mediaType;
	Hashtable<String,String> attributeMap;
	
	public SubmissionProfile() {
		
	}
	
	public SubmissionProfile(IDataReference ref, String method, String action, String mediatype, Hashtable<String,String> attributeMap) {
		this.method = method;
		this.ref = ref;
		this.action = action;
		this.mediaType = mediatype;
		this.attributeMap = attributeMap;
	}

	public IDataReference getRef() {
		return ref;
	}

	public String getMethod() {
		return method;
	}

	public String getAction() {
		return action;
	}

	public String getMediaType() {
		return mediaType;
	}
	
	public String getAttribute(String name) {
	    return attributeMap.get(name);
	}
	
	@SuppressWarnings("unchecked")
	public void readExternal(DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException {
		ref = (IDataReference)ExtUtil.read(in, new ExtWrapTagged(IDataReference.class));
		method = ExtUtil.readString(in);
		action = ExtUtil.readString(in);
		mediaType = ExtUtil.nullIfEmpty(ExtUtil.readString(in));
		attributeMap = (Hashtable<String, String>)ExtUtil.read(in, new ExtWrapMap(String.class, String.class));
	}

	public void writeExternal(DataOutputStream out) throws IOException {
		ExtUtil.write(out, new ExtWrapTagged(ref));
		ExtUtil.writeString(out, method);
		ExtUtil.writeString(out, action);
		ExtUtil.writeString(out, ExtUtil.emptyIfNull(mediaType));
		ExtUtil.write(out, new ExtWrapMap(attributeMap));
	}

	
}

@@ -1,18 +1,17 @@
 /**%0D%0A *
- 
 %0D%0A
+
  */%0D%0Apac
@@ -690,22 +690,20 @@
 handled.
- 
 %0D%0A *
- 
 %0D%0A * @au
@@ -784,19 +784,18 @@
 able %7B%0D%0A
-%09
 %0D%0A
+
 %09IDataRe
@@ -903,19 +903,18 @@
 teMap;%0D%0A
-%09
 %0D%0A
+
 %09public 
@@ -940,19 +940,16 @@
  %7B%0D%0A
-%09%09
 %0D%0A
+
 %09%7D%0D%0A
-%09
 %0D%0A
+
 %09pub
@@ -1445,19 +1445,18 @@
 e;%0D%0A%09%7D%0D%0A
-%09
 %0D%0A
+
 %09public 
@@ -1532,19 +1532,18 @@
 );%0D%0A%09%7D%0D%0A
-%09
 %0D%0A
+
 %09@Suppre
@@ -1813,16 +1813,36 @@
 ction = 
+ExtUtil.nullIfEmpty(
 ExtUtil.
@@ -1851,24 +1851,25 @@
 adString(in)
+)
 ;%0D%0A%09%09mediaTy
@@ -2218,15 +2218,36 @@
 ut, 
+ExtUtil.emptyIfNull(
 action)
+)
 ;%0D%0A%09
@@ -2365,14 +2365,13 @@
 %0D%0A%09%7D%0D%0A%0D%0A
-%09
 %0D%0A%7D%0D%0A

Allow submission profiles that do not specify a destination URL (useful for encrypted forms)