use of org.mycore.mets.model.files.FileGrp in project mycore by MyCoRe-Org.
the class MCRMETSDefaultGenerator method createMets.
private Mets createMets() throws IOException {
Mets mets = new Mets();
String owner = getOwner();
// add dmdsec
DmdSec dmdSec = new DmdSec("dmd_" + owner);
// add amdsec
AmdSec amdSec = new AmdSec("amd_" + owner);
// file sec
FileSec fileSec = new FileSec();
for (MCRMetsFileUse fileUse : MCRMetsFileUse.values()) {
FileGrp fileGrp = new FileGrp(fileUse.toString());
fileSec.addFileGrp(fileGrp);
}
// physical structure
PhysicalStructMap physicalStructMap = new PhysicalStructMap();
PhysicalDiv physicalDiv = new PhysicalDiv("phys_" + owner, "physSequence");
physicalStructMap.setDivContainer(physicalDiv);
// logical structure
MCRILogicalStructMapTypeProvider typeProvider = getTypeProvider();
LogicalStructMap logicalStructMap = new LogicalStructMap();
LogicalDiv logicalDiv = new LogicalDiv("log_" + owner, typeProvider.getType(MCRObjectID.getInstance(owner)), owner, amdSec.getId(), dmdSec.getId());
logicalDiv.setDmdId(dmdSec.getId());
logicalStructMap.setDivContainer(logicalDiv);
// struct Link
StructLink structLink = new StructLink();
// create internal structure
structureMets(getDerivatePath(), getIgnorePaths(), fileSec, physicalDiv, logicalDiv, structLink, 0);
hrefIdMap.clear();
// add to mets
mets.addDmdSec(dmdSec);
mets.addAmdSec(amdSec);
mets.setFileSec(fileSec);
mets.addStructMap(physicalStructMap);
mets.addStructMap(logicalStructMap);
mets.setStructLink(structLink);
return mets;
}
use of org.mycore.mets.model.files.FileGrp 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.FileGrp in project mycore by MyCoRe-Org.
the class MCRMETSHierarchyGenerator method getFileId.
/**
* Runs through all USE="MASTER" files and tries to find the corresponding
* mets:file @ID.
*
* @param uriEncodedLinkedFile the file to find
* @return the fileSec @ID
*/
private Optional<String> getFileId(String uriEncodedLinkedFile) {
FileGrp masterGroup = this.fileSection.getFileGroup(FileGrp.USE_MASTER);
return masterGroup.getFileList().stream().filter(file -> {
String href = file.getFLocat().getHref();
boolean equals = href.equals(uriEncodedLinkedFile);
boolean equalsWithoutSlash = uriEncodedLinkedFile.startsWith("/") && href.equals(uriEncodedLinkedFile.substring(1));
return equals || equalsWithoutSlash;
}).map(File::getId).findFirst();
}
use of org.mycore.mets.model.files.FileGrp in project mycore by MyCoRe-Org.
the class MCRMETSHierarchyGenerator method createFileSection.
/**
* Creates the file section.
*
* @return generated file secion.
*/
protected FileSec createFileSection() throws IOException {
FileSec fileSec = new FileSec();
List<MCRPath> filePaths = MCRMetsSave.listFiles(getDerivatePath(), getIgnorePaths());
List<FileGrp> fileGrps = MCRMetsSave.buildFileGroups(filePaths);
fileGrps.forEach(fileSec::addFileGrp);
for (MCRPath file : filePaths) {
String contentType = MCRContentTypes.probeContentType(file);
FileRef ref = new FileRef(file, contentType);
this.files.add(ref);
}
for (FileRef ref : this.files) {
MCRMetsFileUse use = MCRMetsFileUse.get(ref.path);
FileGrp fileGrp = fileGrps.stream().filter(grp -> grp.getUse().equals(use.toString())).findFirst().orElse(null);
if (fileGrp == null) {
LOGGER.warn("Unable to add file '" + ref.toId() + "' because cannot find corresponding group " + " with @USE='" + use.toString() + "'. Ignore file and continue.");
continue;
}
addFile(ref.toId(), fileGrp, ref.getPath(), ref.getContentType());
}
return fileSec;
}
use of org.mycore.mets.model.files.FileGrp in project mycore by MyCoRe-Org.
the class MCRMetsSave method getFileGroup.
private static Element getFileGroup(Document mets, String fileGrpUSE) {
// alter the mets document
XPathExpression<Element> xpath;
String fileGroupXPathString = String.format(Locale.ROOT, "mets:mets/mets:fileSec/mets:fileGrp[@USE='%s']", fileGrpUSE);
xpath = XPathFactory.instance().compile(fileGroupXPathString, Filters.element(), null, MCRConstants.METS_NAMESPACE);
Element element = xpath.evaluateFirst(mets);
if (element == null) {
// section does not exist
Element fileGroupElement = new FileGrp(fileGrpUSE).asElement();
String fileSectionPath = "mets:mets/mets:fileSec";
xpath = XPathFactory.instance().compile(fileSectionPath, Filters.element(), null, MCRConstants.METS_NAMESPACE);
Element fileSectionElement = xpath.evaluateFirst(mets);
if (fileSectionElement == null) {
throw new MCRPersistenceException("There is no fileSection in mets.xml!");
}
fileSectionElement.addContent(fileGroupElement);
element = fileGroupElement;
}
return element;
}
Aggregations