Search in sources :

Example 6 with ContentAlreadyExistsException

use of com.enonic.xp.content.ContentAlreadyExistsException in project xp by enonic.

the class MoveContentHandler method executeMove.

private Content executeMove() {
    final ContentId sourceId;
    final ContentPath sourcePath;
    if (this.source.startsWith("/")) {
        // source is path
        sourcePath = ContentPath.from(this.source);
        final Content sourceContent = contentService.getByPath(sourcePath);
        sourceId = sourceContent.getId();
    } else {
        // source is key
        sourceId = ContentId.from(this.source);
        final Content sourceContent = contentService.getById(sourceId);
        sourcePath = sourceContent.getPath();
    }
    if (target.endsWith("/")) {
        // /a/b -> /c/d/ => /c/d/b
        return move(sourceId, ContentPath.from(target).asAbsolute());
    } else if (!target.startsWith("/")) {
        // /a/b -> c => /a/c
        return rename(sourceId, target);
    } else {
        // rename+move to target path
        final ContentPath targetPath = ContentPath.from(target);
        final ContentPath targetParent = targetPath.getParentPath();
        if (targetParent.equals(sourcePath.getParentPath())) {
            // /a/b -> /a/c => /a/c
            return rename(sourceId, targetPath.getName());
        }
        // /a/b -> /c/d => /c/d
        if (contentService.contentExists(targetPath)) {
            final Context currentContext = ContextAccessor.current();
            throw new ContentAlreadyExistsException(targetPath, currentContext.getRepositoryId(), currentContext.getBranch());
        }
        // needs to be first renamed to temporary unique name to avoid clashing with siblings with same target name in source parent or with siblings with source name in target parent
        rename(sourceId, uniqueName());
        move(sourceId, targetParent);
        return rename(sourceId, targetPath.getName());
    }
}
Also used : Context(com.enonic.xp.context.Context) Content(com.enonic.xp.content.Content) ContentAlreadyExistsException(com.enonic.xp.content.ContentAlreadyExistsException) ContentId(com.enonic.xp.content.ContentId) ContentPath(com.enonic.xp.content.ContentPath)

Example 7 with ContentAlreadyExistsException

use of com.enonic.xp.content.ContentAlreadyExistsException in project xp by enonic.

the class CreateContentHandlerTest method createContentAlreadyExists.

@Test
public void createContentAlreadyExists() throws Exception {
    final Exception alreadyExistException = new ContentAlreadyExistsException(ContentPath.from("/a/b/mycontent"), RepositoryId.from("some.repo"), Branch.from("draft"));
    when(this.contentService.create(any(CreateContentParams.class))).thenThrow(alreadyExistException);
    final ContentType contentType = ContentType.create().name("test:myContentType").superType(ContentTypeName.structured()).build();
    GetContentTypeParams getContentType = GetContentTypeParams.from(ContentTypeName.from("test:myContentType"));
    when(this.contentTypeService.getByName(Mockito.eq(getContentType))).thenReturn(contentType);
    runFunction("/test/CreateContentHandlerTest.js", "createContentNameAlreadyExists");
}
Also used : GetContentTypeParams(com.enonic.xp.schema.content.GetContentTypeParams) ContentType(com.enonic.xp.schema.content.ContentType) CreateContentParams(com.enonic.xp.content.CreateContentParams) ContentAlreadyExistsException(com.enonic.xp.content.ContentAlreadyExistsException) ContentAlreadyExistsException(com.enonic.xp.content.ContentAlreadyExistsException) Test(org.junit.jupiter.api.Test)

Example 8 with ContentAlreadyExistsException

use of com.enonic.xp.content.ContentAlreadyExistsException in project xp by enonic.

the class CreateMediaHandlerTest method createMediaAutoGenerateNameWithExistingName.

@Test
public void createMediaAutoGenerateNameWithExistingName() throws Exception {
    final ContentAlreadyExistsException exception = new ContentAlreadyExistsException(ContentPath.from("/a/b/my-content.jpg"), RepositoryId.from("some.repo"), Branch.from("draft"));
    Mockito.when(this.contentService.create(Mockito.any(CreateMediaParams.class))).thenThrow(exception).thenAnswer(mock -> createContent((CreateMediaParams) mock.getArguments()[0]));
    Mockito.when(this.contentService.contentExists(Mockito.eq(ContentPath.from("/a/b/my-content.jpg")))).thenReturn(true);
    Mockito.when(this.contentService.contentExists(Mockito.eq(ContentPath.from("/a/b/my-content-1.jpg")))).thenReturn(true);
    Mockito.when(this.contentService.contentExists(Mockito.eq(ContentPath.from("/a/b/my-content-2.jpg")))).thenReturn(true);
    runFunction("/test/CreateMediaHandlerTest.js", "createMediaAutoGenerateName");
}
Also used : CreateMediaParams(com.enonic.xp.content.CreateMediaParams) ContentAlreadyExistsException(com.enonic.xp.content.ContentAlreadyExistsException) Test(org.junit.jupiter.api.Test)

Aggregations

ContentAlreadyExistsException (com.enonic.xp.content.ContentAlreadyExistsException)8 Content (com.enonic.xp.content.Content)3 NodeAlreadyExistAtPathException (com.enonic.xp.node.NodeAlreadyExistAtPathException)3 Test (org.junit.jupiter.api.Test)3 ContentAccessException (com.enonic.xp.content.ContentAccessException)2 ContentId (com.enonic.xp.content.ContentId)2 ContentPath (com.enonic.xp.content.ContentPath)2 CreateContentParams (com.enonic.xp.content.CreateContentParams)2 CreateMediaParams (com.enonic.xp.content.CreateMediaParams)2 Node (com.enonic.xp.node.Node)2 NodeAccessException (com.enonic.xp.node.NodeAccessException)2 ContentType (com.enonic.xp.schema.content.ContentType)2 GetContentTypeParams (com.enonic.xp.schema.content.GetContentTypeParams)2 Branch (com.enonic.xp.branch.Branch)1 CreateContentTranslatorParams (com.enonic.xp.content.CreateContentTranslatorParams)1 MoveContentException (com.enonic.xp.content.MoveContentException)1 MoveContentsResult (com.enonic.xp.content.MoveContentsResult)1 Context (com.enonic.xp.context.Context)1 ContentMapper (com.enonic.xp.lib.content.mapper.ContentMapper)1 CreateNodeParams (com.enonic.xp.node.CreateNodeParams)1