KickJava   Java API By Example, From Geeks To Geeks.

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


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.usermanager.JahiaUser;
52
53 import org.jahia.services.version.EntryLoadRequest;
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 import java.util.Enumeration JavaDoc;
66
67 /**
68  * Action used to edit a post from the Jahia content repository. Compliant with
69  * Blogger API's and MetaWeblogAPI API's editPost method.
70  *
71  * @author Xavier Lawrence
72  */

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

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

100     public EditPostAction(String JavaDoc postID, String JavaDoc userName, String JavaDoc password,
101             Hashtable JavaDoc struct, boolean publish) {
102         
103         super.userName = userName;
104         super.password = password;
105         this.postID = postID;
106         this.struct = struct;
107         this.publish = publish;
108     }
109     
110     /**
111      * Edits a post of the blog of this user and saves the modifications into
112      * Jahia
113      *
114      * @return True if the edition was sucessfull
115      */

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

334     protected Vector JavaDoc getExistingPingURLs(JahiaContainerList list)
335     throws JahiaException {
336         Vector JavaDoc result = new Vector JavaDoc();
337         
338         if (list == null) return result;
339         
340         Enumeration JavaDoc en = list.getContainers();
341         
342         while (en.hasMoreElements()) {
343             JahiaContainer c = (JahiaContainer)en.nextElement();
344             JahiaField f = c.getField(containerNames.getValue(BlogDefinitionNames.
345                     TB_PING_URL));
346             
347             result.addElement(f.getValue());
348             
349         }
350         return result;
351     }
352     
353     /**
354      *
355      */

356     protected void activatePingContainers(JahiaContainerList list, JahiaUser user)
357     throws JahiaException {
358         Enumeration JavaDoc en = list.getContainers();
359         
360         while (en.hasMoreElements()) {
361             JahiaContainer c = (JahiaContainer)en.nextElement();
362             super.activateContainer(c.getID(), user);
363         }
364     }
365 }
366
Popular Tags