1 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 ; 64 import java.util.Vector ; 65 import java.util.Enumeration ; 66 67 73 public class EditPostAction extends AbstractAction { 74 75 static Logger log = Logger.getLogger(EditPostAction.class); 77 78 private String postID; 79 private String content; 80 private Hashtable struct; 81 private boolean publish; 82 83 86 public EditPostAction(String appKey, String postID, String userName, 87 String password, String 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 100 public EditPostAction(String postID, String userName, String password, 101 Hashtable 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 116 public Object execute() throws JahiaException { 117 118 super.init(); 120 121 JahiaUser user = super.checkLogin(); 123 124 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 141 String fieldName = super.containerNames.getValue(BlogDefinitionNames.POST_TITLE); 143 String fieldValue = ""; 144 145 if (content != null) { 148 String tag = "<"+fieldName+">"; 149 String endTag = "</"+fieldName+">"; 150 if (content.indexOf(tag) != -1) { 151 fieldValue = 153 content.substring(content.indexOf(tag) + 7, 154 content.indexOf(endTag)); 155 content = StringUtils.replace(content, tag + fieldValue + 157 endTag, ""); 158 } 159 160 } else { 161 fieldValue = (String )struct.get(MetaPostInfo.TITLE); 162 content = (String )struct.get(MetaPostInfo.DESCRIPTION); 163 Vector categories = (Vector )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 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 fieldName = super.containerNames.getValue(containerNames.POST_DATE); 181 field = postContainer.getField(fieldName); 182 field.setObject(Long.toString(System.currentTimeMillis())); 183 184 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 boolean publishPingContainers = false; 193 194 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 excerpt = (String )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 keywords = (String )struct.get(MetaPostInfo.MT_KEYWORDS); 211 212 if (keywords != null && keywords.length() > 0) { 213 field.setValue(keywords); 214 } 215 } 216 217 containerService.saveContainer(postContainer, 0, jParams); 219 220 Vector tbURLs = (Vector )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 existingPings = getExistingPingURLs(pingURLs); 238 for (int i=0; i<tbURLs.size(); i++) { 239 String url = (String )tbURLs.get(i); 240 241 if (url.length() < 7) continue; 243 244 if (existingPings.contains(url)) { 246 publishPingContainers = true; 247 continue; 248 } 249 250 if (i == 1) { 252 pingURLs = containerService.loadContainerList( 253 listID, LoadFlags.ALL, jParams); 254 } 255 256 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 containerService.saveContainer(pingURLContainer, 272 postContainer.getID(), jParams); 273 274 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 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 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 (true); 329 } 330 331 334 protected Vector getExistingPingURLs(JahiaContainerList list) 335 throws JahiaException { 336 Vector result = new Vector (); 337 338 if (list == null) return result; 339 340 Enumeration 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 356 protected void activatePingContainers(JahiaContainerList list, JahiaUser user) 357 throws JahiaException { 358 Enumeration 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 |