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
pull/2/head
jensp 2016-09-12 17:20:27 +00:00
parent cb0a19410e
commit df09ad3f5c
8 changed files with 91 additions and 86 deletions

View File

@ -99,7 +99,7 @@ public class ContentItemManager {
/** /**
* Creates a new content item in the provided content section and folder * 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 * The folder must be a subfolder of the
* {@link ContentSection#rootDocumentsFolder} of the provided content * {@link ContentSection#rootDocumentsFolder} of the provided content
@ -134,30 +134,29 @@ public class ContentItemManager {
section, section,
folder, folder,
contentType.get().getDefaultWorkflow(), contentType.get().getDefaultWorkflow(),
contentType.get().getDefaultLifecycle(),
type); type);
} }
/** /**
* Creates a new content item in the provided content section and folder * 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 * The folder must be a subfolder of the
* {@link ContentSection#rootDocumentsFolder} of the provided content * {@link ContentSection#rootDocumentsFolder} of the provided content
* section. Otherwise an {@link IllegalArgumentException} is thrown. * section. Otherwise an {@link IllegalArgumentException} is thrown.
* *
* Likewise the provided {@link LifecycleDefinition} and * Likewise the provided {@link WorkflowTemplate} must be defined in the
* {@link WorkflowTemplate} must be defined in the provided content section. * provided content section. Otherwise an {@link IllegalArgumentException}
* Otherwise an {@link IllegalArgumentException} is thrown. * is thrown.
* *
* @param <T> The type of the content item. * @param <T> The type of the content item.
* @param name The name (URL stub) of the new content item. * @param name The name (URL stub) of the new content item.
* @param section The content section in which the item is * @param section The content section in which the item is
* generated. * generated.
* @param folder The folder in which in the item is stored. * @param folder The folder in which in the item is stored.
* @param workflowTemplate * @param workflowTemplate The template for the workflow to apply to the new
* @param lifecycleDefinition * item.
* @param type The type of the new content item. * @param type The type of the new content item.
* *
* @return The new content item. * @return The new content item.
*/ */
@ -167,7 +166,6 @@ public class ContentItemManager {
final ContentSection section, final ContentSection section,
final Category folder, final Category folder,
final WorkflowTemplate workflowTemplate, final WorkflowTemplate workflowTemplate,
final LifecycleDefinition lifecycleDefinition,
final Class<T> type) { final Class<T> type) {
final Optional<ContentType> contentType = typeRepo final Optional<ContentType> contentType = typeRepo
@ -206,11 +204,6 @@ public class ContentItemManager {
item.setVersion(ContentItemVersion.DRAFT); item.setVersion(ContentItemVersion.DRAFT);
item.setContentType(contentType.get()); item.setContentType(contentType.get());
if (lifecycleDefinition != null) {
final Lifecycle lifecycle = lifecycleManager.createLifecycle(
lifecycleDefinition);
item.setLifecycle(lifecycle);
}
if (workflowTemplate != null) { if (workflowTemplate != null) {
final Workflow workflow = workflowManager.createWorkflow( final Workflow workflow = workflowManager.createWorkflow(
workflowTemplate); workflowTemplate);
@ -302,20 +295,23 @@ public class ContentItemManager {
final ContentItem copy; final ContentItem copy;
try { try {
copy = item.getClass().newInstance(); copy = draftItem.getClass().newInstance();
} catch (InstantiationException | IllegalAccessException ex) { } catch (InstantiationException | IllegalAccessException ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
copy.setContentType(contentType.get()); copy.setContentType(contentType.get());
final Lifecycle lifecycle = lifecycleManager.createLifecycle(
contentType.get().getDefaultLifecycle());
final Workflow workflow = workflowManager.createWorkflow(contentType final Workflow workflow = workflowManager.createWorkflow(contentType
.get().getDefaultWorkflow()); .get().getDefaultWorkflow());
copy.setLifecycle(lifecycle); if (draftItem.getWorkflow() != null) {
copy.setWorkflow(workflow); final WorkflowTemplate template = draftItem.getWorkflow()
.getTemplate();
final Workflow copyWorkflow = workflowManager.createWorkflow(
template);
copy.setWorkflow(copyWorkflow);
}
draftItem.getCategories().forEach(categorization -> categoryManager draftItem.getCategories().forEach(categorization -> categoryManager
.addObjectToCategory(copy, categorization.getCategory())); .addObjectToCategory(copy, categorization.getCategory()));
@ -360,7 +356,7 @@ public class ContentItemManager {
if (writeMethod == null) { if (writeMethod == null) {
continue; continue;
} }
if (LocalizedString.class.equals(propType)) { if (LocalizedString.class.equals(propType)) {
final LocalizedString source; final LocalizedString source;
final LocalizedString target; final LocalizedString target;
@ -474,14 +470,49 @@ public class ContentItemManager {
/** /**
* Creates a live version of content item or updates the live version of a * 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. * @param item The content item to publish.
* *
* @return The published content item. * @return The published content item.
*/ */
@SuppressWarnings("unchecked")
public ContentItem publish(final ContentItem item) { 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 draftItem = getDraftVersion(item, ContentItem.class);
final ContentItem liveItem; final ContentItem liveItem;
@ -496,7 +527,11 @@ public class ContentItemManager {
} }
liveItem.setContentType(draftItem.getContentType()); liveItem.setContentType(draftItem.getContentType());
liveItem.setLifecycle(draftItem.getLifecycle());
final Lifecycle lifecycle = lifecycleManager.createLifecycle(
lifecycleDefinition);
liveItem.setLifecycle(lifecycle);
liveItem.setWorkflow(draftItem.getWorkflow()); liveItem.setWorkflow(draftItem.getWorkflow());
draftItem.getCategories().forEach(categorization -> categoryManager draftItem.getCategories().forEach(categorization -> categoryManager

View File

@ -49,7 +49,6 @@ import org.libreccm.workflow.WorkflowTemplate;
import org.libreccm.workflow.WorkflowTemplateRepository; import org.libreccm.workflow.WorkflowTemplateRepository;
import org.librecms.contenttypes.Article; import org.librecms.contenttypes.Article;
import org.librecms.contenttypes.Event; import org.librecms.contenttypes.Event;
import org.librecms.lifecycle.LifecycleDefinition;
import org.librecms.lifecycle.LifecycleDefinitionRepository; import org.librecms.lifecycle.LifecycleDefinitionRepository;
import java.io.File; import java.io.File;
@ -254,8 +253,6 @@ public class ContentItemManagerTest {
assertThat("Name has not the expected value.", assertThat("Name has not the expected value.",
article.getName().getValue(locale), article.getName().getValue(locale),
is(equalTo("new-article"))); 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.", assertThat("workflow was not added to content item.",
article.getWorkflow(), is(not(nullValue()))); article.getWorkflow(), is(not(nullValue())));
@ -333,7 +330,7 @@ public class ContentItemManagerTest {
@ShouldMatchDataSet( @ShouldMatchDataSet(
value = "datasets/org/librecms/contentsection/" value = "datasets/org/librecms/contentsection/"
+ "ContentItemManagerTest/" + "ContentItemManagerTest/"
+ "after-create-contentitem-with-lifecycle-and-workflow.xml", + "after-create-contentitem-with-workflow.xml",
excludeColumns = {"categorization_id", excludeColumns = {"categorization_id",
"lifecycle_id", "lifecycle_id",
"object_id", "object_id",
@ -343,21 +340,18 @@ public class ContentItemManagerTest {
"uuid", "uuid",
"workflow_id" "workflow_id"
}) })
public void createContentItemWithLifecycleAndWorkflow() { public void createContentItemWithWorkflow() {
final ContentSection section = sectionRepo.findByLabel("info"); final ContentSection section = sectionRepo.findByLabel("info");
final Category folder = section.getRootDocumentsFolder(); final Category folder = section.getRootDocumentsFolder();
final WorkflowTemplate workflowTemplate = workflowTemplateRepo final WorkflowTemplate workflowTemplate = workflowTemplateRepo
.findById(-110L); .findById(-110L);
final LifecycleDefinition lifecycleDefinition = lifecycleDefinitionRepo
.findById(-210L);
final Article article = itemManager.createContentItem( final Article article = itemManager.createContentItem(
"new-article", "new-article",
section, section,
folder, folder,
workflowTemplate, workflowTemplate,
lifecycleDefinition,
Article.class); Article.class);
assertThat("DisplayName has not the expected value.", assertThat("DisplayName has not the expected value.",
@ -366,8 +360,6 @@ public class ContentItemManagerTest {
assertThat("Name has not the expected value.", assertThat("Name has not the expected value.",
article.getName().getValue(locale), article.getName().getValue(locale),
is(equalTo("new-article"))); 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.", assertThat("workflow was not added to content item.",
article.getWorkflow(), is(not(nullValue()))); article.getWorkflow(), is(not(nullValue())));
@ -390,20 +382,17 @@ public class ContentItemManagerTest {
@ShouldMatchDataSet("datasets/org/librecms/contentsection/" @ShouldMatchDataSet("datasets/org/librecms/contentsection/"
+ "ContentItemManagerTest/data.xml") + "ContentItemManagerTest/data.xml")
@ShouldThrowException(IllegalArgumentException.class) @ShouldThrowException(IllegalArgumentException.class)
public void createItemTypeNotInSectionWithLifecycleAndWorkflow() { public void createItemTypeNotInSectionWithWorkflow() {
final ContentSection section = sectionRepo.findByLabel("info"); final ContentSection section = sectionRepo.findByLabel("info");
final Category folder = section.getRootDocumentsFolder(); final Category folder = section.getRootDocumentsFolder();
final WorkflowTemplate workflowTemplate = workflowTemplateRepo final WorkflowTemplate workflowTemplate = workflowTemplateRepo
.findById(-110L); .findById(-110L);
final LifecycleDefinition lifecycleDefinition = lifecycleDefinitionRepo
.findById(-210L);
itemManager.createContentItem("Test", itemManager.createContentItem("Test",
section, section,
folder, folder,
workflowTemplate, workflowTemplate,
lifecycleDefinition,
Event.class); Event.class);
} }
@ -414,20 +403,17 @@ public class ContentItemManagerTest {
@ShouldMatchDataSet("datasets/org/librecms/contentsection/" @ShouldMatchDataSet("datasets/org/librecms/contentsection/"
+ "ContentItemManagerTest/data.xml") + "ContentItemManagerTest/data.xml")
@ShouldThrowException(IllegalArgumentException.class) @ShouldThrowException(IllegalArgumentException.class)
public void createItemNameIsNullWithLifecycleAndWorkflow() { public void createItemNameIsNullWithWorkflow() {
final ContentSection section = sectionRepo.findByLabel("info"); final ContentSection section = sectionRepo.findByLabel("info");
final Category folder = section.getRootDocumentsFolder(); final Category folder = section.getRootDocumentsFolder();
final WorkflowTemplate workflowTemplate = workflowTemplateRepo final WorkflowTemplate workflowTemplate = workflowTemplateRepo
.findById(-110L); .findById(-110L);
final LifecycleDefinition lifecycleDefinition = lifecycleDefinitionRepo
.findById(-210L);
itemManager.createContentItem(null, itemManager.createContentItem(null,
section, section,
folder, folder,
workflowTemplate, workflowTemplate,
lifecycleDefinition,
Article.class); Article.class);
} }
@ -442,37 +428,10 @@ public class ContentItemManagerTest {
final ContentSection section = sectionRepo.findByLabel("info"); final ContentSection section = sectionRepo.findByLabel("info");
final Category folder = section.getRootDocumentsFolder(); final Category folder = section.getRootDocumentsFolder();
final LifecycleDefinition lifecycleDefinition = lifecycleDefinitionRepo
.findById(-210L);
itemManager.createContentItem(null, itemManager.createContentItem(null,
section, section,
folder, folder,
null, 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); Article.class);
} }
@ -483,19 +442,16 @@ public class ContentItemManagerTest {
@ShouldMatchDataSet("datasets/org/librecms/contentsection/" @ShouldMatchDataSet("datasets/org/librecms/contentsection/"
+ "ContentItemManagerTest/data.xml") + "ContentItemManagerTest/data.xml")
@ShouldThrowException(IllegalArgumentException.class) @ShouldThrowException(IllegalArgumentException.class)
public void createItemFolderIsNullWithLifecycleAndWorkflow() { public void createItemFolderIsNullWithWorkflow() {
final ContentSection section = sectionRepo.findByLabel("info"); final ContentSection section = sectionRepo.findByLabel("info");
final WorkflowTemplate workflowTemplate = workflowTemplateRepo final WorkflowTemplate workflowTemplate = workflowTemplateRepo
.findById(-110L); .findById(-110L);
final LifecycleDefinition lifecycleDefinition = lifecycleDefinitionRepo
.findById(-210L);
itemManager.createContentItem("Test", itemManager.createContentItem("Test",
section, section,
null, null,
workflowTemplate, workflowTemplate,
lifecycleDefinition,
Article.class); Article.class);
} }

View File

@ -48,10 +48,12 @@ public class DatasetsTest extends DatasetsVerifier {
"/datasets/org/librecms/contentsection/ContentSectionManagerTest/after-create.xml", "/datasets/org/librecms/contentsection/ContentSectionManagerTest/after-create.xml",
"/datasets/org/librecms/contentsection/ContentSectionManagerTest/after-remove-role.xml", "/datasets/org/librecms/contentsection/ContentSectionManagerTest/after-remove-role.xml",
"/datasets/org/librecms/contentsection/ContentSectionManagerTest/after-rename.xml", "/datasets/org/librecms/contentsection/ContentSectionManagerTest/after-rename.xml",
"/datasets/org/librecms/contentsection/ContentItemRepositoryTest/data.xml", "/datasets/org/librecms/contentsection/ContentItemRepositoryTest/data.xml",
"/datasets/org/librecms/contentsection/ContentItemManagerTest/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.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-before.xml",
"/datasets/org/librecms/contentsection/ContentItemManagerTest/move-after.xml", "/datasets/org/librecms/contentsection/ContentItemManagerTest/move-after.xml",
"/datasets/org/librecms/contentsection/ContentItemManagerTest/copy-to-other-folder-after.xml", "/datasets/org/librecms/contentsection/ContentItemManagerTest/copy-to-other-folder-after.xml",

View File

@ -1457,6 +1457,7 @@ create table CCM_CMS.ARTICLE_LEADS (
create table CCM_CORE.WORKFLOWS ( create table CCM_CORE.WORKFLOWS (
WORKFLOW_ID bigint not null, WORKFLOW_ID bigint not null,
TEMPLATE_ID bigint,
primary key (WORKFLOW_ID) 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 alter table CCM_CMS.CONTENT_SECTION_WORKFLOW_TEMPLATES
add constraint FK6kuejkcl9hcbkr8q6bdlatt8q add constraint FK6kuejkcl9hcbkr8q6bdlatt8q
foreign key (CONTENT_SECTION_ID) foreign key (CONTENT_SECTION_ID)
references CCM_CMS.CONTENT_SECTIONS; references CCM_CMS.CONTENT_SECTIONS;
alter table CCM_CORE.WORKFLOWS
add constraint FKol71r1t83h0qe65gglq43far2
foreign key (TEMPLATE_ID)
references CCM_CORE.WORKFLOW_TEMPLATES;

View File

@ -1457,6 +1457,7 @@ CREATE SCHEMA ccm_cms;
create table CCM_CORE.WORKFLOWS ( create table CCM_CORE.WORKFLOWS (
WORKFLOW_ID int8 not null, WORKFLOW_ID int8 not null,
TEMPLATE_ID int8,
primary key (WORKFLOW_ID) primary key (WORKFLOW_ID)
); );
@ -2753,4 +2754,9 @@ create sequence hibernate_sequence start 1 increment 1;
alter table CCM_CMS.CONTENT_SECTION_WORKFLOW_TEMPLATES alter table CCM_CMS.CONTENT_SECTION_WORKFLOW_TEMPLATES
add constraint FK6kuejkcl9hcbkr8q6bdlatt8q add constraint FK6kuejkcl9hcbkr8q6bdlatt8q
foreign key (CONTENT_SECTION_ID) foreign key (CONTENT_SECTION_ID)
references CCM_CMS.CONTENT_SECTIONS; references CCM_CMS.CONTENT_SECTIONS;
alter table CCM_CORE.WORKFLOWS
add constraint FKol71r1t83h0qe65gglq43far2
foreign key (template_id)
references CCM_CORE.WORKFLOW_TEMPLATES;

View File

@ -94,14 +94,14 @@
localized_value="The only phase" localized_value="The only phase"
locale="en" /> locale="en" />
<ccm_cms.lifecycles lifecycle_id="-300" <!--<ccm_cms.lifecycles lifecycle_id="-300"
started="false" started="false"
finished="false" finished="false"
definition_id="-210" /> definition_id="-210" />
<ccm_cms.lifecyle_phases phase_id="-300100" <ccm_cms.lifecyle_phases phase_id="-300100"
started="false" started="false"
finished="false" /> finished="false" />-->
<ccm_core.ccm_objects object_id="-1100" <ccm_core.ccm_objects object_id="-1100"
display_name="info" display_name="info"

View File

@ -104,7 +104,7 @@
localized_value="The only phase" localized_value="The only phase"
locale="en" /> locale="en" />
<ccm_cms.lifecycles lifecycle_id="-300" <!--<ccm_cms.lifecycles lifecycle_id="-300"
started="false" started="false"
finished="false" finished="false"
definition_id="-200" /> definition_id="-200" />
@ -114,7 +114,7 @@
finished="false" /> finished="false" />
<ccm_cms.lifecyle_phases phase_id="-300200" <ccm_cms.lifecyle_phases phase_id="-300200"
started="false" started="false"
finished="false" /> finished="false" />-->
<ccm_core.ccm_objects object_id="-1100" <ccm_core.ccm_objects object_id="-1100"
display_name="info" display_name="info"