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

Prevent duplicate and invalid titles #6326

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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 @@ -367,7 +367,7 @@ public enum ParameterCore implements ParameterInterface {
/**
* Validation of process title via regular expression.
*/
VALIDATE_PROCESS_TITLE_REGEX(new Parameter<>("validateProzessTitelRegex", "[\\w-]*")),
VALIDATE_PROCESS_TITLE_REGEX(new Parameter<>("validateProzessTitelRegex", "^[a-zA-Z0-9_-]+$")),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the new default value not even updated in the kitodo_config.properties files?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering: This is a hard limitation of the Kitodo software, some titles lead to problems later. Should we allow to change that in the config.properties at all? Or should we hardcode that in the application? Additionally one can think of allowing additional regexes which might be checked on top of the minimal requirements. See #6055

cc @solth

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At SLUB we allow even the . inside the process title as for historical reasons this was needed to create the processes. Maybe this can be changed now but this should be discussed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more note: the w meta character in regular expressions stands for a-z, A-Z, 0-9, including _ (underscore) . So the expression can be simplified to ^[\\w-]+$?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about chosing a new parameter name "validateProcessTitle" which avoids the current "denglisch" (English-German mix)?


/**
* Validation of the identifier via regular expression.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ private void createProcessHierarchy()
&& !this.titleRecordLinkTab.getSelectedInsertionPosition().isEmpty()) {
this.processes = new LinkedList<>(Collections.singletonList(this.processes.get(0)));
}
ProcessService.checkTasks(this.getMainProcess(), processDataTab.getDocType());
processTempProcess(this.processes.get(0));
processAncestors();
processChildren();
// main process and it's ancestors need to be saved, so they have IDs before creating their process directories
Expand Down Expand Up @@ -612,21 +612,25 @@ private void processAncestors() throws ProcessGenerationException {
ProcessService.setParentRelations(processes.get(index + 1).getProcess(), process);
}
if (Objects.nonNull(tempProcess.getMetadataNodes())) {
try {
tempProcess.getProcessMetadata().preserve();
ImportService.processTempProcess(tempProcess, rulesetManagement, acquisitionStage, priorityList, null);
} catch (InvalidMetadataValueException | NoSuchMetadataFieldException e) {
throw new ProcessGenerationException("Error creating process hierarchy: invalid metadata found!");
} catch (RulesetNotFoundException e) {
throw new ProcessGenerationException(
"Ruleset not found:" + tempProcess.getProcess().getRuleset().getTitle());
} catch (IOException e) {
throw new ProcessGenerationException("Error reading Ruleset: " + tempProcess.getProcess().getRuleset().getTitle());
}
processTempProcess(tempProcess);
}
}
}

private void processTempProcess(TempProcess tempProcess) throws ProcessGenerationException {
try {
tempProcess.getProcessMetadata().preserve();
ImportService.processTempProcess(tempProcess, rulesetManagement, acquisitionStage, priorityList, null);
} catch (InvalidMetadataValueException | NoSuchMetadataFieldException e) {
throw new ProcessGenerationException("Error creating process hierarchy: invalid metadata found!");
} catch (RulesetNotFoundException e) {
throw new ProcessGenerationException(
"Ruleset not found:" + tempProcess.getProcess().getRuleset().getTitle());
} catch (IOException e) {
throw new ProcessGenerationException("Error reading Ruleset: " + tempProcess.getProcess().getRuleset().getTitle());
}
}

private void saveProcessHierarchyMetadata() {
// save ancestor processes meta.xml files
for (TempProcess tempProcess : this.processes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,65 +75,109 @@ public static void cleanDatabase() throws Exception {
MockDatabase.cleanDatabase();
}

@Test
public void shouldCreateNewProcess() throws Exception {
CreateProcessForm underTest = new CreateProcessForm();
underTest.getProcessDataTab().setDocType("Monograph");
Process newProcess = new Process();
Workpiece newWorkPiece = new Workpiece();
TempProcess tempProcess = new TempProcess(newProcess, newWorkPiece);
underTest.setProcesses(new LinkedList<>(Collections.singletonList(tempProcess)));
underTest.getMainProcess().setProject(ServiceManager.getProjectService().getById(1));
underTest.getMainProcess().setRuleset(ServiceManager.getRulesetService().getById(1));
underTest.getMainProcess().setTitle("title");
// Helper to create and initialize a CreateProcessForm with common properties
private CreateProcessForm setupCreateProcessForm(String docType, String title) throws Exception {
CreateProcessForm form = new CreateProcessForm();
form.getProcessDataTab().setDocType(docType);

Process process = new Process();
Workpiece workPiece = new Workpiece();
TempProcess tempProcess = new TempProcess(process, workPiece);
form.setProcesses(new LinkedList<>(Collections.singletonList(tempProcess)));

form.getMainProcess().setProject(ServiceManager.getProjectService().getById(1));
form.getMainProcess().setRuleset(ServiceManager.getRulesetService().getById(1));
form.getMainProcess().setTitle(title);

form.updateRulesetAndDocType(tempProcess.getProcess().getRuleset());
return form;
}

// Helper to manage script permissions
private void setScriptPermissions(boolean enable) throws Exception {
File script = new File(ConfigCore.getParameter(ParameterCore.SCRIPT_CREATE_DIR_META));
if (!SystemUtils.IS_OS_WINDOWS) {
ExecutionPermission.setExecutePermission(script);
if (enable) {
ExecutionPermission.setExecutePermission(script);
} else {
ExecutionPermission.setNoExecutePermission(script);
}
}
}

// Helper to clean up database and file system
private void cleanUpProcess(Process process) throws Exception {
Integer processId = process.getId();
processService.remove(processId);
fileService.delete(URI.create(processId.toString()));
}

@Test
public void shouldCreateNewProcess() throws Exception {
CreateProcessForm underTest = setupCreateProcessForm("Monograph", "title");

setScriptPermissions(true);
long before = processService.count();
underTest.createNewProcess();
if (!SystemUtils.IS_OS_WINDOWS) {
ExecutionPermission.setNoExecutePermission(script);
}
setScriptPermissions(false);

long after = processService.count();
assertEquals(before + 1, after, "No process was created!");

// clean up database, index and file system
Integer processId = newProcess.getId();
processService.remove(processId);
fileService.delete(URI.create(processId.toString()));
cleanUpProcess(underTest.getMainProcess());
}

/**
* tests creation of processes without workflow.
*/
@Test
public void shouldCreateNewProcessWithoutWorkflow() throws Exception {
CreateProcessForm underTest = new CreateProcessForm();
underTest.getProcessDataTab().setDocType("MultiVolumeWork");
Process newProcess = new Process();
Workpiece newWorkPiece = new Workpiece();
TempProcess tempProcess = new TempProcess(newProcess, newWorkPiece);
underTest.setProcesses(new LinkedList<>(Collections.singletonList(tempProcess)));
underTest.getMainProcess().setProject(ServiceManager.getProjectService().getById(1));
underTest.getMainProcess().setRuleset(ServiceManager.getRulesetService().getById(1));
underTest.getMainProcess().setTitle("title");
CreateProcessForm underTest = setupCreateProcessForm("MultiVolumeWork", "title");

File script = new File(ConfigCore.getParameter(ParameterCore.SCRIPT_CREATE_DIR_META));
ExecutionPermission.setExecutePermission(script);
setScriptPermissions(true);
long before = processService.count();
underTest.createNewProcess();
ExecutionPermission.setNoExecutePermission(script);
setScriptPermissions(false);

long after = processService.count();
assertEquals(before + 1, after, "No process was created!");
assertTrue(underTest.getMainProcess().getTasks().isEmpty(), "Process should not have tasks");
assertNull(underTest.getMainProcess().getSortHelperStatus(), "Process should not have sortHelperStatus");

assertTrue(newProcess.getTasks().isEmpty(), "Process should not have tasks");
assertNull(newProcess.getSortHelperStatus(), "process should not have sortHelperStatus");
cleanUpProcess(underTest.getMainProcess());
}

// clean up database, index and file system
Integer processId = newProcess.getId();
processService.remove(processId);
fileService.delete(URI.create(processId.toString()));
@Test
public void shouldNotCreateNewProcessWithInvalidTitle() throws Exception {
CreateProcessForm underTest = setupCreateProcessForm("Monograph", "title with whitespaces");

long before = processService.count();
underTest.createNewProcess();
long after = processService.count();

assertEquals(before, after, "A process with an invalid title was created!");
}

@Test
public void shouldNotAllowDuplicateTitles() throws Exception {
Copy link
Collaborator Author

@BartChris BartChris Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably have to take into account the configuration here. On some systems this is allowed...

// First process creation
CreateProcessForm underTest = setupCreateProcessForm("Monograph", "title");

setScriptPermissions(true);
long before = processService.count();
underTest.createNewProcess();
setScriptPermissions(false);

long after = processService.count();
assertEquals(before + 1, after, "First process creation failed. No process was created!");

// Second process creation with duplicate title
CreateProcessForm underTestTwo = setupCreateProcessForm("Monograph", "title");

long beforeDuplicate = processService.count();
underTestTwo.createNewProcess();
long afterDuplicate = processService.count();

assertEquals(beforeDuplicate, afterDuplicate, "A duplicate process with the same title was created!");

cleanUpProcess(underTest.getMainProcess());
}

}