Search in sources :

Example 6 with MCRException

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);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) MCRContent(org.mycore.common.content.MCRContent) MCRPersistenceException(org.mycore.common.MCRPersistenceException) MCRPersistenceException(org.mycore.common.MCRPersistenceException) IOException(java.io.IOException) MCRException(org.mycore.common.MCRException)

Example 7 with MCRException

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;
}
Also used : MCRMetaElement(org.mycore.datamodel.metadata.MCRMetaElement) MCRException(org.mycore.common.MCRException) MCRMetaElement(org.mycore.datamodel.metadata.MCRMetaElement) Element(org.jdom2.Element) MCRObjectMetadata(org.mycore.datamodel.metadata.MCRObjectMetadata)

Example 8 with MCRException

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());
}
Also used : MCRException(org.mycore.common.MCRException) InputStream(java.io.InputStream) Element(org.jdom2.Element) Document(org.jdom2.Document)

Example 9 with MCRException

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;
}
Also used : MCRMetaISO8601Date(org.mycore.datamodel.metadata.MCRMetaISO8601Date) MCRMetaLangText(org.mycore.datamodel.metadata.MCRMetaLangText) HashMap(java.util.HashMap) MCRMetaClassification(org.mycore.datamodel.metadata.MCRMetaClassification) MCRMetaInstitutionName(org.mycore.datamodel.metadata.MCRMetaInstitutionName) MCRMetaNumber(org.mycore.datamodel.metadata.MCRMetaNumber) MCRMetaLink(org.mycore.datamodel.metadata.MCRMetaLink) MCRMetaBoolean(org.mycore.datamodel.metadata.MCRMetaBoolean) MCRMetaAccessRule(org.mycore.datamodel.metadata.MCRMetaAccessRule) MCRMetaHistoryDate(org.mycore.datamodel.metadata.MCRMetaHistoryDate) MCRMetaPersonName(org.mycore.datamodel.metadata.MCRMetaPersonName) MCRMetaAddress(org.mycore.datamodel.metadata.MCRMetaAddress) MCRException(org.mycore.common.MCRException) MCRMetaDerivateLink(org.mycore.datamodel.metadata.MCRMetaDerivateLink) MCRException(org.mycore.common.MCRException) JDOMException(org.jdom2.JDOMException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID)

Example 10 with MCRException

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;
}
Also used : MCRException(org.mycore.common.MCRException) Element(org.jdom2.Element)

Aggregations

MCRException (org.mycore.common.MCRException)131 IOException (java.io.IOException)39 Element (org.jdom2.Element)26 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)19 Document (org.jdom2.Document)18 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)18 File (java.io.File)15 MCRConfigurationException (org.mycore.common.config.MCRConfigurationException)12 MCRObject (org.mycore.datamodel.metadata.MCRObject)12 ArrayList (java.util.ArrayList)11 JDOMException (org.jdom2.JDOMException)11 MCRAccessException (org.mycore.access.MCRAccessException)11 MCRPath (org.mycore.datamodel.niofs.MCRPath)10 SAXException (org.xml.sax.SAXException)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 List (java.util.List)7 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)7 SAXParseException (org.xml.sax.SAXParseException)7 URI (java.net.URI)6 Path (java.nio.file.Path)6