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.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 ; 64 import java.util.Vector ; 65 66 72 public class NewPostAction extends AbstractAction { 73 74 static Logger log = Logger.getLogger(NewPostAction.class); 76 77 private String blogID; 78 private String content; 79 private Hashtable struct; 80 private boolean publish; 81 82 private boolean isNewList; 83 84 87 public NewPostAction(String appKey, String blogID, 88 String userName, String password, String 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 102 public NewPostAction(String blogID, String userName, String password, 103 Hashtable 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 117 public Object execute() throws JahiaException { 118 119 super.init(); 121 122 JahiaUser user = super.checkLogin(); 124 125 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 final String 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 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 (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 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 containerService.saveContainer(postContainer, 0, jParams); 179 log.debug("Saving Container for new Post: "+postContainer.getID()); 180 181 postContainer = super.getContainer(postContainer.getID(), 183 jParams.getLocale().toString()); 184 log.debug("Loaded and working on Container: "+postContainer.getID()); 185 186 187 String fieldName = super.containerNames.getValue(BlogDefinitionNames.POST_TITLE); 189 String fieldValue = ""; 190 191 if (content != null) { 194 String tag = "<"+fieldName+">"; 195 String endTag = "</"+fieldName+">"; 196 if (content.indexOf(tag) != -1) { 197 fieldValue = 199 content.substring(content.indexOf(tag) + tag.length(), 200 content.indexOf(endTag)); 201 content = StringUtils.replace(content, tag + fieldValue + 203 endTag, ""); 204 } 205 206 } else { 207 fieldValue = (String )struct.get(MetaPostInfo.TITLE); 208 content = (String )struct.get(MetaPostInfo.DESCRIPTION); 209 Vector categories = (Vector )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 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 fieldName = super.containerNames.getValue(containerNames.POST_DATE); 227 field = postContainer.getField(fieldName); 228 field.setObject(Long.toString(System.currentTimeMillis())); 229 230 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 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 excerpt = (String )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 keywords = (String )struct.get(MetaPostInfo.MT_KEYWORDS); 254 255 if (keywords != null && keywords.length() > 0) { 256 field.setValue(keywords); 257 } 258 } 259 260 containerService.saveContainer(postContainer, 0, jParams); 262 263 Vector tbURLs = (Vector )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 url = (String )tbURLs.get(i); 276 277 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 containerService.saveContainer(pingURLContainer, 300 postContainer.getID(), jParams); 301 302 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 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 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 log.debug("New Post OK, returning postID: "+postContainer.getID()); 353 return Integer.toString(postContainer.getID()); 354 } 355 } 356 | Popular Tags |