Search in sources :

Example 1 with ByteArrayPayload

use of org.javarosa.core.services.transport.payload.ByteArrayPayload in project collect by opendatakit.

the class SavePointTask method doInBackground.

@Override
protected String doInBackground(Void... params) {
    synchronized (lock) {
        if (priority < lastPriorityUsed) {
            Timber.w("Savepoint thread (p=%d) was cancelled (a) because another one is waiting (p=%d)", priority, lastPriorityUsed);
            return null;
        }
        long start = System.currentTimeMillis();
        try {
            FormController formController = Collect.getInstance().getFormController();
            File temp = SaveToDiskTask.getSavepointFile(formController.getInstanceFile().getName());
            ByteArrayPayload payload = formController.getFilledInFormXml();
            if (priority < lastPriorityUsed) {
                Timber.w("Savepoint thread (p=%d) was cancelled (b) because another one is waiting (p=%d)", priority, lastPriorityUsed);
                return null;
            }
            // write out xml
            SaveToDiskTask.exportXmlFile(payload, temp.getAbsolutePath());
            long end = System.currentTimeMillis();
            Timber.i("Savepoint ms: %s to %s", Long.toString(end - start), temp.toString());
            return null;
        } catch (Exception e) {
            String msg = e.getMessage();
            Timber.e(e);
            return msg;
        }
    }
}
Also used : FormController(org.odk.collect.android.logic.FormController) ByteArrayPayload(org.javarosa.core.services.transport.payload.ByteArrayPayload) File(java.io.File)

Example 2 with ByteArrayPayload

use of org.javarosa.core.services.transport.payload.ByteArrayPayload in project collect by opendatakit.

the class FormController method getSubmissionXml.

/**
 * Extract the portion of the form that should be uploaded to the server.
 */
public ByteArrayPayload getSubmissionXml() throws IOException {
    FormInstance instance = getInstance();
    XFormSerializingVisitor serializer = new XFormSerializingVisitor();
    return (ByteArrayPayload) serializer.createSerializedPayload(instance, getSubmissionDataReference());
}
Also used : ByteArrayPayload(org.javarosa.core.services.transport.payload.ByteArrayPayload) XFormSerializingVisitor(org.javarosa.model.xform.XFormSerializingVisitor) FormInstance(org.javarosa.core.model.instance.FormInstance)

Example 3 with ByteArrayPayload

use of org.javarosa.core.services.transport.payload.ByteArrayPayload in project javarosa by opendatakit.

the class XFormSerializingVisitor method createSerializedPayload.

public IDataPayload createSerializedPayload(FormInstance model, IDataReference ref) throws IOException {
    init();
    rootRef = FormInstance.unpackReference(ref);
    if (this.serializer == null) {
        this.setAnswerDataSerializer(new XFormAnswerDataSerializer());
    }
    model.accept(this);
    if (theXmlDoc != null) {
        // TODO: Did this strip necessary data?
        byte[] form = XFormSerializer.getUtfBytes(theXmlDoc);
        if (dataPointers.size() == 0) {
            return new ByteArrayPayload(form, null, IDataPayload.PAYLOAD_TYPE_XML);
        }
        MultiMessagePayload payload = new MultiMessagePayload();
        payload.addPayload(new ByteArrayPayload(form, "xml_submission_file", IDataPayload.PAYLOAD_TYPE_XML));
        for (IDataPointer pointer : dataPointers) {
            payload.addPayload(new DataPointerPayload(pointer));
        }
        return payload;
    } else {
        return null;
    }
}
Also used : XFormAnswerDataSerializer(org.javarosa.xform.util.XFormAnswerDataSerializer) DataPointerPayload(org.javarosa.core.services.transport.payload.DataPointerPayload) ByteArrayPayload(org.javarosa.core.services.transport.payload.ByteArrayPayload) MultiMessagePayload(org.javarosa.core.services.transport.payload.MultiMessagePayload) IDataPointer(org.javarosa.core.data.IDataPointer)

Example 4 with ByteArrayPayload

use of org.javarosa.core.services.transport.payload.ByteArrayPayload in project collect by opendatakit.

the class FormController method getFilledInFormXml.

/**
 * Constructs the XML payload for a filled-in form instance. This payload
 * enables a filled-in form to be re-opened and edited.
 */
public ByteArrayPayload getFilledInFormXml() throws IOException {
    // assume no binary data inside the model.
    FormInstance datamodel = getInstance();
    XFormSerializingVisitor serializer = new XFormSerializingVisitor();
    return (ByteArrayPayload) serializer.createSerializedPayload(datamodel);
}
Also used : ByteArrayPayload(org.javarosa.core.services.transport.payload.ByteArrayPayload) XFormSerializingVisitor(org.javarosa.model.xform.XFormSerializingVisitor) FormInstance(org.javarosa.core.model.instance.FormInstance)

Example 5 with ByteArrayPayload

use of org.javarosa.core.services.transport.payload.ByteArrayPayload in project collect by opendatakit.

the class SaveToDiskTask method exportData.

/**
 * Write's the data to the sdcard, and updates the instances content provider.
 * In theory we don't have to write to disk, and this is where you'd add
 * other methods.
 */
private void exportData(boolean markCompleted) throws IOException, EncryptionException {
    FormController formController = Collect.getInstance().getFormController();
    publishProgress(Collect.getInstance().getString(R.string.survey_saving_collecting_message));
    ByteArrayPayload payload = formController.getFilledInFormXml();
    // write out xml
    String instancePath = formController.getInstanceFile().getAbsolutePath();
    publishProgress(Collect.getInstance().getString(R.string.survey_saving_saving_message));
    exportXmlFile(payload, instancePath);
    // update the uri. We have exported the reloadable instance, so update status...
    // Since we saved a reloadable instance, it is flagged as re-openable so that if any error
    // occurs during the packaging of the data for the server fails (e.g., encryption),
    // we can still reopen the filled-out form and re-save it at a later time.
    updateInstanceDatabase(true, true);
    if (markCompleted) {
        // now see if the packaging of the data for the server would make it
        // non-reopenable (e.g., encryption or send an SMS or other fraction of the form).
        boolean canEditAfterCompleted = formController.isSubmissionEntireForm();
        boolean isEncrypted = false;
        // build a submission.xml to hold the data being submitted
        // and (if appropriate) encrypt the files on the side
        // pay attention to the ref attribute of the submission profile...
        File instanceXml = formController.getInstanceFile();
        File submissionXml = new File(instanceXml.getParentFile(), "submission.xml");
        payload = formController.getSubmissionXml();
        // write out submission.xml -- the data to actually submit to aggregate
        publishProgress(Collect.getInstance().getString(R.string.survey_saving_finalizing_message));
        exportXmlFile(payload, submissionXml.getAbsolutePath());
        // see if the form is encrypted and we can encrypt it...
        EncryptedFormInformation formInfo = EncryptionUtils.getEncryptedFormInformation(uri, formController.getSubmissionMetadata());
        if (formInfo != null) {
            // if we are encrypting, the form cannot be reopened afterward
            canEditAfterCompleted = false;
            // and encrypt the submission (this is a one-way operation)...
            publishProgress(Collect.getInstance().getString(R.string.survey_saving_encrypting_message));
            EncryptionUtils.generateEncryptedSubmission(instanceXml, submissionXml, formInfo);
            isEncrypted = true;
        }
        // At this point, we have:
        // 1. the saved original instanceXml,
        // 2. all the plaintext attachments
        // 2. the submission.xml that is the completed xml (whether encrypting or not)
        // 3. all the encrypted attachments if encrypting (isEncrypted = true).
        // 
        // NEXT:
        // 1. Update the instance database (with status complete).
        // 2. Overwrite the instanceXml with the submission.xml
        // and remove the plaintext attachments if encrypting
        updateInstanceDatabase(false, canEditAfterCompleted);
        if (!canEditAfterCompleted) {
            // delete the restore Xml file.
            if (!instanceXml.delete()) {
                String msg = "Error deleting " + instanceXml.getAbsolutePath() + " prior to renaming submission.xml";
                Timber.e(msg);
                throw new IOException(msg);
            }
            // rename the submission.xml to be the instanceXml
            if (!submissionXml.renameTo(instanceXml)) {
                String msg = "Error renaming submission.xml to " + instanceXml.getAbsolutePath();
                Timber.e(msg);
                throw new IOException(msg);
            }
        } else {
            // (we don't need to delete and rename anything).
            if (!submissionXml.delete()) {
                String msg = "Error deleting " + submissionXml.getAbsolutePath() + " (instance is re-openable)";
                Timber.w(msg);
            }
        }
        // (anything not named instanceXml or anything not ending in .enc)
        if (isEncrypted) {
            if (!EncryptionUtils.deletePlaintextFiles(instanceXml)) {
                Timber.e("Error deleting plaintext files for %s", instanceXml.getAbsolutePath());
            }
        }
    }
}
Also used : FormController(org.odk.collect.android.logic.FormController) ByteArrayPayload(org.javarosa.core.services.transport.payload.ByteArrayPayload) IOException(java.io.IOException) EncryptedFormInformation(org.odk.collect.android.utilities.EncryptionUtils.EncryptedFormInformation) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

ByteArrayPayload (org.javarosa.core.services.transport.payload.ByteArrayPayload)7 FormInstance (org.javarosa.core.model.instance.FormInstance)3 XFormSerializingVisitor (org.javarosa.model.xform.XFormSerializingVisitor)3 File (java.io.File)2 XFormAnswerDataSerializer (org.javarosa.xform.util.XFormAnswerDataSerializer)2 FormController (org.odk.collect.android.logic.FormController)2 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 IDataPointer (org.javarosa.core.data.IDataPointer)1 FormDef (org.javarosa.core.model.FormDef)1 StringData (org.javarosa.core.model.data.StringData)1 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)1 TreeElement (org.javarosa.core.model.instance.TreeElement)1 DataPointerPayload (org.javarosa.core.services.transport.payload.DataPointerPayload)1 MultiMessagePayload (org.javarosa.core.services.transport.payload.MultiMessagePayload)1 ParseResult (org.javarosa.xform.parse.FormParserHelper.ParseResult)1 Test (org.junit.Test)1 EncryptedFormInformation (org.odk.collect.android.utilities.EncryptionUtils.EncryptedFormInformation)1