Skip to content

Commit 027005c

Browse files
author
Dominick Leppich
committed
task: backup existing ALTO results instead of deleting them
1 parent f4deca5 commit 027005c

File tree

3 files changed

+28
-37
lines changed

3 files changed

+28
-37
lines changed

module-base/src/main/java/de/intranda/goobi/plugins/TranscriptionImage.java

-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ public class TranscriptionImage {
1313
private Image image;
1414
private String ocrText;
1515
private Path ocrPath;
16-
private boolean hasAlto;
17-
private Path altoPath;
1816

1917
public void setOcrText(String text) {
2018
this.ocrText = text;

module-base/src/main/java/de/intranda/goobi/plugins/TranscriptionStepPlugin.java

+25-18
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.apache.commons.configuration.SubnodeConfiguration;
3535
import org.apache.commons.io.FilenameUtils;
3636
import org.goobi.beans.Step;
37+
import org.goobi.io.BackupFileManager;
3738
import org.goobi.managedbeans.StepBean;
3839
import org.goobi.production.enums.PluginGuiType;
3940
import org.goobi.production.enums.PluginReturnValue;
@@ -95,9 +96,6 @@ public class TranscriptionStepPlugin implements IStepPluginVersion2 {
9596
private List<String> deletionCommand = null;
9697
@Getter
9798
private boolean altoFolderFound;
98-
@Getter
99-
@Setter
100-
private boolean ignoreAltoFolder;
10199

102100
@Override
103101
public void initialize(Step step, String returnPath) {
@@ -145,18 +143,15 @@ public void initImageList(String configuredImageFolder) throws SwapException, DA
145143
images.clear();
146144
Path path = Paths.get(imageFolder);
147145
String ocrTxtDir = step.getProzess().getOcrTxtDirectory();
148-
String ocrAltoDir = step.getProzess().getOcrAltoDirectory();
149146
if (StorageProvider.getInstance().isFileExists(path)) {
150147
List<Path> imageNameList = storageProvider.listFiles(imageFolder, NIOFileUtils.imageOrObjectNameFilter);
151148
int order = 1;
152149
for (Path imagePath : imageNameList) {
153150
Image image = new Image(step.getProzess(), configuredImageFolder, imagePath.getFileName().toString(), order, 800);
154151
String basename = FilenameUtils.removeExtension(imagePath.getFileName().toString());
155152
Path ocrFile = Paths.get(ocrTxtDir, basename + ".txt");
156-
Path altoPath = Paths.get(ocrAltoDir, basename + ".xml");
157-
boolean hasAlto = Files.exists(altoPath);
158153
String currentOcr = readOcrFile(ocrFile);
159-
images.add(new TranscriptionImage(imagePath.getFileName().toString(), image, currentOcr, ocrFile, hasAlto, altoPath));
154+
images.add(new TranscriptionImage(imagePath.getFileName().toString(), image, currentOcr, ocrFile));
160155
order++;
161156
}
162157
}
@@ -166,18 +161,30 @@ public void initImageList(String configuredImageFolder) throws SwapException, DA
166161
this.setImageToIndex();
167162
}
168163

169-
public void deleteSingleAltoResult() throws IOException {
170-
Files.deleteIfExists(image.getAltoPath());
171-
image.setHasAlto(false);
172-
}
173-
174-
public void deleteAltoFolder() throws SwapException, DAOException, IOException, InterruptedException {
175-
String altoDir = step.getProzess().getOcrAltoDirectory();
176-
StorageProvider.getInstance().deleteDir(Paths.get(altoDir));
177-
for (TranscriptionImage image : this.images) {
178-
image.setHasAlto(false);
164+
public void backupAlto() {
165+
if (!this.isAltoFolderFound()) {
166+
return;
167+
}
168+
try {
169+
StorageProviderInterface sp = StorageProvider.getInstance();
170+
Path ocrFolder = Path.of(step.getProzess().getOcrDirectory());
171+
if (sp.isFileExists(ocrFolder)) {
172+
Path backupPath = Paths.get(ocrFolder.getParent().toString(), BackupFileManager.generateBackupName(ocrFolder.getFileName().toString()));
173+
sp.copyDirectory(ocrFolder, backupPath);
174+
String directoryToKeep = step.getProzess().getOcrTxtDirectory();
175+
if (directoryToKeep.endsWith("/")) {
176+
directoryToKeep = directoryToKeep.substring(0, directoryToKeep.length() - 1);
177+
}
178+
for (Path dir : sp.listFiles(ocrFolder.toString())) {
179+
if (!dir.toString().equals(directoryToKeep)) {
180+
sp.deleteDir(dir);
181+
}
182+
}
183+
}
184+
this.altoFolderFound = false;
185+
} catch (IOException | SwapException e) {
186+
log.error("Error backing up Alto results", e);
179187
}
180-
this.altoFolderFound = false;
181188
}
182189

183190
public String saveOcrAndExit() throws IOException {

module-gui/src/main/webapp/resources/uii/plugin_step_transcription.xhtml

+3-17
Original file line numberDiff line numberDiff line change
@@ -139,23 +139,19 @@
139139
</div>
140140

141141
<div class="box-content">
142-
<ui:fragment rendered="#{AktuelleSchritteForm.myPlugin.altoFolderFound and not AktuelleSchritteForm.myPlugin.ignoreAltoFolder}">
142+
<ui:fragment rendered="#{AktuelleSchritteForm.myPlugin.altoFolderFound}">
143143
<div class="row">
144144
<div class="col-md-6 col-md-offset-3">
145145
<div class="alert alert-warning">
146146
<h:outputText escape="false" value="#{msgs.plugin_transcription_warning_alto}" />
147147
</div>
148148
<p>#{msgs.plugin_transcription_warning_alto_text1}</p>
149149
<p>#{msgs.plugin_transcription_warning_alto_text2}</p>
150-
<h:commandLink styleClass="btn">
151-
#{msgs.ignore}
152-
<f:setPropertyActionListener value="true" target="#{AktuelleSchritteForm.myPlugin.ignoreAltoFolder}"></f:setPropertyActionListener>
153-
</h:commandLink>
154-
<h:commandLink styleClass="btn btn-danger pull-right" action="#{AktuelleSchritteForm.myPlugin.deleteAltoFolder}">#{msgs.plugin_transcription_deleteAltoFolder}</h:commandLink>
150+
<h:commandLink styleClass="btn btn-success pull-right" action="#{AktuelleSchritteForm.myPlugin.backupAlto}">#{msgs.plugin_transcription_overwriteExistingOCR}</h:commandLink>
155151
</div>
156152
</div>
157153
</ui:fragment>
158-
<ui:fragment rendered="#{AktuelleSchritteForm.myPlugin.ignoreAltoFolder or not AktuelleSchritteForm.myPlugin.altoFolderFound}">
154+
<ui:fragment rendered="#{not AktuelleSchritteForm.myPlugin.altoFolderFound}">
159155
<div class="row transcription-row">
160156

161157
<f:subview id="first_image">
@@ -172,16 +168,6 @@
172168
</div>
173169
</div>
174170
<div class="col-md-6 transcription-editor-col">
175-
<h:panelGroup styleClass="blocker" rendered="#{AktuelleSchritteForm.myPlugin.image.hasAlto}">
176-
<div class="alert alert-warning">
177-
<h:outputText escape="false" value="#{msgs.plugin_transcription_warning_single_alto}" />
178-
</div>
179-
<div>
180-
<h:commandLink styleClass="btn btn-danger pull-right" action="#{AktuelleSchritteForm.myPlugin.deleteSingleAltoResult}">
181-
#{msgs.plugin_transcription_deleteSingleAltoResult}
182-
</h:commandLink>
183-
</div>
184-
</h:panelGroup>
185171
<textarea class="ocr-text" id="textarea_0">
186172
#{AktuelleSchritteForm.myPlugin.image.ocrText}
187173
</textarea>

0 commit comments

Comments
 (0)