Skip to content

Commit

Permalink
Merge pull request #2939 from matthias-ronge/KIT-1481
Browse files Browse the repository at this point in the history
Show and control the migration of metadata in the task manager
  • Loading branch information
Kathrin-Huber authored Oct 23, 2019
2 parents 58be5a8 + 3666a47 commit 3757907
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 22 deletions.
60 changes: 49 additions & 11 deletions Kitodo/src/main/java/org/kitodo/production/forms/MigrationForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.kitodo.data.exceptions.DataException;
import org.kitodo.production.enums.ObjectType;
import org.kitodo.production.helper.Helper;
import org.kitodo.production.helper.tasks.MigrationTask;
import org.kitodo.production.helper.tasks.TaskManager;
import org.kitodo.production.migration.TasksToWorkflowConverter;
import org.kitodo.production.services.ServiceManager;
import org.kitodo.production.services.migration.MigrationService;
Expand All @@ -57,6 +59,8 @@ public class MigrationForm extends BaseForm {
private Map<Template, List<Process>> templatesToCreate = new HashMap<>();
private Map<Template, Template> matchingTemplates = new HashMap<>();
private MigrationService migrationService = ServiceManager.getMigrationService();
private boolean metadataShown;
private boolean workflowShown;

/**
* Migrates the meta.xml for all processes in the database (if it's in the
Expand All @@ -65,10 +69,12 @@ public class MigrationForm extends BaseForm {
*/
public void migrateMetadata() {
try {
migrationService.migrateMetadata();
allProjects = ServiceManager.getProjectService().getAll();
projectListShown = true;
metadataShown = true;
workflowShown = false;
} catch (DAOException e) {
Helper.setErrorMessage(ERROR_DATABASE_READING, new Object[] {ObjectType.PROCESS.getTranslationSingular() },
logger, e);
Helper.setErrorMessage("Error during database access", e.getLocalizedMessage(), logger, e);
}
}

Expand All @@ -79,8 +85,10 @@ public void showPossibleProjects() {
try {
allProjects = ServiceManager.getProjectService().getAll();
projectListShown = true;
workflowShown = true;
metadataShown = false;
} catch (DAOException e) {
Helper.setErrorMessage("Error during database access");
Helper.setErrorMessage("Error during database access", e.getLocalizedMessage(), logger, e);
}
}

Expand All @@ -94,13 +102,24 @@ public void showAggregatedProcesses() {
processList.addAll(project.getProcesses());
}
for (Process process : processList) {
if(Objects.isNull(process.getTemplate())) {
if (Objects.isNull(process.getTemplate())) {
addToAggregatedProcesses(aggregatedProcesses, process);
}
}
processListShown = true;
}

/**
* Method for migrating the metadata. This is done when the user clicks the
* button to migrate metadata under the projects selection.
*/
public void convertMetadata() {
for (Project project : selectedProjects) {
TaskManager.addTask(new MigrationTask(project));
}
projectListShown = false;
}

private void addToAggregatedProcesses(Map<String, List<Process>> aggregatedProcesses, Process process) {
List<Task> processTasks = process.getTasks();
processTasks.sort(Comparator.comparingInt(Task::getOrdering));
Expand Down Expand Up @@ -140,6 +159,17 @@ public void setSelectedProjects(List<Project> selectedProjects) {
this.selectedProjects = selectedProjects;
}

/**
* Returns whether the switch for starting the metadata migration should be
* displayed.
*
* @return whether the switch for starting the metadata migration should be
* displayed
*/
public boolean isMetadataShown() {
return metadataShown;
}

/**
* Get projectListShown.
*
Expand All @@ -149,6 +179,15 @@ public boolean isProjectListShown() {
return projectListShown;
}

/**
* Returns whether the switch for creating workflows should be displayed.
*
* @return whether the switch for creating workflows should be displayed
*/
public boolean isWorkflowShown() {
return workflowShown;
}

/**
* Get selectedProjects.
*
Expand Down Expand Up @@ -187,7 +226,7 @@ public int getNumberOfProcesses(String tasks) {

/**
* Uses the aggregated processes to create a new Workflow.
*
*
* @param tasks
* the list of tasks found in the projects
* @return a navigation path
Expand Down Expand Up @@ -230,7 +269,7 @@ public void useExistingWorkflow() {

/**
* Creates a new Workflow from the aggregated processes.
*
*
* @return a navigation path.
*/
public String createNewWorkflow() {
Expand Down Expand Up @@ -265,7 +304,7 @@ public String createNewWorkflow() {
/**
* When the navigation to the migration form is coming from a workflow
* creation the URL contains a WorkflowId.
*
*
* @param workflowId
* the id of the created Workflow
*/
Expand Down Expand Up @@ -311,7 +350,7 @@ public Template getMatchingTemplate(Template template) {

/**
* Uses the existing template to add processes to.
*
*
* @param template
* The template to which's matching template the processes should
* be added
Expand All @@ -331,7 +370,7 @@ public void useExistingTemplate(Template template, Template existingTemplate) {

/**
* Creates a new template.
*
*
* @param template
* The template to create.
*/
Expand All @@ -351,5 +390,4 @@ public void createNewTemplate(Template template) {
}
templatesToCreate.remove(template);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* (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.production.helper.tasks;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.kitodo.data.database.beans.Process;
import org.kitodo.data.database.beans.Project;
import org.kitodo.data.database.exceptions.DAOException;
import org.kitodo.production.helper.Helper;
import org.kitodo.production.services.ServiceManager;
import org.kitodo.production.services.migration.MigrationService;

/**
* Migrates metadata in a separate thread.
*/
public class MigrationTask extends EmptyTask {
private static final Logger logger = LogManager.getLogger(MigrationTask.class);

/**
* Service who has to migrate the processes.
*/
final private MigrationService migrationService;

/**
* List of processes to be migrated.
*/
final private List<Process> processes;

/**
* Migration progress. Points to the index of the next process in case the
* thread is interrupted and restarted. Also used for the progress bar.
*/
private int progress = 0;

/**
* Creates a new migration task.
*
* @param project
* project whose processes are to be migrated
*/
public MigrationTask(Project project) {
super(project.getTitle());
this.migrationService = ServiceManager.getMigrationService();
this.processes = project.getProcesses();
}

/**
* Clone constructor. Provides the ability to restart an export that was
* previously interrupted by the user.
*
* @param sourceMigrationTask
* terminated thread
*/
private MigrationTask(MigrationTask sourceMigrationTask) {
super(sourceMigrationTask);
this.progress = sourceMigrationTask.progress;
this.migrationService = sourceMigrationTask.migrationService;
this.processes = sourceMigrationTask.processes;
}

/**
* This function displays the name in the task manager.
*/
@Override
public String getDisplayName() {
return Helper.getTranslation("MigrationTask");
}

/**
* The method to work the thread.
*/
@Override
public void run() {
String processTitle = null;
try {
for (int index = progress; index < processes.size(); index++) {
final long begin = System.nanoTime();
Process process = processes.get(index);
processTitle = process.getTitle();
setWorkDetail(processTitle);
migrationService.migrateMetadata(process);
if (logger.isTraceEnabled()) {
logger.trace("Migrating {} took {} ms", processTitle,
TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - begin));
}
progress = index + 1;
setProgress(100 * progress / processes.size());
if (isInterrupted()) {
return;
}
}
setProgress(100);
} catch (DAOException exception) {
Helper.setErrorMessage(exception.getLocalizedMessage(), processTitle, logger, exception);
super.setException(exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,21 @@ public static MigrationService getInstance() {
}

/**
* Migrates the meta.xml to the new kitodo format.
* @throws DAOException when Database access fails
* Migrates the meta.xml to the new Kitodo format.
*
* @param process
* process whose meta.xml is to be migrated
* @throws DAOException
* when Database access fails
*/
public void migrateMetadata() throws DAOException {
List<Process> processes = ServiceManager.getProcessService().getAll();
public void migrateMetadata(Process process) throws DAOException {
FileService fileService = ServiceManager.getFileService();
URI metadataFilePath;
for (Process process : processes) {
try {
metadataFilePath = fileService.getMetadataFilePath(process, true, true);
ServiceManager.getDataEditorService().readData(metadataFilePath);
} catch (IOException e) {
Helper.setErrorMessage(e.getLocalizedMessage(), logger, e);
}
try {
metadataFilePath = fileService.getMetadataFilePath(process, true, true);
ServiceManager.getDataEditorService().readData(metadataFilePath);
} catch (IOException e) {
Helper.setErrorMessage(e.getLocalizedMessage(), logger, e);
}
}

Expand Down
2 changes: 2 additions & 0 deletions Kitodo/src/main/resources/messages/messages_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,10 @@ metsRightsOwnerLogo=METS Rechteinhaber Logo
metsRightsOwnerMail=METS Rechteinhaber Kontakt
metsRightsOwnerSite=METS Rechteinhaber URL
metsfilegroups=Unterordner und METS Dateigruppen
migrateMetadata=Metadaten konvertieren
migrateProjects=Migrieren
migrateWorkflow=Workflows erstellen
MigrationTask=Migriere Metadaten des Projekts
missingData=Unvollst\u00E4ndige Daten
missingPlugin=Bitte w\u00E4hlen Sie ein Plugin aus.
modeler=BPMN Modeler
Expand Down
2 changes: 2 additions & 0 deletions Kitodo/src/main/resources/messages/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,10 @@ metsRightsOwnerLogo=METS rights owner logo
metsRightsOwnerMail=METS rights owner contact
metsRightsOwnerSite=METS rights owner URL
metsfilegroups=Sub-folders and METS file groups
migrateMetadata=Migrate metadata
migrateProjects=Migrate projects
migrateWorkflow=Migrate workflows
MigrationTask=Migrating metadata of project
missingData=incomplete data
missingPlugin=Please select a Plugin.
modeler=BPMN Modeler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<h:form id="migrationForm">
<p:commandButton value="migrate"
id="migrateMetadata"
update="systemTabView:migrationForm"
action="#{MigrationForm.migrateMetadata()}"
styleClass="primary"/>
<p:commandButton value="#{msgs.migrateWorkflow}"
Expand All @@ -34,8 +35,15 @@
<f:selectItems value="#{MigrationForm.allProjects}" var="project" itemLabel="#{project.title}"
itemValue="#{project}"/>
</p:selectManyCheckbox>
<p:commandButton value="#{msgs.migrateMetadata}"
id="migrate"
rendered="#{MigrationForm.metadataShown}"
update="systemTabView:migrationForm"
styleClass="primary"
action="#{MigrationForm.convertMetadata()}"/>
<p:commandButton value="#{msgs.migrateProjects}"
id="migrateProject"
rendered="#{MigrationForm.workflowShown}"
update="systemTabView:migrationForm"
styleClass="primary"
action="#{MigrationForm.showAggregatedProcesses()}"/>
Expand Down

0 comments on commit 3757907

Please sign in to comment.