Search in sources :

Example 6 with MCRActiveLinkException

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

the class MCRClassification2Commands method processFromDirectory.

/**
 * Loads or updates MCRClassification from all XML files in a directory.
 *
 * @param directory
 *            the directory containing the XML files
 * @param update
 *            if true, classification will be updated, else Classification
 *            is created
 * @throws MCRActiveLinkException
 */
private static List<String> processFromDirectory(String directory, boolean update) throws MCRActiveLinkException {
    File dir = new File(directory);
    if (!dir.isDirectory()) {
        LOGGER.warn("{} ignored, is not a directory.", directory);
        return null;
    }
    String[] list = dir.list();
    if (list.length == 0) {
        LOGGER.warn("No files found in directory {}", directory);
        return null;
    }
    return Arrays.stream(list).filter(file -> file.endsWith(".xml")).map(file -> (update ? "update" : "load") + " classification from file " + new File(dir, file).getAbsolutePath()).collect(Collectors.toList());
}
Also used : Transformer(javax.xml.transform.Transformer) Arrays(java.util.Arrays) MCRCategoryTransformer(org.mycore.datamodel.classifications2.utils.MCRCategoryTransformer) URL(java.net.URL) MCRXMLTransformer(org.mycore.datamodel.classifications2.utils.MCRXMLTransformer) URISyntaxException(java.net.URISyntaxException) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) MCRConstants(org.mycore.common.MCRConstants) Session(org.hibernate.Session) MCRException(org.mycore.common.MCRException) ArrayList(java.util.ArrayList) Document(org.jdom2.Document) Locale(java.util.Locale) TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) MCRCategoryDAO(org.mycore.datamodel.classifications2.MCRCategoryDAO) MCRCategoryImpl(org.mycore.datamodel.classifications2.impl.MCRCategoryImpl) MCRCommandGroup(org.mycore.frontend.cli.annotation.MCRCommandGroup) MCRLabel(org.mycore.datamodel.classifications2.MCRLabel) MalformedURLException(java.net.MalformedURLException) Format(org.jdom2.output.Format) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) MCRHIBConnection(org.mycore.backend.hibernate.MCRHIBConnection) FileOutputStream(java.io.FileOutputStream) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) IOException(java.io.IOException) MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) Collectors(java.util.stream.Collectors) File(java.io.File) XMLOutputter(org.jdom2.output.XMLOutputter) MCRURIResolver(org.mycore.common.xml.MCRURIResolver) MCRURLContent(org.mycore.common.content.MCRURLContent) List(java.util.List) SAXParseException(org.xml.sax.SAXParseException) Logger(org.apache.logging.log4j.Logger) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand) Paths(java.nio.file.Paths) MCRCategoryDAOFactory(org.mycore.datamodel.classifications2.MCRCategoryDAOFactory) TransformerFactory(javax.xml.transform.TransformerFactory) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) MCRXMLParserFactory(org.mycore.common.xml.MCRXMLParserFactory) MCRCategoryDAOImpl(org.mycore.datamodel.classifications2.impl.MCRCategoryDAOImpl) File(java.io.File)

Example 7 with MCRActiveLinkException

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

the class MCRObjectCommands method replaceParent.

/**
 * Moves object to new parent.
 *
 * @param sourceId
 *            object that should be attached to new parent
 * @param newParentId
 *            the ID of the new parent
 * @throws MCRAccessException see {@link MCRMetadataManager#update(MCRObject)}
 */
@MCRCommand(syntax = "set parent of {0} to {1}", help = "replaces a parent of an object (first parameter) to the given new one (second parameter)", order = 300)
public static void replaceParent(String sourceId, String newParentId) throws MCRPersistenceException, MCRActiveLinkException, MCRAccessException {
    // child
    MCRObject sourceMCRObject = MCRMetadataManager.retrieveMCRObject(MCRObjectID.getInstance(sourceId));
    // old parent
    MCRObjectID oldParentId = sourceMCRObject.getStructure().getParentID();
    MCRObjectID newParentObjectID = MCRObjectID.getInstance(newParentId);
    if (newParentObjectID.equals(oldParentId)) {
        LOGGER.info("Object {} is already child of {}", sourceId, newParentId);
        return;
    }
    MCRObject oldParentMCRObject = null;
    if (oldParentId != null) {
        try {
            oldParentMCRObject = MCRMetadataManager.retrieveMCRObject(oldParentId);
        } catch (Exception exc) {
            LOGGER.error("Unable to get old parent object {}, its probably deleted.", oldParentId, exc);
        }
    }
    // change href to new parent
    LOGGER.info("Setting link in \"{}\" to parent \"{}\"", sourceId, newParentObjectID);
    MCRMetaLinkID parentLinkId = new MCRMetaLinkID("parent", 0);
    parentLinkId.setReference(newParentObjectID, null, null);
    sourceMCRObject.getStructure().setParent(parentLinkId);
    if (oldParentMCRObject != null) {
        // remove Child in old parent
        LOGGER.info("Remove child \"{}\" in old parent \"{}\"", sourceId, oldParentId);
        oldParentMCRObject.getStructure().removeChild(sourceMCRObject.getId());
        LOGGER.info("Update old parent \"{}\n", oldParentId);
        MCRMetadataManager.update(oldParentMCRObject);
    }
    LOGGER.info("Update \"{}\" in datastore (saving new link)", sourceId);
    MCRMetadataManager.update(sourceMCRObject);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Structure: {}", sourceMCRObject.getStructure().isValid());
        LOGGER.debug("Object: {}", sourceMCRObject.isValid());
    }
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) URISyntaxException(java.net.URISyntaxException) JDOMException(org.jdom2.JDOMException) MCRPersistenceException(org.mycore.common.MCRPersistenceException) SAXException(org.xml.sax.SAXException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) TransformerException(javax.xml.transform.TransformerException) MCRException(org.mycore.common.MCRException) MCRAccessException(org.mycore.access.MCRAccessException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 8 with MCRActiveLinkException

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

the class MCRURNGranularOAIRegistrationService method register.

@Override
public MCRDNBURN register(MCRBase obj, String additional, boolean updateObject) throws MCRAccessException, MCRActiveLinkException, MCRPersistentIdentifierException {
    this.validateRegistration(obj, additional);
    MCRObjectDerivate derivate = ((MCRDerivate) obj).getDerivate();
    MCRDNBURN newURN;
    if (additional.equals("")) {
        /* Multiple URN for entire Derivate...  */
        newURN = registerURNsDerivate(obj, additional, derivate);
    } else {
        /* Single URN to one File... */
        newURN = registerSingleURN(obj, additional, derivate);
    }
    try {
        MCRMetadataManager.update(obj);
    } catch (Exception e) {
        throw new MCRPersistentIdentifierException("Error while updating derivate " + obj.getId(), e);
    }
    return newURN;
}
Also used : MCRObjectDerivate(org.mycore.datamodel.metadata.MCRObjectDerivate) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRAccessException(org.mycore.access.MCRAccessException) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) IOException(java.io.IOException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException)

Aggregations

MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)8 MCRAccessException (org.mycore.access.MCRAccessException)5 MCRException (org.mycore.common.MCRException)4 IOException (java.io.IOException)3 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 MCRHIBConnection (org.mycore.backend.hibernate.MCRHIBConnection)2 MCRPersistenceException (org.mycore.common.MCRPersistenceException)2 MCRObject (org.mycore.datamodel.metadata.MCRObject)2 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)2 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)2 MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)2 SAXParseException (org.xml.sax.SAXParseException)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 Field (java.lang.reflect.Field)1 URL (java.net.URL)1 Paths (java.nio.file.Paths)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1