Skip to content

Commit

Permalink
Deprecate the scope argument in the modify function #10774
Browse files Browse the repository at this point in the history
  • Loading branch information
anatol-sialitski committed Dec 2, 2024
1 parent a67c476 commit fad5d17
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 119 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.enonic.xp.repository;

import java.util.Objects;
import java.util.Optional;

import com.google.common.base.Preconditions;

Expand Down Expand Up @@ -29,7 +28,7 @@ private Repository( Builder builder )
this.id = builder.id;
this.branches = builder.branches;
this.settings = builder.settings == null ? RepositorySettings.create().build() : builder.settings;
this.data = Optional.ofNullable( builder.data ).orElse( new PropertyTree() );
this.data = Objects.requireNonNullElseGet( builder.data, PropertyTree::new );
this.attachments = Objects.requireNonNullElseGet( builder.attachments, AttachedBinaries::empty );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.enonic.xp.context.ContextAccessor;
import com.enonic.xp.node.RefreshMode;
import com.enonic.xp.repo.impl.index.IndexServiceInternal;
import com.enonic.xp.repo.impl.repository.IndexNameResolver;
import com.enonic.xp.repository.IndexException;
import com.enonic.xp.repository.RepositoryId;
import com.enonic.xp.security.SystemConstants;

public class RefreshCommand
{
Expand All @@ -24,7 +26,8 @@ private RefreshCommand( Builder builder )

public void execute()
{
final RepositoryId repositoryId = ContextAccessor.current().getRepositoryId();
final RepositoryId repositoryId =
Objects.requireNonNullElse( ContextAccessor.current().getRepositoryId(), SystemConstants.SYSTEM_REPO_ID );

if ( !indexServiceInternal.indicesExists( IndexNameResolver.resolveStorageIndexName( repositoryId ) ) )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,13 @@ public class ModifyRepositoryHandler

private ScriptValue editor;

private String scope;

private Supplier<RepositoryService> repositoryServiceSupplier;

public void setId( final String id )
{
this.id = id;
}

public void setScope( final String scope )
{
this.scope = scope;
}

public void setEditor( final ScriptValue editor )
{
this.editor = editor;
Expand All @@ -62,29 +55,13 @@ private void editRepository( EditableRepository target )

private void updateRepositoryData( final EditableRepository target, final ScriptValue value )
{
if ( value == null )
{
if ( scope != null )
{
target.data.removeProperty( scope );
}
}
else
if ( value != null )
{
final ScriptValueTranslatorResult scriptValueTranslatorResult = new ScriptValueTranslator().create( value );

target.binaryAttachments = ImmutableList.copyOf( scriptValueTranslatorResult.getBinaryAttachments() );

final PropertyTree propertyTree = scriptValueTranslatorResult.getPropertyTree();

if ( scope == null )
{
target.data = propertyTree;
}
else
{
target.data.setSet( scope, propertyTree.getRoot().copy( target.data ) );
}
target.data = propertyTree.getRoot().getSet( "data" ).toTree();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ const valueLib = require('/lib/xp/value');
const stream = testInstance.createByteSource('Hello World');

// Editor to call for repo.
function editor(repoData) {
function editor(repo) {

repoData.myString = 'modified';
repoData.myArray = ['modified1', 'modified2', 'modified3'];
repo.data.myString = 'modified';
repo.data.myArray = ['modified1', 'modified2', 'modified3'];

repoData.myBinaryReference = valueLib.binary('myFile', stream);
repo.data.myBinaryReference = valueLib.binary('myFile', stream);

delete repoData.toBeRemoved;
delete repo.data.toBeRemoved;

return repoData;
return repo;
}

// BEGIN
Expand Down

This file was deleted.

This file was deleted.

10 changes: 6 additions & 4 deletions modules/lib/lib-repo/src/main/resources/lib/xp/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ export type EditorFn<T> = (value: T) => T;
export interface ModifyRepositoryParams {
id: string;
editor: EditorFn<Repository>;
// Deprecated. Should be removed in 8.0
scope?: string | null;
}

Expand All @@ -316,8 +317,6 @@ interface ModifyRepositoryHandler {

setEditor(value: ScriptValue): void;

setScope(value?: string | null): void;

execute(): Repository;
}

Expand All @@ -328,21 +327,24 @@ interface ModifyRepositoryHandler {
*
* @param {object} params JSON with the parameters.
* @param {string} params.id Repository ID.
* @param {string} [params.scope] Scope of the data to retrieve and update.
* @param {string} [params.scope] Deprecated: Scope of the data to retrieve and update.
* @param {function} params.editor Editor callback function.
*
* @returns {object} Repository updated as JSON.
*
*/
export function modify(params: ModifyRepositoryParams): Repository {
if (params.scope) {
throw `The parameter 'scope' is not supported`;
}

checkRequired(params, 'id');
checkRequired(params, 'editor');

const bean: ModifyRepositoryHandler = __.newBean<ModifyRepositoryHandler>('com.enonic.xp.lib.repo.ModifyRepositoryHandler');

bean.setId(params.id);
bean.setEditor(__.toScriptValue(params.editor));
bean.setScope(__.nullOrValue(params.scope));

return __.toNativeObject(bean.execute());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,47 +83,6 @@ void modify()

}

@Test
void modifyScoped()
throws Exception
{
runScript( "/lib/xp/examples/repo/modifyScoped.js" );

ArgumentCaptor<UpdateRepositoryParams> captor = ArgumentCaptor.forClass( UpdateRepositoryParams.class );
Mockito.verify( this.repositoryService, Mockito.times( 1 ) ).updateRepository( captor.capture() );

final UpdateRepositoryParams capturedParams = captor.getValue();

final EditableRepository edited = new EditableRepository( MOCK_REPO );
capturedParams.getEditor().accept( edited );

assertEquals( "toBeModified", edited.source.getData().getString( "myScopedObject.myScopedString" ), "Test is invalid" );
assertEquals( "modified", edited.data.getString( "myScopedObject.myScopedString" ) );

assertEquals( "toBeKeptValue", edited.data.getString( "toBeKept" ) );

final BinaryAttachment attachment = edited.binaryAttachments.get( 0 );
assertEquals( BinaryReference.from( "myFile" ), attachment.getReference() );
assertTrue( attachment.getByteSource().contentEquals( ByteSource.wrap( "Hello World".getBytes() ) ) );
}

@Test
void modifyRemoveProperty()
{
runScript( "/lib/xp/examples/repo/modifyRemoveProperty.js" );

ArgumentCaptor<UpdateRepositoryParams> captor = ArgumentCaptor.forClass( UpdateRepositoryParams.class );
Mockito.verify( this.repositoryService, Mockito.times( 1 ) ).updateRepository( captor.capture() );

final UpdateRepositoryParams capturedParams = captor.getValue();

final EditableRepository edited = new EditableRepository( MOCK_REPO );
capturedParams.getEditor().accept( edited );

assertTrue( edited.source.getData().hasProperty( "myScopedObject" ), "Test is invalid" );
assertFalse( edited.data.hasProperty( "myScopedObject" ) );
}

@SuppressWarnings("unused")
public ByteSource createByteSource( final String value )
{
Expand Down

0 comments on commit fad5d17

Please sign in to comment.