Search in sources :

Example 11 with MCRException

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

the class MCRMetaDefault method createXML.

/**
 * This abstract method create a XML stream for all data in this class,
 * defined by the MyCoRe XML MCRMeta... definition for the given subtag.
 *
 * @exception MCRException
 *                if the content of this class is not valid
 * @return a JDOM Element with the XML MCRMeta... part
 */
public Element createXML() throws MCRException {
    try {
        validate();
    } catch (MCRException exc) {
        debug();
        throw exc;
    }
    Element elm = new Element(subtag);
    if (getLang() != null && getLang().length() > 0)
        elm.setAttribute("lang", getLang(), Namespace.XML_NAMESPACE);
    if (getType() != null && getType().length() > 0) {
        elm.setAttribute("type", getType());
    }
    elm.setAttribute("inherited", Integer.toString(getInherited()));
    return elm;
}
Also used : MCRException(org.mycore.common.MCRException) Element(org.jdom2.Element)

Example 12 with MCRException

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

the class MCRXMLTransformer method getCategory.

public static MCRCategory getCategory(Document xml) throws URISyntaxException {
    MCRCategoryImpl category = new MCRCategoryImpl();
    category.setRoot(category);
    final String classID = xml.getRootElement().getAttributeValue("ID");
    category.setLevel(0);
    category.setId(MCRCategoryID.rootID(classID));
    setURL(xml.getRootElement(), category);
    // setChildren has to be called before setParent (below) can be called without
    // database access see: org.mycore.datamodel.classifications2.impl.MCRAbstractCategoryImpl.getChildren()
    category.setChildren(new ArrayList<>());
    buildChildCategories(classID, xml.getRootElement().getChild("categories").getChildren("category"), category);
    try {
        category.setLabels(getLabels(xml.getRootElement().getChildren("label")));
    } catch (NullPointerException | IllegalArgumentException ex) {
        throw new MCRException("Error while adding labels to classification: " + classID, ex);
    }
    return category;
}
Also used : MCRException(org.mycore.common.MCRException) MCRCategoryImpl(org.mycore.datamodel.classifications2.impl.MCRCategoryImpl)

Example 13 with MCRException

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

the class MCRExtractRelatedItemsEventHandler method extractRelatedItems.

private void extractRelatedItems(final MCREvent evt, final MCRObject object) {
    if (!MCRMODSWrapper.isSupported(object)) {
        return;
    }
    Element mods = new MCRMODSWrapper(object).getMODS();
    MCRObjectID oid = object.getId();
    for (Element relatedItem : mods.getChildren("relatedItem", MCRConstants.MODS_NAMESPACE)) {
        String href = relatedItem.getAttributeValue("href", MCRConstants.XLINK_NAMESPACE);
        LOGGER.info("Found related item in {}, href={}", object.getId(), href);
        // MCR-957: only create releated object if mycoreId
        MCRObjectID mcrIdCheck;
        try {
            mcrIdCheck = MCRObjectID.getInstance(href);
        } catch (Exception e) {
            // not a valid MCRObjectID -> don't create anyway
            continue;
        }
        // create if integer value == 0
        if (mcrIdCheck.getNumberAsInteger() == 0) {
            // MCR-931: check for type='host' and present parent document
            if (!isHost(relatedItem) || object.getStructure().getParentID() == null) {
                MCRObjectID relatedID;
                try {
                    relatedID = createRelatedObject(relatedItem, oid);
                } catch (MCRAccessException e) {
                    throw new MCRException(e);
                }
                href = relatedID.toString();
                LOGGER.info("Setting href of related item to {}", href);
                relatedItem.setAttribute("href", href, MCRConstants.XLINK_NAMESPACE);
                if (isHost(relatedItem)) {
                    LOGGER.info("Setting {} as parent of {}", href, oid);
                    object.getStructure().setParent(relatedID);
                }
            }
        } else if (isParentExists(relatedItem)) {
            MCRObjectID relatedID = MCRObjectID.getInstance(href);
            if (object.getStructure().getParentID() == null) {
                LOGGER.info("Setting {} as parent of {}", href, oid);
                object.getStructure().setParent(relatedID);
            } else if (!object.getStructure().getParentID().equals(relatedID)) {
                LOGGER.info("Setting {} as parent of {}", href, oid);
                object.getStructure().setParent(relatedID);
            }
        }
    }
}
Also used : MCRException(org.mycore.common.MCRException) Element(org.jdom2.Element) MCRAccessException(org.mycore.access.MCRAccessException) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPersistenceException(org.mycore.common.MCRPersistenceException) MCRException(org.mycore.common.MCRException) MCRAccessException(org.mycore.access.MCRAccessException)

Example 14 with MCRException

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

the class MCRMODSCommands method loadFromDirectory.

@MCRCommand(syntax = "load all mods documents from directory {0} for project {1}", help = "Load all MODS documents as MyCoRe Objects for project {1} from directory {0}", order = 10)
public static List<String> loadFromDirectory(String directory, String projectID) {
    File dir = new File(directory);
    if (!dir.isDirectory()) {
        throw new MCRException(MessageFormat.format("File {0} is not a directory.", directory));
    }
    String[] list = dir.list();
    if (list.length == 0) {
        LOGGER.warn("No files found in directory {}", dir);
        return null;
    }
    return Arrays.stream(list).filter(file -> file.endsWith(".xml")).map(file -> MessageFormat.format("load mods document from file {0} for project {1}", new File(dir, file).getAbsolutePath(), projectID)).collect(Collectors.toList());
}
Also used : XMLReaders(org.jdom2.input.sax.XMLReaders) Arrays(java.util.Arrays) MCRConstants(org.mycore.common.MCRConstants) MCRException(org.mycore.common.MCRException) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MessageFormat(java.text.MessageFormat) MCRAccessManager(org.mycore.access.MCRAccessManager) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) MCRMetaIFS(org.mycore.datamodel.metadata.MCRMetaIFS) MCRCommandGroup(org.mycore.frontend.cli.annotation.MCRCommandGroup) MCRAccessException(org.mycore.access.MCRAccessException) SAXBuilder(org.jdom2.input.SAXBuilder) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) MCRAbstractCommands(org.mycore.frontend.cli.MCRAbstractCommands) Collection(java.util.Collection) MCRPersistenceException(org.mycore.common.MCRPersistenceException) IOException(java.io.IOException) MCRAccessInterface(org.mycore.access.MCRAccessInterface) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) Logger(org.apache.logging.log4j.Logger) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRObject(org.mycore.datamodel.metadata.MCRObject) SAXException(org.xml.sax.SAXException) MCRXMLHelper(org.mycore.common.xml.MCRXMLHelper) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) LogManager(org.apache.logging.log4j.LogManager) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRRSSFeedImporter(org.mycore.mods.rss.MCRRSSFeedImporter) Element(org.jdom2.Element) MCRException(org.mycore.common.MCRException) File(java.io.File) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 15 with MCRException

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

the class MCROAISetManager method createSetList.

protected OAIDataList<MCRSet> createSetList() {
    OAIDataList<MCRSet> setList = new OAIDataList<>();
    synchronized (this.setConfigurationMap) {
        for (MCROAISetConfiguration<?, ?, ?> conf : this.setConfigurationMap.values()) {
            MCROAISetHandler<?, ?, ?> handler = conf.getHandler();
            Map<String, MCRSet> setMap = handler.getSetMap();
            synchronized (setMap) {
                setMap.clear();
                Element resolved = MCRURIResolver.instance().resolve(conf.getURI());
                if (resolved == null) {
                    throw new MCRException("Could not resolve set URI " + conf.getURI() + " for set " + conf.getId() + ".");
                }
                for (Element setElement : resolved.getChildren("set", OAIConstants.NS_OAI)) {
                    MCRSet set = createSet(conf.getId(), setElement);
                    setMap.put(set.getSpec(), set);
                    if (!contains(set.getSpec(), setList)) {
                        if (!handler.filter(set)) {
                            setList.add(set);
                        }
                    }
                }
            }
        }
    }
    return setList;
}
Also used : MCRException(org.mycore.common.MCRException) Element(org.jdom2.Element) OAIDataList(org.mycore.oai.pmh.OAIDataList) MCRSet(org.mycore.oai.set.MCRSet)

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