All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mycore.datamodel.metadata.MCRDerivate Maven / Gradle / Ivy

There is a newer version: 2024.05
Show newest version
/*
 * 
 * $Revision$ $Date$
 * 
 * This file is part of *** M y C o R e *** See http://www.mycore.de/ for
 * details.
 * 
 * This program is free software; you can use it, redistribute it and / or
 * modify it under the terms of the GNU General Public License (GPL) as
 * published by the Free Software Foundation; either version 2 of the License or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program, in a file called gpl.txt or license.txt. If not, write to the
 * Free Software Foundation Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307 USA
 */

package org.mycore.datamodel.metadata;

import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.filter.Filters;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;
import org.mycore.common.MCRException;
import org.xml.sax.SAXParseException;

/**
 * This class holds all information of a derivate. For persistence operations
 * see methods of {@link MCRMetadataManager}.
 * 
 * @author Jens Kupferschmidt
 * @author Thomas Scheffler
 * @version $Revision$ $Date: 2010-09-30 17:49:21 +0200 (Thu, 30 Sep
 *          2010) $
 */
public final class MCRDerivate extends MCRBase {

    private static final Logger LOGGER = LogManager.getLogger();

    public static final String ROOT_NAME = "mycorederivate";

    // the object content
    private MCRObjectDerivate mcrDerivate;

    private int order;

    /**
     * This is the constructor of the MCRDerivate class. It make an instance of
     * the parser class and the metadata class.
     * 
     * @exception MCRException
     *                general Exception of MyCoRe
     */
    public MCRDerivate() throws MCRException {
        super();
        mcrDerivate = new MCRObjectDerivate(getId());
        order = 1;
    }

    public MCRDerivate(byte[] bytes, boolean valid) throws SAXParseException {
        this();
        setFromXML(bytes, valid);
    }

    public MCRDerivate(Document doc) {
        this();
        setFromJDOM(doc);
    }

    public MCRDerivate(URI uri) throws SAXParseException, IOException {
        this();
        setFromURI(uri);
    }

    /**
     * This methode return the instance of the MCRObjectDerivate class. If this
     * was not found, null was returned.
     * 
     * @return the instance of the MCRObjectDerivate class
     */
    public MCRObjectDerivate getDerivate() {
        return mcrDerivate;
    }

    /**
     * The given DOM was convert into an internal view of metadata. This are the
     * object ID and the object label, also the blocks structure, flags and
     * metadata.
     * 
     * @exception MCRException
     *                general Exception of MyCoRe
     */
    @Override
    protected void setUp() throws MCRException {
        super.setUp();

        // get the derivate data of the object
        Element derivateElement = jdomDocument.getRootElement().getChild("derivate");
        mcrDerivate = new MCRObjectDerivate(mcrId, derivateElement);
    }

    /**
     * This methode create 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 Document createXML() throws MCRException {
        Document doc = super.createXML();
        Element elm = doc.getRootElement();
        elm.setAttribute("order", String.valueOf(order));
        elm.addContent(mcrDerivate.createXML());
        elm.addContent(mcrService.createXML());
        return doc;
    }

    @Override
    protected String getRootTagName() {
        return ROOT_NAME;
    }

    /**
     * Reads all files and urns from the derivate.
     * 
     * @return A {@link Map} which contains the files as key and the urns as value.
     * If no URN assigned the map will be empty.
     */
    public Map getUrnMap() {
        Map fileUrnMap = new HashMap<>();

        XPathExpression filesetPath = XPathFactory.instance().compile("./mycorederivate/derivate/fileset",
            Filters.element());

        Element result = filesetPath.evaluateFirst(this.createXML());
        if (result == null) {
            return fileUrnMap;
        }
        String urn = result.getAttributeValue("urn");

        if (urn != null) {
            XPathExpression filePath = XPathFactory
                .instance()
                .compile("./mycorederivate/derivate/fileset[@urn='" + urn + "']/file", Filters.element());
            List files = filePath.evaluate(this.createXML());

            for (Element currentFileElement : files) {
                String currentUrn = currentFileElement.getChildText("urn");
                String currentFile = currentFileElement.getAttributeValue("name");
                fileUrnMap.put(currentFile, currentUrn);
            }
        }
        return fileUrnMap;
    }

    /**
     * The method print all informations about this MCRObject.
     */
    public void debug() {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("MCRDerivate ID : {}", mcrId);
            LOGGER.debug("MCRDerivate Label : {}", mcrLabel);
            LOGGER.debug("MCRDerivate Schema : {}", mcrSchema);
            LOGGER.debug("");
        }
    }

    /**
     * Validates this MCRDerivate. This method throws an exception if:
     *  
    *
  • the mcr_id is null
  • *
  • the XML schema is null or empty
  • *
  • the service part is null or invalid
  • *
  • the MCRObjectDerivate is null or invalid
  • *
* * @throws MCRException the MCRDerivate is invalid */ @Override public void validate() throws MCRException { super.validate(); MCRObjectDerivate derivate = getDerivate(); if (derivate == null) { throw new MCRException("The part of '" + getId() + "' is undefined."); } try { derivate.validate(); } catch (Exception exc) { throw new MCRException("The part of '" + getId() + "' is invalid.", exc); } } /** * @return the {@link MCRObjectID} of the owner of the derivate */ public MCRObjectID getOwnerID() { return this.getDerivate().getMetaLink().getXLinkHrefID(); } @Override public void setId(MCRObjectID id) { super.setId(id); this.mcrDerivate.setDerivateID(id); } public int getOrder() { return order; } public void setOrder(int order) { this.order = order; } @Override protected void setFromJDOM(Document doc) { super.setFromJDOM(doc); this.order = Optional.ofNullable(doc.getRootElement().getAttributeValue("order")) .map(Integer::valueOf) .orElse(1); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy