Skip to content

Commit

Permalink
Allow instant publish of modified data fields
Browse files Browse the repository at this point in the history
  • Loading branch information
vbradnitski committed Jan 28, 2025
1 parent 2b56c4e commit 15f2d21
Show file tree
Hide file tree
Showing 9 changed files with 671 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public interface NodeService

Node update( UpdateNodeParams params );

PatchNodeResult patch( PatchNodeParams params );

Node rename( RenameNodeParams params );

PushNodesResult push( NodeIds ids, Branch target );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.enonic.xp.node;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;

import com.enonic.xp.annotation.PublicApi;
import com.enonic.xp.branch.Branch;
import com.enonic.xp.branch.Branches;

@PublicApi
public final class PatchNodeParams
{
private final NodeId id;

private final NodePath path;

private final NodeEditor editor;

private final Branches branches;


private PatchNodeParams( final Builder builder )
{
this.id = builder.id;
this.path = builder.path;
this.editor = builder.editor;
branches = Branches.from( builder.branches.build() );

}

public static Builder create()
{
return new Builder();
}

public NodeId getId()
{
return id;
}

public NodePath getPath()
{
return path;
}

public NodeEditor getEditor()
{
return editor;
}

public Branches getBranches()
{
return branches;
}


public static final class Builder
{
private final ImmutableSet.Builder<Branch> branches = ImmutableSet.builder();

private NodeId id;

private NodePath path;

private NodeEditor editor;


private Builder()
{
}

public Builder id( final NodeId id )
{
this.id = id;
return this;
}

public Builder path( final NodePath path )
{
this.path = path;
return this;
}

public Builder editor( final NodeEditor editor )
{
this.editor = editor;
return this;
}

public Builder addBranches( final Branches branches )
{
this.branches.addAll( branches );
return this;
}

public PatchNodeParams build()
{
this.validate();
return new PatchNodeParams( this );
}

private void validate()
{
if ( this.id == null && this.path == null )
{
throw new NullPointerException( "id and path cannot be both null" );
}
Preconditions.checkNotNull( this.editor, "editor cannot be null" );
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.enonic.xp.node;

import java.util.List;

import com.google.common.collect.ImmutableList;

import com.enonic.xp.annotation.PublicApi;
import com.enonic.xp.branch.Branch;

@PublicApi
public final class PatchNodeResult
{
private final NodeId nodeId;

private final List<BranchResult> results;

private PatchNodeResult( Builder builder )
{
this.nodeId = builder.nodeId;
this.results = builder.results.build();

}

public static Builder create()
{
return new Builder();
}

public NodeId getNodeId()
{
return nodeId;
}

public List<BranchResult> getResults()
{
return results;
}

public Node getResult( final Branch branch )
{
return results.stream().filter( br -> br.branch.equals( branch ) ).map( BranchResult::node ).findAny().orElse( null );
}

public record BranchResult(Branch branch, Node node)
{

}

public static final class Builder
{
private final ImmutableList.Builder<BranchResult> results = ImmutableList.builder();

private NodeId nodeId;

private Builder()
{
}

public Builder addResult( Branch branch, Node node )
{
if ( nodeId == null )
{
nodeId = node.id();
}
else if ( !nodeId.equals( node.id() ) )
{
throw new IllegalArgumentException( "All nodes must have the same id" );
}

results.add( new BranchResult( branch, node ) );
return this;
}

public PatchNodeResult build()
{
return new PatchNodeResult( this );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class NodeEvents

public static final String NODE_UPDATED_EVENT = "node.updated";

public static final String NODE_PATCHED_EVENT = "node.patched";

public static final String NODE_MOVED_EVENT = "node.moved";

public static final String NODE_RENAMED_EVENT = "node.renamed";
Expand Down Expand Up @@ -68,6 +70,11 @@ public static Event updated( final Node updatedNode )
return event( NODE_UPDATED_EVENT, updatedNode );
}

public static Event patched( final Node updatedNode )
{
return event( NODE_PATCHED_EVENT, updatedNode );
}

public static Event permissionsUpdated( final Node updatedNode )
{
return event( NODE_PERMISSIONS_UPDATED, updatedNode );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public final class NodePermissionsResolver
public static void requireContextUserPermissionOrAdmin( final Permission permission, final Node node )
throws NodeAccessException
{
if ( node == null )
{
throw new IllegalArgumentException( "Node cannot be null" );
}

final AuthenticationInfo authInfo = ContextAccessor.current().getAuthInfo();
final boolean hasPermission = hasPermission( authInfo.getPrincipals(), permission, node.getPermissions() );
if ( !hasPermission )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
import com.enonic.xp.node.NodeVersionQueryResult;
import com.enonic.xp.node.Nodes;
import com.enonic.xp.node.NodesHasChildrenResult;
import com.enonic.xp.node.PatchNodeParams;
import com.enonic.xp.node.PatchNodeResult;
import com.enonic.xp.node.PushNodesListener;
import com.enonic.xp.node.PushNodesResult;
import com.enonic.xp.node.RefreshMode;
Expand Down Expand Up @@ -476,6 +478,25 @@ public Node update( final UpdateNodeParams params )
return updatedNode;
}

@Override
public PatchNodeResult patch( final PatchNodeParams params )
{
verifyContext();
final PatchNodeResult result = PatchNodeCommand.create()
.params( params )
.indexServiceInternal( this.indexServiceInternal )
.storageService( this.nodeStorageService )
.searchService( this.nodeSearchService )
.build()
.execute();

final Node node = result.getResult( ContextAccessor.current().getBranch() );

this.eventPublisher.publish( NodeEvents.patched( node ) );

return result;
}

@Override
public Node rename( final RenameNodeParams params )
{
Expand Down
Loading

0 comments on commit 15f2d21

Please sign in to comment.