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);
}
}
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());
}
}
}
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;
}
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());
}
}
}
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());
}
}
Aggregations