use of org.mycore.datamodel.metadata.MCRMetaLinkID in project mycore by MyCoRe-Org.
the class MCRDerivateCommands method linkDerivateToObject.
/**
* Links the given derivate to the given object.
*/
@MCRCommand(syntax = "link derivate {0} to {1}", help = "links the given derivate {0} to the given mycore object {1}", order = 180)
public static void linkDerivateToObject(String derivateId, String objectId) throws Exception {
if (derivateId == null || objectId == null) {
LOGGER.error("Either derivate id or object id is null. Derivate={}, object={}", derivateId, objectId);
return;
}
MCRObjectID derID = MCRObjectID.getInstance(derivateId);
MCRObjectID objID = MCRObjectID.getInstance(objectId);
if (!MCRMetadataManager.exists(objID)) {
throw new Exception("The object with id " + objID + " does not exist");
}
if (!MCRMetadataManager.exists(derID)) {
throw new Exception("The derivate with id " + derID + " does not exist");
}
MCRDerivate derObj = MCRMetadataManager.retrieveMCRDerivate(derID);
MCRMetaLinkID oldDerivateToObjectLink = derObj.getDerivate().getMetaLink();
MCRObjectID oldOwnerId = oldDerivateToObjectLink.getXLinkHrefID();
/* set link to new parent in the derivate object */
LOGGER.info("Setting {} as parent for derivate {}", objID, derID);
derObj.getDerivate().getMetaLink().setReference(objID, oldDerivateToObjectLink.getXLinkLabel(), oldDerivateToObjectLink.getXLinkTitle());
derObj.setLabel("data object from " + objectId + " (prev. owner was " + oldOwnerId);
MCRMetadataManager.updateMCRDerivateXML(derObj);
/* set link to derivate in the new parent */
MCRObject oldOwner = MCRMetadataManager.retrieveMCRObject(oldOwnerId);
List<MCRMetaLinkID> derivates = oldOwner.getStructure().getDerivates();
MCRMetaLinkID oldObjectToDerivateLink = null;
for (MCRMetaLinkID derivate : derivates) {
if (derivate.getXLinkHrefID().equals(derID)) {
oldObjectToDerivateLink = derivate;
}
}
if (oldObjectToDerivateLink == null) {
oldObjectToDerivateLink = new MCRMetaLinkID();
}
LOGGER.info("Linking derivate {} to {}", derID, objID);
MCRMetaLinkID derivateLink = new MCRMetaLinkID();
derivateLink.setReference(derID, oldObjectToDerivateLink.getXLinkLabel(), oldObjectToDerivateLink.getXLinkTitle());
derivateLink.setSubTag("derobject");
MCRMetadataManager.addOrUpdateDerivateToObject(objID, derivateLink);
/* removing link from old parent */
boolean flag = oldOwner.getStructure().removeDerivate(derID);
LOGGER.info("Unlinking derivate {} from object {}. Success={}", derID, oldOwnerId, flag);
MCRMetadataManager.fireUpdateEvent(oldOwner);
}
use of org.mycore.datamodel.metadata.MCRMetaLinkID in project mycore by MyCoRe-Org.
the class MCRObjectCommands method checkDerivatesInObjects.
/**
* Check the derivate links in objects of MCR base ID for existing. It looks to the XML store on the disk to get all
* object IDs.
*
* @param base_id
* the base part of a MCRObjectID e.g. DocPortal_document
*/
@MCRCommand(syntax = "check derivate entries in objects for base {0}", help = "check in all objects with MCR base ID {0} for existing linked derivates", order = 400)
public static void checkDerivatesInObjects(String base_id) throws IOException {
if (base_id == null || base_id.length() == 0) {
LOGGER.error("Base ID missed for check derivate entries in objects for base {0}");
return;
}
MCRXMLMetadataManager mgr = MCRXMLMetadataManager.instance();
List<String> id_list = mgr.listIDsForBase(base_id);
int counter = 0;
int maxresults = id_list.size();
for (String objid : id_list) {
counter++;
LOGGER.info("Processing dataset {} from {} with ID: {}", counter, maxresults, objid);
// get from data
MCRObjectID mcrobjid = MCRObjectID.getInstance(objid);
MCRObject obj = MCRMetadataManager.retrieveMCRObject(mcrobjid);
List<MCRMetaLinkID> derivate_entries = obj.getStructure().getDerivates();
for (MCRMetaLinkID derivate_entry : derivate_entries) {
String derid = derivate_entry.getXLinkHref();
if (!mgr.exists(MCRObjectID.getInstance(derid))) {
LOGGER.error(" !!! Missing derivate {} in database for base ID {}", derid, base_id);
}
}
}
LOGGER.info("Check done for {} entries", Integer.toString(counter));
}
use of org.mycore.datamodel.metadata.MCRMetaLinkID 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;
}
use of org.mycore.datamodel.metadata.MCRMetaLinkID in project mycore by MyCoRe-Org.
the class MCRTransferPackageUtil method importObjectCLI.
/**
* Same as {@link #importObject(Path, String)} but returns a list of derivates which
* should be imported afterwards.
*
* @param targetDirectory
* the directory where the *.tar was unpacked
* @param objectId
* object id to import
* @throws JDOMException
* coulnd't parse the import order xml
* @throws IOException
* when an I/O error prevents a document from being fully parsed
* @throws MCRActiveLinkException
* if object is created (no real update), see {@link MCRMetadataManager#create(MCRObject)}
* @throws MCRAccessException
* if write permission is missing or see {@link MCRMetadataManager#create(MCRObject)}
*/
public static List<String> importObjectCLI(Path targetDirectory, String objectId) throws JDOMException, IOException, MCRActiveLinkException, MCRAccessException {
SAXBuilder sax = new SAXBuilder();
Path targetXML = targetDirectory.resolve(CONTENT_DIRECTORY).resolve(objectId + ".xml");
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(MessageFormat.format("Importing {0}", targetXML.toAbsolutePath().toString()));
}
Document objXML = sax.build(targetXML.toFile());
MCRObject mcr = new MCRObject(objXML);
mcr.setImportMode(true);
List<String> derivates = new LinkedList<>();
// one must copy the ids before updating the mcr objects
for (MCRMetaLinkID id : mcr.getStructure().getDerivates()) {
derivates.add(id.getXLinkHref());
}
// delete children & derivate -> will be added later
mcr.getStructure().clearChildren();
mcr.getStructure().clearDerivates();
// update
MCRMetadataManager.update(mcr);
// return list of derivates
return derivates;
}
use of org.mycore.datamodel.metadata.MCRMetaLinkID in project mycore by MyCoRe-Org.
the class MCRUpdateDerivateServlet method updateDerivateXML.
/**
* Updates derivate xml in the persistence backend
* @param editorSubmission
* MyCoRe derivate as XML
* @return
* MCRObjectID of the MyCoRe object
* @throws SAXParseException
* @throws MCRAccessException
*/
private MCRObjectID updateDerivateXML(Document editorSubmission) throws SAXParseException, IOException, MCRAccessException {
MCRObjectID objectID;
Element root = editorSubmission.getRootElement();
root.setAttribute("noNamespaceSchemaLocation", "datamodel-derivate.xsd", XSI_NAMESPACE);
root.addNamespaceDeclaration(XLINK_NAMESPACE);
root.addNamespaceDeclaration(XSI_NAMESPACE);
byte[] xml = new MCRJDOMContent(editorSubmission).asByteArray();
MCRDerivate der = new MCRDerivate(xml, true);
MCRObjectID derivateID = der.getId();
// store entry of derivate xlink:title in object
objectID = der.getDerivate().getMetaLink().getXLinkHrefID();
MCRObject obj = MCRMetadataManager.retrieveMCRObject(objectID);
MCRObjectStructure structure = obj.getStructure();
MCRMetaLinkID linkID = structure.getDerivateLink(derivateID);
linkID.setXLinkTitle(der.getLabel());
try {
MCRMetadataManager.update(obj);
MCRMetadataManager.update(der);
} catch (MCRPersistenceException | MCRAccessException e) {
throw new MCRPersistenceException("Can't store label of derivate " + derivateID + " in derivate list of object " + objectID + ".", e);
}
return objectID;
}
Aggregations