Search in sources :

Example 1 with FormsRepository

use of org.odk.collect.forms.FormsRepository in project collect by opendatakit.

the class FormsProvider method update.

@Override
public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
    deferDaggerInit();
    String projectId = getProjectId(uri);
    logServerEvent(projectId, AnalyticsEvents.FORMS_PROVIDER_UPDATE);
    FormsRepository formsRepository = getFormsRepository(projectId);
    String formsPath = storagePathProvider.getOdkDirPath(StorageSubdirectory.FORMS, projectId);
    String cachePath = storagePathProvider.getOdkDirPath(StorageSubdirectory.CACHE, projectId);
    int count;
    switch(URI_MATCHER.match(uri)) {
        case FORMS:
            try (Cursor cursor = databaseQuery(projectId, null, where, whereArgs, null, null, null)) {
                while (cursor.moveToNext()) {
                    Form form = getFormFromCurrentCursorPosition(cursor, formsPath, cachePath);
                    ContentValues existingValues = getValuesFromForm(form, formsPath);
                    existingValues.putAll(values);
                    formsRepository.save(getFormFromValues(existingValues, formsPath, cachePath));
                }
                count = cursor.getCount();
            }
            break;
        case FORM_ID:
            Form form = formsRepository.get(ContentUriHelper.getIdFromUri(uri));
            if (form != null) {
                ContentValues existingValues = getValuesFromForm(form, formsPath);
                existingValues.putAll(values);
                formsRepository.save(getFormFromValues(existingValues, formsPath, cachePath));
                count = 1;
            } else {
                count = 0;
            }
            break;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return count;
}
Also used : ContentValues(android.content.ContentValues) DatabaseFormsRepository(org.odk.collect.android.database.forms.DatabaseFormsRepository) FormsRepository(org.odk.collect.forms.FormsRepository) Form(org.odk.collect.forms.Form) DatabaseObjectMapper.getValuesFromForm(org.odk.collect.android.database.DatabaseObjectMapper.getValuesFromForm) Cursor(android.database.Cursor)

Example 2 with FormsRepository

use of org.odk.collect.forms.FormsRepository in project collect by opendatakit.

the class InstanceUploaderTask method onPostExecute.

@Override
protected void onPostExecute(Outcome outcome) {
    synchronized (this) {
        if (outcome != null && stateListener != null) {
            if (outcome.authRequestingServer != null) {
                stateListener.authRequest(outcome.authRequestingServer, outcome.messagesByInstanceId);
            } else {
                stateListener.uploadingComplete(outcome.messagesByInstanceId);
                // Delete instances that were successfully sent and that need to be deleted
                // either because app-level auto-delete is enabled or because the form
                // specifies it.
                Set<String> instanceIds = outcome.messagesByInstanceId.keySet();
                boolean isFormAutoDeleteOptionEnabled;
                // the app preferences set for delete after submission
                if (deleteInstanceAfterSubmission != null) {
                    isFormAutoDeleteOptionEnabled = deleteInstanceAfterSubmission;
                } else {
                    isFormAutoDeleteOptionEnabled = settingsProvider.getUnprotectedSettings().getBoolean(ProjectKeys.KEY_DELETE_AFTER_SEND);
                }
                Stream<Instance> instancesToDelete = instanceIds.stream().map(id -> new InstancesRepositoryProvider(Collect.getInstance()).get().get(Long.parseLong(id))).filter(instance -> instance.getStatus().equals(Instance.STATUS_SUBMITTED)).filter(instance -> shouldFormBeDeleted(formsRepository, instance.getFormId(), instance.getFormVersion(), isFormAutoDeleteOptionEnabled));
                DeleteInstancesTask dit = new DeleteInstancesTask(instancesRepository, formsRepository);
                dit.execute(instancesToDelete.map(Instance::getDbId).toArray(Long[]::new));
            }
        }
    }
}
Also used : InstancesRepository(org.odk.collect.forms.instances.InstancesRepository) ProjectKeys(org.odk.collect.settings.keys.ProjectKeys) SettingsProvider(org.odk.collect.settings.SettingsProvider) Instance(org.odk.collect.forms.instances.Instance) AsyncTask(android.os.AsyncTask) InstanceUploaderListener(org.odk.collect.android.listeners.InstanceUploaderListener) InstanceUploaderUtils.shouldFormBeDeleted(org.odk.collect.android.utilities.InstanceUploaderUtils.shouldFormBeDeleted) Uri(android.net.Uri) Set(java.util.Set) HashMap(java.util.HashMap) InstancesRepositoryProvider(org.odk.collect.android.utilities.InstancesRepositoryProvider) FormsRepository(org.odk.collect.forms.FormsRepository) Stream(java.util.stream.Stream) Collect(org.odk.collect.android.application.Collect) InstancesRepositoryProvider(org.odk.collect.android.utilities.InstancesRepositoryProvider) Instance(org.odk.collect.forms.instances.Instance)

Example 3 with FormsRepository

use of org.odk.collect.forms.FormsRepository in project collect by opendatakit.

the class FormMapActivityTest method setUpActivity.

@Before
public void setUpActivity() {
    CollectHelpers.setupDemoProject();
    AppDependencyComponent component = CollectHelpers.overrideAppDependencyModule(new AppDependencyModule() {

        @Override
        public MapProvider providesMapProvider() {
            MapProvider mapProvider = mock(MapProvider.class);
            when(mapProvider.createMapFragment(ArgumentMatchers.any())).thenReturn(map);
            return mapProvider;
        }
    });
    FormsRepository formsRepository = component.formsRepositoryProvider().get();
    instancesRepository = component.instancesRepositoryProvider().get();
    form = formsRepository.save(testForm);
    Arrays.stream(testInstances).forEach(instancesRepository::save);
    Intent intent = new Intent();
    intent.putExtra(FormMapActivity.EXTRA_FORM_ID, form.getDbId());
    activityController = CollectHelpers.buildThemedActivity(FormMapActivity.class, intent);
    activity = (FormMapActivity) activityController.get();
    activityController.setup();
}
Also used : MapProvider(org.odk.collect.android.geo.MapProvider) FormsRepository(org.odk.collect.forms.FormsRepository) AppDependencyComponent(org.odk.collect.android.injection.config.AppDependencyComponent) Intent(android.content.Intent) AppDependencyModule(org.odk.collect.android.injection.config.AppDependencyModule) Before(org.junit.Before)

Example 4 with FormsRepository

use of org.odk.collect.forms.FormsRepository in project collect by opendatakit.

the class FormsRepositoryTest method getAllNotDeletedByFormIdAndVersion_onlyReturnsNotDeletedFormsThatMatchVersion.

@Test
public void getAllNotDeletedByFormIdAndVersion_onlyReturnsNotDeletedFormsThatMatchVersion() {
    FormsRepository formsRepository = buildSubject();
    formsRepository.save(FormUtils.buildForm("id", "1", getFormFilesPath()).deleted(true).build());
    formsRepository.save(FormUtils.buildForm("id", "1", getFormFilesPath()).deleted(false).build());
    formsRepository.save(FormUtils.buildForm("id", "2", getFormFilesPath()).deleted(true).build());
    formsRepository.save(FormUtils.buildForm("id", "2", getFormFilesPath()).deleted(false).build());
    List<Form> forms = formsRepository.getAllNotDeletedByFormIdAndVersion("id", "2");
    assertThat(forms.size(), is(1));
    assertThat(forms.get(0).getVersion(), equalTo("2"));
}
Also used : FormsRepository(org.odk.collect.forms.FormsRepository) Form(org.odk.collect.forms.Form) Test(org.junit.Test)

Example 5 with FormsRepository

use of org.odk.collect.forms.FormsRepository in project collect by opendatakit.

the class FormsRepositoryTest method save_addsHashBasedOnFormFile.

@Test
public void save_addsHashBasedOnFormFile() {
    FormsRepository formsRepository = buildSubject();
    Form form = FormUtils.buildForm("id", "version", getFormFilesPath()).build();
    assertThat(form.getMD5Hash(), equalTo(null));
    formsRepository.save(form);
    String expectedHash = Md5.getMd5Hash(new File(form.getFormFilePath()));
    assertThat(formsRepository.get(1L).getMD5Hash(), equalTo(expectedHash));
}
Also used : FormsRepository(org.odk.collect.forms.FormsRepository) Form(org.odk.collect.forms.Form) File(java.io.File) Test(org.junit.Test)

Aggregations

FormsRepository (org.odk.collect.forms.FormsRepository)26 Form (org.odk.collect.forms.Form)21 Test (org.junit.Test)20 File (java.io.File)8 Instance (org.odk.collect.forms.instances.Instance)4 Intent (android.content.Intent)3 Uri (android.net.Uri)3 AsyncTask (android.os.AsyncTask)3 BroadcastReceiver (android.content.BroadcastReceiver)2 Context (android.content.Context)2 DialogInterface (android.content.DialogInterface)2 BUTTON_NEGATIVE (android.content.DialogInterface.BUTTON_NEGATIVE)2 BUTTON_POSITIVE (android.content.DialogInterface.BUTTON_POSITIVE)2 IntentFilter (android.content.IntentFilter)2 LocationManager (android.location.LocationManager)2 MediaPlayer (android.media.MediaPlayer)2 Build (android.os.Build)2 Bundle (android.os.Bundle)2 ContextMenu (android.view.ContextMenu)2 ContextMenuInfo (android.view.ContextMenu.ContextMenuInfo)2