Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make search for media available as a Kitodo script #4450

Merged
merged 2 commits into from
Jun 4, 2021
Merged
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 @@ -45,6 +45,7 @@
import org.kitodo.data.database.enums.TaskStatus;
import org.kitodo.data.database.exceptions.DAOException;
import org.kitodo.data.exceptions.DataException;
import org.kitodo.exceptions.InvalidImagesException;
import org.kitodo.production.controller.SecurityAccessController;
import org.kitodo.production.dto.ProcessDTO;
import org.kitodo.production.dto.TaskDTO;
Expand Down Expand Up @@ -666,7 +667,7 @@ private void executeKitodoScriptForProcesses(List<Process> processes, String kit
KitodoScriptService service = new KitodoScriptService();
try {
service.execute(processes, kitodoScript);
} catch (DataException e) {
} catch (DataException | IOException | InvalidImagesException e) {
Helper.setErrorMessage(e.getLocalizedMessage(), logger, e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.lang.text.StrTokenizer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.kitodo.api.dataformat.Workpiece;
import org.kitodo.data.database.beans.Folder;
import org.kitodo.data.database.beans.Process;
import org.kitodo.data.database.beans.Role;
Expand All @@ -36,15 +36,16 @@
import org.kitodo.data.database.enums.TaskStatus;
import org.kitodo.data.exceptions.DataException;
import org.kitodo.exceptions.CommandException;
import org.kitodo.exceptions.InvalidImagesException;
import org.kitodo.export.ExportDms;
import org.kitodo.production.enums.GenerationMode;
import org.kitodo.production.helper.Helper;
import org.kitodo.production.helper.metadata.legacytypeimplementations.LegacyMetsModsDigitalDocumentHelper;
import org.kitodo.production.helper.tasks.TaskManager;
import org.kitodo.production.metadata.copier.CopierData;
import org.kitodo.production.metadata.copier.DataCopier;
import org.kitodo.production.model.Subfolder;
import org.kitodo.production.services.ServiceManager;
import org.kitodo.production.services.data.ProcessService;
import org.kitodo.production.services.dataformat.MetsService;
import org.kitodo.production.services.file.FileService;
import org.kitodo.production.services.image.ImageGenerator;
import org.kitodo.production.thread.TaskImageGeneratorThread;
Expand All @@ -68,7 +69,8 @@ public class KitodoScriptService {
* @param script
* from frontend passed as String
*/
public void execute(List<Process> processes, String script) throws DataException {
public void execute(List<Process> processes, String script)
throws DataException, IOException, InvalidImagesException {
this.parameters = new HashMap<>();
// decompose and capture all script parameters
StrTokenizer tokenizer = new StrTokenizer(script, ' ', '\"');
Expand All @@ -95,7 +97,8 @@ public void execute(List<Process> processes, String script) throws DataException
}
}

private boolean executeScript(List<Process> processes, String script) throws DataException {
private boolean executeScript(List<Process> processes, String script)
throws DataException, IOException, InvalidImagesException {
// call the correct method via the parameter
switch (this.parameters.get("action")) {
case "importFromFileSystem":
Expand Down Expand Up @@ -133,6 +136,16 @@ private boolean executeScript(List<Process> processes, String script) throws Dat
case "doit2":
exportDms(processes, String.valueOf(Boolean.FALSE));
break;
default:
return executeOtherScript(processes, script);
}
return true;
}

private boolean executeOtherScript(List<Process> processes, String script)
throws DataException, IOException, InvalidImagesException {
// call the correct method via the parameter
switch (this.parameters.get("action")) {
case "runscript":
String taskName = this.parameters.get("stepname");
String scriptName = this.parameters.get(SCRIPT);
Expand Down Expand Up @@ -163,6 +176,16 @@ private boolean executeScript(List<Process> processes, String script) throws Dat
case "copyDataToChildren":
copyDataToChildren(processes, script);
break;
default:
return executeRemainingScript(processes, script);
}
return true;
}

private boolean executeRemainingScript(List<Process> processes, String script)
throws DataException, IOException, InvalidImagesException {
// call the correct method via the parameter
switch (this.parameters.get("action")) {
case "generateImages":
String folders = parameters.get("folders");
List<String> foldersList = Arrays.asList("all");
Expand All @@ -176,6 +199,9 @@ private boolean executeScript(List<Process> processes, String script) throws Dat
}
generateImages(processes, mode, foldersList);
break;
case "searchForMedia":
searchForMedia(processes);
break;
default:
Helper.setErrorMessage("Unknown action",
" - use: 'action:addRole, action:setTaskProperty, action:setStepStatus, "
Expand Down Expand Up @@ -379,6 +405,19 @@ private void generateImages(List<Process> processes, GenerationMode generationMo
}
}

private void searchForMedia(List<Process> processes) throws IOException, InvalidImagesException {
FileService fileService = ServiceManager.getFileService();
MetsService metsService = ServiceManager.getMetsService();
ProcessService processService = ServiceManager.getProcessService();

for (Process process : processes) {
URI metadataFileUri = processService.getMetadataFileUri(process);
Workpiece workpiece = metsService.loadWorkpiece(metadataFileUri);
fileService.searchForMedia(process, workpiece);
metsService.saveWorkpiece(workpiece, metadataFileUri);
}
}

private void runScript(List<Process> processes, String taskName, String scriptName) throws DataException {
for (Process process : processes) {
for (Task task : process.getTasks()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
<p:commandButton value="generateImages"
update="executeScriptSelectedForm:selectionScriptFieldTextArea"
onclick="document.getElementById('executeScriptSelectedForm:selectionScriptFieldTextArea').value='action:generateImages folders:all|jpgs/max,jpgs/thumbs,... images:missingOrDamaged|missing|all'"/>
<p:commandButton value="searchForMedia"
update="executeScriptSelectedForm:selectionScriptFieldTextArea"
onclick="document.getElementById('executeScriptSelectedForm:selectionScriptFieldTextArea').value='action:searchForMedia'"/>
</div>

<div>
Expand Down