Search in sources :

Example 11 with Txn

use of org.exist.storage.txn.Txn in project exist by eXist-db.

the class XQueryStartupTrigger method createAutostartCollection.

/**
 * Create autostart collection when not existent
 *
 * @param broker The exist-db broker
 */
private void createAutostartCollection(DBBroker broker) {
    LOG.info("Creating {}", AUTOSTART_COLLECTION);
    final TransactionManager txnManager = broker.getBrokerPool().getTransactionManager();
    try (final Txn txn = txnManager.beginTransaction()) {
        XmldbURI newCollection = XmldbURI.create(AUTOSTART_COLLECTION, true);
        // Create collection
        final Collection created = broker.getOrCreateCollection(txn, newCollection);
        // Set ownership and mode
        PermissionFactory.chown(broker, created, Optional.of(SecurityManager.SYSTEM), Optional.of(SecurityManager.DBA_GROUP));
        PermissionFactory.chmod(broker, created, Optional.of(Permission.DEFAULT_SYSTEM_SECURITY_COLLECTION_PERM), Optional.empty());
        broker.saveCollection(txn, created);
        broker.flush();
        // Commit change
        txnManager.commit(txn);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finished creation of collection");
        }
    } catch (Throwable ex) {
        LOG.error(ex);
    }
}
Also used : TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) XmldbURI(org.exist.xmldb.XmldbURI)

Example 12 with Txn

use of org.exist.storage.txn.Txn in project exist by eXist-db.

the class ElementImpl method setAttributeNode.

private Attr setAttributeNode(final Attr newAttr, final Function<QName, Attr> getFn) {
    final QName attrName = new QName(newAttr.getLocalName(), newAttr.getNamespaceURI(), newAttr.getPrefix(), ElementValue.ATTRIBUTE);
    final Attr existingAttr = getFn.apply(attrName);
    if (existingAttr != null) {
        if (existingAttr.equals(newAttr)) {
            return newAttr;
        }
        // update an existing attribute
        existingAttr.setValue(newAttr.getValue());
        try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker();
            final Txn transaction = broker.getBrokerPool().getTransactionManager().beginTransaction()) {
            if (!(existingAttr instanceof IStoredNode)) {
                throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, "Wrong node type");
            }
            final IStoredNode<?> existing = (IStoredNode<?>) existingAttr;
            if (!existing.getNodeId().isChildOf(nodeId)) {
                throw new DOMException(DOMException.NOT_FOUND_ERR, "node " + existing.getNodeId().getParentId() + " is not a child of element " + nodeId);
            }
            // update old custom indexes
            final IndexController indexes = broker.getIndexController();
            indexes.reindex(transaction, existing, ReindexMode.STORE);
            broker.updateNode(transaction, existing, true);
            transaction.commit();
        } catch (final EXistException e) {
            LOG.error(e);
            throw new DOMException(DOMException.INVALID_ACCESS_ERR, e.getMessage());
        }
        return existingAttr;
    } else {
        try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker()) {
            final AttrImpl attrib = new AttrImpl(attrName, newAttr.getValue(), broker.getBrokerPool().getSymbols());
            return (Attr) appendChild(attrib);
        } catch (final EXistException e) {
            LOG.error(e);
            throw new DOMException(DOMException.INVALID_ACCESS_ERR, e.getMessage());
        }
    }
}
Also used : QName(org.exist.dom.QName) IndexController(org.exist.indexing.IndexController) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException)

Example 13 with Txn

use of org.exist.storage.txn.Txn in project exist by eXist-db.

the class ElementImpl method insertBefore.

@Override
public Node insertBefore(final Node newChild, final Node refChild) throws DOMException {
    if (refChild == null) {
        return appendChild(newChild);
    } else if (!(refChild instanceof IStoredNode)) {
        throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, "Wrong node type");
    }
    final org.exist.dom.NodeListImpl nl = new org.exist.dom.NodeListImpl();
    nl.add(newChild);
    final TransactionManager transact = ownerDocument.getBrokerPool().getTransactionManager();
    try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker();
        final Txn transaction = transact.beginTransaction()) {
        insertBefore(transaction, nl, refChild);
        broker.storeXMLResource(transaction, getOwnerDocument());
        transact.commit(transaction);
        return refChild.getPreviousSibling();
    } catch (final TransactionException e) {
        throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, e.getMessage());
    } catch (final EXistException e) {
        LOG.warn("Exception while inserting node: {}", e.getMessage(), e);
    }
    return null;
}
Also used : org.w3c.dom(org.w3c.dom) TransactionException(org.exist.storage.txn.TransactionException) TransactionManager(org.exist.storage.txn.TransactionManager) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException)

Example 14 with Txn

use of org.exist.storage.txn.Txn in project exist by eXist-db.

the class ElementImpl method setAttribute.

private void setAttribute(final QName attrName, final String value, final Function<QName, Attr> getFn) {
    final Attr existingAttr = getFn.apply(attrName);
    if (existingAttr != null) {
        // update an existing attribute
        existingAttr.setValue(value);
        try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker();
            final Txn transaction = broker.getBrokerPool().getTransactionManager().beginTransaction()) {
            if (!(existingAttr instanceof IStoredNode)) {
                throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, "Wrong node type");
            }
            final IStoredNode<?> existing = (IStoredNode<?>) existingAttr;
            if (!existing.getNodeId().isChildOf(nodeId)) {
                throw new DOMException(DOMException.NOT_FOUND_ERR, "node " + existing.getNodeId().getParentId() + " is not a child of element " + nodeId);
            }
            // update old custom indexes
            final IndexController indexes = broker.getIndexController();
            indexes.reindex(transaction, existing, ReindexMode.STORE);
            broker.updateNode(transaction, existing, true);
            transaction.commit();
        } catch (final EXistException e) {
            LOG.error(e);
            throw new DOMException(DOMException.INVALID_ACCESS_ERR, e.getMessage());
        }
    } else {
        try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker()) {
            final AttrImpl attrib = new AttrImpl(attrName, value, broker.getBrokerPool().getSymbols());
            appendChild(attrib);
        } catch (final EXistException e) {
            LOG.error(e);
            throw new DOMException(DOMException.INVALID_ACCESS_ERR, e.getMessage());
        }
    }
}
Also used : IndexController(org.exist.indexing.IndexController) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException)

Example 15 with Txn

use of org.exist.storage.txn.Txn in project exist by eXist-db.

the class ElementImpl method appendChild.

@Override
public Node appendChild(final Node newChild) throws DOMException {
    if (newChild.getNodeType() != Node.DOCUMENT_NODE && newChild.getOwnerDocument() != null && newChild.getOwnerDocument() != ownerDocument) {
        throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, "Owning document IDs do not match");
    }
    if (newChild == this) {
        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "Cannot append an element to itself");
    }
    if (newChild.getNodeType() == DOCUMENT_NODE) {
        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "A Document Node may not be appended to an element");
    }
    if (newChild.getNodeType() == DOCUMENT_TYPE_NODE) {
        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "A Document Type Node may not be appended to an element");
    }
    if (newChild instanceof IStoredNode) {
        final NodeId newChildId = ((IStoredNode) newChild).getNodeId();
        if (newChildId != null && getNodeId().isDescendantOf(newChildId)) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "The node to append is one of this node's ancestors");
        }
    }
    final TransactionManager transact = ownerDocument.getBrokerPool().getTransactionManager();
    final org.exist.dom.NodeListImpl nl = new org.exist.dom.NodeListImpl();
    nl.add(newChild);
    try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker();
        final Txn transaction = transact.beginTransaction()) {
        appendChildren(transaction, nl, 0);
        broker.storeXMLResource(transaction, getOwnerDocument());
        // bugID 3419602
        transact.commit(transaction);
        return getLastChild();
    } catch (final Exception e) {
        throw new DOMException(DOMException.INVALID_STATE_ERR, e.getMessage());
    }
}
Also used : org.w3c.dom(org.w3c.dom) TransactionManager(org.exist.storage.txn.TransactionManager) NodeId(org.exist.numbering.NodeId) Txn(org.exist.storage.txn.Txn) XMLStreamException(javax.xml.stream.XMLStreamException) EXistException(org.exist.EXistException) IllegalQNameException(org.exist.dom.QName.IllegalQNameException) IOException(java.io.IOException) TransactionException(org.exist.storage.txn.TransactionException)

Aggregations

Txn (org.exist.storage.txn.Txn)358 DBBroker (org.exist.storage.DBBroker)215 BrokerPool (org.exist.storage.BrokerPool)179 Collection (org.exist.collections.Collection)162 TransactionManager (org.exist.storage.txn.TransactionManager)129 Sequence (org.exist.xquery.value.Sequence)84 StringInputSource (org.exist.util.StringInputSource)83 Test (org.junit.Test)80 XmldbURI (org.exist.xmldb.XmldbURI)55 EXistException (org.exist.EXistException)50 PermissionDeniedException (org.exist.security.PermissionDeniedException)38 Source (org.exist.source.Source)37 StringSource (org.exist.source.StringSource)37 DocumentImpl (org.exist.dom.persistent.DocumentImpl)35 InputSource (org.xml.sax.InputSource)33 XQuery (org.exist.xquery.XQuery)32 IOException (java.io.IOException)31 LockedDocument (org.exist.dom.persistent.LockedDocument)28 InputStream (java.io.InputStream)27 Path (java.nio.file.Path)24