package org.commcare.suite.model;

import org.javarosa.core.util.externalizable.DeserializationException;
import org.javarosa.core.util.externalizable.ExtUtil;
import org.javarosa.core.util.externalizable.ExtWrapTagged;
import org.javarosa.core.util.externalizable.Externalizable;
import org.javarosa.core.util.externalizable.PrototypeFactory;
import org.javarosa.xpath.expr.XPathExpression;

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

/**
 * Model for <validation> node in {@link QueryPrompt}
 */
public class QueryPromptValidation implements Externalizable {

    private XPathExpression test;
    private Text message;

    @SuppressWarnings("unused")
    public QueryPromptValidation() {
    }

    public QueryPromptValidation(XPathExpression test, Text message) {
        this.test = test;
        this.message = message;
    }

    @Override
    public void readExternal(DataInputStream in, PrototypeFactory pf)
            throws IOException, DeserializationException {
        test = (XPathExpression)ExtUtil.read(in, new ExtWrapTagged(), pf);
        message = (Text)ExtUtil.read(in, Text.class, pf);
    }

    @Override
    public void writeExternal(DataOutputStream out) throws IOException {
        ExtUtil.write(out, new ExtWrapTagged(test));
        ExtUtil.write(out, message);
    }

    public XPathExpression getTest() {
        return test;
    }

    public Text getMessage() {
        return message;
    }
}

@@ -153,16 +153,78 @@
 xtUtil;%0A
+import org.javarosa.core.util.externalizable.ExtWrapNullable;%0A
 import o
@@ -1200,16 +1200,36 @@
 ead(in, 
+new ExtWrapNullable(
 Text.cla
@@ -1230,16 +1230,17 @@
 xt.class
+)
 , pf);%0A 
@@ -1412,24 +1412,44 @@
 te(out, 
+new ExtWrapNullable(
 message)
 ;%0A    %7D%0A
@@ -1440,16 +1440,17 @@
 message)
+)
 ;%0A    %7D%0A

allow null message