diff --git a/modules/core/core-api/src/main/java/com/enonic/xp/repository/Repository.java b/modules/core/core-api/src/main/java/com/enonic/xp/repository/Repository.java index 416893f2708..50f20b97997 100644 --- a/modules/core/core-api/src/main/java/com/enonic/xp/repository/Repository.java +++ b/modules/core/core-api/src/main/java/com/enonic/xp/repository/Repository.java @@ -1,7 +1,6 @@ package com.enonic.xp.repository; import java.util.Objects; -import java.util.Optional; import com.google.common.base.Preconditions; @@ -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 ); } diff --git a/modules/core/core-repo/src/main/java/com/enonic/xp/repo/impl/node/RefreshCommand.java b/modules/core/core-repo/src/main/java/com/enonic/xp/repo/impl/node/RefreshCommand.java index f35abc0d5e1..c9165fb0ad8 100644 --- a/modules/core/core-repo/src/main/java/com/enonic/xp/repo/impl/node/RefreshCommand.java +++ b/modules/core/core-repo/src/main/java/com/enonic/xp/repo/impl/node/RefreshCommand.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Objects; import com.enonic.xp.context.ContextAccessor; import com.enonic.xp.node.RefreshMode; @@ -9,6 +10,7 @@ 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 { @@ -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 ) ) ) { diff --git a/modules/lib/lib-repo/src/main/java/com/enonic/xp/lib/repo/ModifyRepositoryHandler.java b/modules/lib/lib-repo/src/main/java/com/enonic/xp/lib/repo/ModifyRepositoryHandler.java index 97aadb23205..9bfd833174a 100644 --- a/modules/lib/lib-repo/src/main/java/com/enonic/xp/lib/repo/ModifyRepositoryHandler.java +++ b/modules/lib/lib-repo/src/main/java/com/enonic/xp/lib/repo/ModifyRepositoryHandler.java @@ -24,8 +24,6 @@ public class ModifyRepositoryHandler private ScriptValue editor; - private String scope; - private Supplier repositoryServiceSupplier; public void setId( final String id ) @@ -33,11 +31,6 @@ 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; @@ -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(); } } diff --git a/modules/lib/lib-repo/src/main/resources/lib/xp/examples/repo/modify.js b/modules/lib/lib-repo/src/main/resources/lib/xp/examples/repo/modify.js index db3ae6f8330..7c3ed7e98f7 100644 --- a/modules/lib/lib-repo/src/main/resources/lib/xp/examples/repo/modify.js +++ b/modules/lib/lib-repo/src/main/resources/lib/xp/examples/repo/modify.js @@ -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 diff --git a/modules/lib/lib-repo/src/main/resources/lib/xp/examples/repo/modifyRemoveProperty.js b/modules/lib/lib-repo/src/main/resources/lib/xp/examples/repo/modifyRemoveProperty.js deleted file mode 100644 index 13b07ce4b0c..00000000000 --- a/modules/lib/lib-repo/src/main/resources/lib/xp/examples/repo/modifyRemoveProperty.js +++ /dev/null @@ -1,17 +0,0 @@ -/* global Java, testInstance*/ -const repoLib = require('/lib/xp/repo.js'); - - -// Editor to call for repo. -function editor(repoData) { - return null; -} - -// BEGIN -// Update data -const result = repoLib.modify({ - scope: 'myScopedObject', - editor: editor, - id: 'my-repo' -}); -// END diff --git a/modules/lib/lib-repo/src/main/resources/lib/xp/examples/repo/modifyScoped.js b/modules/lib/lib-repo/src/main/resources/lib/xp/examples/repo/modifyScoped.js deleted file mode 100644 index 2cd1c407817..00000000000 --- a/modules/lib/lib-repo/src/main/resources/lib/xp/examples/repo/modifyScoped.js +++ /dev/null @@ -1,23 +0,0 @@ -/* global Java, testInstance*/ -const repoLib = require('/lib/xp/repo.js'); -const valueLib = require('/lib/xp/value'); - -const stream = testInstance.createByteSource('Hello World'); - -// Editor to call for repo. -function editor(repoData) { - - repoData.myScopedString = 'modified'; - repoData.myScopedBinaryReference = valueLib.binary('myFile', stream); - - return repoData; -} - -// BEGIN -// Update data -const result = repoLib.modify({ - scope: 'myScopedObject', - editor: editor, - id: 'my-repo' -}); -// END diff --git a/modules/lib/lib-repo/src/main/resources/lib/xp/repo.ts b/modules/lib/lib-repo/src/main/resources/lib/xp/repo.ts index 86539ec3106..652f93b03d5 100644 --- a/modules/lib/lib-repo/src/main/resources/lib/xp/repo.ts +++ b/modules/lib/lib-repo/src/main/resources/lib/xp/repo.ts @@ -308,6 +308,7 @@ export type EditorFn = (value: T) => T; export interface ModifyRepositoryParams { id: string; editor: EditorFn; + // Deprecated. Should be removed in 8.0 scope?: string | null; } @@ -316,8 +317,6 @@ interface ModifyRepositoryHandler { setEditor(value: ScriptValue): void; - setScope(value?: string | null): void; - execute(): Repository; } @@ -328,13 +327,17 @@ 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'); @@ -342,7 +345,6 @@ export function modify(params: ModifyRepositoryParams): Repository { bean.setId(params.id); bean.setEditor(__.toScriptValue(params.editor)); - bean.setScope(__.nullOrValue(params.scope)); return __.toNativeObject(bean.execute()); } diff --git a/modules/lib/lib-repo/src/test/java/com/enonic/xp/lib/repo/ModifyRepositoryHandlerTest.java b/modules/lib/lib-repo/src/test/java/com/enonic/xp/lib/repo/ModifyRepositoryHandlerTest.java index 94cffebf806..79e27824398 100644 --- a/modules/lib/lib-repo/src/test/java/com/enonic/xp/lib/repo/ModifyRepositoryHandlerTest.java +++ b/modules/lib/lib-repo/src/test/java/com/enonic/xp/lib/repo/ModifyRepositoryHandlerTest.java @@ -83,47 +83,6 @@ void modify() } - @Test - void modifyScoped() - throws Exception - { - runScript( "/lib/xp/examples/repo/modifyScoped.js" ); - - ArgumentCaptor 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 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 ) {