KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > bean > wizard > NewDiscussionWizard


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the GNU Lesser General Public License as
5  * published by the Free Software Foundation; either version
6  * 2.1 of the License, or (at your option) any later version.
7  * You may obtain a copy of the License at
8  *
9  * http://www.gnu.org/licenses/lgpl.txt
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific
15  * language governing permissions and limitations under the
16  * License.
17  */

18 package org.alfresco.web.bean.wizard;
19
20 import java.io.Serializable JavaDoc;
21 import java.text.MessageFormat JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.faces.context.FacesContext;
26 import javax.faces.event.ActionEvent;
27 import javax.transaction.UserTransaction JavaDoc;
28
29 import org.alfresco.error.AlfrescoRuntimeException;
30 import org.alfresco.model.ContentModel;
31 import org.alfresco.model.ForumModel;
32 import org.alfresco.service.cmr.repository.ChildAssociationRef;
33 import org.alfresco.service.cmr.repository.NodeRef;
34 import org.alfresco.service.namespace.QName;
35 import org.alfresco.web.app.AlfrescoNavigationHandler;
36 import org.alfresco.web.app.Application;
37 import org.alfresco.web.bean.repository.Repository;
38 import org.alfresco.web.ui.common.Utils;
39 import org.alfresco.web.ui.common.component.UIActionLink;
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43 /**
44  * Backing bean class used to create discussions for documents
45  *
46  * @author gavinc
47  */

48 public class NewDiscussionWizard extends NewTopicWizard
49 {
50    private static final Log logger = LogFactory.getLog(NewDiscussionWizard.class);
51    
52    private NodeRef discussingNodeRef;
53
54    /**
55     * @see org.alfresco.web.bean.wizard.AbstractWizardBean#startWizard(javax.faces.event.ActionEvent)
56     */

57    @Override JavaDoc
58    public void startWizard(ActionEvent event)
59    {
60       UIActionLink link = (UIActionLink)event.getComponent();
61       Map JavaDoc<String JavaDoc, String JavaDoc> params = link.getParameterMap();
62       String JavaDoc id = params.get("id");
63       if (id == null || id.length() == 0)
64       {
65          throw new AlfrescoRuntimeException("startDiscussion called without an id");
66       }
67       
68       FacesContext context = FacesContext.getCurrentInstance();
69       UserTransaction JavaDoc tx = null;
70       NodeRef forumNodeRef = null;
71       
72       try
73       {
74          tx = Repository.getUserTransaction(context);
75          tx.begin();
76          
77          this.discussingNodeRef = new NodeRef(Repository.getStoreRef(), id);
78          
79          if (this.nodeService.hasAspect(this.discussingNodeRef, ForumModel.ASPECT_DISCUSSABLE))
80          {
81             throw new AlfrescoRuntimeException("startDiscussion called for an object that already has a discussion!");
82          }
83          
84          // add the discussable aspect
85
this.nodeService.addAspect(this.discussingNodeRef, ForumModel.ASPECT_DISCUSSABLE, null);
86          
87          // create a child forum space using the child association just introduced by
88
// adding the discussable aspect
89
String JavaDoc name = (String JavaDoc)this.nodeService.getProperty(this.discussingNodeRef,
90                ContentModel.PROP_NAME);
91          String JavaDoc msg = Application.getMessage(FacesContext.getCurrentInstance(), "discussion_for");
92          String JavaDoc forumName = MessageFormat.format(msg, new Object JavaDoc[] {name});
93          
94          Map JavaDoc<QName, Serializable JavaDoc> forumProps = new HashMap JavaDoc<QName, Serializable JavaDoc>(1);
95          forumProps.put(ContentModel.PROP_NAME, forumName);
96          ChildAssociationRef childRef = this.nodeService.createNode(this.discussingNodeRef,
97                ForumModel.ASSOC_DISCUSSION,
98                QName.createQName(ForumModel.FORUMS_MODEL_URI, "discussion"),
99                ForumModel.TYPE_FORUM, forumProps);
100          
101          forumNodeRef = childRef.getChildRef();
102
103          // apply the uifacets aspect
104
Map JavaDoc<QName, Serializable JavaDoc> uiFacetsProps = new HashMap JavaDoc<QName, Serializable JavaDoc>(5);
105          uiFacetsProps.put(ContentModel.PROP_ICON, "forum_large");
106          this.nodeService.addAspect(forumNodeRef, ContentModel.ASPECT_UIFACETS, uiFacetsProps);
107          
108          if (logger.isDebugEnabled())
109             logger.debug("created forum for content: " + this.discussingNodeRef.toString());
110          
111          // commit the transaction
112
tx.commit();
113       }
114       catch (Throwable JavaDoc e)
115       {
116          // rollback the transaction
117
try { if (tx != null) {tx.rollback();} } catch (Exception JavaDoc ex) {}
118          Utils.addErrorMessage(MessageFormat.format(Application.getMessage(
119                context, Repository.ERROR_GENERIC), e.getMessage()), e);
120       }
121       
122       // finally setup the context for the forum we just created
123
if (forumNodeRef != null)
124       {
125          this.browseBean.clickSpace(forumNodeRef);
126          
127          // now initialise the wizard and navigate to it
128
super.startWizard(event);
129          context.getApplication().getNavigationHandler().handleNavigation(context, null, "dialog:createDiscussion");
130       }
131    }
132
133    /**
134     * @see org.alfresco.web.bean.wizard.AbstractWizardBean#cancel()
135     */

136    @Override JavaDoc
137    public String JavaDoc cancel()
138    {
139       // if we cancel the creation of a discussion all the setup that was done
140
// when the wizard started needs to be undone i.e. removing the created forum
141
// and the discussable aspect
142

143       FacesContext context = FacesContext.getCurrentInstance();
144       UserTransaction JavaDoc tx = null;
145       
146       try
147       {
148          tx = Repository.getUserTransaction(context);
149          tx.begin();
150          
151          // remove the discussable aspect from the node we were going to discuss!
152
this.nodeService.removeAspect(this.discussingNodeRef, ForumModel.ASPECT_DISCUSSABLE);
153          
154          // delete the forum space created when the wizard started
155
this.browseBean.setActionSpace(this.navigator.getCurrentNode());
156          this.browseBean.deleteSpaceOK();
157          
158          // commit the transaction
159
tx.commit();
160       }
161       catch (Throwable JavaDoc e)
162       {
163          // rollback the transaction
164
try { if (tx != null) {tx.rollback();} } catch (Exception JavaDoc ex) {}
165          Utils.addErrorMessage(MessageFormat.format(Application.getMessage(
166                context, Repository.ERROR_GENERIC), e.getMessage()), e);
167       }
168       
169       // do cancel processing
170
super.cancel();
171       
172       // as we are cancelling the creation of a discussion we know we need to go back
173
// to the browse screen, this also makes sure we don't end up in the forum that
174
// just got deleted!
175
return AlfrescoNavigationHandler.CLOSE_DIALOG_OUTCOME +
176              AlfrescoNavigationHandler.DIALOG_SEPARATOR + "browse";
177    }
178    
179    
180 }
181
Popular Tags