use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCRObjectUtils method restore.
/**
* Restores a MyCoRe Object to the selected revision. Please note that children and derivates
* are not deleted or reverted!
*
* @param mcrId the mycore object identifier
* @param revision The revision to restore to. If this is lower than zero, the last revision is used.
* @return the new {@link MCRObject}
*
* @throws IOException An error occurred while retrieving the revision information. This is most
* likely due an svn error.
* @throws MCRPersistenceException There is no such object with the given id and revision.
* @throws ClassCastException The returning type must be the same as the type of the restored object
*/
public static <T extends MCRBase> T restore(MCRObjectID mcrId, Long revision) throws IOException, MCRPersistenceException {
@SuppressWarnings("unchecked") T mcrBase = (T) (mcrId.getTypeId().equals("derivate") ? new MCRDerivate() : new MCRObject());
// get content
MCRXMLMetadataManager xmlMetadataManager = MCRXMLMetadataManager.instance();
MCRContent content = xmlMetadataManager.retrieveContent(mcrId, revision);
if (content == null) {
throw new MCRPersistenceException("No such object " + mcrId + " with revision " + revision + ".");
}
// store it
try {
mcrBase.setFromJDOM(content.asXML());
if (MCRMetadataManager.exists(mcrId)) {
MCRMetadataManager.update(mcrBase);
} else {
if (mcrBase instanceof MCRObject) {
MCRMetadataManager.create((MCRObject) mcrBase);
} else {
MCRMetadataManager.create((MCRDerivate) mcrBase);
}
}
return mcrBase;
} catch (Exception exc) {
throw new MCRException("Unable to get object " + mcrId + " with revision " + revision + ".", exc);
}
}
use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCRDefaultMetadataShareAgent method shareableMetadataChanged.
/* (non-Javadoc)
* @see org.mycore.datamodel.metadata.share.MCRMetadataShareAgent#inheritableMetadataChanged(org.mycore.datamodel.metadata.MCRObject, org.mycore.datamodel.metadata.MCRObject)
*/
@Override
public boolean shareableMetadataChanged(MCRObject oldVersion, MCRObject newVersion) {
final MCRObjectMetadata md = newVersion.getMetadata();
final MCRObjectMetadata mdold = oldVersion.getMetadata();
final Element newXML = md.createXML();
Element oldXML = null;
try {
oldXML = mdold.createXML();
} catch (MCRException exc) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("The old metadata of the object {} was invalid.", oldVersion.getId(), exc);
}
}
// TODO: handle inheritance of mycore-mods in that component
if (oldXML != null && MCRXMLHelper.deepEqual(newXML, oldXML)) {
return false;
}
int numheritablemd = 0;
int numheritablemdold;
for (int i = 0; i < md.size(); i++) {
final MCRMetaElement melm = md.getMetadataElement(i);
if (melm.isHeritable()) {
numheritablemd++;
try {
final MCRMetaElement melmold = mdold.getMetadataElement(melm.getTag());
final Element jelm = melm.createXML(true);
Element jelmold = null;
try {
jelmold = melmold.createXML(true);
} catch (MCRException exc) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("One of the old metadata elements is invalid.", exc);
}
}
if (jelmold == null || !MCRXMLHelper.deepEqual(jelmold, jelm)) {
return true;
}
} catch (final RuntimeException e) {
return true;
}
}
}
numheritablemdold = (int) StreamSupport.stream(mdold.spliterator(), false).filter(MCRMetaElement::isHeritable).count();
return numheritablemd != numheritablemdold;
}
use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCREditorOutValidator method setDefaultObjectACLs.
/**
* The method add a default ACL-block.
*
* @param service
* @throws IOException
* @throws JDOMException
*/
private void setDefaultObjectACLs(org.jdom2.Element service) throws JDOMException, IOException {
if (!MCRConfiguration.instance().getBoolean("MCR.Access.AddObjectDefaultRule", true)) {
LOGGER.info("Adding object default acl rule is disabled.");
return;
}
String resourcetype = "/editor_default_acls_" + id.getTypeId() + ".xml";
String resourcebase = "/editor_default_acls_" + id.getBase() + ".xml";
// Read stylesheet and add user
InputStream aclxml = MCREditorOutValidator.class.getResourceAsStream(resourcebase);
if (aclxml == null) {
aclxml = MCREditorOutValidator.class.getResourceAsStream(resourcetype);
if (aclxml == null) {
LOGGER.warn("Can't find default object ACL file {} or {}", resourcebase.substring(1), resourcetype.substring(1));
// fallback
String resource = "/editor_default_acls.xml";
aclxml = MCREditorOutValidator.class.getResourceAsStream(resource);
if (aclxml == null) {
return;
}
}
}
Document xml = SAX_BUILDER.build(aclxml);
Element acls = xml.getRootElement().getChild("servacls");
if (acls == null) {
return;
}
for (Element acl : acls.getChildren()) {
Element condition = acl.getChild("condition");
if (condition == null) {
continue;
}
Element rootbool = condition.getChild("boolean");
if (rootbool == null) {
continue;
}
for (Element orbool : rootbool.getChildren("boolean")) {
for (Element firstcond : orbool.getChildren("condition")) {
if (firstcond == null) {
continue;
}
String value = firstcond.getAttributeValue("value");
if (value == null) {
continue;
}
if (value.equals("$CurrentUser")) {
String thisuser = MCRSessionMgr.getCurrentSession().getUserInformation().getUserID();
firstcond.setAttribute("value", thisuser);
continue;
}
if (value.equals("$CurrentGroup")) {
throw new MCRException("The parameter $CurrentGroup in default ACLs is no more supported since MyCoRe 2014.06 because it is not supported in Servlet API 3.0");
}
int i = value.indexOf("$CurrentIP");
if (i != -1) {
String thisip = MCRSessionMgr.getCurrentSession().getCurrentIP();
firstcond.setAttribute("value", value.substring(0, i) + thisip + value.substring(i + 10, value.length()));
}
}
}
}
service.addContent(acls.detach());
}
use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCREditorOutValidator method getValidatorMap.
private static Map<String, MCREditorMetadataValidator> getValidatorMap() {
Map<String, MCREditorMetadataValidator> map = new HashMap<>();
map.put(MCRMetaBoolean.class.getSimpleName(), getObjectCheckInstance(MCRMetaBoolean.class));
map.put(MCRMetaPersonName.class.getSimpleName(), getObjectCheckWithLangInstance(MCRMetaPersonName.class));
map.put(MCRMetaInstitutionName.class.getSimpleName(), getObjectCheckWithLangInstance(MCRMetaInstitutionName.class));
map.put(MCRMetaAddress.class.getSimpleName(), new MCRMetaAdressCheck());
map.put(MCRMetaNumber.class.getSimpleName(), getObjectCheckWithLangNotEmptyInstance(MCRMetaNumber.class));
map.put(MCRMetaLinkID.class.getSimpleName(), getObjectCheckWithLinksInstance(MCRMetaLinkID.class));
map.put(MCRMetaDerivateLink.class.getSimpleName(), getObjectCheckWithLinksInstance(MCRMetaDerivateLink.class));
map.put(MCRMetaLink.class.getSimpleName(), getObjectCheckWithLinksInstance(MCRMetaLink.class));
map.put(MCRMetaISO8601Date.class.getSimpleName(), getObjectCheckWithLangNotEmptyInstance(MCRMetaISO8601Date.class));
map.put(MCRMetaLangText.class.getSimpleName(), getObjectCheckWithLangNotEmptyInstance(MCRMetaLangText.class));
map.put(MCRMetaAccessRule.class.getSimpleName(), getObjectCheckInstance(MCRMetaAccessRule.class));
map.put(MCRMetaClassification.class.getSimpleName(), new MCRMetaClassificationCheck());
map.put(MCRMetaHistoryDate.class.getSimpleName(), new MCRMetaHistoryDateCheck());
Map<String, String> props = MCRConfiguration.instance().getPropertiesMap(CONFIG_PREFIX + "class.");
for (Entry<String, String> entry : props.entrySet()) {
try {
String className = entry.getKey();
className = className.substring(className.lastIndexOf('.') + 1);
LOGGER.info("Adding Validator {} for class {}", entry.getValue(), className);
@SuppressWarnings("unchecked") Class<? extends MCREditorMetadataValidator> cl = (Class<? extends MCREditorMetadataValidator>) Class.forName(entry.getValue());
map.put(className, cl.getDeclaredConstructor().newInstance());
} catch (Exception e) {
final String msg = "Cannot instantiate " + entry.getValue() + " as validator for class " + entry.getKey();
LOGGER.error(msg);
throw new MCRException(msg, e);
}
}
return map;
}
use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.
the class MCREditorOutValidator method checkMetaTags.
/**
* @param datatag
*/
private boolean checkMetaTags(Element datatag) {
String mcrclass = datatag.getAttributeValue("class");
List<Element> datataglist = datatag.getChildren();
Iterator<Element> datatagIt = datataglist.iterator();
while (datatagIt.hasNext()) {
Element datasubtag = datatagIt.next();
MCREditorMetadataValidator validator = VALIDATOR_MAP.get(mcrclass);
String returns = null;
if (validator != null) {
returns = validator.checkDataSubTag(datasubtag);
} else {
LOGGER.warn("Tag <{}> of type {} has no validator defined, fallback to default behaviour", datatag.getName(), mcrclass);
// try to create MCRMetaInterface instance
try {
Class<? extends MCRMetaInterface> metaClass = getClass(mcrclass);
// just checks if class would validate this element
returns = checkMetaObject(datasubtag, metaClass, true);
} catch (ClassNotFoundException e) {
throw new MCRException("Failure while trying fallback. Class not found: " + mcrclass, e);
}
}
if (returns != null) {
datatagIt.remove();
final String msg = datatag.getName() + ": " + returns;
errorlog.add(msg);
}
}
return datatag.getChildren().size() != 0;
}
Aggregations