cancel
Showing results for 
Search instead for 
Did you mean: 

how to create a forum, topic and post in JAVA

michel
Champ in-the-making
Champ in-the-making
hello,

I try to create a discussion (so a forum on a document) in JAVA and I don't know how to create topics and post…

I try this:


NodeRef nodeDiscussion = rs.getNodeRef(0); // from a lucene query to find the document…
         ChildAssociationRef childRef;
         NodeRef destinationForum;
         if(!nodeService.hasAspect(nodeDiscussion, ForumModel.ASPECT_DISCUSSABLE)) {
            nodeService.addAspect(nodeDiscussion, ForumModel.ASPECT_DISCUSSABLE, null);
            String name = (String)this.nodeService.getProperty(nodeDiscussion,
                        ContentModel.PROP_NAME);
             String forumName = I18NUtil.getMessage("coci_service.discussion_for", new Object[] {name});           
               Map<QName, Serializable> forumProps = new HashMap<QName, Serializable>(1);
               forumProps.put(ContentModel.PROP_NAME, forumName);

               childRef = this.nodeService.createNode(nodeDiscussion,
               ForumModel.ASSOC_DISCUSSION,
               QName.createQName(NamespaceService.FORUMS_MODEL_1_0_URI, "discussion"),
               ForumModel.TYPE_FORUM, forumProps);
              
               destinationForum = childRef.getChildRef();

               // apply the uifacets aspect
               Map<QName, Serializable> uiFacetsProps = new HashMap<QName, Serializable>(5);
               uiFacetsProps.put(ApplicationModel.PROP_ICON, "forum");
               this.nodeService.addAspect(destinationForum, ApplicationModel.ASPECT_UIFACETS, uiFacetsProps);
         } else {
             List<ChildAssociationRef> destChildren = this.nodeService.getChildAssocs(nodeDiscussion,
                        ForumModel.ASSOC_DISCUSSION, RegexQNamePattern.MATCH_ALL);
                    
              if (destChildren.size() != 1){
                         throw new IllegalStateException("Locked node has the discussable aspect but does not have 1 child, it has " +
                                                         destChildren.size() + " children!");
              }
              destinationForum = destChildren.get(0).getChildRef();
         }
Map<QName, Serializable> topicProps = new HashMap<QName, Serializable>(1);
            topicProps.put(ContentModel.PROP_NAME, "TopicName");
            ChildAssociationRef topicChildRef = nodeService.createNode(destinationForum,
                  ForumModel.ASSOC_DISCUSSION,
                  QName.createQName(NamespaceService.FORUMS_MODEL_1_0_URI, "discussion"),
                  ForumModel.TYPE_TOPIC, topicProps);
            NodeRef topicNode = topicChildRef.getChildRef();
            Map<QName, Serializable> uiFacetsProps = new HashMap<QName, Serializable>(5);
               uiFacetsProps.put(ApplicationModel.PROP_ICON, "topic");
               this.nodeService.addAspect(topicNode, ApplicationModel.ASPECT_UIFACETS, uiFacetsProps);

But I get this error:


10:25:25,853 ERROR [node.integrity.IntegrityChecker] Found 2 integrity violation
s:
The association source is missing the aspect required for this association:
   Source Node: workspace://SpacesStore/3bb8a352-5089-11dc-9cd2-3daa8b5bd1a3
   Association: Association[ class=ClassDef[name={http://www.alfresco.org/model/
forum/1.0}discussable], name={http://www.alfresco.org/model/forum/1.0}discussion
, target class={http://www.alfresco.org/model/forum/1.0}forum, source role=null,
target role=null]
   Required Source Aspect: {http://www.alfresco.org/model/forum/1.0}discussable
   Actual Source Aspects: [{http://www.alfresco.org/model/content/1.0}auditable,
{http://www.alfresco.org/model/system/1.0}referenceable, {http://www.alfresco.o
rg/model/application/1.0}uifacets]
The association target type is incorrect:
   Target Node: workspace://SpacesStore/3bc6fb34-5089-11dc-9cd2-3daa8b5bd1a3
   Association: Association[ class=ClassDef[name={http://www.alfresco.org/model/
forum/1.0}discussable], name={http://www.alfresco.org/model/forum/1.0}discussion
, target class={http://www.alfresco.org/model/forum/1.0}forum, source role=null,
target role=null]
   Required Target Type: {http://www.alfresco.org/model/forum/1.0}forum
   Actual Target Type: {http://www.alfresco.org/model/forum/1.0}topic
10:25:25,869 ERROR [util.transaction.SpringAwareUserTransaction] Transaction did
n't commit

So what kind of QName must I use to create topics and posts…

thanks in advence

Michel.
2 REPLIES 2

gavinc
Champ in-the-making
Champ in-the-making
A topic is simply a subtype of cm:folder so to create a topic within a forum you simply create a 'normal' space.

You can see this by looking at CreateTopicDialog.finishImpl(), it simply calls the super class 'CreateSpaceDialog' finishImpl method to get the topic created. Then to create the post, this is just a subtype of cm:content, again you can see this being done in CreateTopicDialog.finishImpl() via the following line:

FileInfo fileInfo = this.fileFolderService.create(containerNodeRef, fileName, ForumModel.TYPE_POST);

It looks like you are trying to create another discussion association under the  forum space which is incorrect, hence the error.

michel
Champ in-the-making
Champ in-the-making
Thank you, it works now.