Search in sources :

Example 1 with MCRMetaIFS

use of org.mycore.datamodel.metadata.MCRMetaIFS in project mycore by MyCoRe-Org.

the class MCRRestAPIUploadHelper method uploadDerivate.

/**
 * creates or updates a MyCoRe derivate
 * @param info - the Jersey UriInfo object
 * @param request - the HTTPServletRequest object
 * @param mcrObjID - the MyCoRe Object ID
 * @param label - the label of the new derivate
 * @param overwriteOnExistingLabel, if true an existing MyCoRe derivate with the given label will be returned
 * @return a Jersey Response object
 * @throws MCRRestAPIException
 */
public static Response uploadDerivate(UriInfo info, HttpServletRequest request, String mcrObjID, String label, boolean overwriteOnExistingLabel) throws MCRRestAPIException {
    Response response = Response.status(Status.INTERNAL_SERVER_ERROR).build();
    SignedJWT signedJWT = MCRJSONWebTokenUtil.retrieveAuthenticationToken(request);
    // File fXML = null;
    MCRObjectID mcrObjIDObj = MCRObjectID.getInstance(mcrObjID);
    try (MCRJPATransactionWrapper mtw = new MCRJPATransactionWrapper()) {
        MCRSession session = MCRServlet.getSession(request);
        MCRUserInformation currentUser = session.getUserInformation();
        MCRUserInformation apiUser = MCRUserManager.getUser(MCRJSONWebTokenUtil.retrieveUsernameFromAuthenticationToken(signedJWT));
        session.setUserInformation(apiUser);
        MCRObject mcrObj = MCRMetadataManager.retrieveMCRObject(mcrObjIDObj);
        MCRObjectID derID = null;
        if (overwriteOnExistingLabel) {
            for (MCRMetaLinkID derLink : mcrObj.getStructure().getDerivates()) {
                if (label.equals(derLink.getXLinkLabel()) || label.equals(derLink.getXLinkTitle())) {
                    derID = derLink.getXLinkHrefID();
                }
            }
        }
        if (derID == null) {
            derID = MCRObjectID.getNextFreeId(mcrObjIDObj.getProjectId() + "_derivate");
            MCRDerivate mcrDerivate = new MCRDerivate();
            mcrDerivate.setLabel(label);
            mcrDerivate.setId(derID);
            mcrDerivate.setSchema("datamodel-derivate.xsd");
            mcrDerivate.getDerivate().setLinkMeta(new MCRMetaLinkID("linkmeta", mcrObjIDObj, null, null));
            mcrDerivate.getDerivate().setInternals(new MCRMetaIFS("internal", UPLOAD_DIR.resolve(derID.toString()).toString()));
            MCRMetadataManager.create(mcrDerivate);
            MCRMetadataManager.addOrUpdateDerivateToObject(mcrObjIDObj, new MCRMetaLinkID("derobject", derID, null, label));
        }
        response = Response.created(info.getBaseUriBuilder().path("v1/objects/" + mcrObjID + "/derivates/" + derID).build()).type("application/xml; charset=UTF-8").header(HEADER_NAME_AUTHORIZATION, MCRJSONWebTokenUtil.createJWTAuthorizationHeader(signedJWT)).build();
        session.setUserInformation(currentUser);
    } catch (Exception e) {
        LOGGER.error("Exeption while uploading derivate", e);
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) MCRSession(org.mycore.common.MCRSession) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) SignedJWT(com.nimbusds.jwt.SignedJWT) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRMetaIFS(org.mycore.datamodel.metadata.MCRMetaIFS) MCRUserInformation(org.mycore.common.MCRUserInformation) MCRPersistenceException(org.mycore.common.MCRPersistenceException) MCRRestAPIException(org.mycore.restapi.v1.errors.MCRRestAPIException) MCRAccessException(org.mycore.access.MCRAccessException) IOException(java.io.IOException)

Example 2 with MCRMetaIFS

use of org.mycore.datamodel.metadata.MCRMetaIFS in project mycore by MyCoRe-Org.

the class MCRBasketPersistence method createNewDerivate.

/**
 * Creates a new, empty derivate.
 *
 * @param ownerID the ID of the object owning the new derivate
 * @param derivateOID a free derivate ID to use for the newly created derivate
 * @return the empty derivate that was created.
 * @throws IOException
 * @throws MCRPersistenceException
 * @throws MCRAccessException see {@link MCRMetadataManager#create(MCRDerivate)}
 */
private static MCRDerivate createNewDerivate(MCRObjectID ownerID, MCRObjectID derivateOID) throws MCRPersistenceException, IOException, MCRAccessException {
    MCRDerivate derivate = new MCRDerivate();
    derivate.setId(derivateOID);
    derivate.setLabel("Saved basket data for " + ownerID);
    String schema = MCRConfiguration.instance().getString("MCR.Metadata.Config.derivate", "datamodel-derivate.xml");
    derivate.setSchema(schema.replaceAll(".xml", ".xsd"));
    MCRMetaIFS ifs = new MCRMetaIFS();
    ifs.setSubTag("internal");
    ifs.setSourcePath(null);
    derivate.getDerivate().setInternals(ifs);
    MCRMetaLinkID linkId = new MCRMetaLinkID();
    linkId.setSubTag("linkmeta");
    linkId.setReference(ownerID, null, null);
    derivate.getDerivate().setLinkMeta(linkId);
    MCRMetadataManager.create(derivate);
    return derivate;
}
Also used : MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRMetaIFS(org.mycore.datamodel.metadata.MCRMetaIFS)

Example 3 with MCRMetaIFS

use of org.mycore.datamodel.metadata.MCRMetaIFS in project mycore by MyCoRe-Org.

the class MCRUploadHandlerIFS method createDerivate.

private MCRDerivate createDerivate(MCRObjectID derivateID) throws MCRPersistenceException, IOException, MCRAccessException {
    MCRDerivate derivate = new MCRDerivate();
    derivate.setId(derivateID);
    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(null);
    derivate.getDerivate().setInternals(ifs);
    LOGGER.debug("Creating new derivate with ID {}", this.derivateID);
    MCRMetadataManager.create(derivate);
    setDefaultPermissions(derivateID);
    return derivate;
}
Also used : MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRMetaIFS(org.mycore.datamodel.metadata.MCRMetaIFS)

Example 4 with MCRMetaIFS

use of org.mycore.datamodel.metadata.MCRMetaIFS in project mycore by MyCoRe-Org.

the class MCRSwordUtil method createDerivate.

public static MCRDerivate createDerivate(String documentID) throws MCRPersistenceException, IOException, MCRAccessException {
    final String projectId = MCRObjectID.getInstance(documentID).getProjectId();
    MCRObjectID oid = MCRObjectID.getNextFreeId(projectId, "derivate");
    final String derivateID = oid.toString();
    MCRDerivate derivate = new MCRDerivate();
    derivate.setId(oid);
    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(null);
    derivate.getDerivate().setInternals(ifs);
    LOGGER.debug("Creating new derivate with ID {}", derivateID);
    MCRMetadataManager.create(derivate);
    if (CONFIG.getBoolean("MCR.Access.AddDerivateDefaultRule", true)) {
        MCRAccessInterface AI = MCRAccessManager.getAccessImpl();
        Collection<String> configuredPermissions = AI.getAccessPermissionsFromConfiguration();
        for (String permission : configuredPermissions) {
            MCRAccessManager.addRule(derivateID, permission, MCRAccessManager.getTrueRule(), "default derivate rule");
        }
    }
    final MCRPath rootDir = MCRPath.getPath(derivateID, "/");
    if (Files.notExists(rootDir)) {
        rootDir.getFileSystem().createRoot(derivateID);
    }
    return derivate;
}
Also used : MCRAccessInterface(org.mycore.access.MCRAccessInterface) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRMetaIFS(org.mycore.datamodel.metadata.MCRMetaIFS) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 5 with MCRMetaIFS

use of org.mycore.datamodel.metadata.MCRMetaIFS in project mycore by MyCoRe-Org.

the class MCRIFSTest method createDerivate.

public static MCRDerivate createDerivate(MCRObjectID objectHrefId) {
    MCRDerivate derivate = new MCRDerivate();
    derivate.setId(MCRObjectID.getNextFreeId("mycore_derivate"));
    derivate.setSchema("datamodel-derivate.xsd");
    MCRMetaIFS ifs = new MCRMetaIFS();
    ifs.setSubTag("internal");
    ifs.setSourcePath(null);
    derivate.getDerivate().setInternals(ifs);
    MCRMetaLinkID mcrMetaLinkID = new MCRMetaLinkID();
    mcrMetaLinkID.setReference(objectHrefId.toString(), null, null);
    derivate.getDerivate().setLinkMeta(mcrMetaLinkID);
    return derivate;
}
Also used : MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRMetaIFS(org.mycore.datamodel.metadata.MCRMetaIFS)

Aggregations

MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)6 MCRMetaIFS (org.mycore.datamodel.metadata.MCRMetaIFS)6 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)6 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)2 SignedJWT (com.nimbusds.jwt.SignedJWT)1 IOException (java.io.IOException)1 Response (javax.ws.rs.core.Response)1 MCRAccessException (org.mycore.access.MCRAccessException)1 MCRAccessInterface (org.mycore.access.MCRAccessInterface)1 MCRPersistenceException (org.mycore.common.MCRPersistenceException)1 MCRSession (org.mycore.common.MCRSession)1 MCRUserInformation (org.mycore.common.MCRUserInformation)1 MCRObject (org.mycore.datamodel.metadata.MCRObject)1 MCRPath (org.mycore.datamodel.niofs.MCRPath)1 MCRRestAPIException (org.mycore.restapi.v1.errors.MCRRestAPIException)1