package org.commcare.android.database.user.models;

import org.commcare.cases.model.CaseIndex;
import org.javarosa.core.util.externalizable.DeserializationException;
import org.javarosa.core.util.externalizable.ExtUtil;
import org.javarosa.core.util.externalizable.ExtWrapList;
import org.javarosa.core.util.externalizable.ExtWrapMapPoly;
import org.javarosa.core.util.externalizable.ExtWrapNullable;
import org.javarosa.core.util.externalizable.PrototypeFactory;

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

/**
 * A model extension which reads Resource models from the
 * Version 6 db, and writes them back into the Version 7
 * db.
 *
 * NOTE: This updater is _NOT ROBUST AGAINST METADATA
 * CHANGES_. If the Resource model metadata changes, this
 * needs to be modified to reflect the V6/7 metadata.
 *
 * @author ctsims
 */
public class ACasePreV6Model extends ACase {

    /**
     * Blank constructor for deserialization only!
     */
    public ACasePreV6Model() {

    }

    @Override
    public void readExternal(DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException {
        typeId = ExtUtil.readString(in);
        id = ExtUtil.nullIfEmpty(ExtUtil.readString(in));
        name = ExtUtil.nullIfEmpty(ExtUtil.readString(in));
        closed = ExtUtil.readBool(in);
        dateOpened = (Date)ExtUtil.read(in, new ExtWrapNullable(Date.class), pf);
        recordId = ExtUtil.readInt(in);
        indices = (Vector<CaseIndex>)ExtUtil.read(in, new ExtWrapList(CaseIndexUpdater.class), pf);
        data = (Hashtable)ExtUtil.read(in, new ExtWrapMapPoly(String.class, true), pf);
    }

    @Override
    public void writeExternal(DataOutputStream out) throws IOException {
        ExtUtil.writeString(out, typeId);
        ExtUtil.writeString(out, ExtUtil.emptyIfNull(id));
        ExtUtil.writeString(out, ExtUtil.emptyIfNull(name));
        ExtUtil.writeBool(out, closed);
        ExtUtil.write(out, new ExtWrapNullable(dateOpened));
        ExtUtil.writeNumeric(out, recordId);
        ExtUtil.write(out, new ExtWrapList(indices));
        ExtUtil.write(out, new ExtWrapMapPoly(data));
    }

    public static class CaseIndexUpdater extends CaseIndex {

        /*
         * serialization only!
         */
        public CaseIndexUpdater() {

        }

        @Override
        public void readExternal(DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException {
            mName = ExtUtil.readString(in);
            mTargetId = ExtUtil.readString(in);
            mTargetCaseType = ExtUtil.readString(in);
            mRelationship = CaseIndex.RELATIONSHIP_CHILD;
        }

        @Override
        public void writeExternal(DataOutputStream out) throws IOException {
            ExtUtil.writeString(out, mName);
            ExtUtil.writeString(out, mTargetId);
            ExtUtil.writeString(out, mTargetCaseType);
            ExtUtil.writeString(out, mRelationship);
        }
    }
}

@@ -1092,32 +1092,1016 @@
 del() %7B%0A%0A    %7D%0A%0A
+    @Override%0A    public Object getMetaData(String fieldName) %7B%0A        if (fieldName.equals(INDEX_CASE_ID)) %7B%0A            return id;%0A        %7D else if (fieldName.equals(%22case-type%22)) %7B%0A            return typeId;%0A        %7D else if (fieldName.equals(INDEX_CASE_STATUS)) %7B%0A            return closed ? %22closed%22 : %22open%22;%0A        %7D else if (fieldName.startsWith(INDEX_CASE_INDEX_PRE)) %7B%0A            String name = fieldName.substring(fieldName.lastIndexOf('-') + 1, fieldName.length());%0A%0A            for (CaseIndex index : this.getIndices()) %7B%0A                if (index.getName().equals(name)) %7B%0A                    return index.getTarget();%0A                %7D%0A            %7D%0A            return %22%22;%0A        %7D else %7B%0A            throw new IllegalArgumentException(%22No metadata field %22 + fieldName + %22 in the case storage system%22);%0A        %7D%0A    %7D%0A%0A    @Override%0A    public String%5B%5D getMetaDataFields() %7B%0A        return new String%5B%5D%7BINDEX_CASE_ID, INDEX_CASE_TYPE, INDEX_CASE_STATUS%7D;%0A    %7D%0A%0A%0A
     @Override%0A  

Fix for pre V6 Case Model schema to not index owner id