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());
}
}
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");
}
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");
}
Aggregations