Skip to content

Commit

Permalink
Merge branch 'rename-media-list-function' into test-system
Browse files Browse the repository at this point in the history
  • Loading branch information
solth committed Oct 12, 2023
2 parents 2cbe9d1 + fea9599 commit d8fe9a7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1581,8 +1581,14 @@ public void revertRenaming(BidiMap<URI, URI> filenameMappings, Workpiece workpie
try {
List<URI> tempUris = new LinkedList<>();
for (Entry<URI, URI> mapping : filenameMappings.entrySet()) {
tempUris.add(fileManagementModule.rename(mapping.getKey(), mapping.getValue().toString()
+ TEMP_EXTENSION));
if (mapping.getKey().toString().endsWith(TEMP_EXTENSION)) {
// if current URI has '.tmp' extension, directly revert to original name (without '.tmp' extension)
tempUris.add(fileManagementModule.rename(mapping.getKey(), mapping.getValue().toString()));
} else {
// rename to new filename with '.tmp' extension otherwise
tempUris.add(fileManagementModule.rename(mapping.getKey(), mapping.getValue().toString()
+ TEMP_EXTENSION));
}
}
for (URI tempUri : tempUris) {
fileManagementModule.rename(tempUri, StringUtils.removeEnd(tempUri.toString(), TEMP_EXTENSION));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Objects;

import org.apache.commons.collections4.bidimap.DualHashBidiMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.kitodo.api.dataformat.Workpiece;
import org.kitodo.data.database.beans.Process;
import org.kitodo.data.elasticsearch.exceptions.CustomResponseException;
import org.kitodo.data.exceptions.DataException;
import org.kitodo.production.helper.tasks.EmptyTask;
import org.kitodo.production.metadata.MetadataLock;
import org.kitodo.production.services.ServiceManager;
Expand Down Expand Up @@ -53,26 +52,22 @@ public void run() {
int processId = process.getId();
URI metaXmlUri = ServiceManager.getProcessService().getMetadataFileUri(process);
DualHashBidiMap<URI, URI> renamingMap = new DualHashBidiMap<>();
boolean success = false;
Workpiece workpiece = null;
try {
Workpiece workpiece = ServiceManager.getMetsService().loadWorkpiece(metaXmlUri);
workpiece = ServiceManager.getMetsService().loadWorkpiece(metaXmlUri);
int numberOfRenamedFiles = ServiceManager.getFileService().renameMediaFiles(process, workpiece,
renamingMap);
try (OutputStream out = ServiceManager.getFileService().write(metaXmlUri)) {
ServiceManager.getMetsService().save(workpiece, out);
ServiceManager.getProcessService().saveToIndex(process, false);
success = true;
logger.info("Renamed " + numberOfRenamedFiles + " media files for process " + process.getId());
} catch (CustomResponseException | DataException e) {
logger.error(e.getMessage());
}
if (!success) {
ServiceManager.getFileService().revertRenaming(renamingMap, workpiece);
}
MetadataLock.setFree(processId);
} catch (IOException | URISyntaxException e) {
logger.error(e.getMessage());
if (Objects.nonNull(workpiece)) {
ServiceManager.getFileService().revertRenaming(renamingMap.inverseBidiMap(), workpiece);
}
}
MetadataLock.setFree(processId);
setProgress((100 / processes.size()) * (processes.indexOf(process) + 1));
}
setProgress(100);
Expand Down

0 comments on commit d8fe9a7

Please sign in to comment.