Skip to content

Commit

Permalink
Prevent deleting ImportConfigurations that are assigned as project de…
Browse files Browse the repository at this point in the history
…fault configurations
  • Loading branch information
solth committed Jul 5, 2022
1 parent 7b7e21d commit 3b84d2f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* (c) Kitodo. Key to digital objects e. V. <[email protected]>
*
* This file is part of the Kitodo project.
*
* It is licensed under GNU General Public License version 3 or later.
*
* For the full copyright and license information, please read the
* GPL3-License.txt file that was distributed with this source code.
*/

package org.kitodo.exceptions;

import org.kitodo.production.helper.Helper;

/**
* This exception is thrown when the user tries to delete an ImportConfiguration that is currently in use.
*/
public class ImportConfigurationInUseException extends RuntimeException {

private static final String CONFIG_IN_USE = "importConfig.assignedAsDefaultConfig";

/**
* Constructor with message.
*
* @param configurationTitle title of the ImportConfiguration
* @param projectTitle title of the Project to which the ImportConfiguration is assigned as default configuration
*/
public ImportConfigurationInUseException(String configurationTitle, String projectTitle) {
super(Helper.getTranslation(CONFIG_IN_USE, configurationTitle, projectTitle));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.logging.log4j.Logger;
import org.kitodo.data.database.beans.ImportConfiguration;
import org.kitodo.data.database.exceptions.DAOException;
import org.kitodo.exceptions.ImportConfigurationInUseException;
import org.kitodo.production.enums.ObjectType;
import org.kitodo.production.helper.Helper;
import org.kitodo.production.services.ServiceManager;
Expand Down Expand Up @@ -67,6 +68,10 @@ public void deleteById(int id) {
ServiceManager.getImportConfigurationService().removeFromDatabase(id);
} catch (DAOException e) {
Helper.setErrorMessage(ERROR_DELETING, new Object[] {ObjectType.IMPORT_CONFIGURATION.getTranslationSingular() }, logger, e);
} catch (ImportConfigurationInUseException e) {
Helper.setErrorMessage(
Helper.getTranslation(ERROR_DELETING, ObjectType.IMPORT_CONFIGURATION.getTranslationSingular()),
e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
import java.util.Objects;

import org.kitodo.data.database.beans.ImportConfiguration;
import org.kitodo.data.database.beans.Project;
import org.kitodo.data.database.exceptions.DAOException;
import org.kitodo.data.database.persistence.ImportConfigurationDAO;
import org.kitodo.data.exceptions.DataException;
import org.kitodo.exceptions.ImportConfigurationInUseException;
import org.kitodo.production.services.ServiceManager;
import org.kitodo.production.services.data.base.SearchDatabaseService;
import org.primefaces.model.SortOrder;

Expand Down Expand Up @@ -89,4 +92,23 @@ public Long countDatabaseRows() throws DAOException {
public Long countResults(Map filters) throws DAOException, DataException {
return countDatabaseRows();
}

/**
* Delete import configuration identified by ID. If the corresponding configuration is currently used as default
* configuration for a project, an exception is thrown.
* @param id of import configuration to delete
* @throws DAOException if import configuration could not be deleted from database
* @throws ImportConfigurationInUseException if import configuration is assigned as default configuration to at
* least one project
*/
@Override
public void removeFromDatabase(Integer id) throws DAOException, ImportConfigurationInUseException {
for (Project project : ServiceManager.getProjectService().getAll()) {
ImportConfiguration defaultConfiguration = project.getDefaultImportConfiguration();
if (Objects.nonNull(defaultConfiguration) && Objects.equals(defaultConfiguration.getId(), id)) {
throw new ImportConfigurationInUseException(defaultConfiguration.getTitle(), project.getTitle());
}
}
dao.remove(id);
}
}
1 change: 1 addition & 0 deletions Kitodo/src/main/resources/messages/messages_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ images=Bilder
imagesRead=Images lesen
imagesWrite=Images schreiben
importConfig.addSearchField=Suchfeld hinzuf\u00fcgen
importConfig.assignedAsDefaultConfig=Importkonfiguration ''{0}'' wird derzeit als Standardkonfiguration von Projekt ''{1}'' verwendet!
importConfig.configuration=Importkonfiguration
importConfig.configurations=Importkonfigurationen
importConfig.defaultConfiguration=Default-Konfiguration
Expand Down
1 change: 1 addition & 0 deletions Kitodo/src/main/resources/messages/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ imagesRead=Read images
imagesWrite=Write images
importChildren=Import child processes
importConfig.addSearchField=Add search field
importConfig.assignedAsDefaultConfig=Import configuration ''{0}'' is configured as default configuration of project ''{1}''!
importConfig.configuration=Import configuration
importConfig.configurations=Import configurations
importConfig.defaultConfiguration=Default configuration
Expand Down

0 comments on commit 3b84d2f

Please sign in to comment.