Search in sources :

Example 6 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.

the class CompactInstanceWrapper method readExternal.

/**
 * deserialize a compact instance. note the retrieval of the template data instance
 */
public void readExternal(DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException {
    int formID = ExtUtil.readInt(in);
    instance = getTemplateInstance(formID).clone();
    instance.setID(ExtUtil.readInt(in));
    instance.setDateSaved((Date) ExtUtil.read(in, new ExtWrapNullable(Date.class)));
    // formID, name, schema, versions, and namespaces are all invariants of the template instance
    TreeElement root = instance.getRoot();
    readTreeElement(root, in, pf);
}
Also used : Date(java.util.Date) ExtWrapNullable(org.javarosa.core.util.externalizable.ExtWrapNullable) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 7 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.

the class CompactInstanceWrapper method readTreeElement.

/**
 * recursively read in a node of the instance, by filling out the template instance
 * @param e
 * @param ref
 * @param in
 * @param pf
 * @throws IOException
 * @throws DeserializationException
 */
private void readTreeElement(TreeElement e, DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException {
    TreeElement templ = instance.getTemplatePath(e.getRef());
    boolean isGroup = !templ.isLeaf();
    if (isGroup) {
        List<String> childTypes = new ArrayList<String>(templ.getNumChildren());
        for (int i = 0; i < templ.getNumChildren(); i++) {
            String childName = templ.getChildAt(i).getName();
            if (!childTypes.contains(childName)) {
                childTypes.add(childName);
            }
        }
        for (int i = 0; i < childTypes.size(); i++) {
            String childName = childTypes.get(i);
            TreeReference childTemplRef = e.getRef().extendRef(childName, 0);
            TreeElement childTempl = instance.getTemplatePath(childTemplRef);
            boolean repeatable = childTempl.isRepeatable();
            int n = ExtUtil.readInt(in);
            boolean relevant = (n > 0);
            if (!repeatable && n > 1) {
                throw new DeserializationException("Detected repeated instances of a non-repeatable node");
            }
            if (repeatable) {
                int mult = e.getChildMultiplicity(childName);
                for (int j = mult - 1; j >= 0; j--) {
                    e.removeChild(childName, j);
                }
                for (int j = 0; j < n; j++) {
                    TreeReference dstRef = e.getRef().extendRef(childName, j);
                    try {
                        instance.copyNode(childTempl, dstRef);
                    } catch (InvalidReferenceException ire) {
                        // If there is an invalid reference, this is a malformed instance,
                        // so we'll throw a Deserialization exception.
                        TreeReference r = ire.getInvalidReference();
                        if (r == null) {
                            throw new DeserializationException("Null Reference while attempting to deserialize! " + ire.getMessage());
                        } else {
                            throw new DeserializationException("Invalid Reference while attemtping to deserialize! Reference: " + r.toString(true) + " | " + ire.getMessage());
                        }
                    }
                    TreeElement child = e.getChild(childName, j);
                    child.setRelevant(true);
                    readTreeElement(child, in, pf);
                }
            } else {
                TreeElement child = e.getChild(childName, 0);
                child.setRelevant(relevant);
                if (relevant) {
                    readTreeElement(child, in, pf);
                }
            }
        }
    } else {
        e.setValue((IAnswerData) ExtUtil.read(in, new ExtWrapAnswerData(e.getDataType())));
    }
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) ArrayList(java.util.ArrayList) DeserializationException(org.javarosa.core.util.externalizable.DeserializationException) InvalidReferenceException(org.javarosa.core.model.instance.InvalidReferenceException) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 8 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.

the class TreeElementChildrenList method addAll.

/**
 * Adds all of the provided children
 */
public void addAll(Iterable<TreeElement> childIterable) {
    for (TreeElement child : childIterable) {
        checkAndSetSameNameAndNormalMult(child.getName(), child.getMultiplicity());
        children.add(child);
    }
}
Also used : TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 9 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.

the class RestoreUtils method importRMS.

public static void importRMS(FormInstance dm, IStorageUtility storage, Class type, String path) {
    if (!Externalizable.class.isAssignableFrom(type) || !Restorable.class.isAssignableFrom(type)) {
        return;
    }
    boolean idMatters = Persistable.class.isAssignableFrom(type);
    String childName = ((Restorable) PrototypeFactory.getInstance(type)).getRestorableType();
    TreeElement e = dm.resolveReference(absRef(path, dm));
    List<TreeElement> children = e.getChildrenWithName(childName);
    for (int i = 0; i < children.size(); i++) {
        FormInstance child = subDataModel(children.get(i));
        Restorable inst = (Restorable) PrototypeFactory.getInstance(type);
        // restore record id first so 'importData' has access to it
        int recID = -1;
        if (idMatters) {
            recID = ((Integer) getValue(RECORD_ID_TAG, child)).intValue();
            ((Persistable) inst).setID(recID);
        }
        inst.importData(child);
        try {
            if (idMatters) {
                storage.write((Persistable) inst);
            } else {
                storage.add((Externalizable) inst);
            }
        } catch (Exception ex) {
            throw new RuntimeException("Error importing RMS during restore! [" + type.getName() + ":" + recID + "]; " + ex.getMessage());
        }
    }
}
Also used : Persistable(org.javarosa.core.services.storage.Persistable) FormInstance(org.javarosa.core.model.instance.FormInstance) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 10 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.

the class RestoreUtils method applyDataType.

public static void applyDataType(FormInstance dm, String path, TreeReference parent, int dataType) {
    TreeReference ref = childRef(path, parent);
    List<TreeReference> v = new EvaluationContext(dm).expandReference(ref);
    for (int i = 0; i < v.size(); i++) {
        TreeElement e = dm.resolveReference(v.get(i));
        e.setDataType(dataType);
    }
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) TreeElement(org.javarosa.core.model.instance.TreeElement)

Aggregations

TreeElement (org.javarosa.core.model.instance.TreeElement)86 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)33 TreeReference (org.javarosa.core.model.instance.TreeReference)28 FormInstance (org.javarosa.core.model.instance.FormInstance)16 ArrayList (java.util.ArrayList)15 Constraint (org.javarosa.core.model.condition.Constraint)11 Test (org.junit.Test)10 Element (org.kxml2.kdom.Element)9 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)8 FormDef (org.javarosa.core.model.FormDef)7 InstanceInitializationFactory (org.javarosa.core.model.instance.InstanceInitializationFactory)7 IOException (java.io.IOException)6 IFormElement (org.javarosa.core.model.IFormElement)6 StringData (org.javarosa.core.model.data.StringData)6 HashMap (java.util.HashMap)5 IAnswerData (org.javarosa.core.model.data.IAnswerData)4 File (java.io.File)3 GroupDef (org.javarosa.core.model.GroupDef)3 IDataReference (org.javarosa.core.model.IDataReference)3 QuestionDef (org.javarosa.core.model.QuestionDef)3