Search in sources :

Example 1 with MCRContent

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

the class MCRDefaultConfigurationLoader method loadFromFile.

/**
 * Loads configuration properties from a specified properties file and adds
 * them to the properties currently set. This method scans the <CODE>
 * CLASSPATH</CODE> for the properties file, it may be a plain file, but
 * may also be located in a zip or jar file. If the properties file contains
 * a property called <CODE>MCR.Configuration.Include</CODE>, the files
 * specified in that property will also be read. Multiple include files have
 * to be separated by spaces or colons.
 *
 * @param filename
 *            the properties file to be loaded
 * @throws MCRConfigurationException
 *             if the file can not be loaded
 */
private void loadFromFile(String filename) {
    File mycoreProperties = new File(filename);
    MCRContent input = null;
    try {
        if (mycoreProperties.canRead()) {
            input = new MCRFileContent(mycoreProperties);
        } else {
            URL url = this.getClass().getResource("/" + filename);
            if (url == null) {
                throw new MCRConfigurationException("Could not find file or resource:" + filename);
            }
            input = new MCRURLContent(url);
        }
        loadFromContent(input);
    } catch (IOException e) {
        String name = input == null ? filename : input.getSystemId();
        throw new MCRConfigurationException("Could not load configuration from: " + name, e);
    }
}
Also used : MCRFileContent(org.mycore.common.content.MCRFileContent) MCRURLContent(org.mycore.common.content.MCRURLContent) IOException(java.io.IOException) File(java.io.File) MCRContent(org.mycore.common.content.MCRContent) URL(java.net.URL)

Example 2 with MCRContent

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

the class MCRXMLMetadataManager method retrieveContent.

public MCRContent retrieveContent(MCRObjectID mcrid) throws IOException {
    MCRContent metadata;
    MCRStoredMetadata storedMetadata = retrieveStoredMetadata(mcrid);
    if (storedMetadata == null || storedMetadata.isDeleted()) {
        return null;
    }
    metadata = storedMetadata.getMetadata();
    return metadata;
}
Also used : MCRStoredMetadata(org.mycore.datamodel.ifs2.MCRStoredMetadata) MCRContent(org.mycore.common.content.MCRContent)

Example 3 with MCRContent

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

the class MCRObjectUtils method restore.

/**
 * Restores a MyCoRe Object to the selected revision. Please note that children and derivates
 * are not deleted or reverted!
 *
 * @param mcrId the mycore object identifier
 * @param revision The revision to restore to. If this is lower than zero, the last revision is used.
 * @return the new {@link MCRObject}
 *
 * @throws IOException An error occurred while retrieving the revision information. This is most
 *          likely due an svn error.
 * @throws MCRPersistenceException There is no such object with the given id and revision.
 * @throws ClassCastException The returning type must be the same as the type of the restored object
 */
public static <T extends MCRBase> T restore(MCRObjectID mcrId, Long revision) throws IOException, MCRPersistenceException {
    @SuppressWarnings("unchecked") T mcrBase = (T) (mcrId.getTypeId().equals("derivate") ? new MCRDerivate() : new MCRObject());
    // get content
    MCRXMLMetadataManager xmlMetadataManager = MCRXMLMetadataManager.instance();
    MCRContent content = xmlMetadataManager.retrieveContent(mcrId, revision);
    if (content == null) {
        throw new MCRPersistenceException("No such object " + mcrId + " with revision " + revision + ".");
    }
    // store it
    try {
        mcrBase.setFromJDOM(content.asXML());
        if (MCRMetadataManager.exists(mcrId)) {
            MCRMetadataManager.update(mcrBase);
        } else {
            if (mcrBase instanceof MCRObject) {
                MCRMetadataManager.create((MCRObject) mcrBase);
            } else {
                MCRMetadataManager.create((MCRDerivate) mcrBase);
            }
        }
        return mcrBase;
    } catch (Exception exc) {
        throw new MCRException("Unable to get object " + mcrId + " with revision " + revision + ".", exc);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) MCRContent(org.mycore.common.content.MCRContent) MCRPersistenceException(org.mycore.common.MCRPersistenceException) MCRPersistenceException(org.mycore.common.MCRPersistenceException) IOException(java.io.IOException) MCRException(org.mycore.common.MCRException)

Example 4 with MCRContent

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

the class MCRMetadataStoreTest method retrieve.

@Test
public void retrieve() throws Exception {
    Document xml1 = new Document(new Element("root"));
    int id = getMetaDataStore().create(new MCRJDOMContent(xml1)).getID();
    MCRStoredMetadata sm1 = getMetaDataStore().retrieve(id);
    MCRContent xml2 = sm1.getMetadata();
    assertEquals(new MCRJDOMContent(xml1).asString(), xml2.asString());
}
Also used : Element(org.jdom2.Element) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent) Test(org.junit.Test)

Example 5 with MCRContent

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

the class MCRMetadataStoreTest method createDocumentInt.

@Test
public void createDocumentInt() throws Exception {
    Document xml1 = new Document(new Element("root"));
    try {
        getMetaDataStore().create(new MCRJDOMContent(xml1), 0);
        fail("metadata store allows to save with id \"0\".");
    } catch (Exception e) {
    // test passed
    }
    int id = getMetaDataStore().getNextFreeID();
    assertTrue(id > 0);
    MCRStoredMetadata sm1 = getMetaDataStore().create(new MCRJDOMContent(xml1), id);
    assertNotNull(sm1);
    MCRContent xml2 = getMetaDataStore().retrieve(id).getMetadata();
    assertEquals(new MCRJDOMContent(xml1).asString(), xml2.asString());
    getMetaDataStore().create(new MCRJDOMContent(xml1), id + 1);
    MCRContent xml3 = getMetaDataStore().retrieve(id + 1).getMetadata();
    assertEquals(new MCRJDOMContent(xml1).asString(), xml3.asString());
}
Also used : Element(org.jdom2.Element) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent) Test(org.junit.Test)

Aggregations

MCRContent (org.mycore.common.content.MCRContent)63 Document (org.jdom2.Document)21 MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)20 IOException (java.io.IOException)16 Element (org.jdom2.Element)13 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)11 MCRPath (org.mycore.datamodel.niofs.MCRPath)10 Test (org.junit.Test)8 MCRPathContent (org.mycore.common.content.MCRPathContent)7 MCRParameterCollector (org.mycore.common.xsl.MCRParameterCollector)6 File (java.io.File)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 JDOMException (org.jdom2.JDOMException)5 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 MCRException (org.mycore.common.MCRException)4 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)4 URL (java.net.URL)3 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)3