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, "");
}
}
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;
}
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);
}
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;
}
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);
}
}
Aggregations