| 1 package com.dotmarketing.factories; 2 3 import java.util.ArrayList ; 4 import java.util.HashSet ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Set ; 8 9 import javax.servlet.http.HttpServletRequest ; 10 11 import com.dotmarketing.beans.Identifier; 12 import com.dotmarketing.beans.Inode; 13 import com.dotmarketing.beans.WebAsset; 14 import com.dotmarketing.cache.LiveCache; 15 import com.dotmarketing.cache.PageNotFoundCache; 16 import com.dotmarketing.cache.WorkingCache; 17 import com.dotmarketing.menubuilders.RefreshMenus; 18 import com.dotmarketing.portlets.categories.model.Category; 19 import com.dotmarketing.portlets.containers.model.Container; 20 import com.dotmarketing.portlets.contentlet.factories.ContentletFactory; 21 import com.dotmarketing.portlets.contentlet.model.Contentlet; 22 import com.dotmarketing.portlets.files.factories.FileFactory; 23 import com.dotmarketing.portlets.files.model.File; 24 import com.dotmarketing.portlets.folders.model.Folder; 25 import com.dotmarketing.portlets.htmlpages.factories.HTMLPageFactory; 26 import com.dotmarketing.portlets.htmlpages.model.HTMLPage; 27 import com.dotmarketing.portlets.links.model.Link; 28 import com.dotmarketing.portlets.templates.model.Template; 29 import com.dotmarketing.services.ContainerServices; 30 import com.dotmarketing.services.ContentletMapServices; 31 import com.dotmarketing.services.ContentletServices; 32 import com.dotmarketing.services.PageServices; 33 import com.dotmarketing.services.TemplateServices; 34 import com.dotmarketing.util.Config; 35 import com.dotmarketing.util.Logger; 36 import com.liferay.portal.model.User; 37 import com.liferay.portal.util.PortalUtil; 38 39 40 41 42 46 public class PublishFactory { 47 48 54 @SuppressWarnings ("unchecked") 55 public static boolean publishAsset(Inode webAsset,HttpServletRequest req) 56 { 57 User user = null; 58 try { 59 user = PortalUtil.getUser(req); 60 } catch (Exception e1) { 61 Logger.error(PublishFactory.class, "publishAsset: Cannot obtain the user from the request.", e1); 62 return false; 63 } 64 65 if (!PermissionFactory.doesUserHavePermission(webAsset, Config.getIntProperty("PERMISSION_PUBLISH"), user)) { 66 Logger.debug(PublishFactory.class, "publishAsset: user = " + user.getEmailAddress() + ", don't have permissions to publish: " + webAsset); 67 return false; 68 } 69 70 if (webAsset instanceof WebAsset) 71 { 72 WebAssetFactory.publishAsset((WebAsset) webAsset); 73 } 74 75 if (webAsset instanceof com.dotmarketing.portlets.files.model.File) 76 { 77 LiveCache.addToLiveAssetToCache((WebAsset) webAsset); 79 WorkingCache.addToWorkingAssetToCache((WebAsset) webAsset); 80 } 81 82 if (webAsset instanceof Container) { 83 84 ContainerServices.publishContainerToFile((Container)webAsset); 86 } 87 88 if (webAsset instanceof Contentlet) { 89 90 Logger.debug(PublishFactory.class, "*****I'm a Contentlet -- Publishing"); 91 92 String condition = "live=" + com.dotmarketing.db.DbConnectionFactory.getDBTrue(); 93 java.util.List files = InodeFactory.getChildrenClassByCondition(webAsset, File.class, condition); 95 java.util.Iterator filesIter = files.iterator(); 96 while (filesIter.hasNext()) { 97 File file = (File) filesIter.next(); 99 Logger.debug(PublishFactory.class, "*****I'm a Contentlet -- Publishing my File Child=" + file.getInode()); 100 publishAsset(file,req); 101 } 102 103 Logger.debug(PublishFactory.class, "IM HERE BEFORE PUBLISHING LINKS FOR A CONTENTLET!!!!!!!"); 105 java.util.List links = InodeFactory.getChildrenClassByCondition(webAsset, Link.class, condition); 106 java.util.Iterator linksIter = links.iterator(); 107 while (linksIter.hasNext()) { 108 Link link = (Link) linksIter.next(); 110 Logger.debug(PublishFactory.class, "*****I'm a Contentlet -- Publishing my Link Child=" + link.getInode()); 111 publishAsset(link,req); 112 } 113 ContentletServices.publishContentletToFile((Contentlet)webAsset); 115 116 ContentletMapServices.publishContentletMapToFile((Contentlet)webAsset); 118 119 ContentletFactory.reIndexContentlet((Contentlet) webAsset); 120 121 } 122 123 if (webAsset instanceof Template) { 124 125 Logger.debug(PublishFactory.class, "*****I'm a Template -- Publishing"); 126 127 java.util.List identifiers = InodeFactory.getChildrenClass(webAsset, Identifier.class); 129 java.util.Iterator identifiersIter = identifiers.iterator(); 130 while (identifiersIter.hasNext()) { 131 132 Identifier identifier = (Identifier) identifiersIter.next(); 133 Container container = (Container) IdentifierFactory.getWorkingChildOfClass(identifier, Container.class); 134 135 Logger.debug(PublishFactory.class, "*****I'm a Template -- Publishing my Container Child=" + container.getInode()); 136 publishAsset(container,req); 137 } 138 TemplateServices.publishTemplateToFile((Template)webAsset); 140 141 } 142 143 if (webAsset instanceof HTMLPage) 144 { 145 146 Logger.debug(PublishFactory.class, "*****I'm an HTML Page -- Publishing"); 147 148 Template templateParent = HTMLPageFactory.getHTMLPageTemplate(webAsset, true); 150 151 if (templateParent.getInode() > 0) { 152 153 Logger.debug(PublishFactory.class, "*****I'm an HTML Page -- Publishing my Child Template=" + templateParent.getInode()); 154 publishAsset(templateParent,req); 156 157 java.util.List identifiers = InodeFactory.getChildrenClass(templateParent, Identifier.class); 159 java.util.Iterator identifiersIter = identifiers.iterator(); 160 while (identifiersIter.hasNext()) { 161 162 Identifier identifier = (Identifier) identifiersIter.next(); 163 Logger.debug(PublishFactory.class, "*****I'm an HTML Page -- Publishing identifier=" + identifier.getInode()); 164 Container container = (Container) IdentifierFactory.getWorkingChildOfClass(identifier, Container.class); 165 166 List categories = InodeFactory.getParentsOfClass(container, Category.class); 167 List contentlets = null; 168 169 if (categories.size() == 0) { 170 Logger.debug(PublishFactory.class, "*******HTML Page Publishing Static Container"); 171 Identifier idenHtmlPage = IdentifierFactory.getIdentifierByInode(webAsset); 172 Identifier idenContainer = IdentifierFactory.getIdentifierByInode(container); 173 contentlets = ContentletFactory.getWorkingContentletsByOrderAndParents(idenHtmlPage.getInode(),idenContainer.getInode()); 174 } 175 else { 176 177 Logger.debug(PublishFactory.class, "*******HTML Page Publishing Dynamic Container"); 178 Iterator catsIter = categories.iterator(); 179 Set contentletSet = new HashSet (); 180 181 String condition = "working=" + com.dotmarketing.db.DbConnectionFactory.getDBTrue() + " and deleted=" + com.dotmarketing.db.DbConnectionFactory.getDBFalse(); 182 String sort = (container.getSortContentletsBy() == null) ? "sort_order" : container.getSortContentletsBy(); 183 184 while (catsIter.hasNext()) { 185 Category category = (Category) catsIter.next(); 186 List contentletsChildren = InodeFactory.getChildrenClassByConditionAndOrderBy(category, 187 Contentlet.class, condition, sort); 188 if (contentletsChildren != null && contentletsChildren.size() > 0) { 189 contentletSet.addAll(contentletsChildren); 190 } 191 } 192 contentlets = new ArrayList (); 193 contentlets.addAll(contentletSet); 194 } 195 java.util.Iterator contentletsIter = contentlets.iterator(); 197 while (contentletsIter.hasNext()) { 198 Contentlet contentlet = (Contentlet) contentletsIter.next(); 200 Logger.debug(PublishFactory.class, "*****I'm an HTML Page -- Publishing my Contentlet Child=" + contentlet.getInode()); 201 publishAsset(contentlet,req); 202 } 203 } 204 205 } 206 207 HTMLPage htmlPage = (HTMLPage) webAsset; 208 209 LiveCache.addToLiveAssetToCache((WebAsset) webAsset); 210 WorkingCache.addToWorkingAssetToCache((WebAsset) webAsset); 211 PageServices.publishPageToFile((HTMLPage)webAsset); 213 214 RefreshMenus.deleteMenus(); 216 217 PageNotFoundCache.removePageFromCache(htmlPage); 220 if(htmlPage.getURI().indexOf("index") != -1){ 221 String htmlPath = htmlPage.getURI().substring(0,htmlPage.getURI().lastIndexOf("/")+1); 222 PageNotFoundCache.removePageFromCache(htmlPath); 223 } 224 225 } 226 227 if (webAsset instanceof Folder) { 228 229 Folder parentFolder = (Folder) webAsset; 230 231 Logger.debug(PublishFactory.class, "*****I'm a Folder -- Publishing" + parentFolder.getName()); 232 233 String condition = "working=" + com.dotmarketing.db.DbConnectionFactory.getDBTrue() + " and deleted=" + com.dotmarketing.db.DbConnectionFactory.getDBFalse(); 234 235 java.util.List foldersListSubChildren = InodeFactory.getChildrenClass(parentFolder,Folder.class); 237 java.util.List linksListSubChildren = InodeFactory.getChildrenClassByCondition(parentFolder,Link.class,condition); 239 java.util.List htmlPagesSubListChildren = InodeFactory.getChildrenClassByCondition(parentFolder,HTMLPage.class,condition); 241 java.util.List filesListSubChildren = InodeFactory.getChildrenClassByCondition(parentFolder,File.class,condition); 243 java.util.List templatesListSubChildren = InodeFactory.getChildrenClassByCondition(parentFolder,Template.class,condition); 245 java.util.List containersListSubChildren = InodeFactory.getChildrenClassByCondition(parentFolder,Container.class,condition); 247 248 java.util.List elements = new java.util.ArrayList (); 250 elements.addAll(foldersListSubChildren); 251 elements.addAll(linksListSubChildren); 252 elements.addAll(htmlPagesSubListChildren); 253 elements.addAll(filesListSubChildren); 254 elements.addAll(templatesListSubChildren); 255 elements.addAll(containersListSubChildren); 256 257 java.util.Iterator elementsIter = elements.iterator(); 258 while (elementsIter.hasNext()) { 259 Inode inode = (Inode) elementsIter.next(); 260 Logger.debug(PublishFactory.class, "*****I'm a Folder -- Publishing my Inode Child=" + inode.getInode()); 261 publishAsset(inode,req); 262 } 263 } 264 265 if (webAsset instanceof Link) { 266 List contentlets = InodeFactory.getParentsOfClass(webAsset, Contentlet.class); 267 Iterator it = contentlets.iterator(); 268 while (it.hasNext()) { 269 Contentlet cont = (Contentlet) it.next(); 270 if (cont.isLive()) { 271 ContentletServices.writeContentletToFile(cont, false); 272 } 273 } 274 } 275 276 return true; 277 278 } 279 280 public static void publishAllLiveAssets() { 281 if(Config.CONTEXT == null) return; 282 Logger.debug(PublishFactory.class, "DOTCMS: Building list of live HTMLPages and Files"); 283 java.util.List list = HTMLPageFactory.getLiveHTMLPages(); 284 285 Iterator i = list.iterator(); 286 while (i.hasNext()) { 287 HTMLPage htmlPage = (HTMLPage) i.next(); 288 LiveCache.addToLiveAssetToCache(htmlPage); 289 } 290 291 list = FileFactory.getLiveFiles(); 292 i = list.iterator(); 293 while (i.hasNext()) { 294 com.dotmarketing.portlets.files.model.File file = (com.dotmarketing.portlets.files.model.File) i.next(); 295 LiveCache.addToLiveAssetToCache((WebAsset) file); 296 } 297 298 } 299 300 301 302 public static java.util.List getRelatedAssets(Inode webAsset,java.util.List relatedAssets) { 303 304 if (webAsset instanceof Contentlet) { 305 306 Logger.debug(PublishFactory.class, "*****I'm a Contentlet -- PrePublishing"); 307 308 String condition = "working=" + com.dotmarketing.db.DbConnectionFactory.getDBTrue(); 309 310 java.util.List files = InodeFactory.getChildrenClassByCondition(webAsset, File.class, condition); 312 java.util.Iterator filesIter = files.iterator(); 313 while (filesIter.hasNext()) { 314 File file = (File) filesIter.next(); 315 if (!file.isLive()) { 316 relatedAssets.add(file); 317 } 318 } 319 320 java.util.List links = InodeFactory.getChildrenClassByCondition(webAsset, Link.class, condition); 322 java.util.Iterator linksIter = links.iterator(); 323 while (linksIter.hasNext()) { 324 Link link = (Link) linksIter.next(); 325 if (!link.isLive()) { 326 relatedAssets.add(link); 327 } 328 } 329 330 } 331 332 if (webAsset instanceof Template) { 333 334 Logger.debug(PublishFactory.class, "*****I'm a Template -- PrePublishing"); 335 336 java.util.List identifiers = InodeFactory.getChildrenClass(webAsset, Identifier.class); 338 java.util.Iterator identifiersIter = identifiers.iterator(); 339 while (identifiersIter.hasNext()) { 340 341 Identifier identifier = (Identifier)identifiersIter.next(); 342 Container container = (Container) IdentifierFactory.getWorkingChildOfClass(identifier,Container.class); 343 if (!container.isLive()) { 344 relatedAssets.add(container); 345 } 346 } 347 348 } 349 350 if (webAsset instanceof HTMLPage) { 351 352 Logger.debug(PublishFactory.class, "*****I'm an HTML Page -- PrePublishing"); 353 354 Template templateParent = HTMLPageFactory.getHTMLPageTemplate(webAsset,true); 356 357 if (templateParent.getInode()>0) { 358 359 if (!templateParent.isLive()) { 360 relatedAssets.add(templateParent); 361 } 362 relatedAssets = getRelatedAssets(templateParent,relatedAssets); 364 365 java.util.List identifiers = InodeFactory.getChildrenClass(templateParent, Identifier.class); 367 java.util.Iterator identifiersIter = identifiers.iterator(); 368 while (identifiersIter.hasNext()) { 369 370 Identifier identifier = (Identifier)identifiersIter.next(); 371 Container container = (Container) IdentifierFactory.getWorkingChildOfClass(identifier,Container.class); 372 373 List categories = InodeFactory.getParentsOfClass(container, Category.class); 374 List contentlets = null; 375 376 if (categories.size() == 0) { 377 Logger.debug(PublishFactory.class, "*******HTML Page PrePublishing Static Container"); 378 Identifier idenHtmlPage = IdentifierFactory.getIdentifierByInode(webAsset); 379 Identifier idenContainer = IdentifierFactory.getIdentifierByInode(container); 380 contentlets = ContentletFactory.getWorkingContentletsByOrderAndParents(idenHtmlPage.getInode(),idenContainer.getInode()); 381 } 382 else { 383 384 Logger.debug(PublishFactory.class, "*******HTML Page PrePublishing Dynamic Container"); 385 Iterator catsIter = categories.iterator(); 386 Set contentletSet = new HashSet (); 387 388 String condition = "working=" + com.dotmarketing.db.DbConnectionFactory.getDBTrue() + " and deleted=" + com.dotmarketing.db.DbConnectionFactory.getDBFalse(); 389 String sort = (container.getSortContentletsBy() == null) ? "sort_order" : container.getSortContentletsBy(); 390 391 while (catsIter.hasNext()) { 392 Category category = (Category) catsIter.next(); 393 List contentletsChildren = InodeFactory.getChildrenClassByConditionAndOrderBy(category, 394 Contentlet.class, condition, sort); 395 if (contentletsChildren != null && contentletsChildren.size() > 0) { 396 contentletSet.addAll(contentletsChildren); 397 } 398 } 399 contentlets = new ArrayList (); 400 contentlets.addAll(contentletSet); 401 } 402 java.util.Iterator contentletsIter = contentlets.iterator(); 403 while (contentletsIter.hasNext()) { 404 Contentlet contentlet = (Contentlet)contentletsIter.next(); 406 if (!contentlet.isLive()) { 407 relatedAssets.add(contentlet); 408 } 409 relatedAssets = getRelatedAssets(contentlet,relatedAssets); 411 } 412 } 413 414 } 415 416 } 417 418 if (webAsset instanceof Folder) { 419 420 Folder parentFolder = (Folder) webAsset; 421 422 Logger.debug(PublishFactory.class, "*****I'm a Folder -- PrePublishing" + parentFolder.getName()); 423 424 String condition = "working=" + com.dotmarketing.db.DbConnectionFactory.getDBTrue() + " and deleted=" + com.dotmarketing.db.DbConnectionFactory.getDBFalse(); 425 426 java.util.List foldersListSubChildren = InodeFactory.getChildrenClass(parentFolder,Folder.class); 428 java.util.List linksListSubChildren = InodeFactory.getChildrenClassByCondition(parentFolder,Link.class,condition); 430 java.util.List htmlPagesSubListChildren = InodeFactory.getChildrenClassByCondition(parentFolder,HTMLPage.class,condition); 432 java.util.List filesListSubChildren = InodeFactory.getChildrenClassByCondition(parentFolder,File.class,condition); 434 java.util.List templatesListSubChildren = InodeFactory.getChildrenClassByCondition(parentFolder,Template.class,condition); 436 java.util.List containersListSubChildren = InodeFactory.getChildrenClassByCondition(parentFolder,Container.class,condition); 438 439 java.util.List elements = new java.util.ArrayList (); 441 elements.addAll(foldersListSubChildren); 442 elements.addAll(linksListSubChildren); 443 elements.addAll(htmlPagesSubListChildren); 444 elements.addAll(filesListSubChildren); 445 elements.addAll(templatesListSubChildren); 446 elements.addAll(containersListSubChildren); 447 448 449 450 java.util.Iterator elementsIter = elements.iterator(); 451 while (elementsIter.hasNext()) { 452 Inode asset = (Inode) elementsIter.next(); 453 if (asset instanceof WebAsset) { 454 if (!((WebAsset)asset).isLive()) { 455 relatedAssets.add(asset); 456 } 457 } 458 relatedAssets = getRelatedAssets(asset,relatedAssets); 460 } 461 462 } 463 464 return relatedAssets; 465 466 } 467 468 } 469 | Popular Tags |