use of org.mycore.datamodel.metadata.MCRObjectID in project mycore by MyCoRe-Org.
the class MCRAccessManager method checkPermissionForReadingDerivate.
/**
* checks whether the current user has the permission to read/see a derivate check is also against the mcrobject,
* the derivate belongs to both checks must return true <br>
* it is needed in MCRFileNodeServlet and MCRZipServlet
*
* @param derID
* String ID of a MyCoRe-Derivate
* @return true if the access is allowed otherwise it return false
*/
public static boolean checkPermissionForReadingDerivate(String derID) {
// derID must be a derivate ID
boolean accessAllowed = false;
MCRObjectID objectId = MCRMetadataManager.getObjectId(MCRObjectID.getInstance(derID), 10, TimeUnit.MINUTES);
if (objectId != null) {
accessAllowed = checkPermission(objectId, PERMISSION_READ) && checkPermission(derID, PERMISSION_READ);
} else {
accessAllowed = checkPermission(derID, PERMISSION_READ);
LogManager.getLogger("MCRAccessManager.class").warn("no mcrobject could be found for derivate: {}", derID);
}
return accessAllowed;
}
use of org.mycore.datamodel.metadata.MCRObjectID in project mycore by MyCoRe-Org.
the class MCRCreatorRuleStrategy method isCreatorRuleAvailable.
public boolean isCreatorRuleAvailable(String id, String permission) {
if (MCRAccessManager.PERMISSION_WRITE.equals(permission)) {
MCRObjectID mcrObjectId = null;
try {
mcrObjectId = MCRObjectID.getInstance(id);
MCRUserInformation currentUser = MCRSessionMgr.getCurrentSession().getUserInformation();
if (currentUser.isUserInRole(CREATOR_ROLE) && objectStatusIsSubmitted(mcrObjectId)) {
if (isCurrentUserCreator(mcrObjectId, currentUser)) {
return true;
}
}
} catch (RuntimeException e) {
if (mcrObjectId == null) {
LOGGER.debug("id is not a valid object ID", e);
} else {
LOGGER.warn("Eror while checking permission.", e);
}
}
}
return false;
}
use of org.mycore.datamodel.metadata.MCRObjectID 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.MCRObjectID in project mycore by MyCoRe-Org.
the class MCRDerivateCommands method exportDerivate.
/**
* @param dir
* @param trans
* @param nid
* @throws FileNotFoundException
* @throws TransformerException
* @throws IOException
*/
private static void exportDerivate(File dir, Transformer trans, String nid) throws TransformerException, IOException {
// store the XML file
Document xml = null;
MCRDerivate obj;
MCRObjectID derivateID = MCRObjectID.getInstance(nid);
try {
obj = MCRMetadataManager.retrieveMCRDerivate(derivateID);
String path = obj.getDerivate().getInternals().getSourcePath();
// reset from the absolute to relative path, for later reload
LOGGER.info("Old Internal Path ====>{}", path);
obj.getDerivate().getInternals().setSourcePath(nid);
LOGGER.info("New Internal Path ====>{}", nid);
// add ACL's
Collection<String> l = ACCESS_IMPL.getPermissionsForID(nid);
for (String permission : l) {
Element rule = ACCESS_IMPL.getRule(nid, permission);
obj.getService().addRule(permission, rule);
}
// build JDOM
xml = obj.createXML();
} catch (MCRException ex) {
LOGGER.warn("Could not read {}, continue with next ID", nid);
return;
}
File xmlOutput = new File(dir, derivateID + ".xml");
FileOutputStream out = new FileOutputStream(xmlOutput);
dir = new File(dir, derivateID.toString());
if (trans != null) {
trans.setParameter("dirname", dir.getPath());
StreamResult sr = new StreamResult(out);
trans.transform(new org.jdom2.transform.JDOMSource(xml), sr);
} else {
new org.jdom2.output.XMLOutputter().output(xml, out);
out.flush();
out.close();
}
LOGGER.info("Object {} stored under {}.", nid, xmlOutput);
// store the derivate file under dirname
if (!dir.isDirectory()) {
dir.mkdir();
}
MCRPath rootPath = MCRPath.getPath(derivateID.toString(), "/");
Files.walkFileTree(rootPath, new MCRTreeCopier(rootPath, dir.toPath()));
LOGGER.info("Derivate {} saved under {} and {}.", nid, dir, xmlOutput);
}
use of org.mycore.datamodel.metadata.MCRObjectID 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);
}
Aggregations