Skip to content

Commit

Permalink
Extracts the renaming of processes into the process service
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-ronge committed Jun 18, 2024
1 parent 53d5def commit 8c283d8
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 92 deletions.
104 changes: 12 additions & 92 deletions Kitodo/src/main/java/org/kitodo/production/forms/ProcessForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public class ProcessForm extends TemplateBaseForm {
private Property property;
private final FilterMenu filterMenu = new FilterMenu(this);
private final transient FileService fileService = ServiceManager.getFileService();
private final transient ProcessService processService = ServiceManager.getProcessService();
private final transient WorkflowControllerService workflowControllerService = new WorkflowControllerService();
private final String processEditPath = MessageFormat.format(REDIRECT_PATH, "processEdit");

Expand Down Expand Up @@ -185,7 +186,7 @@ public String save() {
}

try {
ServiceManager.getProcessService().save(this.process, true);
processService.save(this.process, true);
return processesPage;
} catch (DataException e) {
Helper.setErrorMessage(ERROR_SAVING, new Object[] {ObjectType.PROCESS.getTranslationSingular() },
Expand All @@ -204,7 +205,7 @@ public String save() {
*/
public String createProcessAsChild(ProcessDTO processDTO) {
try {
Process process = ServiceManager.getProcessService().getById(processDTO.getId());
Process process = processService.getById(processDTO.getId());
if (Objects.nonNull(process.getTemplate()) && Objects.nonNull(process.getProject())) {
return CREATE_PROCESS_PATH + "&templateId=" + process.getTemplate().getId() + "&projectId="
+ process.getProject().getId() + "&parentId=" + process.getId();
Expand Down Expand Up @@ -271,7 +272,7 @@ private boolean renameAfterProcessTitleChanged() {
return false;
} else {
try {
renameProcess(this.process, this.newProcessTitle);
processService.renameProcess(this.process, this.newProcessTitle);
} catch (IOException | RuntimeException e) {
Helper.setErrorMessage("errorRenaming", new Object[] {Helper.getTranslation("directory") }, logger, e);
}
Expand All @@ -282,77 +283,6 @@ private boolean renameAfterProcessTitleChanged() {
return true;
}

/**
* Renames a process. In particular, the variable {@code (processtitle)}
* must be adjusted. This also applies to the directory structure.
*
* @param process
* process to be renamed
* @param newProcessTitle
* new process name
* @throws IOException
* if an error occurs while accessing the file system
*/
// FIXME: Functionality should not be implemented in form classes
public void renameProcess(Process process, String newProcessTitle) throws IOException {
renamePropertiesValuesForProcessTitle(process.getProperties(), process.getTitle(), newProcessTitle);
renamePropertiesValuesForProcessTitle(process.getTemplates(), process.getTitle(), newProcessTitle);
removePropertiesWithEmptyTitle(process.getWorkpieces(), process);

renameImageDirectories(process, newProcessTitle);
renameOcrDirectories(process, newProcessTitle);
renameDefinedDirectories(process, newProcessTitle);

process.setTitle(this.newProcessTitle);
}

private void renamePropertiesValuesForProcessTitle(List<Property> properties, String processTitle, String newProcessTitle) {
for (Property property : properties) {
if (Objects.nonNull(property.getValue()) && property.getValue().contains(processTitle)) {
property.setValue(property.getValue().replaceAll(processTitle, newProcessTitle));
}
}
}

private void renameImageDirectories(Process process, String newProcessTitle) throws IOException {
URI imageDirectory = fileService.getImagesDirectory(process);
renameDirectories(imageDirectory, process, newProcessTitle);
}

private void renameOcrDirectories(Process process, String newProcessTitle) throws IOException {
URI ocrDirectory = fileService.getOcrDirectory(process);
renameDirectories(ocrDirectory, process, newProcessTitle);
}

private void renameDirectories(URI directory, Process process, String newProcessTitle) throws IOException {
if (fileService.isDirectory(directory)) {
List<URI> subDirs = fileService.getSubUris(directory);
for (URI imageDir : subDirs) {
if (fileService.isDirectory(imageDir)) {
fileService.renameFile(imageDir, imageDir.toString().replace(process.getTitle(), newProcessTitle));
}
}
}
}

private void renameDefinedDirectories(Process process, String newProcessTitle) {
String[] processDirs = ConfigCore.getStringArrayParameter(ParameterCore.PROCESS_DIRS);
for (String processDir : processDirs) {
// TODO: check it out
URI processDirAbsolute = ServiceManager.getProcessService().getProcessDataDirectory(process)
.resolve(processDir.replace("(processtitle)", process.getTitle()));

File dir = new File(processDirAbsolute);
boolean renamed;
if (dir.isDirectory()) {
renamed = dir.renameTo(new File(dir.getAbsolutePath().replace(process.getTitle(), newProcessTitle)));
if (!renamed) {
Helper.setErrorMessage("errorRenaming", new Object[] {dir.getName() });
}
}
}
}

/**
* Remove template properties.
*/
Expand Down Expand Up @@ -523,7 +453,7 @@ public void setTaskStatusDown() {
private void refreshParent() {
try {
if (Objects.nonNull(process.getParent())) {
this.process.setParent(ServiceManager.getProcessService().getById(process.getParent().getId()));
this.process.setParent(processService.getById(process.getParent().getId()));
}
} catch (DAOException e) {
Helper.setErrorMessage(ERROR_LOADING_ONE,
Expand Down Expand Up @@ -754,7 +684,7 @@ public void deleteProperty() {
this.process.getProperties().remove(this.property);

List<Property> propertiesToFilterTitle = this.process.getProperties();
removePropertiesWithEmptyTitle(propertiesToFilterTitle, this.process);
processService.removePropertiesWithEmptyTitle(propertiesToFilterTitle, this.process);
loadProcessProperties();
}

Expand All @@ -768,16 +698,6 @@ public void duplicateProperty() {
loadProcessProperties();
}

// TODO: is it really a case that title is empty?
private void removePropertiesWithEmptyTitle(List<Property> properties, Process process) {
for (Property processProperty : properties) {
if (Objects.isNull(processProperty.getTitle()) || processProperty.getTitle().isEmpty()) {
processProperty.getProcesses().clear();
process.getProperties().remove(processProperty);
}
}
}

/**
* Get dockets for select list.
*
Expand Down Expand Up @@ -877,7 +797,7 @@ public void load(int id) {
}
try {
if (id != 0) {
setProcess(ServiceManager.getProcessService().getById(id));
setProcess(processService.getById(id));
}
setSaveDisabled(true);
} catch (DAOException e) {
Expand Down Expand Up @@ -996,7 +916,7 @@ public void setFilter(String filter) {
* @return String containing titles of current tasks of given process
*/
public String getCurrentTaskTitles(ProcessDTO processDTO) {
return ServiceManager.getProcessService().createProgressTooltip(processDTO);
return processService.createProgressTooltip(processDTO);
}

/**
Expand All @@ -1006,7 +926,7 @@ public String getCurrentTaskTitles(ProcessDTO processDTO) {
*/
public List<Process> getAllParentProcesses(int processId) {
try {
return ProcessService.getAllParentProcesses(ServiceManager.getProcessService().getById(processId));
return ProcessService.getAllParentProcesses(processService.getById(processId));
} catch (DAOException e) {
Helper.setErrorMessage(ERROR_LOADING_ONE, new Object[] {ObjectType.PROCESS.getTranslationSingular(), processId }, logger, e);
return new ArrayList<>();
Expand All @@ -1022,7 +942,7 @@ public List<Process> getAllParentProcesses(int processId) {
*/
public int getNumberOfChildProcesses(int processId) {
try {
return ServiceManager.getProcessService().getNumberOfChildren(processId);
return processService.getNumberOfChildren(processId);
} catch (DAOException e) {
Helper.setErrorMessage(ERROR_LOADING_ONE, new Object[] {ObjectType.PROCESS.getTranslationSingular(), processId }, logger, e);
return 0;
Expand Down Expand Up @@ -1052,7 +972,7 @@ public String convertProcessingDate(Date date) {
* @return List of filtered tasks as DTO objects
*/
public List<TaskDTO> getCurrentTasksForUser(ProcessDTO processDTO) {
return ServiceManager.getProcessService().getCurrentTasksForUser(processDTO, ServiceManager.getUserService().getCurrentUser());
return processService.getCurrentTasksForUser(processDTO, ServiceManager.getUserService().getCurrentUser());
}

/**
Expand All @@ -1061,7 +981,7 @@ public List<TaskDTO> getCurrentTasksForUser(ProcessDTO processDTO) {
*/
public String getAmount() {
try {
return ServiceManager.getProcessService().count(ServiceManager.getProcessService()
return processService.count(processService
.getQueryForFilter(isShowClosedProcesses(), isShowInactiveProjects(), filter)).toString();
} catch (DataException e) {
Helper.setErrorMessage(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2566,6 +2566,87 @@ public static void createXML(Process process, User user) throws IOException {
xmlExport.exportXmlLog(getDocketData(process), destination);
}

/**
* Renames a process. In particular, the variable {@code (processtitle)}
* must be adjusted. This also applies to the directory structure.
*
* @param process
* process to be renamed
* @param newProcessTitle
* new process name
* @throws IOException
* if an error occurs while accessing the file system
*/
public void renameProcess(Process process, String newProcessTitle) throws IOException {
renamePropertiesValuesForProcessTitle(process.getProperties(), process.getTitle(), newProcessTitle);
renamePropertiesValuesForProcessTitle(process.getTemplates(), process.getTitle(), newProcessTitle);
removePropertiesWithEmptyTitle(process.getWorkpieces(), process);

renameImageDirectories(process, newProcessTitle);
renameOcrDirectories(process, newProcessTitle);
renameDefinedDirectories(process, newProcessTitle);

process.setTitle(newProcessTitle);
}

private void renamePropertiesValuesForProcessTitle(List<Property> properties, String processTitle, String newProcessTitle) {
for (Property property : properties) {
if (Objects.nonNull(property.getValue()) && property.getValue().contains(processTitle)) {
property.setValue(property.getValue().replaceAll(processTitle, newProcessTitle));
}
}
}


// TODO: is it really a case that title is empty?
public void removePropertiesWithEmptyTitle(List<Property> properties, Process process) {
for (Property processProperty : properties) {
if (Objects.isNull(processProperty.getTitle()) || processProperty.getTitle().isEmpty()) {
processProperty.getProcesses().clear();
process.getProperties().remove(processProperty);
}
}
}

private void renameImageDirectories(Process process, String newProcessTitle) throws IOException {
URI imageDirectory = fileService.getImagesDirectory(process);
renameDirectories(imageDirectory, process, newProcessTitle);
}

private void renameOcrDirectories(Process process, String newProcessTitle) throws IOException {
URI ocrDirectory = fileService.getOcrDirectory(process);
renameDirectories(ocrDirectory, process, newProcessTitle);
}

private void renameDirectories(URI directory, Process process, String newProcessTitle) throws IOException {
if (fileService.isDirectory(directory)) {
List<URI> subDirs = fileService.getSubUris(directory);
for (URI imageDir : subDirs) {
if (fileService.isDirectory(imageDir)) {
fileService.renameFile(imageDir, imageDir.toString().replace(process.getTitle(), newProcessTitle));
}
}
}
}

private void renameDefinedDirectories(Process process, String newProcessTitle) {
String[] processDirs = ConfigCore.getStringArrayParameter(ParameterCore.PROCESS_DIRS);
for (String processDir : processDirs) {
// TODO: check it out
URI processDirAbsolute = ServiceManager.getProcessService().getProcessDataDirectory(process)
.resolve(processDir.replace("(processtitle)", process.getTitle()));

File dir = new File(processDirAbsolute);
boolean renamed;
if (dir.isDirectory()) {
renamed = dir.renameTo(new File(dir.getAbsolutePath().replace(process.getTitle(), newProcessTitle)));
if (!renamed) {
Helper.setErrorMessage("errorRenaming", new Object[] {dir.getName() });
}
}
}
}

/**
* Create and return PieChartModel for given process values.
*
Expand Down

0 comments on commit 8c283d8

Please sign in to comment.