Search in sources :

Example 26 with Instance

use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.

the class InstancesRepositoryTest method save_returnsInstanceWithId.

@Test
public void save_returnsInstanceWithId() {
    InstancesRepository instancesRepository = buildSubject();
    Instance instance = instancesRepository.save(InstanceUtils.buildInstance("formid", "1", getInstancesDir()).build());
    assertThat(instancesRepository.get(instance.getDbId()), is(instance));
}
Also used : Instance(org.odk.collect.forms.instances.Instance) InstancesRepository(org.odk.collect.forms.instances.InstancesRepository) Test(org.junit.Test)

Example 27 with Instance

use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.

the class InstancesRepositoryTest method getAllNotDeleted_returnsUndeletedInstances.

@Test
public void getAllNotDeleted_returnsUndeletedInstances() {
    InstancesRepository instancesRepository = buildSubject();
    instancesRepository.save(InstanceUtils.buildInstance("deleted", "1", getInstancesDir()).status(Instance.STATUS_COMPLETE).deletedDate(System.currentTimeMillis()).build());
    instancesRepository.save(InstanceUtils.buildInstance("undeleted", "1", getInstancesDir()).status(Instance.STATUS_COMPLETE).build());
    List<Instance> allNotDeleted = instancesRepository.getAllNotDeleted();
    assertThat(allNotDeleted.size(), is(1));
    assertThat(allNotDeleted.get(0).getFormId(), is("undeleted"));
}
Also used : Instance(org.odk.collect.forms.instances.Instance) InstancesRepository(org.odk.collect.forms.instances.InstancesRepository) Test(org.junit.Test)

Example 28 with Instance

use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.

the class ExternalAppsUtils method getValueRepresentedBy.

public static Object getValueRepresentedBy(String text, TreeReference reference) throws XPathSyntaxException {
    if (text.startsWith("'")) {
        // treat this as a constant parameter but not require an ending quote
        return text.endsWith("'") ? text.substring(1, text.length() - 1) : text.substring(1);
    }
    FormDef formDef = Collect.getInstance().getFormController().getFormDef();
    FormInstance formInstance = formDef.getInstance();
    EvaluationContext evaluationContext = new EvaluationContext(formDef.getEvaluationContext(), reference);
    if (text.startsWith("/")) {
        // treat this is an xpath
        XPathPathExpr pathExpr = XPathReference.getPathExpr(text);
        XPathNodeset xpathNodeset = pathExpr.eval(formInstance, evaluationContext);
        return XPathFuncExpr.unpack(xpathNodeset);
    } else if (text.equals("instanceProviderID()")) {
        // instanceProviderID returns -1 if the current instance has not been saved to disk already
        String path = Collect.getInstance().getFormController().getInstanceFile().getAbsolutePath();
        String instanceProviderID = "-1";
        Instance instance = new InstancesRepositoryProvider(Collect.getInstance()).get().getOneByPath(path);
        if (instance != null) {
            instanceProviderID = instance.getDbId().toString();
        }
        return instanceProviderID;
    } else {
        // treat this as a function
        XPathExpression xpathExpression = XPathParseTool.parseXPath(text);
        return xpathExpression.eval(formInstance, evaluationContext);
    }
}
Also used : XPathExpression(org.javarosa.xpath.expr.XPathExpression) InstancesRepositoryProvider(org.odk.collect.android.utilities.InstancesRepositoryProvider) FormInstance(org.javarosa.core.model.instance.FormInstance) Instance(org.odk.collect.forms.instances.Instance) FormDef(org.javarosa.core.model.FormDef) XPathPathExpr(org.javarosa.xpath.expr.XPathPathExpr) XPathNodeset(org.javarosa.xpath.XPathNodeset) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) FormInstance(org.javarosa.core.model.instance.FormInstance)

Example 29 with Instance

use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.

the class DatabaseInstancesRepository method delete.

@Override
public void delete(Long id) {
    Instance instance = get(id);
    databaseConnection.getWriteableDatabase().delete(INSTANCES_TABLE_NAME, _ID + "=?", new String[] { String.valueOf(id) });
    deleteInstanceFiles(instance);
}
Also used : Instance(org.odk.collect.forms.instances.Instance) DatabaseObjectMapper.getValuesFromInstance(org.odk.collect.android.database.DatabaseObjectMapper.getValuesFromInstance)

Example 30 with Instance

use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.

the class InstanceSubmitter method submitInstances.

public Pair<Boolean, String> submitInstances(List<Instance> toUpload) throws SubmitException {
    if (toUpload.isEmpty()) {
        throw new SubmitException(Type.NOTHING_TO_SUBMIT);
    }
    String protocol = generalSettings.getString(ProjectKeys.KEY_PROTOCOL);
    InstanceUploader uploader;
    Map<String, String> resultMessagesByInstanceId = new HashMap<>();
    String deviceId = null;
    boolean anyFailure = false;
    if (protocol.equals(ProjectKeys.PROTOCOL_GOOGLE_SHEETS)) {
        if (permissionsProvider.isGetAccountsPermissionGranted()) {
            String googleUsername = googleAccountsManager.getLastSelectedAccountIfValid();
            if (googleUsername.isEmpty()) {
                throw new SubmitException(Type.GOOGLE_ACCOUNT_NOT_SET);
            }
            googleAccountsManager.selectAccount(googleUsername);
            uploader = new InstanceGoogleSheetsUploader(googleApiProvider.getDriveApi(googleUsername), googleApiProvider.getSheetsApi(googleUsername));
        } else {
            throw new SubmitException(Type.GOOGLE_ACCOUNT_NOT_PERMITTED);
        }
    } else {
        OpenRosaHttpInterface httpInterface = Collect.getInstance().getComponent().openRosaHttpInterface();
        uploader = new InstanceServerUploader(httpInterface, new WebCredentialsUtils(generalSettings), new HashMap<>(), generalSettings);
        deviceId = new PropertyManager().getSingularProperty(PropertyManager.PROPMGR_DEVICE_ID);
    }
    for (Instance instance : toUpload) {
        try {
            String destinationUrl;
            if (protocol.equals(ProjectKeys.PROTOCOL_GOOGLE_SHEETS)) {
                destinationUrl = uploader.getUrlToSubmitTo(instance, null, null, generalSettings.getString(KEY_GOOGLE_SHEETS_URL));
                if (!InstanceUploaderUtils.doesUrlRefersToGoogleSheetsFile(destinationUrl)) {
                    anyFailure = true;
                    resultMessagesByInstanceId.put(instance.getDbId().toString(), SPREADSHEET_UPLOADED_TO_GOOGLE_DRIVE);
                    continue;
                }
            } else {
                destinationUrl = uploader.getUrlToSubmitTo(instance, deviceId, null, null);
            }
            String customMessage = uploader.uploadOneSubmission(instance, destinationUrl);
            resultMessagesByInstanceId.put(instance.getDbId().toString(), customMessage != null ? customMessage : getLocalizedString(Collect.getInstance(), R.string.success));
            // communicated to the user. Maybe successful delete should also be communicated?
            if (InstanceUploaderUtils.shouldFormBeDeleted(formsRepository, instance.getFormId(), instance.getFormVersion(), generalSettings.getBoolean(ProjectKeys.KEY_DELETE_AFTER_SEND))) {
                new InstanceDeleter(new InstancesRepositoryProvider(Collect.getInstance()).get(), new FormsRepositoryProvider(Collect.getInstance()).get()).delete(instance.getDbId());
            }
            String action = protocol.equals(ProjectKeys.PROTOCOL_GOOGLE_SHEETS) ? "HTTP-Sheets auto" : "HTTP auto";
            String label = Collect.getFormIdentifierHash(instance.getFormId(), instance.getFormVersion());
            analytics.logEvent(SUBMISSION, action, label);
        } catch (UploadException e) {
            Timber.d(e);
            anyFailure = true;
            resultMessagesByInstanceId.put(instance.getDbId().toString(), e.getDisplayMessage());
        }
    }
    return new Pair<>(anyFailure, InstanceUploaderUtils.getUploadResultMessage(instancesRepository, Collect.getInstance(), resultMessagesByInstanceId));
}
Also used : InstanceServerUploader(org.odk.collect.android.upload.InstanceServerUploader) InstancesRepositoryProvider(org.odk.collect.android.utilities.InstancesRepositoryProvider) HashMap(java.util.HashMap) Instance(org.odk.collect.forms.instances.Instance) PropertyManager(org.odk.collect.android.logic.PropertyManager) UploadException(org.odk.collect.android.upload.UploadException) LocalizedApplicationKt.getLocalizedString(org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString) FormsRepositoryProvider(org.odk.collect.android.utilities.FormsRepositoryProvider) InstanceUploader(org.odk.collect.android.upload.InstanceUploader) OpenRosaHttpInterface(org.odk.collect.android.openrosa.OpenRosaHttpInterface) InstanceGoogleSheetsUploader(org.odk.collect.android.gdrive.InstanceGoogleSheetsUploader) WebCredentialsUtils(org.odk.collect.android.utilities.WebCredentialsUtils) Pair(android.util.Pair)

Aggregations

Instance (org.odk.collect.forms.instances.Instance)62 Test (org.junit.Test)28 InstancesRepository (org.odk.collect.forms.instances.InstancesRepository)23 InstancesRepositoryProvider (org.odk.collect.android.utilities.InstancesRepositoryProvider)10 LocalizedApplicationKt.getLocalizedString (org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString)9 File (java.io.File)7 DatabaseObjectMapper.getValuesFromInstance (org.odk.collect.android.database.DatabaseObjectMapper.getValuesFromInstance)7 Uri (android.net.Uri)6 Form (org.odk.collect.forms.Form)6 InstanceUtils.buildInstance (org.odk.collect.formstest.InstanceUtils.buildInstance)6 Pair (android.util.Pair)5 ArrayList (java.util.ArrayList)5 FormsRepositoryProvider (org.odk.collect.android.utilities.FormsRepositoryProvider)5 Date (java.util.Date)4 FormController (org.odk.collect.android.javarosawrapper.FormController)4 MapPoint (org.odk.collect.geo.maps.MapPoint)4 ContentValues (android.content.ContentValues)3 Intent (android.content.Intent)3 Cursor (android.database.Cursor)3 AsyncTask (android.os.AsyncTask)2