use of org.mycore.datamodel.metadata.MCRObjectDerivate in project mycore by MyCoRe-Org.
the class MCRDerivateURNUtils method getDFGViewerURL.
public static URL getDFGViewerURL(MCRPIRegistrationInfo urn) {
URL url = null;
try {
MCRObjectID derivateId = MCRObjectID.getInstance(urn.getMycoreID());
MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(derivateId);
String mainDoc = Optional.ofNullable(derivate.getDerivate()).map(MCRObjectDerivate::getInternals).map(MCRMetaIFS::getMainDoc).orElseThrow(() -> new RuntimeException("Could not get main doc for " + derivateId));
String spec = null;
String baseURL = MCRFrontendUtil.getBaseURL();
String id = URLEncoder.encode(derivateId.toString(), "UTF-8");
if (mainDoc != null && mainDoc.length() > 0) {
String mainDocEnc = URLEncoder.encode(mainDoc, "UTF-8");
spec = MessageFormat.format(baseURL + "servlets/MCRDFGLinkServlet?deriv={0}&file={1}", id, mainDocEnc);
} else {
spec = baseURL + "servlets/MCRDFGLinkServlet?deriv=" + id;
}
LOGGER.debug("Generated URL for urn {} is {}", urn.getIdentifier(), spec);
url = new URL(spec);
} catch (UnsupportedEncodingException | MalformedURLException e) {
LOGGER.error("Could not create dfg viewer url", e);
}
return url;
}
use of org.mycore.datamodel.metadata.MCRObjectDerivate in project mycore by MyCoRe-Org.
the class MCRURNGranularRESTRegistrationService method registerURN.
private MCRDNBURN registerURN(MCRDerivate deriv, String filePath) {
MCRObjectID derivID = deriv.getId();
Function<String, Integer> countCreatedPI = s -> MCRPersistentIdentifierManager.getInstance().getCreatedIdentifiers(derivID, getType(), getRegistrationServiceID()).size();
int seed = Optional.of(filePath).filter(p -> !"".equals(p)).map(countCreatedPI).map(count -> count + 1).orElse(1);
MCRDNBURN derivURN = Optional.ofNullable(deriv.getDerivate()).map(MCRObjectDerivate::getURN).flatMap(new MCRDNBURNParser()::parse).orElseGet(() -> createNewURN(deriv));
String setID = derivID.getNumberAsString();
GranularURNGenerator granularURNGen = new GranularURNGenerator(seed, derivURN, setID);
Function<MCRPath, Supplier<String>> generateURN = p -> granularURNGen.getURNSupplier();
derivateFileStream.apply(deriv).filter(notInIgnoreList().and(matchFile(filePath))).sorted().collect(Collectors.toMap(generateURN, p -> p, (m1, m2) -> m1, LinkedHashMap::new)).forEach(createFileMetadata(deriv).andThen(persistURN(deriv)));
try {
MCRMetadataManager.update(deriv);
} catch (MCRPersistenceException | MCRAccessException e) {
LOGGER.error("Error while updating derivate {}", derivID, e);
}
EntityTransaction transaction = MCREntityManagerProvider.getCurrentEntityManager().getTransaction();
if (!transaction.isActive()) {
transaction.begin();
}
transaction.commit();
return derivURN;
}
use of org.mycore.datamodel.metadata.MCRObjectDerivate in project mycore by MyCoRe-Org.
the class MCRUploadHandlerIFS method updateMainFile.
private void updateMainFile() throws IOException {
String mainFile = derivate.getDerivate().getInternals().getMainDoc();
MCRObjectDerivate der = MCRMetadataManager.retrieveMCRDerivate(getOrCreateDerivateID()).getDerivate();
boolean hasNoMainFile = ((der.getInternals().getMainDoc() == null) || (der.getInternals().getMainDoc().trim().isEmpty()));
if ((mainFile == null) || mainFile.trim().isEmpty() && hasNoMainFile) {
mainFile = getPathOfMainFile();
LOGGER.debug("Setting main file to {}", mainFile);
derivate.getDerivate().getInternals().setMainDoc(mainFile);
MCRMetadataManager.updateMCRDerivateXML(derivate);
}
}
use of org.mycore.datamodel.metadata.MCRObjectDerivate in project mycore by MyCoRe-Org.
the class MCRURNGranularOAIRegistrationService method registerURNsDerivate.
private MCRDNBURN registerURNsDerivate(MCRBase obj, String additional, MCRObjectDerivate derivate) throws MCRPersistentIdentifierException {
LOGGER.info("Add URNs to all files of {}", obj.getId());
Session session = MCRHIBConnection.instance().getSession();
Path path = MCRPath.getPath(obj.getId().toString(), "/");
MCRFileCollectingFileVisitor<Path> collectingFileVisitor = new MCRFileCollectingFileVisitor<>();
try {
Files.walkFileTree(path, collectingFileVisitor);
} catch (IOException e) {
throw new MCRPersistentIdentifierException("Could not walk derivate file tree!", e);
}
List<String> ignoreFileNamesList = getIgnoreFileList();
List<Predicate<String>> predicateList = ignoreFileNamesList.stream().map(Pattern::compile).map(Pattern::asPredicate).collect(Collectors.toList());
List<MCRPath> pathList = collectingFileVisitor.getPaths().stream().filter(file -> predicateList.stream().noneMatch(p -> p.test(file.toString().split(":")[1]))).map(p -> (MCRPath) p).sorted().collect(Collectors.toList());
MCRDNBURN newURN = getNewIdentifier(obj.getId(), additional);
String setID = obj.getId().getNumberAsString();
for (int pathListIndex = 0; pathListIndex < pathList.size(); pathListIndex++) {
MCRDNBURN subURN = newURN.toGranular(setID, pathListIndex + 1, pathList.size());
derivate.getOrCreateFileMetadata(pathList.get(pathListIndex), subURN.asString()).setUrn(subURN.asString());
MCRPI databaseEntry = new MCRPI(subURN.asString(), getType(), obj.getId().toString(), pathList.get(pathListIndex).getOwnerRelativePath(), this.getRegistrationServiceID(), null);
session.save(databaseEntry);
}
derivate.setURN(newURN.asString());
MCRPI databaseEntry = new MCRPI(newURN.asString(), getType(), obj.getId().toString(), "", this.getRegistrationServiceID(), new Date());
session.save(databaseEntry);
return newURN;
}
use of org.mycore.datamodel.metadata.MCRObjectDerivate in project mycore by MyCoRe-Org.
the class MCRURNGranularOAIRegistrationService method register.
@Override
public MCRDNBURN register(MCRBase obj, String additional, boolean updateObject) throws MCRAccessException, MCRActiveLinkException, MCRPersistentIdentifierException {
this.validateRegistration(obj, additional);
MCRObjectDerivate derivate = ((MCRDerivate) obj).getDerivate();
MCRDNBURN newURN;
if (additional.equals("")) {
/* Multiple URN for entire Derivate... */
newURN = registerURNsDerivate(obj, additional, derivate);
} else {
/* Single URN to one File... */
newURN = registerSingleURN(obj, additional, derivate);
}
try {
MCRMetadataManager.update(obj);
} catch (Exception e) {
throw new MCRPersistentIdentifierException("Error while updating derivate " + obj.getId(), e);
}
return newURN;
}
Aggregations