use of org.mycore.mets.model.struct.PhysicalDiv in project mycore by MyCoRe-Org.
the class MCRMetsMods2IIIFConverter method convert.
public MCRIIIFManifest convert() {
MCRIIIFManifest manifest = new MCRIIIFManifest();
// root chapter ^= manifest metadata
LogicalStructMap logicalStructMap = (LogicalStructMap) mets.getStructMap("LOGICAL");
LogicalDiv divContainer = logicalStructMap.getDivContainer();
List<MCRIIIFMetadata> metadata = extractMedataFromLogicalDiv(mets, divContainer);
manifest.metadata = metadata;
manifest.setId(this.identifier);
PhysicalStructMap physicalStructMap = (PhysicalStructMap) mets.getStructMap("PHYSICAL");
PhysicalDiv physicalDivContainer = physicalStructMap.getDivContainer();
String id = physicalDivContainer.getId();
MCRIIIFSequence sequence = new MCRIIIFSequence(id);
List<PhysicalSubDiv> children = physicalDivContainer.getChildren();
MCRIIIFImageImpl imageImpl = MCRIIIFImageImpl.getInstance(getImageImplName());
MCRIIIFImageProfile profile = imageImpl.getProfile();
profile.setId(MCRIIIFImageUtil.getProfileLink(imageImpl));
sequence.canvases = children.stream().map(physicalSubDiv -> {
String order = physicalSubDiv.asElement().getAttributeValue("ORDER");
String orderLabel = physicalSubDiv.getOrderLabel();
String contentids = physicalSubDiv.getContentids();
String label = Stream.of(order, orderLabel, contentids).filter(o -> o != null && !o.isEmpty()).collect(Collectors.joining(" - "));
String identifier = this.physicalIdentifierMap.get(physicalSubDiv);
try {
MCRIIIFImageInformation information = imageImpl.getInformation(identifier);
MCRIIIFCanvas canvas = new MCRIIIFCanvas(identifier, label, information.width, information.height);
MCRIIIFAnnotation annotation = new MCRIIIFAnnotation(identifier, canvas);
canvas.images.add(annotation);
MCRIIIFResource resource = new MCRIIIFResource(information.getId(), MCRDCMIType.Image);
resource.setWidth(information.width);
resource.setHeight(information.height);
MCRIIIFService service = new MCRIIIFService(information.getId(), profile.getContext());
service.profile = MCRIIIFImageProfile.IIIF_PROFILE_2_0;
resource.setService(service);
annotation.setResource(resource);
return canvas;
} catch (MCRIIIFImageNotFoundException | MCRIIIFImageProvidingException e) {
throw new MCRException("Error while providing ImageInfo for " + identifier, e);
} catch (MCRAccessException e) {
LOGGER.warn("User has no access to {}", identifier);
return null;
}
}).filter(Objects::nonNull).collect(Collectors.toList());
manifest.sequences.add(sequence);
List<MCRIIIFRange> complete = new ArrayList<>();
processDivContainer(complete, divContainer);
manifest.structures.addAll(complete);
manifest.setLabel(metadata.stream().filter(m -> m.getLabel().equals("title")).findFirst().get().getStringValue().get());
return manifest;
}
use of org.mycore.mets.model.struct.PhysicalDiv in project mycore by MyCoRe-Org.
the class MCRMETSHierarchyGenerator method createStructLink.
/**
* Creates the mets:structLink part of the mets.xml
*
* @return a newly generated StructLink.
*/
protected StructLink createStructLink() {
StructLink structLink = new StructLink();
String currentLogicalDivId = logicalStructMap.getDivContainer().getId();
PhysicalDiv physicalDiv = this.physicalStructMap.getDivContainer();
List<PhysicalSubDiv> subDivList = physicalDiv.getChildren();
for (PhysicalSubDiv physLink : subDivList) {
if (structLinkMap.containsKey(physLink.getId())) {
ArrayList<String> logicalIdList = new ArrayList<>(structLinkMap.get(physLink.getId()));
Collections.sort(logicalIdList);
for (String logicalId : logicalIdList) {
currentLogicalDivId = logicalId;
structLink.addSmLink(new SmLink(currentLogicalDivId, physLink.getId()));
}
} else {
structLink.addSmLink(new SmLink(currentLogicalDivId, physLink.getId()));
}
}
return structLink;
}
use of org.mycore.mets.model.struct.PhysicalDiv in project mycore by MyCoRe-Org.
the class MCRMETSHierarchyGenerator method createPhysicalStruct.
/**
* This method creates the physical structure map.
*
* @return generated pyhiscal struct map secion.
*/
protected PhysicalStructMap createPhysicalStruct() {
PhysicalStructMap pstr = new PhysicalStructMap();
PhysicalDiv physicalDiv = new PhysicalDiv("phys_" + this.mcrDer.getId(), PhysicalDiv.TYPE_PHYS_SEQ);
pstr.setDivContainer(physicalDiv);
// run through file references
for (FileRef ref : this.files) {
String physId = ref.toPhysId();
PhysicalSubDiv page = physicalDiv.get(physId);
if (page == null) {
page = new PhysicalSubDiv(physId, PhysicalSubDiv.TYPE_PAGE);
getOrderLabel(ref.toId()).ifPresent(page::setOrderLabel);
physicalDiv.add(page);
}
page.add(new Fptr(ref.toId()));
}
return pstr;
}
use of org.mycore.mets.model.struct.PhysicalDiv 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.PhysicalDiv 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