use of org.odk.collect.forms.Form in project collect by opendatakit.
the class DatabaseFormsRepository method getFormsFromCursor.
@NotNull
private static List<Form> getFormsFromCursor(Cursor cursor, String formsPath, String cachePath) {
List<Form> forms = new ArrayList<>();
if (cursor != null) {
cursor.moveToPosition(-1);
while (cursor.moveToNext()) {
Form form = getFormFromCurrentCursorPosition(cursor, formsPath, cachePath);
forms.add(form);
}
}
return forms;
}
use of org.odk.collect.forms.Form in project collect by opendatakit.
the class DatabaseFormsRepository method deleteForms.
private void deleteForms(String selection, String[] selectionArgs) {
List<Form> forms = queryForForms(selection, selectionArgs);
for (Form form : forms) {
deleteFilesForForm(form);
}
SQLiteDatabase writeableDatabase = databaseConnection.getWriteableDatabase();
writeableDatabase.delete(FORMS_TABLE_NAME, selection, selectionArgs);
}
use of org.odk.collect.forms.Form 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;
}
use of org.odk.collect.forms.Form in project collect by opendatakit.
the class FormDeleter method delete.
public void delete(Long id) {
Form form = formsRepository.get(id);
List<Instance> instancesForVersion = instancesRepository.getAllNotDeletedByFormIdAndVersion(form.getFormId(), form.getVersion());
// If there's more than one form with the same formid/version, trust the user that they want to truly delete this one
// because otherwise it may not ever be removed (instance deletion only deletes one corresponding form).
List<Form> formsWithSameFormIdVersion = formsRepository.getAllByFormIdAndVersion(form.getFormId(), form.getVersion());
if (instancesForVersion.isEmpty() || formsWithSameFormIdVersion.size() > 1) {
formsRepository.delete(id);
} else {
formsRepository.softDelete(form.getDbId());
}
}
use of org.odk.collect.forms.Form in project collect by opendatakit.
the class ServerFormDownloader method downloadXform.
/**
* Takes the formName and the URL and attempts to download the specified file. Returns a file
* object representing the downloaded file.
*/
private FileResult downloadXform(String formName, String url, FormDownloaderListener stateListener, File tempDir, String formsDirPath) throws FormSourceException, IOException, FormDownloadException.DownloadingInterrupted {
InputStream xform = formSource.fetchForm(url);
String fileName = getFormFileName(formName, formsDirPath);
File tempFormFile = new File(tempDir + File.separator + fileName);
writeFile(xform, tempFormFile, tempDir, stateListener);
// we've downloaded the file, and we may have renamed it
// make sure it's not the same as a file we already have
Form form = formsRepository.getOneByMd5Hash(Md5.getMd5Hash(tempFormFile));
if (form != null) {
// delete the file we just downloaded, because it's a duplicate
FileUtils.deleteAndReport(tempFormFile);
// set the file returned to the file we already had
return new FileResult(new File(form.getFormFilePath()), false);
} else {
return new FileResult(tempFormFile, true);
}
}
Aggregations