KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > blogs > actions > NewPostAction


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution Sàrl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * ----- END LICENSE BLOCK -----
36  */

37
38 package org.jahia.blogs.actions;
39
40 import org.jahia.blogs.model.MetaPostInfo;
41
42 import org.jahia.registries.JahiaContainerDefinitionsRegistry;
43
44 import org.jahia.data.containers.JahiaContainerList;
45 import org.jahia.data.containers.JahiaContainer;
46 import org.jahia.data.containers.JahiaContainerDefinition;
47
48 import org.jahia.data.fields.LoadFlags;
49 import org.jahia.data.fields.JahiaField;
50
51 import org.jahia.services.version.EntryLoadRequest;
52
53 import org.jahia.services.usermanager.JahiaUser;
54
55 import org.jahia.data.events.JahiaEvent;
56
57 import org.jahia.exceptions.JahiaException;
58
59 import org.apache.commons.lang.StringUtils;
60
61 import org.apache.log4j.Logger;
62
63 import java.util.Hashtable JavaDoc;
64 import java.util.Vector JavaDoc;
65
66 /**
67  * Action used to add a new post to the Jahia content repository. Compliant with
68  * Blogger API's and MetaWeblog API's newPost method.
69  *
70  * @author Xavier Lawrence
71  */

72 public class NewPostAction extends AbstractAction {
73     
74     // log4j logger
75
static Logger log = Logger.getLogger(NewPostAction.class);
76     
77     private String JavaDoc blogID;
78     private String JavaDoc content;
79     private Hashtable JavaDoc struct;
80     private boolean publish;
81     
82     private boolean isNewList;
83     
84     /**
85      * Creates a new instance of NewPostAction (Blogger API)
86      */

87     public NewPostAction(String JavaDoc appKey, String JavaDoc blogID,
88             String JavaDoc userName, String JavaDoc password, String JavaDoc content,
89             boolean publish) {
90         
91         super.appKey = appKey;
92         super.userName = userName;
93         super.password = password;
94         this.blogID = blogID;
95         this.content = content;
96         this.publish = publish;
97     }
98     
99     /**
100      * Creates a new instance of NewPostAction (MetaWeblog API)
101      */

102     public NewPostAction(String JavaDoc blogID, String JavaDoc userName, String JavaDoc password,
103             Hashtable JavaDoc struct, boolean publish) {
104         
105         super.userName = userName;
106         super.password = password;
107         this.blogID = blogID;
108         this.struct = struct;
109         this.publish = publish;
110     }
111     
112     /**
113      * Adds a post to the blog of this user into Jahia
114      *
115      * @return The PostID as a String
116      */

117     public Object JavaDoc execute() throws JahiaException {
118         
119         // Create commmon resources
120
super.init();
121         
122         // First check that the user is registered to this site.
123
JahiaUser user = super.checkLogin();
124         
125         // Set the correct page and check write access
126
if (!super.changePage(Integer.parseInt(blogID)).checkWriteAccess(user)) {
127             throw new JahiaException(
128                     "You do not have write access to Blog: "+blogID,
129                     "You do not have write access to Page: "+blogID,
130                     JahiaException.ACL_ERROR,
131                     JahiaException.WARNING_SEVERITY);
132         }
133         
134         // Name of the containerList containing all the posts of the blog
135
final String JavaDoc containerListName = super.containerNames.getValue(
136                 BlogDefinitionNames.BLOG_POSTS_LIST_NAME);
137         final int containerListID = containerService.getContainerListID(
138                 containerListName, Integer.parseInt(blogID));
139         
140         JahiaContainerList entryList = null;
141         if (containerListID != -1) {
142             // This is the ContainerList containing all the entries of a Blog
143
entryList = containerService.loadContainerList(
144                     containerListID, LoadFlags.ALL, jParams);
145         }
146         
147         int containerID, listID, rank, aclID, versionID;
148         containerID = listID = rank = aclID = versionID = 0;
149         
150         // If the ContainerList does not exist (== null), we are posting the
151
// very first post and all the container params have allready been set
152
// above (= 0)
153
if (entryList != null) {
154             listID = entryList.getID();
155             aclID = entryList.getAclID();
156             isNewList = false;
157             
158         } else {
159             isNewList = true;
160         }
161         
162         JahiaContainerDefinition def = JahiaContainerDefinitionsRegistry.
163                 getInstance().getDefinition(jParams.getSiteID(), containerListName);
164         
165         // The container for the new post
166
JahiaContainer postContainer = new JahiaContainer(
167                 containerID,
168                 jParams.getJahiaID(),
169                 jParams.getPageID(),
170                 listID,
171                 rank,
172                 aclID,
173                 def.getID(),
174                 versionID,
175                 EntryLoadRequest.STAGING_WORKFLOW_STATE);
176         
177         // Save the new Container
178
containerService.saveContainer(postContainer, 0, jParams);
179         log.debug("Saving Container for new Post: "+postContainer.getID());
180         
181         // Load the Container and check the structure
182
postContainer = super.getContainer(postContainer.getID(),
183                 jParams.getLocale().toString());
184         log.debug("Loaded and working on Container: "+postContainer.getID());
185         
186         /* Set all the fields' value with the right data */
187         // set title field
188
String JavaDoc fieldName = super.containerNames.getValue(BlogDefinitionNames.POST_TITLE);
189         String JavaDoc fieldValue = "";
190         
191         // If content != null -> we are using the Blogger API
192
// If content == null -> we are using the MetaWebLog API
193
if (content != null) {
194             String JavaDoc tag = "<"+fieldName+">";
195             String JavaDoc endTag = "</"+fieldName+">";
196             if (content.indexOf(tag) != -1) {
197                 // contains only the title value
198
fieldValue =
199                         content.substring(content.indexOf(tag) + tag.length(),
200                         content.indexOf(endTag));
201                 // contains the post body without the title
202
content = StringUtils.replace(content, tag + fieldValue +
203                         endTag, "");
204             }
205             
206         } else {
207             fieldValue = (String JavaDoc)struct.get(MetaPostInfo.TITLE);
208             content = (String JavaDoc)struct.get(MetaPostInfo.DESCRIPTION);
209             Vector JavaDoc categories = (Vector JavaDoc)struct.get(MetaPostInfo.CATEGORIES);
210             super.setCategories(categories, postContainer);
211         }
212         
213         JahiaField field = postContainer.getField(fieldName);
214         log.debug("Setting value of field: "+field.getID() +"; "+
215                 field.getLanguageCode());
216         field.setValue(fieldValue);
217         
218         // set body field
219
fieldName = super.containerNames.getValue(containerNames.POST_BODY);
220         field = postContainer.getField(fieldName);
221         log.debug("Setting value of field: "+field.getID() +"; "+
222                 field.getLanguageCode());
223         super.setValue(field, content);
224         
225         // set posting date
226
fieldName = super.containerNames.getValue(containerNames.POST_DATE);
227         field = postContainer.getField(fieldName);
228         field.setObject(Long.toString(System.currentTimeMillis()));
229         
230         // set the author of the message
231
fieldName = super.containerNames.getValue(containerNames.POST_AUTHOR);
232         field = postContainer.getField(fieldName);
233         log.debug("Setting value of field: "+field.getID() +"; "+
234                 field.getLanguageCode());
235         field.setValue(userName);
236         
237         // check if extra Movable Type are present in the struct
238
if (struct != null) {
239             
240             fieldName = containerNames.getValue(containerNames.POST_EXCERPT);
241             field = postContainer.getField(fieldName);
242             if (struct.containsKey(MetaPostInfo.MT_EXCERPT)) {
243                 String JavaDoc excerpt = (String JavaDoc)struct.get(MetaPostInfo.MT_EXCERPT);
244                 
245                 if (excerpt != null && excerpt.length() > 0) {
246                     field.setValue(excerpt);
247                 }
248             }
249             
250             fieldName = containerNames.getValue(containerNames.POST_KEYWORDS);
251             field = postContainer.getField(fieldName);
252             if (struct.containsKey(MetaPostInfo.MT_KEYWORDS)) {
253                 String JavaDoc keywords = (String JavaDoc)struct.get(MetaPostInfo.MT_KEYWORDS);
254                 
255                 if (keywords != null && keywords.length() > 0) {
256                     field.setValue(keywords);
257                 }
258             }
259             
260             // Save the fields since some are required for the TB ping
261
containerService.saveContainer(postContainer, 0, jParams);
262             
263             Vector JavaDoc tbURLs = (Vector JavaDoc)struct.get(MetaPostInfo.MT_TB_PING_URLS);
264             
265             if (tbURLs != null && tbURLs.size() > 0) {
266                 
267                 def = JahiaContainerDefinitionsRegistry.
268                         getInstance().getDefinition(jParams.getSiteID(),
269                         containerNames.getValue(containerNames.BLOG_TB_PING_LIST));
270                 
271                 JahiaContainerList pingURLs = null;
272                 int ctnListID = -1;
273                 
274                 for (int i=0; i<tbURLs.size(); i++) {
275                     String JavaDoc url = (String JavaDoc)tbURLs.get(i);
276                     
277                     // Ignore any empty or too short urls
278
if (url.length() < 7) continue;
279                     
280                     if (i == 1) {
281                         pingURLs = containerService.loadContainerList(
282                                 ctnListID, LoadFlags.ALL, jParams);
283                     }
284                     
285                     if (pingURLs != null && pingURLs.getID() > 0) {
286                         listID = pingURLs.getID();
287                         aclID = pingURLs.getAclID();
288                         
289                     } else {
290                         listID = aclID = 0;
291                     }
292                     
293                     JahiaContainer pingURLContainer = new JahiaContainer(
294                             0, jParams.getJahiaID(), jParams.getPageID(),
295                             listID, 0, aclID, def.getID(), 0,
296                             EntryLoadRequest.STAGING_WORKFLOW_STATE);
297                     
298                     // Save the new Container
299
containerService.saveContainer(pingURLContainer,
300                             postContainer.getID(), jParams);
301                     
302                     // Load the Container and check the structure
303
pingURLContainer = super.getContainer(pingURLContainer.getID(),
304                             jParams.getLocale().toString());
305                     
306                     log.debug("pingURLContainer: "+pingURLContainer.getID());
307                     ctnListID = pingURLContainer.getListID();
308                     
309                     field = pingURLContainer.getField(containerNames.
310                             getValue(BlogDefinitionNames.TB_PING_URL));
311                     field.setValue(url);
312                     
313                     containerService.saveContainer(pingURLContainer,
314                             postContainer.getID(), jParams);
315                     
316                     // Notify the listener so it can send the ping
317
JahiaEvent theEvent = new JahiaEvent(this, jParams,
318                             pingURLContainer);
319                     servicesRegistry.getJahiaEventService().
320                             fireAddContainer(theEvent);
321                     
322                     if (i == 0) {
323                         super.activateContainerList(pingURLContainer.getListID(),
324                                 user, pingURLContainer.getPageID());
325                     }
326                     
327                     if (publish) {
328                         // publish the trackBack ping url container
329
super.activateContainer(pingURLContainer.getID(), user);
330                     }
331                     
332                     pingURLContainer = null;
333                     url = null;
334                 }
335             }
336         }
337         
338         containerService.saveContainer(postContainer, 0, jParams);
339         
340         if (isNewList) {
341             super.activateContainerList(postContainer.getListID(), user,
342                     postContainer.getPageID());
343         }
344         
345         if (publish) {
346             super.activateContainer(postContainer.getID(), user);
347         }
348         
349         super.flushPageCacheThatDisplayContainer(postContainer);
350         
351         // The container ID is the postID
352
log.debug("New Post OK, returning postID: "+postContainer.getID());
353         return Integer.toString(postContainer.getID());
354     }
355 }
356
Popular Tags