Search in sources :

Example 76 with MCRException

use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.

the class MCRMetadataManager method update.

/**
 * Updates the derivate or creates it if it does not exist yet.
 *
 * @throws MCRPersistenceException
 *                if a persistence problem is occurred
 * @throws MCRAccessException
 *                if write permission to object or derivate is missing
 */
public static void update(final MCRDerivate mcrDerivate) throws MCRPersistenceException, MCRAccessException {
    MCRObjectID id = mcrDerivate.getId();
    // check deletion mark
    if (MCRMarkManager.instance().isMarkedForDeletion(id)) {
        return;
    }
    if (!MCRMetadataManager.exists(id)) {
        MCRMetadataManager.create(mcrDerivate);
        return;
    }
    if (!MCRAccessManager.checkPermission(id, PERMISSION_WRITE)) {
        throw MCRAccessException.missingPermission("Update derivate", id.toString(), PERMISSION_WRITE);
    }
    File fileSourceDirectory = null;
    if (mcrDerivate.getDerivate().getInternals() != null && mcrDerivate.getDerivate().getInternals().getSourcePath() != null) {
        fileSourceDirectory = new File(mcrDerivate.getDerivate().getInternals().getSourcePath());
        if (!fileSourceDirectory.exists()) {
            LOGGER.warn("{}: the directory {} was not found.", id, fileSourceDirectory);
            fileSourceDirectory = null;
        }
    }
    // get the old Item
    MCRDerivate old = MCRMetadataManager.retrieveMCRDerivate(id);
    // remove the old link to metadata
    MCRMetaLinkID oldLink = old.getDerivate().getMetaLink();
    MCRMetaLinkID newLink = mcrDerivate.getDerivate().getMetaLink();
    if (!oldLink.equals(newLink)) {
        MCRObjectID oldMetadataObjectID = oldLink.getXLinkHrefID();
        MCRObjectID newMetadataObjectID = newLink.getXLinkHrefID();
        if (!oldMetadataObjectID.equals(newLink.getXLinkHrefID())) {
            try {
                MCRMetadataManager.removeDerivateFromObject(oldMetadataObjectID, id);
            } catch (final MCRException e) {
                LOGGER.warn(e.getMessage(), e);
            }
        }
        // add the link to metadata
        final MCRMetaLinkID der = new MCRMetaLinkID("derobject", id, null, mcrDerivate.getLabel(), newLink.getXLinkRole());
        addOrUpdateDerivateToObject(newMetadataObjectID, der);
    }
    // update the derivate
    mcrDerivate.getService().setDate("createdate", old.getService().getDate("createdate"));
    if (!mcrDerivate.getService().isFlagTypeSet(MCRObjectService.FLAG_TYPE_CREATEDBY)) {
        for (String flagCreatedBy : old.getService().getFlags(MCRObjectService.FLAG_TYPE_CREATEDBY)) {
            mcrDerivate.getService().addFlag(MCRObjectService.FLAG_TYPE_CREATEDBY, flagCreatedBy);
        }
    }
    MCRMetadataManager.updateMCRDerivateXML(mcrDerivate);
    // update to IFS
    if (fileSourceDirectory != null) {
        Path sourcePath = fileSourceDirectory.toPath();
        MCRPath targetPath = MCRPath.getPath(id.toString(), "/");
        try {
            Files.walkFileTree(sourcePath, new MCRTreeCopier(sourcePath, targetPath));
        } catch (Exception exc) {
            throw new MCRPersistenceException("Unable to update IFS. Copy failed from " + sourcePath.toAbsolutePath() + " to target " + targetPath.toAbsolutePath(), exc);
        }
    }
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRTreeCopier(org.mycore.datamodel.niofs.utils.MCRTreeCopier) MCRException(org.mycore.common.MCRException) MCRPath(org.mycore.datamodel.niofs.MCRPath) File(java.io.File) MCRPersistenceException(org.mycore.common.MCRPersistenceException) MCRException(org.mycore.common.MCRException) JDOMException(org.jdom2.JDOMException) MCRAccessException(org.mycore.access.MCRAccessException) MCRPersistenceException(org.mycore.common.MCRPersistenceException) IOException(java.io.IOException) PersistenceException(javax.persistence.PersistenceException) SAXException(org.xml.sax.SAXException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException)

Example 77 with MCRException

use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.

the class MCRObject method createXML.

/**
 * This method creates a XML stream for all object data.
 *
 * @exception MCRException
 *                if the content of this class is not valid
 * @return a JDOM Document with the XML data of the object as byte array
 */
@Override
public final Document createXML() throws MCRException {
    try {
        Document doc = super.createXML();
        Element elm = doc.getRootElement();
        elm.addContent(mcr_struct.createXML());
        elm.addContent(mcr_metadata.createXML());
        elm.addContent(mcr_service.createXML());
        return doc;
    } catch (MCRException exc) {
        throw new MCRException("The content of '" + mcr_id + "' is invalid.", exc);
    }
}
Also used : MCRException(org.mycore.common.MCRException) Element(org.jdom2.Element) Document(org.jdom2.Document)

Example 78 with MCRException

use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.

the class MCRBase method setUp.

protected void setUp() {
    if (jdom_document == null) {
        throw new MCRException("The JDOM document is null or empty.");
    }
    org.jdom2.Element rootElement = jdom_document.getRootElement();
    setId(MCRObjectID.getInstance(rootElement.getAttributeValue("ID")));
    setLabel(rootElement.getAttributeValue("label"));
    setVersion(rootElement.getAttributeValue("version"));
    setSchema(rootElement.getAttribute("noNamespaceSchemaLocation", XSI_NAMESPACE).getValue());
    // get the service data of the object
    Element serviceElement = rootElement.getChild("service");
    if (serviceElement != null) {
        mcr_service.setFromDOM(serviceElement);
    }
}
Also used : MCRException(org.mycore.common.MCRException) Element(org.jdom2.Element) Element(org.jdom2.Element)

Example 79 with MCRException

use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.

the class MCRDerivate method validate.

/**
 * Validates this MCRDerivate. This method throws an exception if:
 *  <ul>
 *  <li>the mcr_id is null</li>
 *  <li>the XML schema is null or empty</li>
 *  <li>the service part is null or invalid</li>
 *  <li>the MCRObjectDerivate is null or invalid</li>
 *  </ul>
 *
 * @throws MCRException the MCRDerivate is invalid
 */
@Override
public void validate() throws MCRException {
    super.validate();
    MCRObjectDerivate derivate = getDerivate();
    if (derivate == null) {
        throw new MCRException("The <derivate> part of '" + getId() + "' is undefined.");
    }
    try {
        derivate.validate();
    } catch (Exception exc) {
        throw new MCRException("The <derivate> part of '" + getId() + "' is invalid.", exc);
    }
}
Also used : MCRException(org.mycore.common.MCRException) IOException(java.io.IOException) MCRException(org.mycore.common.MCRException) SAXParseException(org.xml.sax.SAXParseException)

Example 80 with MCRException

use of org.mycore.common.MCRException in project mycore by MyCoRe-Org.

the class MCREditorOutValidator method checkMetaObject.

public static String checkMetaObject(Element datasubtag, Class<? extends MCRMetaInterface> metaClass, boolean keepLang) {
    if (!keepLang) {
        datasubtag.removeAttribute("lang", XML_NAMESPACE);
    }
    MCRMetaInterface test = null;
    try {
        test = metaClass.getDeclaredConstructor().newInstance();
    } catch (Exception e) {
        throw new MCRException("Could not instantiate " + metaClass.getCanonicalName());
    }
    test.setFromDOM(datasubtag);
    test.validate();
    return null;
}
Also used : MCRException(org.mycore.common.MCRException) MCRMetaInterface(org.mycore.datamodel.metadata.MCRMetaInterface) MCRException(org.mycore.common.MCRException) JDOMException(org.jdom2.JDOMException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException)

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