use of org.mycore.datamodel.metadata.MCRObject 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);
}
}
use of org.mycore.datamodel.metadata.MCRObject 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.MCRObject 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.MCRObject in project mycore by MyCoRe-Org.
the class MCRObjectCommands method xslt.
private static void xslt(String objectId, String xslFilePath, boolean force) throws IOException, JDOMException, SAXException, URISyntaxException, TransformerException, MCRPersistenceException, MCRAccessException, ParserConfigurationException {
File xslFile = new File(xslFilePath);
URL xslURL;
if (!xslFile.exists()) {
try {
xslURL = new URL(xslFilePath);
} catch (MalformedURLException e) {
LOGGER.error("XSL parameter is not a file or URL: {}", xslFilePath);
return;
}
} else {
xslURL = xslFile.toURI().toURL();
}
MCRObjectID mcrId = MCRObjectID.getInstance(objectId);
Document document = MCRXMLMetadataManager.instance().retrieveXML(mcrId);
// do XSL transform
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setErrorListener(MCRErrorListener.getInstance());
transformerFactory.setURIResolver(MCRURIResolver.instance());
XMLReader xmlReader = MCRXMLParserFactory.getNonValidatingParser().getXMLReader();
xmlReader.setEntityResolver(MCREntityResolver.instance());
SAXSource styleSource = new SAXSource(xmlReader, new InputSource(xslURL.toURI().toString()));
Transformer transformer = transformerFactory.newTransformer(styleSource);
for (Entry<String, String> property : MCRConfiguration.instance().getPropertiesMap().entrySet()) {
transformer.setParameter(property.getKey(), property.getValue());
}
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "no");
JDOMResult result = new JDOMResult();
transformer.transform(new JDOMSource(document), result);
Document resultDocument = Objects.requireNonNull(result.getDocument(), "Could not get transformation result");
String originalName = document.getRootElement().getName();
String resultName = resultDocument.getRootElement().getName();
if (!force && !originalName.equals(resultName)) {
LOGGER.error("{}: root name '{}' does not match result name '{}'.", objectId, originalName, resultName);
return;
}
// update on diff
if (MCRXMLHelper.deepEqual(document, resultDocument)) {
return;
}
if (resultName.equals(MCRObject.ROOT_NAME)) {
MCRMetadataManager.update(new MCRObject(resultDocument));
} else if (resultName.equals(MCRDerivate.ROOT_NAME)) {
MCRMetadataManager.update(new MCRDerivate(resultDocument));
} else {
LOGGER.error("Unable to transform '{}' because unknown result root name '{}'.", objectId, resultName);
}
}
use of org.mycore.datamodel.metadata.MCRObject in project mycore by MyCoRe-Org.
the class MCRIFSTest method createObject.
public static MCRObject createObject() {
MCRObject object = new MCRObject();
object.setId(MCRObjectID.getNextFreeId("mycore_object"));
object.setSchema("noSchema");
return object;
}
Aggregations