Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,7 @@ public void testDownloadBatch_ExpectPass() {
synchronizer.uploadInstanceFile(destFile2, cat2.instanceFileDownloadUri);

synchronizer.downloadInstanceFileBatch(listOfCats, testTableRes.getInstanceFilesUri(),
rowId, testTableId);
rowId, testTableId, false);

synchronizer.deleteTable(testTableRes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.opendatakit.aggregate.odktables.rest.entity.TableResource;
import org.opendatakit.exception.ServicesAvailabilityException;
import org.opendatakit.logging.WebLogger;
import org.opendatakit.properties.CommonToolProperties;
import org.opendatakit.properties.PropertiesSingleton;
import org.opendatakit.services.R;
import org.opendatakit.services.sync.service.exceptions.AccessDeniedException;
import org.opendatakit.services.sync.service.exceptions.NoAppNameSpecifiedException;
Expand All @@ -37,6 +39,7 @@
import org.opendatakit.sync.service.TableLevelResult;
import org.opendatakit.utilities.ODKFileUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

Expand Down Expand Up @@ -163,13 +166,15 @@ private class SyncTask implements Runnable {
private final boolean onlyVerifySettings;
private final boolean push;
private final SyncAttachmentState attachmentState;
private PropertiesSingleton properties;

public SyncTask(Context context, String versionCodeString) {
this.context = context;
this.versionCodeString = versionCodeString;
this.onlyVerifySettings = true;
this.push = false;
this.attachmentState = SyncAttachmentState.NONE;
this.properties = CommonToolProperties.get(context, appName);
}

public SyncTask(Context context, String versionCodeString, boolean push, SyncAttachmentState attachmentState) {
Expand All @@ -178,6 +183,7 @@ public SyncTask(Context context, String versionCodeString, boolean push, SyncAtt
this.onlyVerifySettings = false;
this.push = push;
this.attachmentState = attachmentState;
this.properties = CommonToolProperties.get(context, appName);
}

@Override
Expand Down Expand Up @@ -316,8 +322,13 @@ private void sync() {
// was an app-level sync failure or if the particular tableId
// experienced a table-level sync failure in the preceeding step.

String prevDownloadAttachmentStateStr = properties.getProperty(CommonToolProperties.KEY_PREV_SYNC_ATTACHMENT_STATE);
SyncAttachmentState prevDownloadAttachmentState = SyncAttachmentState.valueOf(prevDownloadAttachmentStateStr);
try {
rowDataProcessor.synchronizeDataRowsAndAttachments(workingListOfTables, attachmentState);
rowDataProcessor.synchronizeDataRowsAndAttachments(workingListOfTables, attachmentState, prevDownloadAttachmentState);
if (SyncAttachmentState.involvesDownload(attachmentState)) {
properties.setProperties(Collections.singletonMap(CommonToolProperties.KEY_PREV_SYNC_ATTACHMENT_STATE, attachmentState.name()));
}
} catch (ServicesAvailabilityException e) {
WebLogger.getLogger(appName).printStackTrace(e);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ public void uploadInstanceFileBatch(List<CommonFileAttachmentTerms> batch,

@Override
public void downloadInstanceFileBatch(List<CommonFileAttachmentTerms> filesToDownload,
String serverInstanceFileUri, String instanceId, String tableId) throws HttpClientWebException, IOException {
String serverInstanceFileUri, String instanceId, String tableId, boolean reduceImageSize) throws HttpClientWebException, IOException {
// boolean downloadedAllFiles = true;

URI instanceFilesDownloadUri = wrapper.constructInstanceFileBulkDownloadUri(serverInstanceFileUri, instanceId);
Expand All @@ -1061,6 +1061,7 @@ public void downloadInstanceFileBatch(List<CommonFileAttachmentTerms> filesToDow
for (CommonFileAttachmentTerms cat : filesToDownload) {
OdkTablesFileManifestEntry entry = new OdkTablesFileManifestEntry();
entry.filename = cat.rowPathUri;
entry.reduceImage = String.valueOf(reduceImageSize);
entries.add(entry);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,9 @@ public void uploadInstanceFileBatch(List<CommonFileAttachmentTerms> batch, Strin
}

@Override
public void downloadInstanceFileBatch(List<CommonFileAttachmentTerms> filesToDownload, String serverInstanceFileUri, String instanceId, String tableId) throws HttpClientWebException {
public void downloadInstanceFileBatch(List<CommonFileAttachmentTerms> filesToDownload, String serverInstanceFileUri, String instanceId, String tableId, boolean reduceImageSize) throws HttpClientWebException {
try {
getRemoteInterface().downloadInstanceFileBatch(filesToDownload, serverInstanceFileUri, instanceId, tableId);
getRemoteInterface().downloadInstanceFileBatch(filesToDownload, serverInstanceFileUri, instanceId, tableId, reduceImageSize);
} catch (RemoteException e) {
rethrowException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ void uploadInstanceFileBatch(List<CommonFileAttachmentTerms> batch, String serve
*/
@Override
void downloadInstanceFileBatch(List<CommonFileAttachmentTerms> filesToDownload,
String serverInstanceFileUri, String instanceId, String tableId)
String serverInstanceFileUri, String instanceId, String tableId, boolean reduceImageSize)
throws HttpClientWebException, IOException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ private boolean compareAndDownloadConfigFile(String tableId, OdkTablesFileManife
* the server. Based upon that and the attachmentState actions, it determines
* whether the row can be transitioned into the synced state (from synced_pending_files).
*
* @param serverInstanceFileUri
* @param serverInstanceFileUri Note that this does not identify a file on a server
* @param tableId
* @param localRow
* @param attachmentState
Expand Down Expand Up @@ -816,7 +816,12 @@ public boolean syncRowLevelFileAttachments(String serverInstanceFileUri, String
// Check if the server and local versions match
String localMd5 = ODKFileUtils.getMd5Hash(sc.getAppName(), cat.localFile);

if (!localMd5.equals(entry.md5hash)) {
if (attachmentState == SyncAttachmentState.REDUCED_DOWNLOAD
|| attachmentState == SyncAttachmentState.SYNC_WITH_REDUCED_DOWNLOAD) {
if (!(localMd5.equals(entry.md5hash) || localMd5.equals(entry.reducedImageMd5Hash))) {
filesToDownloadSizes.put(cat, entry.contentLength);
}
} else if (!localMd5.equals(entry.md5hash)) {
// Found, but it is wrong locally, so we need to pull it
log.e(LOGTAG, "syncRowLevelFileAttachments Row-level Manifest: md5Hash on server does not match local file hash!");
filesToDownloadSizes.put(cat, entry.contentLength);
Expand Down Expand Up @@ -868,6 +873,7 @@ public boolean syncRowLevelFileAttachments(String serverInstanceFileUri, String
log.i(LOGTAG, "syncRowLevelFileAttachments no files to send to server -- they are all synced");
fullySyncedUploads = true;
} else if (attachmentState.equals(SyncAttachmentState.SYNC) ||
attachmentState.equals(SyncAttachmentState.SYNC_WITH_REDUCED_DOWNLOAD) ||
attachmentState.equals(SyncAttachmentState.UPLOAD)) {
long batchSize = 0;
List<CommonFileAttachmentTerms> batch = new LinkedList<CommonFileAttachmentTerms>();
Expand Down Expand Up @@ -899,14 +905,20 @@ public boolean syncRowLevelFileAttachments(String serverInstanceFileUri, String
fullySyncedUploads = true;
}
// 5) Download the files from the server
boolean atleast1Download = false;
if (filesToDownloadSizes.isEmpty()){
log.i(LOGTAG, "syncRowLevelFileAttachments no files to fetch from server -- they are all synced");
fullySyncedDownloads = !impossibleToFullySyncDownloadsServerMissingFileToDownload;
} else if (attachmentState.equals(SyncAttachmentState.SYNC) ||
attachmentState.equals(SyncAttachmentState.DOWNLOAD)) {
attachmentState.equals(SyncAttachmentState.SYNC_WITH_REDUCED_DOWNLOAD) ||
attachmentState.equals(SyncAttachmentState.DOWNLOAD) ||
attachmentState.equals(SyncAttachmentState.REDUCED_DOWNLOAD)) {
atleast1Download = true;
long batchSize = 0;
List<CommonFileAttachmentTerms> batch = new LinkedList<CommonFileAttachmentTerms>();

boolean reduceImageSize = attachmentState == SyncAttachmentState.REDUCED_DOWNLOAD ||
attachmentState == SyncAttachmentState.SYNC_WITH_REDUCED_DOWNLOAD;
for (CommonFileAttachmentTerms fileAttachment : filesToDownloadSizes.keySet()) {

// Check if adding the file exceeds the batch limit. If so, download the current batch
Expand All @@ -917,7 +929,7 @@ public boolean syncRowLevelFileAttachments(String serverInstanceFileUri, String
!batch.isEmpty()) {
log.i(LOGTAG, "syncRowLevelFileAttachments downloading batch for " + instanceId);
sc.getSynchronizer().downloadInstanceFileBatch(batch,
serverInstanceFileUri, instanceId, tableId);
serverInstanceFileUri, instanceId, tableId, reduceImageSize);
batch.clear();
batchSize = 0;
}
Expand All @@ -930,14 +942,14 @@ public boolean syncRowLevelFileAttachments(String serverInstanceFileUri, String
// download the final batch
log.i(LOGTAG, "syncRowLevelFileAttachments downloading batch for " + instanceId);
sc.getSynchronizer().downloadInstanceFileBatch(batch, serverInstanceFileUri,
instanceId, tableId);
instanceId, tableId, reduceImageSize);
}

fullySyncedDownloads = !impossibleToFullySyncDownloadsServerMissingFileToDownload;
}

if ( attachmentState == SyncAttachmentState.NONE ||
((fullySyncedUploads || (attachmentState == SyncAttachmentState.DOWNLOAD)) &&
((fullySyncedUploads || (attachmentState == SyncAttachmentState.DOWNLOAD || attachmentState == SyncAttachmentState.REDUCED_DOWNLOAD)) &&
(fullySyncedDownloads || (attachmentState == SyncAttachmentState.UPLOAD))) ) {
// there may be synced_pending_files rows, but all of the uploads we want to do
// have been uploaded, and all of the downloads we want to do have been downloaded.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private void exception(String method, String tableId, Exception e, TableLevelRes
* @throws ServicesAvailabilityException
*/
public void synchronizeDataRowsAndAttachments(List<TableResource> workingListOfTables,
SyncAttachmentState attachmentState) throws ServicesAvailabilityException {
SyncAttachmentState attachmentState, SyncAttachmentState prevAttachmentState) throws ServicesAvailabilityException {
log.i(TAG, "entered synchronizeDataRowsAndAttachments()");

DbHandle db = null;
Expand All @@ -167,7 +167,7 @@ public void synchronizeDataRowsAndAttachments(List<TableResource> workingListOfT
}

synchronizeTableDataRowsAndAttachments(te, orderedDefns, displayName,
attachmentState);
attachmentState, prevAttachmentState);

// report our table-level sync status up to the server.
TableLevelResult tlr = sc.getTableLevelResult(tableId);
Expand Down Expand Up @@ -240,7 +240,7 @@ public void synchronizeDataRowsAndAttachments(List<TableResource> workingListOfT
*/
private void synchronizeTableDataRowsAndAttachments(
TableDefinitionEntry te, OrderedColumns orderedColumns, String displayName,
SyncAttachmentState attachmentState) throws ServicesAvailabilityException {
SyncAttachmentState attachmentState, SyncAttachmentState prevAttachmentState) throws ServicesAvailabilityException {

ArrayList<ColumnDefinition> fileAttachmentColumns = new ArrayList<ColumnDefinition>();
for (ColumnDefinition cd : orderedColumns.getColumnDefinitions()) {
Expand Down Expand Up @@ -335,7 +335,7 @@ private void synchronizeTableDataRowsAndAttachments(
if (!refreshFromServer) {
try {
syncAttachmentsProcessor
.syncAttachments(tableResource, te, orderedColumns, fileAttachmentColumns, attachmentState);
.syncAttachments(tableResource, te, orderedColumns, fileAttachmentColumns, attachmentState, prevAttachmentState);
} catch (Exception e) {
exception("synchronizeTableDataRowsAndAttachments - syncing attachments with server", tableId, e,
tableLevelResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.opendatakit.database.queries.BindArgs;
import org.opendatakit.database.service.DbHandle;
import org.opendatakit.exception.ServicesAvailabilityException;
import org.opendatakit.properties.CommonToolProperties;
import org.opendatakit.properties.PropertiesSingleton;
import org.opendatakit.provider.DataTableColumns;
import org.opendatakit.services.R;
import org.opendatakit.services.sync.service.SyncExecutionContext;
Expand Down Expand Up @@ -87,7 +89,7 @@ public ProcessRowDataSyncAttachments(SyncExecutionContext sharedContext) {
public void syncAttachments(TableResource tableResource,
TableDefinitionEntry te, OrderedColumns orderedColumns,
ArrayList<ColumnDefinition> fileAttachmentColumns,
SyncAttachmentState attachmentState) throws ServicesAvailabilityException {
SyncAttachmentState attachmentState, SyncAttachmentState prevAttachmentState) throws ServicesAvailabilityException {

// Prepare the tableLevelResult.
String tableId = te.getTableId();
Expand Down Expand Up @@ -137,16 +139,25 @@ public void syncAttachments(TableResource tableResource,


String sqlCommand;
BindArgs bindArgs = new BindArgs(new Object[]{ SyncState.in_conflict.name(),
SyncState.synced_pending_files.name() });
BindArgs bindArgs;
if (SyncAttachmentState.involvesReducedImgDownload(prevAttachmentState) && SyncAttachmentState.involvesFullSizeImgDownload(attachmentState)) {
// If the previous involved a reduced download and the curr involves a full download,
// we need to consider files that are in the "synced" state.
bindArgs = new BindArgs(new Object[]{ SyncState.in_conflict.name(),
SyncState.synced_pending_files.name(), SyncState.synced.name() });
} else {
// Add synced_pending_files twice to match sql query
bindArgs = new BindArgs(new Object[]{ SyncState.in_conflict.name(),
SyncState.synced_pending_files.name(), SyncState.synced_pending_files.name() });
}

{
StringBuilder sqlCommandBuilder = new StringBuilder();
sqlCommandBuilder.append("INSERT INTO ").append(local_id_table)
.append(" (").append(ID_COLUMN).append(" ) SELECT DISTINCT ")
.append(DataTableColumns.ID).append(" FROM ").append(tableId)
.append(" WHERE ")
.append(DataTableColumns.SYNC_STATE).append(" IN (?, ?) AND ")
.append(DataTableColumns.SYNC_STATE).append(" IN (?, ?, ?) AND ")
.append(DataTableColumns.ID).append(" NOT IN (SELECT DISTINCT ")
.append(DataTableColumns.ID).append(" FROM ").append(tableId).append(" WHERE ")
.append(DataTableColumns.SAVEPOINT_TYPE).append(" IS NULL)");
Expand Down Expand Up @@ -244,7 +255,9 @@ public void syncAttachments(TableResource tableResource,
// anything and never update the state to synced (it must stay in in_conflict)
syncAttachments = true;
}
} else if (state == SyncState.synced_pending_files) {
} else if (state == SyncState.synced_pending_files ||
(state == SyncState.synced &&
SyncAttachmentState.involvesReducedImgDownload(prevAttachmentState) && SyncAttachmentState.involvesFullSizeImgDownload(attachmentState))) { //
// if we succeed in fetching and deleting the local files to match the server
// then update the state to synced.
syncAttachments = true;
Expand All @@ -254,7 +267,6 @@ public void syncAttachments(TableResource tableResource,
// And try to push the file attachments...
try {
boolean outcome = true;

SyncAttachmentState filteredAttachmentState = (state == SyncState.in_conflict ?
SyncAttachmentState.DOWNLOAD :
attachmentState);
Expand Down Expand Up @@ -300,12 +312,18 @@ public void syncAttachments(TableResource tableResource,
case SYNC:
idString = R.string.sync_syncing_attachments_server_row;
break;
case SYNC_WITH_REDUCED_DOWNLOAD:
idString = R.string.sync_syncing_attachments_server_row;
break;
case UPLOAD:
idString = R.string.sync_uploading_attachments_server_row;
break;
case DOWNLOAD:
idString = R.string.sync_downloading_attachments_server_row;
break;
case REDUCED_DOWNLOAD:
idString = R.string.sync_downloading_attachments_server_row;
break;
}

publishUpdateNotification(idString, tableId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ void uploadInstanceFileBatch(List<CommonFileAttachmentTerms> batch, String serve
* @throws IOException
*/
void downloadInstanceFileBatch(List<CommonFileAttachmentTerms> filesToDownload,
String serverInstanceFileUri, String instanceId, String tableId)
String serverInstanceFileUri, String instanceId, String tableId, boolean reduceImageSize)
throws HttpClientWebException, IOException;

/**
Expand Down
4 changes: 4 additions & 0 deletions services_app/src/main/res/values-es/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
<item>UPLOAD</item>
<item>DOWNLOAD</item>
<item>NONE</item>
<item>REDUCED_DOWNLOAD</item>
<item>SYNC_WITH_REDUCED_DOWNLOAD</item>
</string-array>
<string-array name="sync_attachment_option_names">
<item>Sincronizar Todos</item>
<item>Solo enviar archivos (imágenes, audio, video) al servidor</item>
<item>Solo recibir archivos (imágenes, audio, video) del servidor</item>
<item>No sincronizar archivos</item>
<item>Solo recibir archivos (imágenes (pequeño), audio, video) al servidor</item>
<item>Sincronizar Todos, recibir imágenes pequeño</item>
</string-array>
</resources>
4 changes: 4 additions & 0 deletions services_app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
<item>UPLOAD</item>
<item>DOWNLOAD</item>
<item>NONE</item>
<item>REDUCED_DOWNLOAD</item>
<item>SYNC_WITH_REDUCED_DOWNLOAD</item>
</string-array>
<string-array name="sync_attachment_option_names">
<item>Fully Sync Attachments</item>
<item>Upload Attachments Only</item>
<item>Download Attachments Only</item>
<item>Do Not Sync Attachments</item>
<item>Download Attachments Only, Reduce Img</item>
<item>Fully Sync Attachments, Reduce Img</item>
</string-array>
</resources>