Search in sources :

Example 1 with MCRException

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

the class MCRMetadataManager method create.

/**
 * Stores the derivate.
 *
 * @param mcrDerivate
 *            derivate instance to store
 * @throws MCRPersistenceException
 *            if a persistence problem is occurred
 * @throws MCRAccessException
 *            if write permission to object is missing
 */
public static void create(final MCRDerivate mcrDerivate) throws MCRPersistenceException, MCRAccessException {
    if (exists(mcrDerivate.getId())) {
        throw new MCRPersistenceException("The derivate " + mcrDerivate.getId() + " already exists, nothing done.");
    }
    try {
        mcrDerivate.validate();
    } catch (MCRException exc) {
        throw new MCRPersistenceException("The derivate " + mcrDerivate.getId() + " is not valid.", exc);
    }
    final MCRObjectID objid = mcrDerivate.getDerivate().getMetaLink().getXLinkHrefID();
    if (!MCRAccessManager.checkPermission(objid, PERMISSION_WRITE)) {
        throw MCRAccessException.missingPermission("Add derivate " + mcrDerivate.getId() + " to object.", objid.toString(), PERMISSION_WRITE);
    }
    byte[] objectBackup;
    try {
        objectBackup = MCRXMLMetadataManager.instance().retrieveBLOB(objid);
    } catch (IOException ioExc) {
        throw new MCRPersistenceException("Unable to retrieve xml blob of " + objid);
    }
    if (objectBackup == null) {
        throw new MCRPersistenceException("Cannot find " + objid + " to attach derivate " + mcrDerivate.getId() + " to it.");
    }
    // prepare the derivate metadata and store under the XML table
    if (mcrDerivate.getService().getDate("createdate") == null || !mcrDerivate.isImportMode()) {
        mcrDerivate.getService().setDate("createdate");
    }
    if (mcrDerivate.getService().getDate("modifydate") == null || !mcrDerivate.isImportMode()) {
        mcrDerivate.getService().setDate("modifydate");
    }
    // handle events
    fireEvent(mcrDerivate, null, MCREvent.CREATE_EVENT);
    // add the link to metadata
    final MCRMetaLinkID der = new MCRMetaLinkID();
    der.setReference(mcrDerivate.getId().toString(), null, mcrDerivate.getLabel());
    der.setSubTag("derobject");
    try {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("adding Derivate in data store");
        }
        MCRMetadataManager.addOrUpdateDerivateToObject(objid, der);
    } catch (final Exception e) {
        MCRMetadataManager.restore(mcrDerivate, objid, objectBackup);
        // throw final exception
        throw new MCRPersistenceException("Error while creatlink to MCRObject " + objid + ".", e);
    }
    // create data in IFS
    if (mcrDerivate.getDerivate().getInternals() != null) {
        MCRObjectID derId = mcrDerivate.getId();
        MCRPath rootPath = MCRPath.getPath(derId.toString(), "/");
        if (mcrDerivate.getDerivate().getInternals().getSourcePath() == null) {
            try {
                rootPath.getFileSystem().createRoot(rootPath.getOwner());
                BasicFileAttributes attrs = Files.readAttributes(rootPath, BasicFileAttributes.class);
                if (!(attrs.fileKey() instanceof String)) {
                    throw new MCRPersistenceException("Cannot get ID from newely created directory, as it is not a String." + rootPath);
                }
                mcrDerivate.getDerivate().getInternals().setIFSID(attrs.fileKey().toString());
            } catch (IOException ioExc) {
                throw new MCRPersistenceException("Cannot create root of '" + rootPath.getOwner() + "' or read the file attributes.", ioExc);
            }
        } else {
            final String sourcepath = mcrDerivate.getDerivate().getInternals().getSourcePath();
            final File f = new File(sourcepath);
            if (f.exists()) {
                try {
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug("Starting File-Import");
                    }
                    importDerivate(derId.toString(), f.toPath());
                    BasicFileAttributes attrs = Files.readAttributes(rootPath, BasicFileAttributes.class);
                    if (!(attrs.fileKey() instanceof String)) {
                        throw new MCRPersistenceException("Cannot get ID from newely created directory, as it is not a String." + rootPath);
                    }
                    mcrDerivate.getDerivate().getInternals().setIFSID(attrs.fileKey().toString());
                } catch (final Exception e) {
                    if (Files.exists(rootPath)) {
                        deleteDerivate(derId.toString());
                    }
                    MCRMetadataManager.restore(mcrDerivate, objid, objectBackup);
                    throw new MCRPersistenceException("Can't add derivate to the IFS", e);
                }
            } else {
                LOGGER.warn("Empty derivate, the File or Directory -->{}<--  was not found.", sourcepath);
            }
        }
    }
}
Also used : MCRException(org.mycore.common.MCRException) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRPersistenceException(org.mycore.common.MCRPersistenceException) File(java.io.File) 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) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 2 with MCRException

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

the class MCRObject method validate.

/**
 * Validates this MCRObject. 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 structure part is null or invalid</li>
 *  <li>the metadata part is null or invalid</li>
 *  </ul>
 *
 * @throws MCRException the MCRObject is invalid
 */
@Override
public void validate() {
    super.validate();
    MCRObjectStructure structure = getStructure();
    MCRObjectMetadata metadata = getMetadata();
    if (structure == null) {
        throw new MCRException("The <structure> part of '" + getId() + "' is undefined.");
    }
    if (metadata == null) {
        throw new MCRException("The <metadata> part of '" + getId() + "' is undefined.");
    }
    try {
        structure.validate();
    } catch (MCRException exc) {
        throw new MCRException("The <structure> part of '" + getId() + "' is invalid.", exc);
    }
    try {
        metadata.validate();
    } catch (MCRException exc) {
        throw new MCRException("The <metadata> part of '" + getId() + "' is invalid.", exc);
    }
}
Also used : MCRException(org.mycore.common.MCRException)

Example 3 with MCRException

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

the class MCRObjectDerivate method createXML.

/**
 * This methode create a XML stream for all derivate data.
 *
 * @exception MCRException
 *                if the content of this class is not valid
 * @return a JDOM Element with the XML data of the structure data part
 */
public final org.jdom2.Element createXML() throws MCRException {
    try {
        validate();
    } catch (MCRException exc) {
        throw new MCRException("The content is not valid.", exc);
    }
    org.jdom2.Element elm = new org.jdom2.Element("derivate");
    elm.setAttribute("display", String.valueOf(display));
    org.jdom2.Element linkmetas = new org.jdom2.Element("linkmetas");
    linkmetas.setAttribute("class", "MCRMetaLinkID");
    linkmetas.setAttribute("heritable", "false");
    linkmetas.addContent(linkmeta.createXML());
    elm.addContent(linkmetas);
    if (externals.size() != 0) {
        org.jdom2.Element extEl = new org.jdom2.Element("externals");
        extEl.setAttribute("class", "MCRMetaLink");
        extEl.setAttribute("heritable", "false");
        for (MCRMetaLink external : externals) {
            extEl.addContent(external.createXML());
        }
        elm.addContent(extEl);
    }
    if (internals != null) {
        org.jdom2.Element intEl = new org.jdom2.Element("internals");
        intEl.setAttribute("class", "MCRMetaIFS");
        intEl.setAttribute("heritable", "false");
        intEl.addContent(internals.createXML());
        elm.addContent(intEl);
    }
    if (titles.size() != 0) {
        org.jdom2.Element titEl = new org.jdom2.Element("titles");
        titEl.setAttribute("class", "MCRMetaLangText");
        titEl.setAttribute("heritable", "false");
        for (MCRMetaLangText title : titles) {
            titEl.addContent(title.createXML());
        }
        elm.addContent(titEl);
    }
    if (this.derivateURN != null || !files.isEmpty()) {
        Element fileset = new Element("fileset");
        if (this.derivateURN != null) {
            fileset.setAttribute("urn", this.derivateURN);
        }
        Collections.sort(files);
        for (MCRFileMetadata file : files) {
            fileset.addContent(file.createXML());
        }
        elm.addContent(fileset);
    }
    return elm;
}
Also used : MCRException(org.mycore.common.MCRException) Element(org.jdom2.Element) Element(org.jdom2.Element)

Example 4 with MCRException

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

the class MCRObjectService method createXML.

/**
 * This method create a XML stream for all structure data.
 *
 * @exception MCRException
 *                if the content of this class is not valid
 * @return a JDOM Element with the XML data of the structure data part
 */
public final org.jdom2.Element createXML() throws MCRException {
    try {
        validate();
    } catch (MCRException exc) {
        throw new MCRException("The content is not valid.", exc);
    }
    org.jdom2.Element elm = new org.jdom2.Element("service");
    if (dates.size() != 0) {
        org.jdom2.Element elmm = new org.jdom2.Element("servdates");
        elmm.setAttribute("class", "MCRMetaISO8601Date");
        for (MCRMetaISO8601Date date : dates) {
            elmm.addContent(date.createXML());
        }
        elm.addContent(elmm);
    }
    if (rules.size() != 0) {
        org.jdom2.Element elmm = new org.jdom2.Element("servacls");
        elmm.setAttribute("class", "MCRMetaAccessRule");
        for (MCRMetaAccessRule rule : rules) {
            elmm.addContent(rule.createXML());
        }
        elm.addContent(elmm);
    }
    if (flags.size() != 0) {
        org.jdom2.Element elmm = new org.jdom2.Element("servflags");
        elmm.setAttribute("class", "MCRMetaLangText");
        for (MCRMetaLangText flag : flags) {
            elmm.addContent(flag.createXML());
        }
        elm.addContent(elmm);
    }
    if (state != null) {
        org.jdom2.Element elmm = new org.jdom2.Element("servstates");
        elmm.setAttribute("class", "MCRMetaClassification");
        MCRMetaClassification stateClass = new MCRMetaClassification("servstate", 0, null, state);
        elmm.addContent(stateClass.createXML());
        elm.addContent(elmm);
    }
    return elm;
}
Also used : MCRException(org.mycore.common.MCRException) Element(org.jdom2.Element) Element(org.jdom2.Element)

Example 5 with MCRException

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

the class MCRObjectStructure method createXML.

/**
 * <em>createXML</em> is the inverse of setFromDOM and converts the
 * structure's memory copy into an XML string.
 *
 * @exception MCRException
 *                if the content of this class is not valid
 * @return org.jdom2.Element the structure XML string
 */
public final Element createXML() throws MCRException {
    try {
        validate();
    } catch (MCRException exc) {
        throw new MCRException("The content is not valid.", exc);
    }
    Element elm = new Element("structure");
    if (parent != null) {
        Element elmm = new Element("parents");
        elmm.setAttribute("class", "MCRMetaLinkID");
        elmm.addContent(parent.createXML());
        elm.addContent(elmm);
    }
    if (children.size() > 0) {
        Element elmm = new Element("children");
        elmm.setAttribute("class", "MCRMetaLinkID");
        for (MCRMetaLinkID child : getChildren()) {
            elmm.addContent(child.createXML());
        }
        elm.addContent(elmm);
    }
    if (derivates.size() > 0) {
        Element elmm = new Element("derobjects");
        elmm.setAttribute("class", "MCRMetaLinkID");
        for (MCRMetaLinkID derivate : getDerivates()) {
            elmm.addContent(derivate.createXML());
        }
        elm.addContent(elmm);
    }
    return elm;
}
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