use of org.mycore.mets.model.files.File 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;
}
use of org.mycore.mets.model.files.File in project mycore by MyCoRe-Org.
the class MCRMETSDefaultGenerator method sortFileToGrp.
private void sortFileToGrp(FileSec fileSec, Map.Entry<MCRPath, BasicFileAttributes> file, String fileID, final String href, MCRMetsFileUse fileUse) throws IOException {
// file
File metsFile = new File(fileID, MCRContentTypes.probeContentType(file.getKey()));
FLocat fLocat = new FLocat(LOCTYPE.URL, href);
metsFile.setFLocat(fLocat);
for (FileGrp fileGrp : fileSec.getFileGroups()) {
if (fileGrp.getUse().equalsIgnoreCase(fileUse.toString())) {
fileGrp.addFile(metsFile);
}
}
}
use of org.mycore.mets.model.files.File in project mycore by MyCoRe-Org.
the class MCRMETSHierarchyGenerator method addFile.
private void addFile(String id, FileGrp fileGroup, MCRPath path, String mimeType) {
File imageFile = new File(id, mimeType);
try {
final String href = MCRXMLFunctions.encodeURIPath(path.getOwnerRelativePath().substring(1), true);
FLocat fLocat = new FLocat(LOCTYPE.URL, href);
imageFile.setFLocat(fLocat);
fileGroup.addFile(imageFile);
} catch (URISyntaxException uriSyntaxException) {
LOGGER.error("invalid href", uriSyntaxException);
}
}
use of org.mycore.mets.model.files.File 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.files.File in project mycore by MyCoRe-Org.
the class MCRMetsSave method updateOnFileAdd.
// TODO: should use mets-model api
/**
* Alters the mets file
*
* @param mets
* the unmodified source
* @param file
* the file to add
* @return the modified mets or null if an exception occures
*/
private static Document updateOnFileAdd(Document mets, MCRPath file) {
try {
// check for file existance (if a derivate with mets.xml is uploaded
String relPath = MCRXMLFunctions.encodeURIPath(file.getOwnerRelativePath().substring(1));
// Check if file already exists -> if yes do nothing
String fileExistPathString = "mets:mets/mets:fileSec/mets:fileGrp/mets:file/mets:FLocat[@xlink:href='" + relPath + "']";
XPathExpression<Element> xpath = XPathFactory.instance().compile(fileExistPathString, Filters.element(), null, MCRConstants.METS_NAMESPACE, MCRConstants.XLINK_NAMESPACE);
if (xpath.evaluate(mets).size() > 0) {
String msgTemplate = "The File : '%s' already exists in mets.xml";
LOGGER.warn(String.format(Locale.ROOT, msgTemplate, relPath));
return null;
} else {
String msgTemplate = "The File : '%s' does not exists in mets.xml";
LOGGER.warn(String.format(Locale.ROOT, msgTemplate, relPath));
}
// add to file section
String contentType = MCRContentTypes.probeContentType(file);
LOGGER.warn(MessageFormat.format("Content Type is : {0}", contentType));
String fileGrpUSE = getFileGroupUse(file);
String fileId = MessageFormat.format("{0}_{1}", fileGrpUSE.toLowerCase(Locale.ROOT), getFileBase(relPath));
org.mycore.mets.model.files.File fileAsMetsFile = new org.mycore.mets.model.files.File(fileId, contentType);
FLocat fLocat = new FLocat(LOCTYPE.URL, relPath);
fileAsMetsFile.setFLocat(fLocat);
Element fileSec = getFileGroup(mets, fileGrpUSE);
fileSec.addContent(fileAsMetsFile.asElement());
if (fileGrpUSE.equals(MCRMetsFileUse.DEFAULT.toString())) {
updateOnImageFile(mets, fileId, relPath);
} else {
updateOnCustomFile(mets, fileId, relPath);
}
} catch (Exception ex) {
LOGGER.error("Error occured while adding file {} to the existing mets file", file, ex);
return null;
}
return mets;
}
Aggregations