use of org.mycore.datamodel.metadata.MCRDerivate in project mycore by MyCoRe-Org.
the class MCRDerivateURNUtils method getURL.
public static URL getURL(MCRPIRegistrationInfo piInfo) {
String derivateID = piInfo.getMycoreID();
if (piInfo.getService().endsWith("-dfg")) {
return getDFGViewerURL(piInfo);
}
try {
// the base urn, links to frontpage (metadata + viewer)
if (piInfo.getAdditional() == null || piInfo.getAdditional().trim().length() == 0) {
MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivateID));
return new URL(MCRFrontendUtil.getBaseURL() + "receive/" + derivate.getOwnerID() + "?derivate=" + derivateID);
} else // an urn for a certain file, links to iview2
{
MCRPath file = MCRPath.getPath(derivateID, piInfo.getAdditional());
if (!Files.exists(file)) {
LOGGER.warn("File {} in object {} could NOT be found", file.getFileName().toString(), derivateID);
return null;
}
if (!isFileSupported(file)) {
LOGGER.info("File is not displayable within iView2. Use {} as url", MCRFileNodeServlet.class.getSimpleName());
String filePath = "/" + file.getOwner() + "/" + file.getFileName();
return new URL(MCRFrontendUtil.getBaseURL() + "servlets/" + MCRFileNodeServlet.class.getSimpleName() + filePath);
}
return new URL(getViewerURL(file));
}
} catch (MalformedURLException e) {
LOGGER.error("Malformed URL for URN {}", piInfo.getIdentifier(), e);
}
return null;
}
use of org.mycore.datamodel.metadata.MCRDerivate 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.MCRDerivate 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.MCRDerivate in project mycore by MyCoRe-Org.
the class MCRURNGranularRESTRegistrationService method defaultDerivateFileStream.
private static Stream<MCRPath> defaultDerivateFileStream(MCRDerivate derivate) {
MCRObjectID derivateId = derivate.getId();
Path derivRoot = MCRPath.getPath(derivateId.toString(), "/");
try {
return Files.walk(derivRoot).map(MCRPath::toMCRPath).filter(p -> !Files.isDirectory(p)).filter(p -> !p.equals(derivRoot));
} catch (IOException e) {
LOGGER.error("I/O error while access the starting file of derivate {}!", derivateId, e);
} catch (SecurityException s) {
LOGGER.error("No access to starting file of derivate {}!", derivateId, s);
}
return Stream.empty();
}
use of org.mycore.datamodel.metadata.MCRDerivate in project mycore by MyCoRe-Org.
the class MCRDerivateCommands method synchronizeDerivateForID.
/**
* The method sychronize the xlink:label of the mycorederivate with the
* xlink:label of the derivate reference of mycoreobject.
*
* @param id
* the MCRObjectID as String
*/
@MCRCommand(syntax = "synchronize derivate with ID {0}", help = "The command read a derivate with the MCRObjectID {0} and synchronize the xlink:label with the derivate entry of the mycoreobject.", order = 170)
public static void synchronizeDerivateForID(String id) {
MCRObjectID mid = null;
try {
mid = MCRObjectID.getInstance(id);
} catch (Exception e) {
LOGGER.error("The String {} is not a MCRObjectID.", id);
return;
}
// set mycoreobject
MCRDerivate der = MCRMetadataManager.retrieveMCRDerivate(mid);
String label = der.getLabel();
String href = der.getDerivate().getMetaLink().getXLinkHref();
MCRObject obj = MCRMetadataManager.retrieveMCRObject(MCRObjectID.getInstance(href));
int size = obj.getStructure().getDerivates().size();
boolean isset = false;
for (int i = 0; i < size; i++) {
MCRMetaLinkID link = obj.getStructure().getDerivates().get(i);
if (link.getXLinkHref().equals(mid.toString())) {
String oldlabel = link.getXLinkLabel();
if (oldlabel != null && !oldlabel.trim().equals(label)) {
obj.getStructure().getDerivates().get(i).setXLinkTitle(label);
isset = true;
}
break;
}
}
// update mycoreobject
if (isset) {
MCRMetadataManager.fireUpdateEvent(obj);
LOGGER.info("Synchronized {}", mid);
}
}
Aggregations