Skip to content

Commit

Permalink
Check if forms contain entity attachemnts as well
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed Oct 25, 2024
1 parent 8dd74a9 commit 57c2b95
Showing 1 changed file with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.odk.collect.forms.FormSource;
import org.odk.collect.forms.FormSourceException;
import org.odk.collect.forms.FormsRepository;
import org.odk.collect.forms.MediaFile;
import org.odk.collect.shared.files.FileExt;
import org.odk.collect.shared.strings.Md5;

Expand Down Expand Up @@ -90,6 +91,7 @@ private void processOneForm(ServerFormDetails fd, OngoingWorkListener stateListe
String tempMediaPath = new File(tempDir, "media").getAbsolutePath();
FileResult fileResult = null;
boolean newAttachmentsDetected = false;
boolean entityAttachmentsDetected = false;

try {
// get the xml file
Expand All @@ -99,6 +101,7 @@ private void processOneForm(ServerFormDetails fd, OngoingWorkListener stateListe
// download media files if there are any
if (fd.getManifest() != null && !fd.getManifest().getMediaFiles().isEmpty()) {
newAttachmentsDetected = ServerFormUseCases.downloadMediaFiles(fd, formSource, formsRepository, tempMediaPath, tempDir, entitiesRepository, stateListener);
entityAttachmentsDetected = fd.getManifest().getMediaFiles().stream().anyMatch(MediaFile::isEntityList);
}

ServerFormUseCases.copySavedFileFromPreviousFormVersionIfExists(formsRepository, fd.getFormId(), tempMediaPath);
Expand Down Expand Up @@ -138,7 +141,7 @@ private void processOneForm(ServerFormDetails fd, OngoingWorkListener stateListe
}

try {
installEverything(tempMediaPath, fileResult, formMetadata, formsDirPath, newAttachmentsDetected);
installEverything(tempMediaPath, fileResult, formMetadata, formsDirPath, newAttachmentsDetected, entityAttachmentsDetected);
} catch (FormDownloadException.DiskError e) {
cleanUp(fileResult, tempMediaPath);
throw e;
Expand All @@ -150,7 +153,7 @@ private boolean isSubmissionOk(FormMetadata formMetadata) {
return submission == null || Validator.isUrlValid(submission);
}

private void installEverything(String tempMediaPath, FileResult fileResult, FormMetadata formMetadata, String formsDirPath, boolean newAttachmentsDetected) throws FormDownloadException.DiskError {
private void installEverything(String tempMediaPath, FileResult fileResult, FormMetadata formMetadata, String formsDirPath, boolean newAttachmentsDetected, boolean entityAttachmentsDetected) throws FormDownloadException.DiskError {
FormResult formResult;

File formFile;
Expand All @@ -171,7 +174,7 @@ private void installEverything(String tempMediaPath, FileResult fileResult, Form
}

// Save form in database
formResult = findOrCreateForm(formFile, formMetadata);
formResult = findOrCreateForm(formFile, formMetadata, entityAttachmentsDetected);

// move the media files in the media folder
if (tempMediaPath != null) {
Expand Down Expand Up @@ -208,21 +211,21 @@ private void cleanUp(FileResult fileResult, String tempMediaPath) {
}
}

private FormResult findOrCreateForm(File formFile, FormMetadata formMetadata) {
private FormResult findOrCreateForm(File formFile, FormMetadata formMetadata, boolean entityAttachmentsDetected) {
final String formFilePath = formFile.getAbsolutePath();
String mediaPath = FileUtils.constructMediaPath(formFilePath);

Form existingForm = formsRepository.getOneByPath(formFile.getAbsolutePath());

if (existingForm == null) {
Form newForm = saveNewForm(formMetadata, formFile, mediaPath);
Form newForm = saveNewForm(formMetadata, formFile, mediaPath, entityAttachmentsDetected);
return new FormResult(newForm, true);
} else {
return new FormResult(existingForm, false);
}
}

private Form saveNewForm(FormMetadata formMetadata, File formFile, String mediaPath) {
private Form saveNewForm(FormMetadata formMetadata, File formFile, String mediaPath, boolean entityAttachmentsDetected) {
Form form = new Form.Builder()
.formFilePath(formFile.getAbsolutePath())
.formMediaPath(mediaPath)
Expand All @@ -234,7 +237,7 @@ private Form saveNewForm(FormMetadata formMetadata, File formFile, String mediaP
.autoDelete(formMetadata.getAutoDelete())
.autoSend(formMetadata.getAutoSend())
.geometryXpath(formMetadata.getGeometryXPath())
.usesEntities(formMetadata.isEntityForm())
.usesEntities(formMetadata.isEntityForm() || entityAttachmentsDetected)
.build();

return formsRepository.save(form);
Expand Down

0 comments on commit 57c2b95

Please sign in to comment.