From df09ad3f5c9df35eb19b7cef5f9f4f1e3df099ae Mon Sep 17 00:00:00 2001 From: jensp Date: Mon, 12 Sep 2016 17:20:27 +0000 Subject: [PATCH] CCM NG: Some work on the ContentItemManager and related classes (not finished yet) git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4294 8810af33-2d31-482b-a856-94f89814c4df --- .../contentsection/ContentItemManager.java | 93 +++++++++++++------ .../{CmsTest.java => CmsTest.java.off} | 0 .../ContentItemManagerTest.java | 56 ++--------- .../librecms/contentsection/DatasetsTest.java | 4 +- .../scripts/create_ccm_cms_schema.sql | 8 +- .../scripts/create_ccm_cms_schema.sql | 8 +- ...fter-create-contentitem-with-workflow.xml} | 4 +- .../after-create-contentitem.xml | 4 +- 8 files changed, 91 insertions(+), 86 deletions(-) rename ccm-cms/src/test/java/org/librecms/{CmsTest.java => CmsTest.java.off} (100%) rename ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/{after-create-contentitem-with-lifecycle-and-workflow.xml => after-create-contentitem-with-workflow.xml} (99%) diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemManager.java b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemManager.java index 0a87e0438..f141851fe 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemManager.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemManager.java @@ -99,7 +99,7 @@ public class ContentItemManager { /** * Creates a new content item in the provided content section and folder - * with the default lifecycle and workflow. + * with the workflow. * * The folder must be a subfolder of the * {@link ContentSection#rootDocumentsFolder} of the provided content @@ -134,30 +134,29 @@ public class ContentItemManager { section, folder, contentType.get().getDefaultWorkflow(), - contentType.get().getDefaultLifecycle(), type); } /** * Creates a new content item in the provided content section and folder - * with the provided lifecycle and workflow. + * with specific workflow. * * The folder must be a subfolder of the * {@link ContentSection#rootDocumentsFolder} of the provided content * section. Otherwise an {@link IllegalArgumentException} is thrown. * - * Likewise the provided {@link LifecycleDefinition} and - * {@link WorkflowTemplate} must be defined in the provided content section. - * Otherwise an {@link IllegalArgumentException} is thrown. + * Likewise the provided {@link WorkflowTemplate} must be defined in the + * provided content section. Otherwise an {@link IllegalArgumentException} + * is thrown. * - * @param The type of the content item. - * @param name The name (URL stub) of the new content item. - * @param section The content section in which the item is - * generated. - * @param folder The folder in which in the item is stored. - * @param workflowTemplate - * @param lifecycleDefinition - * @param type The type of the new content item. + * @param The type of the content item. + * @param name The name (URL stub) of the new content item. + * @param section The content section in which the item is + * generated. + * @param folder The folder in which in the item is stored. + * @param workflowTemplate The template for the workflow to apply to the new + * item. + * @param type The type of the new content item. * * @return The new content item. */ @@ -167,7 +166,6 @@ public class ContentItemManager { final ContentSection section, final Category folder, final WorkflowTemplate workflowTemplate, - final LifecycleDefinition lifecycleDefinition, final Class type) { final Optional contentType = typeRepo @@ -206,11 +204,6 @@ public class ContentItemManager { item.setVersion(ContentItemVersion.DRAFT); item.setContentType(contentType.get()); - if (lifecycleDefinition != null) { - final Lifecycle lifecycle = lifecycleManager.createLifecycle( - lifecycleDefinition); - item.setLifecycle(lifecycle); - } if (workflowTemplate != null) { final Workflow workflow = workflowManager.createWorkflow( workflowTemplate); @@ -302,20 +295,23 @@ public class ContentItemManager { final ContentItem copy; try { - copy = item.getClass().newInstance(); + copy = draftItem.getClass().newInstance(); } catch (InstantiationException | IllegalAccessException ex) { throw new RuntimeException(ex); } copy.setContentType(contentType.get()); - final Lifecycle lifecycle = lifecycleManager.createLifecycle( - contentType.get().getDefaultLifecycle()); final Workflow workflow = workflowManager.createWorkflow(contentType .get().getDefaultWorkflow()); - copy.setLifecycle(lifecycle); - copy.setWorkflow(workflow); + if (draftItem.getWorkflow() != null) { + final WorkflowTemplate template = draftItem.getWorkflow() + .getTemplate(); + final Workflow copyWorkflow = workflowManager.createWorkflow( + template); + copy.setWorkflow(copyWorkflow); + } draftItem.getCategories().forEach(categorization -> categoryManager .addObjectToCategory(copy, categorization.getCategory())); @@ -360,7 +356,7 @@ public class ContentItemManager { if (writeMethod == null) { continue; } - + if (LocalizedString.class.equals(propType)) { final LocalizedString source; final LocalizedString target; @@ -474,14 +470,49 @@ public class ContentItemManager { /** * Creates a live version of content item or updates the live version of a - * content item if there already a live version. + * content item if there already a live version using the default lifecycle + * for the content type of the provided item. * * @param item The content item to publish. * * @return The published content item. */ - @SuppressWarnings("unchecked") public ContentItem publish(final ContentItem item) { + if (item == null) { + throw new IllegalArgumentException( + "The item to publish can't be null."); + } + + final LifecycleDefinition lifecycleDefinition = item.getContentType() + .getDefaultLifecycle(); + + return publish(item, lifecycleDefinition); + } + + /** + * Creates a live version of content item or updates the live version of a + * content item if there already a live version. + * + * @param item The content item to publish. + * @param lifecycleDefinition The definition of the lifecycle to use for the + * new item. + * + * @return The published content item. + */ + @SuppressWarnings("unchecked") + public ContentItem publish(final ContentItem item, + final LifecycleDefinition lifecycleDefinition) { + if (item == null) { + throw new IllegalArgumentException( + "The item to publish can't be null."); + } + + if (lifecycleDefinition == null) { + throw new IllegalArgumentException( + "The lifecycle definition for the " + + "lifecycle of the item to publish can't be null."); + } + final ContentItem draftItem = getDraftVersion(item, ContentItem.class); final ContentItem liveItem; @@ -496,7 +527,11 @@ public class ContentItemManager { } liveItem.setContentType(draftItem.getContentType()); - liveItem.setLifecycle(draftItem.getLifecycle()); + + final Lifecycle lifecycle = lifecycleManager.createLifecycle( + lifecycleDefinition); + + liveItem.setLifecycle(lifecycle); liveItem.setWorkflow(draftItem.getWorkflow()); draftItem.getCategories().forEach(categorization -> categoryManager diff --git a/ccm-cms/src/test/java/org/librecms/CmsTest.java b/ccm-cms/src/test/java/org/librecms/CmsTest.java.off similarity index 100% rename from ccm-cms/src/test/java/org/librecms/CmsTest.java rename to ccm-cms/src/test/java/org/librecms/CmsTest.java.off diff --git a/ccm-cms/src/test/java/org/librecms/contentsection/ContentItemManagerTest.java b/ccm-cms/src/test/java/org/librecms/contentsection/ContentItemManagerTest.java index 60c3329ca..f6777a91b 100644 --- a/ccm-cms/src/test/java/org/librecms/contentsection/ContentItemManagerTest.java +++ b/ccm-cms/src/test/java/org/librecms/contentsection/ContentItemManagerTest.java @@ -49,7 +49,6 @@ import org.libreccm.workflow.WorkflowTemplate; import org.libreccm.workflow.WorkflowTemplateRepository; import org.librecms.contenttypes.Article; import org.librecms.contenttypes.Event; -import org.librecms.lifecycle.LifecycleDefinition; import org.librecms.lifecycle.LifecycleDefinitionRepository; import java.io.File; @@ -254,8 +253,6 @@ public class ContentItemManagerTest { assertThat("Name has not the expected value.", article.getName().getValue(locale), is(equalTo("new-article"))); - assertThat("lifecycle was not added to content item.", - article.getLifecycle(), is(not(nullValue()))); assertThat("workflow was not added to content item.", article.getWorkflow(), is(not(nullValue()))); @@ -333,7 +330,7 @@ public class ContentItemManagerTest { @ShouldMatchDataSet( value = "datasets/org/librecms/contentsection/" + "ContentItemManagerTest/" - + "after-create-contentitem-with-lifecycle-and-workflow.xml", + + "after-create-contentitem-with-workflow.xml", excludeColumns = {"categorization_id", "lifecycle_id", "object_id", @@ -343,21 +340,18 @@ public class ContentItemManagerTest { "uuid", "workflow_id" }) - public void createContentItemWithLifecycleAndWorkflow() { + public void createContentItemWithWorkflow() { final ContentSection section = sectionRepo.findByLabel("info"); final Category folder = section.getRootDocumentsFolder(); final WorkflowTemplate workflowTemplate = workflowTemplateRepo .findById(-110L); - final LifecycleDefinition lifecycleDefinition = lifecycleDefinitionRepo - .findById(-210L); - + final Article article = itemManager.createContentItem( "new-article", section, folder, workflowTemplate, - lifecycleDefinition, Article.class); assertThat("DisplayName has not the expected value.", @@ -366,8 +360,6 @@ public class ContentItemManagerTest { assertThat("Name has not the expected value.", article.getName().getValue(locale), is(equalTo("new-article"))); - assertThat("lifecycle was not added to content item.", - article.getLifecycle(), is(not(nullValue()))); assertThat("workflow was not added to content item.", article.getWorkflow(), is(not(nullValue()))); @@ -390,20 +382,17 @@ public class ContentItemManagerTest { @ShouldMatchDataSet("datasets/org/librecms/contentsection/" + "ContentItemManagerTest/data.xml") @ShouldThrowException(IllegalArgumentException.class) - public void createItemTypeNotInSectionWithLifecycleAndWorkflow() { + public void createItemTypeNotInSectionWithWorkflow() { final ContentSection section = sectionRepo.findByLabel("info"); final Category folder = section.getRootDocumentsFolder(); final WorkflowTemplate workflowTemplate = workflowTemplateRepo .findById(-110L); - final LifecycleDefinition lifecycleDefinition = lifecycleDefinitionRepo - .findById(-210L); itemManager.createContentItem("Test", section, folder, workflowTemplate, - lifecycleDefinition, Event.class); } @@ -414,20 +403,17 @@ public class ContentItemManagerTest { @ShouldMatchDataSet("datasets/org/librecms/contentsection/" + "ContentItemManagerTest/data.xml") @ShouldThrowException(IllegalArgumentException.class) - public void createItemNameIsNullWithLifecycleAndWorkflow() { + public void createItemNameIsNullWithWorkflow() { final ContentSection section = sectionRepo.findByLabel("info"); final Category folder = section.getRootDocumentsFolder(); final WorkflowTemplate workflowTemplate = workflowTemplateRepo .findById(-110L); - final LifecycleDefinition lifecycleDefinition = lifecycleDefinitionRepo - .findById(-210L); itemManager.createContentItem(null, section, folder, workflowTemplate, - lifecycleDefinition, Article.class); } @@ -442,37 +428,10 @@ public class ContentItemManagerTest { final ContentSection section = sectionRepo.findByLabel("info"); final Category folder = section.getRootDocumentsFolder(); - final LifecycleDefinition lifecycleDefinition = lifecycleDefinitionRepo - .findById(-210L); - itemManager.createContentItem(null, section, folder, null, - lifecycleDefinition, - Article.class); - } - - // Create content item with lifecycle and workflow name empty - @Test(expected = IllegalArgumentException.class) - @InSequence(2500) - @UsingDataSet("datasets/org/librecms/contentsection/" - + "ContentItemManagerTest/data.xml") - @ShouldMatchDataSet("datasets/org/librecms/contentsection/" - + "ContentItemManagerTest/data.xml") - @ShouldThrowException(IllegalArgumentException.class) - public void createItemNoLifecycle() { - final ContentSection section = sectionRepo.findByLabel("info"); - final Category folder = section.getRootDocumentsFolder(); - - final WorkflowTemplate workflowTemplate = workflowTemplateRepo - .findById(-110L); - - itemManager.createContentItem(null, - section, - folder, - workflowTemplate, - null, Article.class); } @@ -483,19 +442,16 @@ public class ContentItemManagerTest { @ShouldMatchDataSet("datasets/org/librecms/contentsection/" + "ContentItemManagerTest/data.xml") @ShouldThrowException(IllegalArgumentException.class) - public void createItemFolderIsNullWithLifecycleAndWorkflow() { + public void createItemFolderIsNullWithWorkflow() { final ContentSection section = sectionRepo.findByLabel("info"); final WorkflowTemplate workflowTemplate = workflowTemplateRepo .findById(-110L); - final LifecycleDefinition lifecycleDefinition = lifecycleDefinitionRepo - .findById(-210L); itemManager.createContentItem("Test", section, null, workflowTemplate, - lifecycleDefinition, Article.class); } diff --git a/ccm-cms/src/test/java/org/librecms/contentsection/DatasetsTest.java b/ccm-cms/src/test/java/org/librecms/contentsection/DatasetsTest.java index 0b00f10a6..24c4714a3 100644 --- a/ccm-cms/src/test/java/org/librecms/contentsection/DatasetsTest.java +++ b/ccm-cms/src/test/java/org/librecms/contentsection/DatasetsTest.java @@ -48,10 +48,12 @@ public class DatasetsTest extends DatasetsVerifier { "/datasets/org/librecms/contentsection/ContentSectionManagerTest/after-create.xml", "/datasets/org/librecms/contentsection/ContentSectionManagerTest/after-remove-role.xml", "/datasets/org/librecms/contentsection/ContentSectionManagerTest/after-rename.xml", + "/datasets/org/librecms/contentsection/ContentItemRepositoryTest/data.xml", + "/datasets/org/librecms/contentsection/ContentItemManagerTest/data.xml", "/datasets/org/librecms/contentsection/ContentItemManagerTest/after-create-contentitem.xml", - "/datasets/org/librecms/contentsection/ContentItemManagerTest/after-create-contentitem-with-lifecycle-and-workflow.xml", + "/datasets/org/librecms/contentsection/ContentItemManagerTest/after-create-contentitem-with-workflow.xml", "/datasets/org/librecms/contentsection/ContentItemManagerTest/move-before.xml", "/datasets/org/librecms/contentsection/ContentItemManagerTest/move-after.xml", "/datasets/org/librecms/contentsection/ContentItemManagerTest/copy-to-other-folder-after.xml", diff --git a/ccm-cms/src/test/resources-wildfly-remote-h2-mem/scripts/create_ccm_cms_schema.sql b/ccm-cms/src/test/resources-wildfly-remote-h2-mem/scripts/create_ccm_cms_schema.sql index 62b9c42b6..d6f22fd72 100644 --- a/ccm-cms/src/test/resources-wildfly-remote-h2-mem/scripts/create_ccm_cms_schema.sql +++ b/ccm-cms/src/test/resources-wildfly-remote-h2-mem/scripts/create_ccm_cms_schema.sql @@ -1457,6 +1457,7 @@ create table CCM_CMS.ARTICLE_LEADS ( create table CCM_CORE.WORKFLOWS ( WORKFLOW_ID bigint not null, + TEMPLATE_ID bigint, primary key (WORKFLOW_ID) ); @@ -2753,4 +2754,9 @@ create sequence hibernate_sequence start with 1 increment by 1; alter table CCM_CMS.CONTENT_SECTION_WORKFLOW_TEMPLATES add constraint FK6kuejkcl9hcbkr8q6bdlatt8q foreign key (CONTENT_SECTION_ID) - references CCM_CMS.CONTENT_SECTIONS; \ No newline at end of file + references CCM_CMS.CONTENT_SECTIONS; + + alter table CCM_CORE.WORKFLOWS + add constraint FKol71r1t83h0qe65gglq43far2 + foreign key (TEMPLATE_ID) + references CCM_CORE.WORKFLOW_TEMPLATES; \ No newline at end of file diff --git a/ccm-cms/src/test/resources-wildfly-remote-pgsql/scripts/create_ccm_cms_schema.sql b/ccm-cms/src/test/resources-wildfly-remote-pgsql/scripts/create_ccm_cms_schema.sql index 30154a888..d10765aa7 100644 --- a/ccm-cms/src/test/resources-wildfly-remote-pgsql/scripts/create_ccm_cms_schema.sql +++ b/ccm-cms/src/test/resources-wildfly-remote-pgsql/scripts/create_ccm_cms_schema.sql @@ -1457,6 +1457,7 @@ CREATE SCHEMA ccm_cms; create table CCM_CORE.WORKFLOWS ( WORKFLOW_ID int8 not null, + TEMPLATE_ID int8, primary key (WORKFLOW_ID) ); @@ -2753,4 +2754,9 @@ create sequence hibernate_sequence start 1 increment 1; alter table CCM_CMS.CONTENT_SECTION_WORKFLOW_TEMPLATES add constraint FK6kuejkcl9hcbkr8q6bdlatt8q foreign key (CONTENT_SECTION_ID) - references CCM_CMS.CONTENT_SECTIONS; \ No newline at end of file + references CCM_CMS.CONTENT_SECTIONS; + + alter table CCM_CORE.WORKFLOWS + add constraint FKol71r1t83h0qe65gglq43far2 + foreign key (template_id) + references CCM_CORE.WORKFLOW_TEMPLATES; \ No newline at end of file diff --git a/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/after-create-contentitem-with-lifecycle-and-workflow.xml b/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/after-create-contentitem-with-workflow.xml similarity index 99% rename from ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/after-create-contentitem-with-lifecycle-and-workflow.xml rename to ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/after-create-contentitem-with-workflow.xml index 593a90164..d19e69256 100644 --- a/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/after-create-contentitem-with-lifecycle-and-workflow.xml +++ b/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/after-create-contentitem-with-workflow.xml @@ -94,14 +94,14 @@ localized_value="The only phase" locale="en" /> - + finished="false" />--> - @@ -114,7 +114,7 @@ finished="false" /> + finished="false" />-->