Skip to content

Commit

Permalink
Merge pull request kitodo#6354 from effective-webwork/workflow-error-…
Browse files Browse the repository at this point in the history
…message

Display error message when saving workflow with existing title
  • Loading branch information
solth authored Dec 22, 2024
2 parents 288384d + 6a40b07 commit dfa8434
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 11 deletions.
23 changes: 13 additions & 10 deletions Kitodo/src/main/java/org/kitodo/production/forms/WorkflowForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public WorkflowForm() {
/**
* Get list of workflow statues for select list.
*
* @return list of SelectItem objects
* @return array of SelectItem objects
*/
public WorkflowStatus[] getWorkflowStatuses() {
return WorkflowStatus.values();
Expand Down Expand Up @@ -160,7 +160,10 @@ public String saveAndRedirect() {
} else {
return this.stayOnCurrentPage;
}
} catch (IOException | DAOException | DataException e) {
} catch (DataException e) {
Helper.setErrorMessage(e.getLocalizedMessage(), logger, e);
return this.stayOnCurrentPage;
} catch (IOException | DAOException e) {
Helper.setErrorMessage("errorDiagramFile", new Object[] {this.workflow.getTitle() }, logger, e);
return this.stayOnCurrentPage;
} catch (WorkflowException e) {
Expand Down Expand Up @@ -226,7 +229,11 @@ public String cancel() {
*/
public void archive() {
this.workflow.setStatus(WorkflowStatus.ARCHIVED);
saveWorkflow();
try {
saveWorkflow();
} catch (DataException e) {
Helper.setErrorMessage(e.getLocalizedMessage(), logger, e);
}
}

/**
Expand Down Expand Up @@ -326,12 +333,8 @@ private String encodeXMLDiagramName(String xmlDiagramName) {
return xmlDiagramName;
}

private void saveWorkflow() {
try {
ServiceManager.getWorkflowService().save(this.workflow, true);
} catch (DataException e) {
Helper.setErrorMessage(e.getLocalizedMessage(), logger, e);
}
private void saveWorkflow() throws DataException {
ServiceManager.getWorkflowService().saveWorkflow(this.workflow);
}

/**
Expand Down Expand Up @@ -535,7 +538,7 @@ public void setMigration(boolean migration) {
/**
* Get language.
*
* @return language of the currently logged in user
* @return language of the currently logged-in user
*/
public String getLanguage() {
return ServiceManager.getUserService().getCurrentUser().getLanguage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,28 @@ public List<Workflow> getAvailableWorkflows() {
public List<Workflow> getAllActiveWorkflows() {
return dao.getAllActive();
}

private List<Workflow> getByTitle(String title) {
return dao.getByQuery("FROM Workflow WHERE title = :title", Collections.singletonMap("title", title));
}

/**
* Save given workflow if it is an existing workflow (e.g. if it does have an ID)
* or if it is a new workflow and no workflow with the same name exists.
*
* @param workflow the object to save
*
* @throws DataException if the given workflow is a new workflow and a workflow with the same name already exists
*/
public void saveWorkflow(Workflow workflow) throws DataException {
if (Objects.nonNull(workflow.getId())) {
save(workflow, true);
} else {
if (getByTitle(workflow.getTitle()).isEmpty()) {
save(workflow, true);
} else {
throw new DataException(Helper.getTranslation("duplicateWorkflowTitle", workflow.getTitle()));
}
}
}
}
1 change: 1 addition & 0 deletions Kitodo/src/main/resources/messages/errors_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ docketNotFound=Die angegebene Datei konnte nicht gefunden werden.
docketTitleDuplicated=Der Laufzettel mit den gleichen Titel existiert schon.
docTypeNotFound=docType ''{0}'' wurde nicht im selektierten Regelsatz gefunden
duplicatedTitles=Es wurden dublette Ausgabe-Bezeichnungen gefunden. Dies erzeugt gegebenenfalls dublette Vorgangstitel!
duplicateWorkflowTitle=Ein Workflow mit dem Titel "{0}" existiert bereits.
emptySourceFolder=Der Quellordner zur Bildgenerierung ist leer
errorDataIncomplete=Unvollst\u00E4ndige Daten\:
errorDatabaseReading=Fehler beim Datenbanklesen von ''{0}'' with ID {1}.
Expand Down
1 change: 1 addition & 0 deletions Kitodo/src/main/resources/messages/errors_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ docketNotFound=The specified file could not be found.
docketTitleDuplicated=The docket with the same title exists already.
docTypeNotFound=docType ''{0}'' could not be found in selected ruleset
duplicatedTitles=Duplicate issue designations were found. This may produce duplicate process titles!
duplicateWorkflowTitle=Workflow with title "{0}" already exists.
emptySourceFolder=The source folder is empty
errorDataIncomplete=Incomplete data\:
errorDatabaseReading=Error on reading database for ''{0}'' with ID {1}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package org.kitodo.production.services.data;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
Expand All @@ -22,11 +23,13 @@
import org.kitodo.MockDatabase;
import org.kitodo.SecurityTestUtils;
import org.kitodo.data.database.beans.Workflow;
import org.kitodo.data.exceptions.DataException;
import org.kitodo.production.helper.Helper;
import org.kitodo.production.services.ServiceManager;

public class WorkflowServiceIT {

private WorkflowService workflowService = ServiceManager.getWorkflowService();
private final WorkflowService workflowService = ServiceManager.getWorkflowService();

@BeforeAll
public static void prepareDatabase() throws Exception {
Expand Down Expand Up @@ -62,4 +65,14 @@ public void shouldGetAvailableWorkflows() throws Exception {

SecurityTestUtils.cleanSecurityContext();
}

@Test
public void shouldNotSaveNewWorkflowWithExistingTitle() {
Workflow workflow = new Workflow("test");
String expectedExceptionMessage = Helper.getTranslation("duplicateWorkflowTitle", "test");
DataException dataException = assertThrows(DataException.class,
() -> workflowService.saveWorkflow(workflow),
"Expected DataException to be thrown when saving a new workflow with an existing title");
assertEquals(expectedExceptionMessage, dataException.getMessage());
}
}

0 comments on commit dfa8434

Please sign in to comment.