Search in sources :

Example 41 with EXistCollectionManagementService

use of org.exist.xmldb.EXistCollectionManagementService in project exist by eXist-db.

the class MoveResourceRecoveryTest method xmldbStore.

private void xmldbStore() throws XMLDBException, URISyntaxException, IOException {
    final org.xmldb.api.base.Collection root = DatabaseManager.getCollection(XmldbURI.LOCAL_DB, "admin", "");
    final EXistCollectionManagementService mgr = (EXistCollectionManagementService) root.getService("CollectionManagementService", "1.0");
    org.xmldb.api.base.Collection test = root.getChildCollection("test");
    if (test == null) {
        test = mgr.createCollection("test");
    }
    org.xmldb.api.base.Collection test2 = test.getChildCollection("test2");
    if (test2 == null) {
        test2 = mgr.createCollection("test2");
    }
    final String sample;
    try (final InputStream is = SAMPLES.getRomeoAndJulietSample()) {
        sample = InputStreamUtil.readString(is, UTF_8);
    }
    final Resource res = test2.createResource("test3.xml", "XMLResource");
    res.setContent(sample);
    test2.storeResource(res);
    mgr.moveResource(XmldbURI.create(XmldbURI.ROOT_COLLECTION + "/test2/test3.xml"), TestConstants.TEST_COLLECTION_URI, XmldbURI.create("new_test3.xml"));
}
Also used : EXistCollectionManagementService(org.exist.xmldb.EXistCollectionManagementService) InputStream(java.io.InputStream) Resource(org.xmldb.api.base.Resource) Database(org.xmldb.api.base.Database)

Example 42 with EXistCollectionManagementService

use of org.exist.xmldb.EXistCollectionManagementService in project exist by eXist-db.

the class XMLDBRename method evalWithCollection.

/* (non-Javadoc)
	 * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
	 */
public Sequence evalWithCollection(Collection collection, Sequence[] args, Sequence contextSequence) throws XPathException {
    if (getSignature().getArgumentCount() == 3) {
        final XmldbURI doc = new AnyURIValue(args[1].itemAt(0).getStringValue()).toXmldbURI();
        try {
            final Resource resource = collection.getResource(doc.toString());
            if (resource == null) {
                logger.error("Resource {} not found", doc);
                throw new XPathException(this, "Resource " + doc + " not found");
            }
            final String newName = args[2].itemAt(0).getStringValue();
            final EXistCollectionManagementService service = (EXistCollectionManagementService) collection.getService("CollectionManagementService", "1.0");
            service.moveResource(doc, (XmldbURI) null, XmldbURI.xmldbUriFor(newName));
        } catch (final XMLDBException e) {
            logger.error(e.getMessage());
            throw new XPathException(this, "XMLDB exception caught: " + e.getMessage(), e);
        } catch (final URISyntaxException e) {
            logger.error(e.getMessage());
            throw new XPathException(this, "URI exception: " + e.getMessage(), e);
        }
    } else {
        try {
            final String newName = args[1].itemAt(0).getStringValue();
            final EXistCollectionManagementService service = (EXistCollectionManagementService) collection.getService("CollectionManagementService", "1.0");
            service.move(XmldbURI.xmldbUriFor(collection.getName()), null, XmldbURI.xmldbUriFor(newName));
        } catch (final XMLDBException e) {
            logger.error("Cannot rename collection: {}", e.getMessage());
            throw new XPathException(this, "Cannot rename collection: " + e.getMessage(), e);
        } catch (final URISyntaxException e) {
            logger.error(e.getMessage());
            throw new XPathException(this, "URI exception: " + e.getMessage(), e);
        }
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : XPathException(org.exist.xquery.XPathException) EXistCollectionManagementService(org.exist.xmldb.EXistCollectionManagementService) AnyURIValue(org.exist.xquery.value.AnyURIValue) Resource(org.xmldb.api.base.Resource) XMLDBException(org.xmldb.api.base.XMLDBException) URISyntaxException(java.net.URISyntaxException) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

EXistCollectionManagementService (org.exist.xmldb.EXistCollectionManagementService)42 Collection (org.xmldb.api.base.Collection)31 Resource (org.xmldb.api.base.Resource)25 BinaryResource (org.xmldb.api.modules.BinaryResource)19 Test (org.junit.Test)18 UserManagementService (org.exist.xmldb.UserManagementService)16 XMLResource (org.xmldb.api.modules.XMLResource)13 XmldbURI (org.exist.xmldb.XmldbURI)12 URISyntaxException (java.net.URISyntaxException)11 Database (org.xmldb.api.base.Database)7 EXistResource (org.exist.xmldb.EXistResource)6 ExtendedResource (org.exist.xmldb.ExtendedResource)6 XMLDBException (org.xmldb.api.base.XMLDBException)5 Path (java.nio.file.Path)3 IndexQueryService (org.exist.xmldb.IndexQueryService)3 XPathException (org.exist.xquery.XPathException)3 InputStream (java.io.InputStream)2 BuildException (org.apache.tools.ant.BuildException)2 AnyURIValue (org.exist.xquery.value.AnyURIValue)2 ResourceSet (org.xmldb.api.base.ResourceSet)2