11/*
2+ * Elemental
3+ * Copyright (C) 2024, Evolved Binary Ltd
4+ *
5+ 6+ * https://www.evolvedbinary.com | https://www.elemental.xyz
7+ *
8+ * This library is free software; you can redistribute it and/or
9+ * modify it under the terms of the GNU Lesser General Public
10+ * License as published by the Free Software Foundation; version 2.1.
11+ *
12+ * This library is distributed in the hope that it will be useful,
13+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+ * Lesser General Public License for more details.
16+ *
17+ * You should have received a copy of the GNU Lesser General Public
18+ * License along with this library; if not, write to the Free Software
19+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+ *
21+ * NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+ * The original license header is included below.
23+ *
24+ * =====================================================================
25+ *
226 * eXist-db Open Source Native XML Database
327 * Copyright (C) 2001 The eXist-db Authors
428 *
3963import org .exist .xquery .XPathException ;
4064import org .w3c .dom .NodeList ;
4165
66+ import javax .annotation .Nullable ;
67+
4268/**
4369 * Implements an XUpdate insert-after or insert-before modification.
4470 *
@@ -61,8 +87,7 @@ public class Insert extends Modification {
6187 * @param namespaces the namespaces.
6288 * @param variables the variables.
6389 */
64- public Insert (DBBroker broker , DocumentSet docs , String selectStmt ,
65- Map <String , String > namespaces , Map <String , Object > variables ) {
90+ public Insert (final DBBroker broker , final DocumentSet docs , final String selectStmt , @ Nullable final Map <String , String > namespaces , @ Nullable final Map <String , Object > variables ) {
6691 super (broker , docs , selectStmt , namespaces , variables );
6792 }
6893
@@ -76,44 +101,50 @@ public Insert(DBBroker broker, DocumentSet docs, String selectStmt,
76101 * @param namespaces the namespaces.
77102 * @param variables the variables.
78103 */
79- public Insert (DBBroker broker , DocumentSet docs , String selectStmt ,
80- int mode , Map <String , String > namespaces , Map <String , Object > variables ) {
104+ public Insert (final DBBroker broker , final DocumentSet docs , final String selectStmt , final int mode , @ Nullable final Map <String , String > namespaces , @ Nullable final Map <String , Object > variables ) {
81105 this (broker , docs , selectStmt , namespaces , variables );
82106 this .mode = mode ;
83107 }
84108
85109 @ Override
86- public long process (Txn transaction ) throws PermissionDeniedException , LockException ,
87- EXistException , XPathException , TriggerException {
110+ public long process (final Txn transaction ) throws PermissionDeniedException , LockException , EXistException , XPathException , TriggerException {
88111 final NodeList children = content ;
89- if (children .getLength () == 0 ) {return 0 ;}
112+ if (children .getLength () == 0 ) {
113+ return 0 ;
114+ }
115+
90116 try {
91117 final StoredNode [] ql = selectAndLock (transaction );
92118 final NotificationService notifier = broker .getBrokerPool ().getNotificationService ();
93119 final int len = children .getLength ();
94- if (LOG .isDebugEnabled ())
95- {
96- LOG .debug ("found {} nodes to insert" , len );}
120+ if (LOG .isDebugEnabled ()) {
121+ LOG .debug ("found {} nodes to insert" , len );
122+ }
123+
97124 for (final StoredNode node : ql ) {
98125 final DocumentImpl doc = node .getOwnerDocument ();
99126 if (!doc .getPermissions ().validate (broker .getCurrentSubject (), Permission .WRITE )) {
100127 throw new PermissionDeniedException ("permission to update document denied" );
101128 }
129+
102130 final NodeImpl parent = (NodeImpl ) getParent (node );
103131 switch (mode ) {
132+
104133 case INSERT_BEFORE :
105134 parent .insertBefore (transaction , children , node );
106135 break ;
136+
107137 case INSERT_AFTER :
108138 parent .insertAfter (transaction , children , node );
109139 break ;
110140 }
141+
111142 doc .setLastModified (System .currentTimeMillis ());
112- modifiedDocuments . add (doc );
143+ addModifiedDocument (doc );
113144 broker .storeXMLResource (transaction , doc );
114145 notifier .notifyUpdate (doc , UpdateListener .UPDATE );
115146 }
116- checkFragmentation (transaction , modifiedDocuments );
147+ checkFragmentation (transaction );
117148 return ql .length ;
118149 } finally {
119150 unlockDocuments (transaction );
0 commit comments