diff --git a/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/CreateProcessForm.java b/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/CreateProcessForm.java index d29c1af9abb..7435ef13322 100644 --- a/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/CreateProcessForm.java +++ b/Kitodo/src/main/java/org/kitodo/production/forms/createprocess/CreateProcessForm.java @@ -392,7 +392,7 @@ private void createProcessHierarchy() && !this.titleRecordLinkTab.getSelectedInsertionPosition().isEmpty()) { this.processes = new LinkedList<>(Collections.singletonList(this.processes.get(0))); } - ImportService.checkTasks(this.getMainProcess(), processDataTab.getDocType()); + ProcessService.checkTasks(this.getMainProcess(), processDataTab.getDocType()); processAncestors(); processChildren(); // main process and it's ancestors need to be saved so they have IDs before creating their process directories diff --git a/Kitodo/src/main/java/org/kitodo/production/helper/tasks/HierarchyMigrationTask.java b/Kitodo/src/main/java/org/kitodo/production/helper/tasks/HierarchyMigrationTask.java index 2f1ed67ee6c..66780e1862b 100644 --- a/Kitodo/src/main/java/org/kitodo/production/helper/tasks/HierarchyMigrationTask.java +++ b/Kitodo/src/main/java/org/kitodo/production/helper/tasks/HierarchyMigrationTask.java @@ -25,6 +25,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.kitodo.api.Metadata; import org.kitodo.api.MetadataEntry; import org.kitodo.api.dataformat.IncludedStructuralElement; import org.kitodo.api.dataformat.Workpiece; @@ -165,7 +166,7 @@ public void run() { * @param process * process to migrate */ - private void migrate(Process process) throws IOException, ProcessGenerationException, DataException, DAOException, CommandException { + void migrate(Process process) throws IOException, ProcessGenerationException, DataException, DAOException, CommandException { logger.info("Starting to convert process {} (ID {})...", process.getTitle(), process.getId()); long begin = System.nanoTime(); migrateMetadataFiles(process); @@ -227,13 +228,16 @@ private static Optional getParentRecordId(Process process) throws IOExce * current number of the child process */ private List createParentProcess(Process childProcess) - throws ProcessGenerationException, IOException, DataException, CommandException { + throws ProcessGenerationException, IOException, DataException, CommandException, DAOException { processGenerator.generateProcess(childProcess.getTemplate().getId(), childProcess.getProject().getId()); Process parentProcess = processGenerator.getGeneratedProcess(); processService.save(parentProcess); fileService.createProcessLocation(parentProcess); createParentMetsFile(childProcess); + checkTaskAndId(parentProcess); + processService.save(parentProcess); + parentProcess = ServiceManager.getProcessService().getById(parentProcess.getId()); ArrayList parentData = new ArrayList<>(); parentData.add(parentProcess.getId()); URI metadataFilePath = fileService.getMetadataFilePath(childProcess); @@ -242,6 +246,33 @@ private List createParentProcess(Process childProcess) return parentData; } + private void checkTaskAndId(Process parentProcess) throws IOException { + URI parentMetadataFilePath = fileService.getMetadataFilePath(parentProcess, true, true); + Workpiece workpiece = ServiceManager.getMetsService().loadWorkpiece(parentMetadataFilePath); + ProcessService.checkTasks(parentProcess, workpiece.getRootElement().getType()); + Collection metadata = workpiece.getRootElement().getMetadata(); + String shortedTitle = ""; + String catalogIdentifier = ""; + for (Metadata metadatum : metadata) { + if (metadatum.getKey().equals("TSL_ATS")) { + shortedTitle = ((MetadataEntry) metadatum).getValue(); + } + if (metadatum.getKey().equals("CatalogIDDigital")) { + catalogIdentifier = ((MetadataEntry) metadatum).getValue(); + } + } + String title = ""; + if (!shortedTitle.isEmpty()) { + title += shortedTitle + '_'; + } + if (!catalogIdentifier.isEmpty()) { + title += catalogIdentifier; + } + parentProcess.setTitle(title); + workpiece.setId(parentProcess.getId().toString()); + ServiceManager.getMetsService().saveWorkpiece(workpiece,parentMetadataFilePath); + } + /** * Links parent process and child process in the database. The processes are * saved. diff --git a/Kitodo/src/main/java/org/kitodo/production/migration/NewspaperProcessesMigrator.java b/Kitodo/src/main/java/org/kitodo/production/migration/NewspaperProcessesMigrator.java index 4a2aff7894d..b91db33ff8a 100644 --- a/Kitodo/src/main/java/org/kitodo/production/migration/NewspaperProcessesMigrator.java +++ b/Kitodo/src/main/java/org/kitodo/production/migration/NewspaperProcessesMigrator.java @@ -44,8 +44,6 @@ import org.kitodo.data.exceptions.DataException; import org.kitodo.exceptions.CommandException; import org.kitodo.exceptions.ProcessGenerationException; -import org.kitodo.production.dto.BatchDTO; -import org.kitodo.production.dto.ProcessDTO; import org.kitodo.production.helper.tasks.NewspaperMigrationTask; import org.kitodo.production.helper.tasks.TaskManager; import org.kitodo.production.metadata.MetadataEditor; @@ -53,7 +51,6 @@ import org.kitodo.production.process.ProcessGenerator; import org.kitodo.production.services.ServiceManager; import org.kitodo.production.services.data.BatchService; -import org.kitodo.production.services.data.ImportService; import org.kitodo.production.services.data.ProcessService; import org.kitodo.production.services.dataeditor.DataEditorService; import org.kitodo.production.services.dataformat.MetsService; @@ -550,7 +547,7 @@ public void createOverallProcess() throws ProcessGenerationException, IOExceptio processGenerator.generateProcess(templateId, projectId); overallProcess = processGenerator.getGeneratedProcess(); overallProcess.setTitle(getTitle()); - ImportService.checkTasks(overallProcess, overallWorkpiece.getRootElement().getType()); + ProcessService.checkTasks(overallProcess, overallWorkpiece.getRootElement().getType()); processService.saveToDatabase(overallProcess); ServiceManager.getFileService().createProcessLocation(overallProcess); overallWorkpiece.setId(overallProcess.getId().toString()); @@ -586,7 +583,7 @@ public void createNextYearProcess() throws ProcessGenerationException, IOExcepti processGenerator.generateProcess(templateId, projectId); Process yearProcess = processGenerator.getGeneratedProcess(); yearProcess.setTitle(yearTitle); - ImportService.checkTasks(yearProcess, yearToCreate.getValue().getType()); + ProcessService.checkTasks(yearProcess, yearToCreate.getValue().getType()); processService.saveToDatabase(yearProcess); MetadataEditor.addLink(overallWorkpiece.getRootElement(), yearProcess.getId()); diff --git a/Kitodo/src/main/java/org/kitodo/production/process/NewspaperProcessesGenerator.java b/Kitodo/src/main/java/org/kitodo/production/process/NewspaperProcessesGenerator.java index 9bf04822f76..9d343d218a8 100644 --- a/Kitodo/src/main/java/org/kitodo/production/process/NewspaperProcessesGenerator.java +++ b/Kitodo/src/main/java/org/kitodo/production/process/NewspaperProcessesGenerator.java @@ -61,7 +61,6 @@ import org.kitodo.production.model.bibliography.course.IndividualIssue; import org.kitodo.production.process.field.AdditionalField; import org.kitodo.production.services.ServiceManager; -import org.kitodo.production.services.data.ImportService; import org.kitodo.production.services.data.ProcessService; import org.kitodo.production.services.data.RulesetService; import org.kitodo.production.services.dataformat.MetsService; @@ -657,7 +656,7 @@ private void saveAndCloseCurrentYearProcess() throws DataException, IOException final long begin = System.nanoTime(); metsService.saveWorkpiece(yearWorkpiece, yearMetadataFileUri); - ImportService.checkTasks(yearProcess, yearWorkpiece.getRootElement().getType()); + ProcessService.checkTasks(yearProcess, yearWorkpiece.getRootElement().getType()); processService.save(yearProcess); this.yearProcess = null; @@ -723,7 +722,7 @@ private void createNewYearProcess(String yearMark, Map genericFi String title = makeTitle(yearTitleDefinition.orElse("+'_'+#YEAR"), genericFields); getGeneratedProcess().setTitle(title); - ImportService.checkTasks(getGeneratedProcess(), yearType); + ProcessService.checkTasks(getGeneratedProcess(), yearType); processService.save(getGeneratedProcess()); processService.refresh(getGeneratedProcess()); @@ -788,7 +787,7 @@ private void finish() throws DataException, IOException { overallProcess.getTitle()); } metsService.saveWorkpiece(overallWorkpiece, overallMetadataFileUri); - ImportService.checkTasks(overallProcess, overallWorkpiece.getRootElement().getType()); + ProcessService.checkTasks(overallProcess, overallWorkpiece.getRootElement().getType()); processService.save(overallProcess); if (logger.isTraceEnabled()) { diff --git a/Kitodo/src/main/java/org/kitodo/production/services/data/ImportService.java b/Kitodo/src/main/java/org/kitodo/production/services/data/ImportService.java index 4d47b8d4fff..e2d3f730ccd 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/data/ImportService.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/data/ImportService.java @@ -1119,25 +1119,10 @@ public static void processTempProcess(TempProcess tempProcess, Template template createProcessTitle(tempProcess, managementInterface, acquisitionStage, priorityList, processDetails); Process process = tempProcess.getProcess(); addProperties(tempProcess.getProcess(), template, processDetails, docType, tempProcess.getProcess().getTitle()); - checkTasks(process, docType); + ProcessService.checkTasks(process, docType); updateTasks(process); } - /** - * Checks if an imported Process should be created with Tasks and removes them if not, - * depending on the configuration of the doctype. - * @param process the process to check. - * @param docType the doctype to check in the ruleset. - */ - public static void checkTasks(Process process, String docType) throws IOException { - // remove tasks from process, if doctype is configured not to use a workflow - Collection divisionsWithNoWorkflow = ServiceManager.getRulesetService() - .openRuleset(process.getRuleset()).getDivisionsWithNoWorkflow(); - if (divisionsWithNoWorkflow.contains(docType)) { - process.getTasks().clear(); - } - } - /** * Imports a process and saves it to database. * @param ppn the ppn to import diff --git a/Kitodo/src/main/java/org/kitodo/production/services/data/ProcessService.java b/Kitodo/src/main/java/org/kitodo/production/services/data/ProcessService.java index 3252d2864cc..864a17d0e15 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/data/ProcessService.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/data/ProcessService.java @@ -220,6 +220,21 @@ public static void emptyCache() { RULESET_CACHE_FOR_CREATE_FROM_CALENDAR.clear(); } + /** + * Checks if an imported Process should be created with Tasks and removes them if not, + * depending on the configuration of the doctype. + * @param process the process to check. + * @param docType the doctype to check in the ruleset. + */ + public static void checkTasks(Process process, String docType) throws IOException { + // remove tasks from process, if doctype is configured not to use a workflow + Collection divisionsWithNoWorkflow = ServiceManager.getRulesetService() + .openRuleset(process.getRuleset()).getDivisionsWithNoWorkflow(); + if (divisionsWithNoWorkflow.contains(docType)) { + process.getTasks().clear(); + } + } + @Override public Long countDatabaseRows() throws DAOException { return countDatabaseRows("SELECT COUNT(*) FROM Process"); diff --git a/Kitodo/src/test/java/org/kitodo/production/helper/tasks/HierarchyMigrationTaskIT.java b/Kitodo/src/test/java/org/kitodo/production/helper/tasks/HierarchyMigrationTaskIT.java new file mode 100644 index 00000000000..afc572cbf61 --- /dev/null +++ b/Kitodo/src/test/java/org/kitodo/production/helper/tasks/HierarchyMigrationTaskIT.java @@ -0,0 +1,188 @@ +/* + * (c) Kitodo. Key to digital objects e. V. + * + * 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.production.helper.tasks; + +import java.io.File; +import java.io.IOException; +import java.util.Collections; +import java.util.List; + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.SystemUtils; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.kitodo.ExecutionPermission; +import org.kitodo.MockDatabase; +import org.kitodo.config.ConfigCore; +import org.kitodo.config.enums.ParameterCore; +import org.kitodo.data.database.beans.Project; +import org.kitodo.data.database.exceptions.DAOException; +import org.kitodo.data.exceptions.DataException; +import org.kitodo.exceptions.CommandException; +import org.kitodo.exceptions.ProcessGenerationException; +import org.kitodo.production.services.ServiceManager; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +public class HierarchyMigrationTaskIT { + + private static Project project; + private static final File script = new File(ConfigCore.getParameter(ParameterCore.SCRIPT_CREATE_DIR_META)); + + /** + * prepares database. + */ + @BeforeClass + public static void prepareDatabase() throws Exception { + if (!SystemUtils.IS_OS_WINDOWS) { + ExecutionPermission.setExecutePermission(script); + } + + MockDatabase.startNode(); + MockDatabase.insertProcessesFull(); + + project = ServiceManager.getProjectService().getById(1); + moveMetaFileAway(2, "meta_MigrationTaskIT.tmp"); + moveMetaFileAway(4, "meta_MigrationTaskIT_4.tmp"); + createTestMetafile(); + createTestMetaAnchorfile(); + } + + /** + * cleans database. + */ + @AfterClass + public static void cleanDatabase() throws Exception { + if (!SystemUtils.IS_OS_WINDOWS) { + ExecutionPermission.setNoExecutePermission(script); + } + + MockDatabase.stopNode(); + MockDatabase.cleanDatabase(); + cleanUp(); + } + + /** + * Tests migration of hierarchies. + */ + @Test + public void testHierarchyMigration() throws DAOException, ProcessGenerationException, CommandException, + DataException, IOException, SAXException, ParserConfigurationException { + HierarchyMigrationTask hierarchyMigrationTask = new HierarchyMigrationTask(Collections.singletonList(project)); + hierarchyMigrationTask.migrate(ServiceManager.getProcessService().getById(2)); + Assert.assertTrue("Tasks should have been removed", + ServiceManager.getProcessService().getById(4).getTasks().isEmpty()); + Assert.assertEquals("JahrdeDeG_404810993", ServiceManager.getProcessService().getById(4).getTitle()); + Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder() + .parse(new File("src/test/resources/metadata/4/meta.xml")); + String documentId = document.getElementsByTagName("mets:metsDocumentID").item(0).getTextContent(); + Assert.assertEquals("DocumentId not set", "4", documentId); + } + + private static void createTestMetaAnchorfile() throws Exception { + List lines = Collections.singletonList("\n" + + "\n" + + " \n" + + " \n" + + " Kitodo - kitodo-ugh-2.1.3-kitodo-ugh-2.1.1-11-g4b06eaa - 30−July−2019\n" + + " Kitodo\n" + + " Converted by Kitodo - Data Editor - 3.2.1-SNAPSHOT (2021-03-29T07:53:32Z)\n" + + " \n" + " \n" + + " \n" + " \n" + + " \n" + " \n" + + " \n" + + " 404810993\n" + + " JahrdeDeG\n" + + " Freiberg\n" + + " LDP Sachsen\n" + + " MultiVolumeWork\n" + + " \n" + " \n" + " \n" + + " \n" + " \n" + + " \n" + + " \n" + + " \n" + " \n" + + " \n" + " \n" + "\n"); + File processHome = new File(ConfigCore.getKitodoDataDirectory(), "2"); + FileUtils.writeLines(new File(processHome, "meta_anchor.xml"), "UTF-8", lines); + } + + private static void createTestMetafile() throws Exception { + List lines = Collections.singletonList("\n" + + "\n" + + " \n" + + " \n" + + " Kitodo - kitodo-ugh-2.1.3-kitodo-ugh-2.1.1-11-g4b06eaa - 30−July−2019\n" + + " Kitodo\n" + + " Converted by Kitodo - Data Editor - 3.2.1-SNAPSHOT (2021-03-29T07:53:32Z)\n" + + " \n" + " \n" + + " \n" + " \n" + + " \n" + " \n" + + " \n" + + " 404810993-19130000\n" + + " 404810993\n" + + " 25.1913/14\n" + + " 19130000\n" + + " Antiqua\n" + + " ger\n" + + " PeriodicalVolume\n" + + " \n" + " \n" + " \n" + + " \n" + " \n" + " \n" + + " \n" + + " \n" + + " \n" + " \n" + " \n" + + " \n" + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + " \n" + " \n" + "\n"); + File processHome = new File(ConfigCore.getKitodoDataDirectory(), "2"); + FileUtils.writeLines(new File(processHome, "meta.xml"), "UTF-8", lines); + } + + private static void cleanUp() { + File processHome = new File(ConfigCore.getKitodoDataDirectory(), "2"); + File originalMeta = new File(processHome, "meta_MigrationTaskIT.tmp"); + if (originalMeta.exists()) { + File metaFile = new File(processHome, "meta.xml"); + metaFile.delete(); + originalMeta.renameTo(metaFile); + } + + processHome = new File(ConfigCore.getKitodoDataDirectory(), "4"); + originalMeta = new File(processHome, "meta_MigrationTaskIT_4.tmp"); + if (originalMeta.exists()) { + File metaFile = new File(processHome, "meta.xml"); + metaFile.delete(); + originalMeta.renameTo(metaFile); + } + new File("src/test/resources/metadata/2/meta_anchor.migrated").delete(); + } + + private static void moveMetaFileAway(int recordNumber, String tempFileName) throws Exception { + File processHome = new File(ConfigCore.getKitodoDataDirectory(), Integer.toString(recordNumber)); + new File(processHome, "meta.xml").renameTo(new File(processHome, tempFileName)); + } + +}