Skip to content
Snippets Groups Projects
Commit 09558252 authored by Andreas Romeyke's avatar Andreas Romeyke
Browse files

- refactoring, reordered pid-processing

- refactoring, replaced if-elsif-branch with regular expression matcher
- refactoring, added some comments
parent 826febd8
Branches
Tags
No related merge requests found
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
</fr:x_form> </fr:x_form>
</pl:initParameters> </pl:initParameters>
<pl:description>repairs broken file original paths</pl:description> <pl:description>repairs broken file original paths</pl:description>
<pl:version>1.08</pl:version> <pl:version>1.09</pl:version>
<pl:materialType>DIGITAL</pl:materialType> <pl:materialType>DIGITAL</pl:materialType>
<pl:module>Repository</pl:module> <pl:module>Repository</pl:module>
<pl:generalType>TASK</pl:generalType> <pl:generalType>TASK</pl:generalType>
......
...@@ -11,58 +11,62 @@ import java.time.LocalDateTime; ...@@ -11,58 +11,62 @@ import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DnxMoveFileOriginalPath implements RepositoryTaskPlugin { public class DnxMoveFileOriginalPath implements RepositoryTaskPlugin {
private ExLogger log = ExLogger.getExLogger(DnxMoveFileOriginalPath.class); private ExLogger log = ExLogger.getExLogger(DnxMoveFileOriginalPath.class);
private boolean success = true;
public DnxMoveFileOriginalPath() { public DnxMoveFileOriginalPath() {
super(); super();
} }
public TaskResults execute(IEEditor ieEditor, Map<String, String> initParams, TaskResults taskResults) { public TaskResults execute(IEEditor ieEditor, Map<String, String> initParams, TaskResults taskResults) {
log.info("Executing DnxMoveFileOriginalPath for " + ieEditor.getIEPid()); log.info("Executing DnxMoveFileOriginalPath for " + ieEditor.getIEPid());
init(initParams); init(initParams);
// get all rep pids boolean IEsuccess = true;
List<String> repPids;
try {
repPids = ieEditor.getReps();
} catch (DigitoolException e) {
success = false;
e.printStackTrace();
taskResults.addResult(ieEditor.getIEPid(), null, false, "no RepPids found!");
return taskResults;
}
int countFiles=0; int countFiles=0;
int countSuccess=0; int countSuccess=0;
// get all file pids // get all rep pids
for (String repPid: repPids) { try {
List<String> filePids; List<String> repPids = ieEditor.getReps();
try { for (String repPid: repPids) {
filePids = ieEditor.getFilesForRep( repPid ); // get all file pids
} catch (DigitoolException e) {
success = false;
e.printStackTrace();
taskResults.addResult(ieEditor.getIEPid(), null, false, "no FilePids found for RepPid=" + repPid + " !");
return taskResults;
}
for (String filePid: filePids) {
try { try {
countFiles++; List<String> filePids = ieEditor.getFilesForRep(repPid);
if (replaceFileOriginalPath(ieEditor, repPid, filePid)) { countSuccess++; } for (String filePid: filePids) {
taskResults.addResult(ieEditor.getIEPid(), null, true, "filepid=" + filePid + " processed"); try {
countFiles++;
if (replaceFileOriginalPath(ieEditor, repPid, filePid)) { countSuccess++; }
taskResults.addResult(ieEditor.getIEPid(), null, true, "filepid=" + filePid + " processed");
} catch (DigitoolException e) {
IEsuccess = false;
String err = "unknown Digitool exception: " + e;
log.error( err );
e.printStackTrace();
taskResults.addResult(ieEditor.getIEPid(), null, false, err);
}
}
} catch (DigitoolException e) { } catch (DigitoolException e) {
success = false; IEsuccess = false;
e.printStackTrace(); String err = "no FilePids found for RepPid=" + repPid + ", Digitool exception: " + e;
taskResults.addResult(ieEditor.getIEPid(), null, false, "unknown Digitool exception: " + e); log.warn( err );
return taskResults; taskResults.addResult(ieEditor.getIEPid(), null, false, err);
} }
} }
} } catch (DigitoolException e) {
IEsuccess = false;
String err = "no RepPids found, Digitool exception: " + e;
log.warn( err );
taskResults.addResult(ieEditor.getIEPid(), null, false, err);
}
// now add an event // now add an event
try { try {
DnxDocumentHelper ieDnxH = ieEditor.getDnxHelperForIE(); DnxDocumentHelper ieDnxH = ieEditor.getDnxHelperForIE();
List<DnxDocumentHelper.Event> eventList = ieDnxH.getEvents(); List<DnxDocumentHelper.Event> eventList = ieDnxH.getEvents();
DnxDocumentHelper.Event event = ieDnxH.new Event(); DnxDocumentHelper.Event event = ieDnxH.new Event();
event.setEventIdentifierType("Rosetta-SupportCase"); event.setEventIdentifierType("Rosetta-SupportCase");
...@@ -71,7 +75,7 @@ public class DnxMoveFileOriginalPath implements RepositoryTaskPlugin { ...@@ -71,7 +75,7 @@ public class DnxMoveFileOriginalPath implements RepositoryTaskPlugin {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
event.setEventDateTime(date.format(formatter)); event.setEventDateTime(date.format(formatter));
event.setEventType("external repair using DnxMoveFileOriginalPath plugin"); event.setEventType("external repair using DnxMoveFileOriginalPath plugin");
if (success) { if (IEsuccess) {
event.setEventOutcome1("success"); event.setEventOutcome1("success");
} else { } else {
event.setEventOutcome1("fail"); event.setEventOutcome1("fail");
...@@ -82,12 +86,13 @@ public class DnxMoveFileOriginalPath implements RepositoryTaskPlugin { ...@@ -82,12 +86,13 @@ public class DnxMoveFileOriginalPath implements RepositoryTaskPlugin {
event.setLinkingAgentIdentifierValue1("plugin programmed by Andreas Romeyke (SLUB Dresden), code available at https://github.com/SLUB-digitalpreservation/fileoriginalpath_plugin4rosetta"); event.setLinkingAgentIdentifierValue1("plugin programmed by Andreas Romeyke (SLUB Dresden), code available at https://github.com/SLUB-digitalpreservation/fileoriginalpath_plugin4rosetta");
eventList.add( event ); eventList.add( event );
ieDnxH.setEvents( eventList ); ieDnxH.setEvents( eventList );
ieEditor.setDnxForIE( ieDnxH ); ieEditor.setDnxForIE( ieDnxH ); /* necessary to commit changes on IEpid */
} catch (DigitoolException e) { } catch (DigitoolException e) {
String err = "unknown Digitool exception: " + e;
log.error( err );
e.printStackTrace(); e.printStackTrace();
} }
return taskResults; return taskResults;
} }
...@@ -96,23 +101,15 @@ public class DnxMoveFileOriginalPath implements RepositoryTaskPlugin { ...@@ -96,23 +101,15 @@ public class DnxMoveFileOriginalPath implements RepositoryTaskPlugin {
DnxDocumentHelper ieDnxH = ieEditor.getDnxHelper(filePid); DnxDocumentHelper ieDnxH = ieEditor.getDnxHelper(filePid);
DnxDocumentHelper.GeneralFileCharacteristics ieGfs = ieDnxH.getGeneralFileCharacteristics(); DnxDocumentHelper.GeneralFileCharacteristics ieGfs = ieDnxH.getGeneralFileCharacteristics();
String currentFileOriginalPath = ieGfs.getFileOriginalPath(); String currentFileOriginalPath = ieGfs.getFileOriginalPath();
String updatedFileOriginalPath = currentFileOriginalPath; String updatedFileOriginalPath = currentFileOriginalPath; /* default to current to avoid empty string */
boolean updated = false; boolean updated = false;
if (currentFileOriginalPath.startsWith("file://data/")) { final String replacement = "file://";
updatedFileOriginalPath=currentFileOriginalPath.replaceFirst("^file://data/", "file://"); Pattern pattern = Pattern.compile("^(file://|/)?data/");
updated = true; Matcher matcher = pattern.matcher( currentFileOriginalPath );
}
else if (currentFileOriginalPath.startsWith("data/")) {
updatedFileOriginalPath=currentFileOriginalPath.replaceFirst("^data/", "file://");
updated = true;
}
else if (currentFileOriginalPath.startsWith("/data/")) {
updatedFileOriginalPath=currentFileOriginalPath.replaceFirst("^/data/", "file://");
updated = true;
}
/* if something is updated */ /* if something is updated */
if (updated) { if (matcher.matches()) { // update it
// update it updatedFileOriginalPath = matcher.replaceFirst( replacement );
updated = true;
log.info("update needed for IE " + ieEditor.getIEPid() + " RepPid " + repPid + " FilePid " + filePid + log.info("update needed for IE " + ieEditor.getIEPid() + " RepPid " + repPid + " FilePid " + filePid +
" (old file_original_path='" + currentFileOriginalPath + "') to new file_original_path='" + " (old file_original_path='" + currentFileOriginalPath + "') to new file_original_path='" +
updatedFileOriginalPath + "'" updatedFileOriginalPath + "'"
...@@ -124,7 +121,7 @@ public class DnxMoveFileOriginalPath implements RepositoryTaskPlugin { ...@@ -124,7 +121,7 @@ public class DnxMoveFileOriginalPath implements RepositoryTaskPlugin {
} }
ieGfs.setFileOriginalPath(updatedFileOriginalPath); ieGfs.setFileOriginalPath(updatedFileOriginalPath);
ieDnxH.setGeneralFileCharacteristics(ieGfs); ieDnxH.setGeneralFileCharacteristics(ieGfs);
ieEditor.setDnx(ieDnxH, filePid); ieEditor.setDnx(ieDnxH, filePid); /* necessary to commit changes on filePID */
return updated; return updated;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment