package org.commcare.suite.model;

import org.javarosa.core.io.StreamsUtil;
import org.javarosa.core.services.storage.Persistable;
import org.javarosa.core.util.externalizable.DeserializationException;
import org.javarosa.core.util.externalizable.ExtUtil;
import org.javarosa.core.util.externalizable.PrototypeFactory;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

/**
 * @author Phillip Mates (pmates@dimagi.com)
 */
public class UserRestore implements Persistable {
    public static final String STORAGE_KEY = "UserRestore";
    private int recordId = -1;
    private String restore;

    public UserRestore() {
    }

    public static UserRestore buildInMemoryUserRestore(InputStream restoreStream) throws IOException {
        UserRestore userRestore = new UserRestore();
        userRestore.restore = new String(StreamsUtil.inputStreamToByteArray(restoreStream));

        return userRestore;
    }

    public InputStream getRestoreStream() {
        try {
            return new ByteArrayInputStream(restore.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void readExternal(DataInputStream in, PrototypeFactory pf)
            throws IOException, DeserializationException {
        this.recordId = ExtUtil.readInt(in);
        this.restore = ExtUtil.readString(in);
    }

    @Override
    public void writeExternal(DataOutputStream out) throws IOException {
        ExtUtil.writeNumeric(out, recordId);
        ExtUtil.writeString(out, restore);
    }

    @Override
    public void setID(int ID) {
        recordId = ID;
    }

    @Override
    public int getID() {
        return recordId;
    }
}

@@ -65,24 +65,185 @@
 treamsUtil;%0A
+import org.javarosa.core.reference.InvalidReferenceException;%0Aimport org.javarosa.core.reference.Reference;%0Aimport org.javarosa.core.reference.ReferenceManager;%0A
 import org.j
@@ -285,16 +285,16 @@
 stable;%0A
-
 import o
@@ -682,16 +682,111 @@
 n;%0A%0A/**%0A
+ * User restore xml file sometimes present in apps.%0A * Used for offline (demo user) logins.%0A *%0A
  * @auth
@@ -994,16 +994,46 @@
 restore;
+%0A    private String reference;
 %0A%0A    pu
@@ -1052,32 +1052,118 @@
 tore() %7B%0A    %7D%0A%0A
+    public UserRestore(String reference) %7B%0A        this.reference = reference;%0A    %7D%0A%0A
     public stati
@@ -1471,24 +1471,528 @@
 eStream() %7B%0A
+        if (reference == null) %7B%0A            return getStreamFromReference();%0A        %7D else %7B%0A            return getInMemeoryStream();%0A        %7D%0A%0A    %7D%0A    private InputStream getStreamFromReference() %7B%0A        try %7B%0A            Reference local = ReferenceManager._().DeriveReference(reference);%0A            return local.getStream();%0A        %7D catch (IOException %7C InvalidReferenceException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A    %7D%0A%0A    private InputStream getInMemeoryStream() %7B%0A
         try 
@@ -2379,20 +2379,22 @@
  this.re
-stor
+ferenc
 e = ExtU
@@ -2587,20 +2587,22 @@
 (out, re
-stor
+ferenc
 e);%0A    

Add file backed version of UserRestore