use of org.mycore.mets.model.struct.PhysicalSubDiv in project mycore by MyCoRe-Org.
the class MCRMETSHierarchyGenerator method updateStructLinkMapUsingDerivateLinks.
/**
* Fills the structLinkMap for a single logical mets:div using derivate link information.
*
* @param logicalDiv the logical div to handle
* @param mcrObject the mycore object linked in the logical div (mets:div/@ID == mycore object id)
*/
protected void updateStructLinkMapUsingDerivateLinks(LogicalDiv logicalDiv, MCRObject mcrObject) {
// by derivate link
Optional<String> linkedFileOptional = getLinkedFile(mcrObject);
linkedFileOptional.flatMap(this::getFileId).ifPresent(fileId -> {
PhysicalSubDiv physicalDiv = getPhysicalDiv(fileId);
addToStructLinkMap(logicalDiv, physicalDiv);
});
}
use of org.mycore.mets.model.struct.PhysicalSubDiv in project mycore by MyCoRe-Org.
the class MCRMetsSave method updateOnFileDelete.
/**
* @param mets
* @param file
* @return
*/
private static Document updateOnFileDelete(Document mets, MCRPath file) {
Mets modifiedMets;
try {
modifiedMets = new Mets(mets);
String href = file.getOwnerRelativePath().substring(1);
PhysicalStructMap physStructMap = (PhysicalStructMap) modifiedMets.getStructMap(PhysicalStructMap.TYPE);
PhysicalDiv divContainer = physStructMap.getDivContainer();
// search the right group and remove the file from the group
List<FileGrp> fileGroups = modifiedMets.getFileSec().getFileGroups();
for (FileGrp fileGrp : fileGroups) {
if (fileGrp.contains(href)) {
org.mycore.mets.model.files.File fileToRemove = fileGrp.getFileByHref(href);
fileGrp.removeFile(fileToRemove);
ArrayList<PhysicalSubDiv> physicalSubDivsToRemove = new ArrayList<>();
// remove file from mets:mets/mets:structMap[@TYPE='PHYSICAL']
for (PhysicalSubDiv physicalSubDiv : divContainer.getChildren()) {
ArrayList<Fptr> fptrsToRemove = new ArrayList<>();
for (Fptr fptr : physicalSubDiv.getChildren()) {
if (fptr.getFileId().equals(fileToRemove.getId())) {
if (fileGrp.getUse().equals(FileGrp.USE_MASTER)) {
physicalSubDivsToRemove.add(physicalSubDiv);
} else {
fptrsToRemove.add(fptr);
}
}
}
for (Fptr fptrToRemove : fptrsToRemove) {
LOGGER.warn(String.format(Locale.ROOT, "remove fptr \"%s\" from mets.xml of \"%s\"", fptrToRemove.getFileId(), file.getOwner()));
physicalSubDiv.remove(fptrToRemove);
}
}
for (PhysicalSubDiv physicalSubDivToRemove : physicalSubDivsToRemove) {
// remove links in mets:structLink section
List<SmLink> list = modifiedMets.getStructLink().getSmLinkByTo(physicalSubDivToRemove.getId());
LogicalStructMap logicalStructMap = (LogicalStructMap) modifiedMets.getStructMap(LogicalStructMap.TYPE);
for (SmLink linkToRemove : list) {
LOGGER.warn(String.format(Locale.ROOT, "remove smLink from \"%s\" to \"%s\"", linkToRemove.getFrom(), linkToRemove.getTo()));
modifiedMets.getStructLink().removeSmLink(linkToRemove);
// modify logical struct Map
String logID = linkToRemove.getFrom();
// the deleted file was not directly assigned to a structure
if (logicalStructMap.getDivContainer().getId().equals(logID)) {
continue;
}
LogicalDiv logicalDiv = logicalStructMap.getDivContainer().getLogicalSubDiv(logID);
if (logicalDiv == null) {
LOGGER.error("Could not find {} with id {}", LogicalDiv.class.getSimpleName(), logID);
LOGGER.error("Mets document remains unchanged");
return mets;
}
// there are still files for this logical sub div, nothing to do
if (modifiedMets.getStructLink().getSmLinkByFrom(logicalDiv.getId()).size() > 0) {
continue;
}
// the logical div has other divs included, nothing to do
if (logicalDiv.getChildren().size() > 0) {
continue;
}
/*
* the log div might be in a hierarchy of divs, which may now be empty
* (only containing empty directories), if so the parent of the log div
* must be deleted
* */
handleParents(logicalDiv, modifiedMets);
logicalStructMap.getDivContainer().remove(logicalDiv);
}
divContainer.remove(physicalSubDivToRemove);
}
}
}
} catch (Exception ex) {
LOGGER.error("Error occured while removing file {} from the existing mets file", file, ex);
return null;
}
return modifiedMets.asDocument();
}
use of org.mycore.mets.model.struct.PhysicalSubDiv in project mycore by MyCoRe-Org.
the class MCRMetsSave method updateOnImageFile.
private static void updateOnImageFile(Document mets, String fileId, String path) {
LOGGER.debug("FILE is a image!");
// check if custom files are present and save the ids
String[] customFileGroups = { MCRMetsFileUse.TRANSCRIPTION.toString(), MCRMetsFileUse.ALTO.toString(), MCRMetsFileUse.TRANSLATION.toString() };
// add to structMap physical
PhysicalSubDiv div = new PhysicalSubDiv(PhysicalSubDiv.ID_PREFIX + fileId, PhysicalSubDiv.TYPE_PAGE);
div.add(new Fptr(fileId));
Arrays.stream(customFileGroups).map(customFileGroup -> searchFileInGroup(mets, path, customFileGroup)).filter(Objects::nonNull).map(Fptr::new).forEach(div::add);
// actually alter the mets document
Element structMapPhys = getPhysicalStructmap(mets);
structMapPhys.addContent(div.asElement());
// add to structLink
SmLink smLink = getDefaultSmLink(mets, div);
Element structLink = getStructLink(mets);
structLink.addContent(smLink.asElement());
}
use of org.mycore.mets.model.struct.PhysicalSubDiv in project mycore by MyCoRe-Org.
the class MCRMetsSave method updateURNsInMetsDocument.
/**
* Inserts the given URNs into the {@link Mets} Object.
* @param mets the {@link Mets} object were the URNs should be inserted.
* @param fileUrnMap a {@link Map} wich contains the file as key and the urn as as value
*/
public static void updateURNsInMetsDocument(Mets mets, Map<String, String> fileUrnMap) throws UnsupportedEncodingException {
// put all files of the mets in a list
List<FileGrp> fileGroups = mets.getFileSec().getFileGroups();
List<File> files = new ArrayList<>();
for (FileGrp fileGrp : fileGroups) {
files.addAll(fileGrp.getFileList());
}
// combine the filename and the id in a map
Map<String, String> idFileMap = new HashMap<>();
for (File file : files) {
idFileMap.put(file.getId(), file.getFLocat().getHref());
}
List<PhysicalSubDiv> childs = ((PhysicalStructMap) mets.getStructMap(PhysicalStructMap.TYPE)).getDivContainer().getChildren();
for (PhysicalSubDiv divChild : childs) {
String idMets = divChild.getChildren().get(0).getFileId();
// check if there is a URN for the file
String file = "/" + URLDecoder.decode(idFileMap.get(idMets), "UTF-8");
if (fileUrnMap.containsKey(file)) {
divChild.setContentids(fileUrnMap.get(file));
}
}
}
use of org.mycore.mets.model.struct.PhysicalSubDiv in project mycore by MyCoRe-Org.
the class MCRDFGLinkServlet method getOrderNumber.
private static int getOrderNumber(Document metsDoc, String fileHref) {
int orderNumber = -1;
String fileID = null;
try {
Mets mets = new Mets(metsDoc);
List<FileGrp> fileGroups = mets.getFileSec().getFileGroups();
for (FileGrp fileGrp : fileGroups) {
List<File> fileList = fileGrp.getFileList();
for (File file : fileList) {
FLocat fLocat = file.getFLocat();
if (fLocat.getHref().equals(MCRXMLFunctions.encodeURIPath(fileHref)))
fileID = file.getId();
}
}
if (fileID != null) {
PhysicalStructMap structMap = (PhysicalStructMap) mets.getStructMap(PhysicalStructMap.TYPE);
PhysicalDiv rootDiv = structMap.getDivContainer();
List<PhysicalSubDiv> children = rootDiv.getChildren();
for (int index = 0; index < children.size(); index++) {
PhysicalSubDiv physicalSubDiv = children.get(index);
List<Fptr> fptrList = physicalSubDiv.getChildren();
for (Fptr fptr : fptrList) {
if (fptr.getFileId().equals(fileID))
orderNumber = index + 1;
}
}
}
} catch (Exception e) {
throw new MCRPersistenceException("could not parse mets.xml", e);
}
return orderNumber;
}
Aggregations