KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > workflowtool > actions > examples > CreateNews


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.applications.workflowtool.actions.examples;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32
33 import org.dom4j.Document;
34 import org.dom4j.Element;
35 import org.exolab.castor.jdo.Database;
36 import org.exolab.castor.jdo.PersistenceException;
37 import org.exolab.castor.jdo.TransactionNotInProgressException;
38 import org.infoglue.cms.controllers.kernel.impl.simple.CastorDatabaseService;
39 import org.infoglue.cms.controllers.kernel.impl.simple.ContentControllerProxy;
40 import org.infoglue.cms.controllers.kernel.impl.simple.ContentStateController;
41 import org.infoglue.cms.controllers.kernel.impl.simple.ContentVersionController;
42 import org.infoglue.cms.controllers.kernel.impl.simple.LanguageController;
43 import org.infoglue.cms.entities.content.Content;
44 import org.infoglue.cms.entities.content.ContentVO;
45 import org.infoglue.cms.entities.content.ContentVersion;
46 import org.infoglue.cms.entities.content.ContentVersionVO;
47 import org.infoglue.cms.entities.management.Language;
48 import org.infoglue.cms.security.InfoGluePrincipal;
49 import org.infoglue.cms.util.CmsPropertyHandler;
50 import org.infoglue.cms.util.ConstraintExceptionBuffer;
51 import org.infoglue.cms.util.dom.DOMBuilder;
52 import org.infoglue.cms.util.workflow.CustomWorkflowAction;
53 import org.infoglue.deliver.controllers.kernel.impl.simple.ExtranetController;
54
55 import com.opensymphony.module.propertyset.PropertySet;
56 import com.opensymphony.workflow.WorkflowException;
57
58 public class CreateNews implements CustomWorkflowAction
59 {
60     public CreateNews()
61     {
62     }
63     
64     
65     public void invokeAction(String JavaDoc callerUserName, HttpServletRequest JavaDoc request, Map JavaDoc args, PropertySet ps) throws WorkflowException
66     {
67         System.out.println("Inside CreateNews.invokeAction");
68         System.out.println("callerUserName:" + callerUserName);
69         
70         /*
71         Iterator paramsIterator = request.getParameterMap().keySet().iterator();
72         while(paramsIterator.hasNext())
73         {
74             String key = (String)paramsIterator.next();
75             System.out.println("key:" + key);
76             Object value = request.getParameterMap().get(key);
77             System.out.println("value:" + value);
78         }
79         
80         Iterator psIterator = ps.getKeys().iterator();
81         while(psIterator.hasNext())
82         {
83             String key = (String)psIterator.next();
84             System.out.println("key:" + key);
85             //Object value = ps.getObject(key);
86             //System.out.println("value:" + value);
87         }
88         */

89         InfoGluePrincipal infoGluePrincipal = (InfoGluePrincipal)request.getSession().getAttribute("org.infoglue.cms.security.user");
90         if(infoGluePrincipal == null)
91         {
92             try
93             {
94                 Map JavaDoc arguments = new HashMap JavaDoc();
95                 arguments.put("j_username", CmsPropertyHandler.getAnonymousUser());
96                 arguments.put("j_password", CmsPropertyHandler.getAnonymousPassword());
97
98                 infoGluePrincipal = (InfoGluePrincipal) ExtranetController.getController().getAuthenticatedPrincipal(arguments);
99             }
100             catch (Exception JavaDoc e2)
101             {
102                 e2.printStackTrace();
103             }
104         }
105         
106         Integer JavaDoc parentContentId = new Integer JavaDoc(ps.getString("parentContentId"));
107         Integer JavaDoc contentTypeDefinitionId = new Integer JavaDoc(ps.getString("contentTypeDefinitionId"));
108         Integer JavaDoc repositoryId = new Integer JavaDoc(ps.getString("repositoryId"));
109         
110         Integer JavaDoc languageId = new Integer JavaDoc(1);
111         if(ps.getString("languageId") != null && !ps.getString("languageId").equalsIgnoreCase(""))
112             languageId = new Integer JavaDoc(ps.getString("languageId"));
113         
114         String JavaDoc name = ps.getString("name");
115         String JavaDoc title = ps.getString("title");
116         String JavaDoc navigationTitle = ps.getString("navigationTitle");
117         String JavaDoc leadIn = ps.getString("leadIn");
118         String JavaDoc fullText = ps.getString("fullText");
119         
120         System.out.println("name:" + name);
121         System.out.println("title:" + title);
122         System.out.println("navigationTitle:" + navigationTitle);
123         System.out.println("leadIn:" + leadIn);
124         System.out.println("fullText:" + fullText);
125         
126         ContentVO contentVO = new ContentVO();
127         contentVO.setName(name);
128         contentVO.setIsBranch(new Boolean JavaDoc(false));
129         contentVO.setCreatorName(infoGluePrincipal.getName());
130         
131         Database db = null;
132
133         try
134         {
135             db = CastorDatabaseService.getDatabase();
136             ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
137             
138             db.begin();
139         
140             Content newContent = ContentControllerProxy.getContentController().create(db, parentContentId, contentTypeDefinitionId, repositoryId, contentVO);
141         
142             DOMBuilder domBuilder = new DOMBuilder();
143             Document document = domBuilder.createDocument();
144
145             Element rootElement = domBuilder.addElement(document, "article");
146             domBuilder.addAttribute(rootElement, "xmlns", "x-schema:ArticleSchema.xml");
147             Element attributesElement = domBuilder.addElement(rootElement, "attributes");
148
149             Element titleElement = domBuilder.addElement(attributesElement, "Title");
150             domBuilder.addCDATAElement(titleElement, title);
151
152             Element navTitleElement = domBuilder.addElement(attributesElement, "NavigationTitle");
153             domBuilder.addCDATAElement(navTitleElement, navigationTitle);
154
155             Element leadElement = domBuilder.addElement(attributesElement, "Leadin");
156             domBuilder.addCDATAElement(leadElement, leadIn);
157
158             Element leftColumnElement = domBuilder.addElement(attributesElement, "FullText");
159             domBuilder.addCDATAElement(leftColumnElement, fullText);
160             
161             String JavaDoc versionValue = document.asXML();
162             System.out.println("versionValue:" + versionValue);
163             
164             ContentVersionVO newContentVersionVO = new ContentVersionVO();
165             newContentVersionVO.setVersionComment("Generated");
166             newContentVersionVO.setVersionModifier(infoGluePrincipal.getName());
167             newContentVersionVO.setVersionValue(versionValue);
168
169             Language language = LanguageController.getController().getLanguageWithId(languageId, db);
170             ContentVersion newContentVersion = ContentVersionController.getContentVersionController().create(newContent, language, newContentVersionVO, null, db);
171             
172             db.commit();
173             
174             db.begin();
175             
176             List JavaDoc events = new ArrayList JavaDoc();
177             ContentVersion publishContentVersion = ContentStateController.changeState(newContentVersion.getContentVersionId(), ContentVersionVO.PUBLISH_STATE, "Auto", true, infoGluePrincipal, newContent.getId(), events);
178
179             db.commit();
180         }
181         catch (Exception JavaDoc e)
182         {
183             try
184             {
185                 db.rollback();
186             }
187             catch (TransactionNotInProgressException e1)
188             {
189                 e1.printStackTrace();
190             }
191             e.printStackTrace();
192             throw new WorkflowException(e);
193         }
194         finally
195         {
196             try
197             {
198                 db.close();
199             }
200             catch (PersistenceException e1)
201             {
202                 e1.printStackTrace();
203             }
204         }
205     }
206
207 }
208
Popular Tags