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

- added collision-detection if a replaced fileOriginalPath already exists

- bugfix, Matcher.matches() works only on whole string, therefore Matcher.lookingAt() is used
parent 09558252
No related branches found
No related tags found
No related merge requests found
......@@ -7,7 +7,7 @@
</fr:x_form>
</pl:initParameters>
<pl:description>repairs broken file original paths</pl:description>
<pl:version>1.09</pl:version>
<pl:version>1.11</pl:version>
<pl:materialType>DIGITAL</pl:materialType>
<pl:module>Repository</pl:module>
<pl:generalType>TASK</pl:generalType>
......
......@@ -9,8 +9,10 @@ import com.exlibris.digitool.repository.api.RepositoryTaskPlugin;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -22,6 +24,11 @@ public class DnxMoveFileOriginalPath implements RepositoryTaskPlugin {
super();
}
class FileOriginalPathCollisionException extends Exception {
FileOriginalPathCollisionException(String message) {
super(message);
}
}
public TaskResults execute(IEEditor ieEditor, Map<String, String> initParams, TaskResults taskResults) {
log.info("Executing DnxMoveFileOriginalPath for " + ieEditor.getIEPid());
......@@ -32,31 +39,56 @@ public class DnxMoveFileOriginalPath implements RepositoryTaskPlugin {
// get all rep pids
try {
List<String> repPids = ieEditor.getReps();
// get all file pids
for (String repPid: repPids) {
// get all file pids
try {
List<String> filePids = ieEditor.getFilesForRep(repPid);
for (String filePid: filePids) {
// for each rep pid mark all fileoriginalpaths to detect copies
Set<String> filePaths = new HashSet<>();
for (String filePid : filePids) {
try {
DnxDocumentHelper ieDnxH = ieEditor.getDnxHelper(filePid);
DnxDocumentHelper.GeneralFileCharacteristics ieGfs = ieDnxH.getGeneralFileCharacteristics();
String currentFileOriginalPath = ieGfs.getFileOriginalPath();
filePaths.add( currentFileOriginalPath );
} catch (DigitoolException e) {
IEsuccess = false;
String err = "unknown Digitool exception: " + e;
log.error(err);
e.printStackTrace();
taskResults.addResult(ieEditor.getIEPid(), null, false, err);
}
}
countFiles+= filePids.size();
// process each filePid for replace
for (String filePid : filePids) {
try {
countFiles++;
if (replaceFileOriginalPath(ieEditor, repPid, filePid)) { countSuccess++; }
if (replaceFileOriginalPath(ieEditor, repPid, filePid, filePaths)) {
countSuccess++;
}
taskResults.addResult(ieEditor.getIEPid(), null, true, "filepid=" + filePid + " processed");
} catch (DigitoolException e) {
IEsuccess = false;
String err = "unknown Digitool exception: " + e;
log.error( err );
log.error(err);
e.printStackTrace();
taskResults.addResult(ieEditor.getIEPid(), null, false, err);
} catch (FileOriginalPathCollisionException e) {
IEsuccess = false;
log.error(e.getMessage());
e.printStackTrace();
taskResults.addResult(ieEditor.getIEPid(), null, false, e.getMessage());
}
}
} catch (DigitoolException e) {
IEsuccess = false;
String err = "no FilePids found for RepPid=" + repPid + ", Digitool exception: " + e;
log.warn( err );
log.warn(err);
taskResults.addResult(ieEditor.getIEPid(), null, false, err);
}
}
} catch (DigitoolException e) {
IEsuccess = false;
String err = "no RepPids found, Digitool exception: " + e;
......@@ -96,8 +128,10 @@ public class DnxMoveFileOriginalPath implements RepositoryTaskPlugin {
}
/* replace file original path if it starts with "file://data/" or "/data/" */
private boolean replaceFileOriginalPath(IEEditor ieEditor, String repPid, String filePid) throws DigitoolException {
private boolean replaceFileOriginalPath(IEEditor ieEditor, String repPid, String filePid, Set<String> filePaths) throws DigitoolException, FileOriginalPathCollisionException {
DnxDocumentHelper ieDnxH = ieEditor.getDnxHelper(filePid);
DnxDocumentHelper.GeneralFileCharacteristics ieGfs = ieDnxH.getGeneralFileCharacteristics();
String currentFileOriginalPath = ieGfs.getFileOriginalPath();
......@@ -107,8 +141,14 @@ public class DnxMoveFileOriginalPath implements RepositoryTaskPlugin {
Pattern pattern = Pattern.compile("^(file://|/)?data/");
Matcher matcher = pattern.matcher( currentFileOriginalPath );
/* if something is updated */
if (matcher.matches()) { // update it
if (matcher.lookingAt()) { // update it
updatedFileOriginalPath = matcher.replaceFirst( replacement );
if (filePaths.contains( updatedFileOriginalPath )) {
throw new FileOriginalPathCollisionException("update needed for IE " + ieEditor.getIEPid() + " RepPid " + repPid + " FilePid " + filePid +
" (old file_original_path='" + currentFileOriginalPath + "') to new file_original_path='" +
updatedFileOriginalPath + "', but new file_original_path already exists!"
);
}
updated = true;
log.info("update needed for IE " + ieEditor.getIEPid() + " RepPid " + repPid + " FilePid " + filePid +
" (old file_original_path='" + currentFileOriginalPath + "') to new file_original_path='" +
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment