use of org.mycore.mets.model.struct.LogicalDiv in project mycore by MyCoRe-Org.
the class MCRMETSHierarchyGenerator method validateLogicalStruct.
/**
* Its important to remove not linked logical divs without children to
* get a valid logical structure.
*
* @param logicalDiv the logical div to check
* @return true if the logical struct is valid otherwise false
*/
private boolean validateLogicalStruct(LogicalDiv logicalDiv) {
// has link
String logicalDivId = logicalDiv.getId();
for (List<String> logivalDivIDs : structLinkMap.values()) {
if (logivalDivIDs.contains(logicalDivId)) {
return true;
}
}
// has children with link
Iterator<LogicalDiv> it = logicalDiv.getChildren().iterator();
while (it.hasNext()) {
LogicalDiv child = it.next();
if (validateLogicalStruct(child)) {
return true;
}
// nothing -> delete it
it.remove();
}
return false;
}
use of org.mycore.mets.model.struct.LogicalDiv in project mycore by MyCoRe-Org.
the class MCRMetsSave method updateFiles.
/**
* Call this method to update the mets.xml if files of the derivate have changed. Files will be added or removed
* from the mets:fileSec and mets:StructMap[@type=PHYSICAL]. The mets:structLink part will be rebuild after.
*
* <p>This method takes care of the group assignment. For example: image files will be added to the MASTER
* group and ALTO files to the ALTO group. It will also bundle files with the same name e.g. sample1.tiff and
* alto/sample1.xml to the same physical struct map div.</p>
*
* <p><b>Important:</b> This method does not update the mets.xml in the derivate, its just updating the given mets
* instance.</p>
*
* @param mets the mets to update
* @param derivatePath path to the derivate -> required for looking up new files
* @throws IOException derivate couldn't be read
*/
public static void updateFiles(Mets mets, final MCRPath derivatePath) throws IOException {
List<String> metsFiles = mets.getFileSec().getFileGroups().stream().flatMap(g -> g.getFileList().stream()).map(File::getFLocat).map(FLocat::getHref).collect(Collectors.toList());
List<String> derivateFiles = Files.walk(derivatePath).filter(MCRStreamUtils.not(Files::isDirectory)).map(MCRPath::toMCRPath).map(MCRPath::getOwnerRelativePath).map(path -> path.substring(1)).filter(href -> !"mets.xml".equals(href)).collect(Collectors.toList());
ArrayList<String> removedFiles = new ArrayList<>(metsFiles);
removedFiles.removeAll(derivateFiles);
ArrayList<String> addedFiles = new ArrayList<>(derivateFiles);
Collections.sort(addedFiles);
addedFiles.removeAll(metsFiles);
StructLink structLink = mets.getStructLink();
PhysicalStructMap physicalStructMap = mets.getPhysicalStructMap();
List<String> unlinkedLogicalIds = new ArrayList<>();
// remove files
PhysicalDiv physicalDiv = physicalStructMap.getDivContainer();
removedFiles.forEach(href -> {
File file = null;
// remove from fileSec
for (FileGrp grp : mets.getFileSec().getFileGroups()) {
file = grp.getFileByHref(href);
if (file != null) {
grp.removeFile(file);
break;
}
}
if (file == null) {
return;
}
// remove from physical
PhysicalSubDiv physicalSubDiv = physicalDiv.byFileId(file.getId());
physicalSubDiv.remove(physicalSubDiv.getFptr(file.getId()));
if (physicalSubDiv.getChildren().isEmpty()) {
physicalDiv.remove(physicalSubDiv);
}
// remove from struct link
structLink.getSmLinkByTo(physicalSubDiv.getId()).forEach(smLink -> {
structLink.removeSmLink(smLink);
if (structLink.getSmLinkByFrom(smLink.getFrom()).isEmpty()) {
unlinkedLogicalIds.add(smLink.getFrom());
}
});
});
// fix unlinked logical divs
if (!unlinkedLogicalIds.isEmpty()) {
// get first physical div
List<PhysicalSubDiv> physicalChildren = physicalStructMap.getDivContainer().getChildren();
String firstPhysicalID = physicalChildren.isEmpty() ? physicalStructMap.getDivContainer().getId() : physicalChildren.get(0).getId();
// a logical div is not linked anymore -> link with first physical div
unlinkedLogicalIds.forEach(from -> structLink.addSmLink(new SmLink(from, firstPhysicalID)));
}
// get last logical div
LogicalDiv divContainer = mets.getLogicalStructMap().getDivContainer();
List<LogicalDiv> descendants = divContainer.getDescendants();
LogicalDiv lastLogicalDiv = descendants.isEmpty() ? divContainer : descendants.get(descendants.size() - 1);
// add files
addedFiles.forEach(href -> {
MCRPath filePath = (MCRPath) derivatePath.resolve(href);
String fileBase = getFileBase(href);
try {
String fileGroupUse = MCRMetsSave.getFileGroupUse(filePath);
// build file
String mimeType = MCRContentTypes.probeContentType(filePath);
String fileId = fileGroupUse.toLowerCase(Locale.ROOT) + "_" + fileBase;
File file = new File(fileId, mimeType);
file.setFLocat(new FLocat(LOCTYPE.URL, href));
// fileSec
FileGrp fileGroup = mets.getFileSec().getFileGroup(fileGroupUse);
if (fileGroup == null) {
fileGroup = new FileGrp(fileGroupUse);
mets.getFileSec().addFileGrp(fileGroup);
}
fileGroup.addFile(file);
// structMap physical
String existingFileID = mets.getFileSec().getFileGroups().stream().filter(grp -> !grp.getUse().equals(fileGroupUse)).flatMap(grp -> grp.getFileList().stream()).filter(brotherFile -> fileBase.equals(getFileBase(brotherFile.getFLocat().getHref()))).map(File::getId).findAny().orElse(null);
PhysicalSubDiv physicalSubDiv;
if (existingFileID != null) {
// there is a file (e.g. img or alto) which the same file base -> add the file to this mets:div
physicalSubDiv = physicalDiv.byFileId(existingFileID);
physicalSubDiv.add(new Fptr(file.getId()));
} else {
// there is no mets:div with this file
physicalSubDiv = new PhysicalSubDiv(PhysicalSubDiv.ID_PREFIX + fileBase, PhysicalSubDiv.TYPE_PAGE);
physicalSubDiv.add(new Fptr(file.getId()));
physicalDiv.add(physicalSubDiv);
}
// add to struct link
structLink.addSmLink(new SmLink(lastLogicalDiv.getId(), physicalSubDiv.getId()));
} catch (Exception exc) {
LOGGER.error("Unable to add file {} to mets.xml of {}", href, derivatePath.getOwner(), exc);
}
});
}
use of org.mycore.mets.model.struct.LogicalDiv in project mycore by MyCoRe-Org.
the class MCRSimpleModelXMLConverter method buildLogicalSubDiv.
private static void buildLogicalSubDiv(MCRMetsSection metsSection, LogicalDiv parent, Map<MCRMetsSection, String> sectionIdMap, Map<String, String> idToNewIDMap) {
String id = metsSection.getId();
LogicalDiv logicalSubDiv = new LogicalDiv(id, metsSection.getType(), metsSection.getLabel());
if (metsSection.getAltoLinks().size() > 0) {
Fptr fptr = new Fptr();
List<Seq> seqList = fptr.getSeqList();
Seq seq = new Seq();
seqList.add(seq);
metsSection.getAltoLinks().forEach(al -> {
Area area = new Area();
seq.getAreaList().add(area);
area.setBetype("IDREF");
area.setBegin(al.getBegin());
area.setEnd(al.getEnd());
String oldID = al.getFile().getId();
if (!idToNewIDMap.containsKey(oldID)) {
throw new MCRException("Could not get new id for: " + oldID);
}
area.setFileId(idToNewIDMap.get(oldID));
});
logicalSubDiv.getFptrList().add(fptr);
}
sectionIdMap.put(metsSection, id);
parent.add(logicalSubDiv);
int count = 1;
for (MCRMetsSection section : metsSection.getMetsSectionList()) {
buildLogicalSubDiv(section, logicalSubDiv, sectionIdMap, idToNewIDMap);
}
}
use of org.mycore.mets.model.struct.LogicalDiv in project mycore by MyCoRe-Org.
the class MCRXMLSimpleModelConverter method buidRootSection.
private static MCRMetsSection buidRootSection(Mets mets, Map<String, MCRMetsSection> idSectionMap, Map<String, MCRMetsFile> idFileMap) {
IStructMap structMap = mets.getStructMap(LogicalStructMap.TYPE);
LogicalStructMap logicalStructMap = (LogicalStructMap) structMap;
LogicalDiv divContainer = logicalStructMap.getDivContainer();
return buildSection(divContainer, idSectionMap, null, idFileMap);
}
use of org.mycore.mets.model.struct.LogicalDiv in project mycore by MyCoRe-Org.
the class MCRXMLSimpleModelConverter method buildSection.
private static MCRMetsSection buildSection(LogicalDiv current, Map<String, MCRMetsSection> idSectionMap, MCRMetsSection parent, Map<String, MCRMetsFile> idFileMap) {
MCRMetsSection metsSection = new MCRMetsSection();
metsSection.setId(current.getId());
metsSection.setLabel(current.getLabel());
metsSection.setType(current.getType());
metsSection.setParent(parent);
current.getFptrList().forEach(fptr -> fptr.getSeqList().forEach(seq -> {
seq.getAreaList().forEach(area -> {
String fileId = area.getFileId();
String begin = area.getBegin();
String end = area.getEnd();
if (!idFileMap.containsKey(fileId)) {
throw new MCRException("No file with id " + fileId + " found!");
}
MCRMetsFile file = idFileMap.get(fileId);
MCRMetsAltoLink e = new MCRMetsAltoLink(file, begin, end);
metsSection.addAltoLink(e);
});
}));
if (idSectionMap != null) {
idSectionMap.put(current.getId(), metsSection);
}
List<MCRMetsSection> childSectionList = metsSection.getMetsSectionList();
current.getChildren().stream().map(section -> MCRXMLSimpleModelConverter.buildSection(section, idSectionMap, metsSection, idFileMap)).forEachOrdered(metsSection::addSection);
return metsSection;
}
Aggregations