/** * Builds a list with with right content types and media urls assigned of a specific Object * * @param obj the object * @return a list of entrys Media-Type, URL */ public List<Map.Entry<String, URI>> getMediaList(MCRObject obj) { List<Map.Entry<String, URI>> entryList = new ArrayList<>(); Optional<MCRObjectID> derivateIdOptional = MCRMetadataManager.getDerivateIds(obj.getId(), 1, TimeUnit.MINUTES) .stream().findFirst(); derivateIdOptional.ifPresent(derivateId -> { MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(derivateId); String mainDoc = derivate.getDerivate().getInternals().getMainDoc(); MCRPath mainDocumentPath = MCRPath.getPath(derivateId.toString(), mainDoc); try { String contentType = Optional.ofNullable(MCRContentTypes.probeContentType(mainDocumentPath)) .orElse("application/octet-stream"); entryList.add(new AbstractMap.SimpleEntry<>(contentType, new URI(this.registerURL + MCRXMLFunctions .encodeURIPath("/servlets/MCRFileNodeServlet/" + derivateId + "/" + mainDoc)))); } catch (IOException | URISyntaxException e) { LOGGER.error("Error while detecting the file to register!", e); } }); return entryList; }
der.getDerivate().getInternals().setIFSID(difs.getID()); der.getDerivate().getInternals().setSourcePath(derDir.toString()); der.getDerivate().getInternals().setMainDoc(maindoc); der.getDerivate().getInternals().setMainDoc(path);
derivate.getDerivate().setLinkMeta(linkId); MCRMetaIFS ifs = new MCRMetaIFS(); ifs.setSubTag("internal"); ifs.setSourcePath(null); ifs.setMainDoc(der.getMainDoc()); derivate.getDerivate().setInternals(ifs);
private static MCRDerivate createDerivate(MCRObjectID documentID, File fileDir) throws MCRPersistenceException, IOException, MCRAccessException { MCRDerivate derivate = new MCRDerivate(); derivate.setId(MCRObjectID.getNextFreeId(documentID.getProjectId(), "derivate")); derivate.setLabel("data object from " + documentID); String schema = CONFIG.getString("MCR.Metadata.Config.derivate", "datamodel-derivate.xml").replaceAll(".xml", ".xsd"); derivate.setSchema(schema); MCRMetaLinkID linkId = new MCRMetaLinkID(); linkId.setSubTag("linkmeta"); linkId.setReference(documentID, null, null); derivate.getDerivate().setLinkMeta(linkId); MCRMetaIFS ifs = new MCRMetaIFS(); ifs.setSubTag("internal"); ifs.setSourcePath(fileDir.getAbsolutePath()); derivate.getDerivate().setInternals(ifs); LOGGER.debug("Creating new derivate with ID {}", derivate.getId()); MCRMetadataManager.create(derivate); setDefaultPermissions(derivate.getId()); return derivate; }
mcrDerivate.getDerivate().setLinkMeta(new MCRMetaLinkID("linkmeta", mcrObjIDObj, null, null)); mcrDerivate.getDerivate() .setInternals(new MCRMetaIFS("internal", UPLOAD_DIR.resolve(derID.toString()).toString()));
/** * Sets a main file if not present. * @param derivateID the id of the derivate */ private static void setDefaultMainFile(String derivateID) { MCRPath path = MCRPath.getPath(derivateID, "/"); try { MCRFileCollectingFileVisitor<Path> visitor = new MCRFileCollectingFileVisitor<>(); Files.walkFileTree(path, visitor); MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivateID)); visitor.getPaths().stream() .map(MCRPath.class::cast) .filter(p -> !p.getOwnerRelativePath().endsWith(".xml")) .findFirst() .ifPresent(file -> { derivate.getDerivate().getInternals().setMainDoc(file.getOwnerRelativePath()); try { MCRMetadataManager.update(derivate); } catch (MCRPersistenceException | MCRAccessException e) { LOGGER.error("Could not set main file!", e); } }); } catch (IOException e) { LOGGER.error("Could not set main file!", e); } } }
derivate.getDerivate().setLinkMeta(linkId); MCRMetaIFS ifs = new MCRMetaIFS(); ifs.setSubTag("internal"); ifs.setSourcePath(null);
.map(d -> { String nameOfMainFile = MCRMetadataManager.retrieveMCRDerivate(d).getDerivate().getInternals() .getMainDoc(); return nameOfMainFile.isEmpty() ? null : MCRPath.getPath(d.toString(), '/' + nameOfMainFile); })
/** * @param derivateID * ID of derivate * @return empty String or absolute path to main file of derivate if file is supported. */ public static String getSupportedMainFile(String derivateID) { try { MCRDerivate deriv = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivateID)); String nameOfMainFile = deriv.getDerivate().getInternals().getMainDoc(); // verify support if (nameOfMainFile != null && !nameOfMainFile.equals("")) { MCRPath mainFile = MCRPath.getPath(derivateID, '/' + nameOfMainFile); if (isFileSupported(mainFile)) { return mainFile.getRoot().relativize(mainFile).toString(); } } } catch (Exception e) { LOGGER.warn("Could not get main file of derivate.", e); } return ""; }
/** * returns the URL of the main document of a derivate * * @param info - the Jersey UriInfo object * @param mcrObjID - the MyCoRe Object ID * @param mcrDerID - the MyCoRe Derivate ID * * @return the Resolving URL for the main document of the derivate * @throws IOException */ public static String retrieveMaindocURL(UriInfo info, String mcrObjID, String mcrDerID, Application app) throws IOException { try { MCRObjectID mcrObj = MCRObjectID.getInstance(mcrObjID); MCRDerivate derObj = retrieveMCRDerivate(mcrObj, mcrDerID); String maindoc = derObj.getDerivate().getInternals().getMainDoc(); String baseURL = MCRJerseyUtil.getBaseURL(info, app) + MCRConfiguration.instance().getString("MCR.RestAPI.v1.Files.URL.path"); baseURL = baseURL.replace("${mcrid}", mcrObj.toString()).replace("${derid}", derObj.getId().toString()); return baseURL + maindoc; } catch (MCRRestAPIException rae) { return null; } }