Search in sources :

Example 1 with MCRMetaLinkID

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

the class MCRLinkTableManager method create.

/**
 * Creates all references for the given object. You should call {@link #delete(MCRObjectID)} before using this
 * method otherwise doublets could occur.
 *
 * @param obj the object to create the references
 */
public void create(MCRObject obj) {
    MCRObjectID mcrId = obj.getId();
    // set new entries
    MCRObjectMetadata meta = obj.getMetadata();
    // use Set for category collection to remove duplicates if there are any
    Collection<MCRCategoryID> categories = new HashSet<>();
    meta.stream().flatMap(MCRMetaElement::stream).forEach(inf -> {
        if (inf instanceof MCRMetaClassification) {
            String classId = ((MCRMetaClassification) inf).getClassId();
            String categId = ((MCRMetaClassification) inf).getCategId();
            categories.add(new MCRCategoryID(classId, categId));
        } else if (inf instanceof MCRMetaLinkID) {
            addReferenceLink(mcrId.toString(), ((MCRMetaLink) inf).getXLinkHref(), MCRLinkTableManager.ENTRY_TYPE_REFERENCE, "");
        } else if (inf instanceof MCRMetaDerivateLink) {
            addReferenceLink(mcrId.toString(), ((MCRMetaLink) inf).getXLinkHref(), MCRLinkTableManager.ENTRY_TYPE_DERIVATE_LINK, "");
        }
    });
    MCRCategoryID state = obj.getService().getState();
    if (state != null) {
        categories.add(state);
    }
    if (categories.size() > 0) {
        MCRCategLinkReference objectReference = new MCRCategLinkReference(mcrId);
        MCRCategLinkServiceFactory.getInstance().setLinks(objectReference, categories);
    }
    // add derivate reference
    MCRObjectStructure structure = obj.getStructure();
    for (int i = 0; i < structure.getDerivates().size(); i++) {
        MCRMetaLinkID lid = structure.getDerivates().get(i);
        addReferenceLink(obj.getId(), lid.getXLinkHrefID(), MCRLinkTableManager.ENTRY_TYPE_DERIVATE, "");
    }
    // add parent reference
    if (structure.getParentID() != null) {
        addReferenceLink(mcrId, structure.getParentID(), MCRLinkTableManager.ENTRY_TYPE_PARENT, "");
    }
}
Also used : MCRMetaClassification(org.mycore.datamodel.metadata.MCRMetaClassification) MCRMetaDerivateLink(org.mycore.datamodel.metadata.MCRMetaDerivateLink) MCRObjectMetadata(org.mycore.datamodel.metadata.MCRObjectMetadata) MCRMetaLink(org.mycore.datamodel.metadata.MCRMetaLink) MCRObjectStructure(org.mycore.datamodel.metadata.MCRObjectStructure) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCategLinkReference(org.mycore.datamodel.classifications2.MCRCategLinkReference) HashSet(java.util.HashSet)

Example 2 with MCRMetaLinkID

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

the class MCRExtractRelatedItemsEventHandler method createRelatedObject.

private MCRObjectID createRelatedObject(Element relatedItem, MCRObjectID childID) throws MCRPersistenceException, MCRAccessException {
    MCRMODSWrapper wrapper = new MCRMODSWrapper();
    MCRObject object = wrapper.getMCRObject();
    MCRObjectID oid = MCRObjectID.getNextFreeId(childID.getBase());
    if (oid.equals(childID)) {
        oid = MCRObjectID.getNextFreeId(childID.getBase());
    }
    object.setId(oid);
    if (isHost(relatedItem)) {
        object.getStructure().addChild(new MCRMetaLinkID("child", childID, childID.toString(), childID.toString()));
    }
    Element mods = cloneRelatedItem(relatedItem);
    wrapper.setMODS(mods);
    LOGGER.info("create object {}", oid);
    MCRMetadataManager.create(object);
    return oid;
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) Element(org.jdom2.Element) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Example 3 with MCRMetaLinkID

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

the class MCRRestAPIObjectsHelper method retrieveMCRDerivate.

private static MCRDerivate retrieveMCRDerivate(MCRObject mcrObj, String derIDString) throws MCRRestAPIException {
    // the default value for the key
    String derKey = "mcr";
    if (derIDString.contains(":")) {
        int pos = derIDString.indexOf(":");
        derKey = derIDString.substring(0, pos);
        derIDString = derIDString.substring(pos + 1);
        if (!derKey.equals("mcr") && !derKey.equals("label")) {
            throw new MCRRestAPIException(Response.Status.BAD_REQUEST, new MCRRestAPIError(MCRRestAPIError.CODE_WRONG_ID, "The ID is not valid.", "The prefix is unkown. Only 'mcr' or 'label' are allowed."));
        }
    }
    String matchedDerID = null;
    for (MCRMetaLinkID check : mcrObj.getStructure().getDerivates()) {
        if (derKey.equals("mcr")) {
            if (check.getXLinkHref().equals(derIDString)) {
                matchedDerID = check.getXLinkHref();
                break;
            }
        }
        if (derKey.equals("label")) {
            if (derIDString.equals(check.getXLinkLabel()) || derIDString.equals(check.getXLinkTitle())) {
                matchedDerID = check.getXLinkHref();
                break;
            }
        }
    }
    if (matchedDerID == null) {
        throw new MCRRestAPIException(Response.Status.NOT_FOUND, new MCRRestAPIError(MCRRestAPIError.CODE_NOT_FOUND, "Derivate " + derIDString + " not found.", "The MyCoRe Object with id '" + mcrObj.getId() + "' does not contain a derivate with id '" + derIDString + "'."));
    }
    MCRObjectID derID = MCRObjectID.getInstance(matchedDerID);
    if (!MCRMetadataManager.exists(derID)) {
        throw new MCRRestAPIException(Response.Status.NOT_FOUND, new MCRRestAPIError(MCRRestAPIError.CODE_NOT_FOUND, "There is no derivate with the id '" + matchedDerID + "'.", null));
    }
    return MCRMetadataManager.retrieveMCRDerivate(derID);
}
Also used : MCRRestAPIException(org.mycore.restapi.v1.errors.MCRRestAPIException) MCRRestAPIError(org.mycore.restapi.v1.errors.MCRRestAPIError) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Example 4 with MCRMetaLinkID

use of org.mycore.datamodel.metadata.MCRMetaLinkID 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 5 with MCRMetaLinkID

use of org.mycore.datamodel.metadata.MCRMetaLinkID 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);
    }
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) JDOMException(org.jdom2.JDOMException) MCRPersistenceException(org.mycore.common.MCRPersistenceException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) TransformerException(javax.xml.transform.TransformerException) MCRException(org.mycore.common.MCRException) MCRAccessException(org.mycore.access.MCRAccessException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Aggregations

MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)22 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)15 MCRObject (org.mycore.datamodel.metadata.MCRObject)13 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)10 MCRAccessException (org.mycore.access.MCRAccessException)7 IOException (java.io.IOException)6 MCRPersistenceException (org.mycore.common.MCRPersistenceException)6 MCRMetaIFS (org.mycore.datamodel.metadata.MCRMetaIFS)6 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)5 Element (org.jdom2.Element)4 MCRException (org.mycore.common.MCRException)4 TransformerException (javax.xml.transform.TransformerException)3 JDOMException (org.jdom2.JDOMException)3 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)3 MCRPath (org.mycore.datamodel.niofs.MCRPath)3 MCRRestAPIException (org.mycore.restapi.v1.errors.MCRRestAPIException)3 SAXException (org.xml.sax.SAXException)3 SAXParseException (org.xml.sax.SAXParseException)3 FileNotFoundException (java.io.FileNotFoundException)2 Document (org.jdom2.Document)2