KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > factories > PublishFactory


1 package com.dotmarketing.factories;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashSet JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Set JavaDoc;
8
9 import javax.servlet.http.HttpServletRequest JavaDoc;
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 /**
43  *
44  * @author maria, david (2005)
45  */

46 public class PublishFactory {
47
48     /**
49      * This method publish an asset and its dependencies based on the permissions
50      * @param webAsset Asset to be publish
51      * @param req
52      * @return true if the asset was published, false if not
53      */

54     @SuppressWarnings JavaDoc("unchecked")
55     public static boolean publishAsset(Inode webAsset,HttpServletRequest JavaDoc req)
56     {
57         User user = null;
58         try {
59             user = PortalUtil.getUser(req);
60         } catch (Exception JavaDoc 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             // publishing a file
78
LiveCache.addToLiveAssetToCache((WebAsset) webAsset);
79             WorkingCache.addToWorkingAssetToCache((WebAsset) webAsset);
80         }
81
82         if (webAsset instanceof Container) {
83
84             //saves to live folder under velocity
85
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 JavaDoc condition = "live=" + com.dotmarketing.db.DbConnectionFactory.getDBTrue();
93             //gets all not live file children
94
java.util.List JavaDoc files = InodeFactory.getChildrenClassByCondition(webAsset, File.class, condition);
95             java.util.Iterator JavaDoc filesIter = files.iterator();
96             while (filesIter.hasNext()) {
97                 //publishes each one
98
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             //gets all not live link children
104
Logger.debug(PublishFactory.class, "IM HERE BEFORE PUBLISHING LINKS FOR A CONTENTLET!!!!!!!");
105             java.util.List JavaDoc links = InodeFactory.getChildrenClassByCondition(webAsset, Link.class, condition);
106             java.util.Iterator JavaDoc linksIter = links.iterator();
107             while (linksIter.hasNext()) {
108                 //publishes each one
109
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             //writes the contentlet to a live directory under velocity folder
114
ContentletServices.publishContentletToFile((Contentlet)webAsset);
115             
116             //writes the contentlet object to a file
117
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             //gets all identifier children
128
java.util.List JavaDoc identifiers = InodeFactory.getChildrenClass(webAsset, Identifier.class);
129             java.util.Iterator JavaDoc 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             //writes the template to a live directory under velocity folder
139
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             //gets working (not published) template parent for this html page
149
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                 //if it exists it publishes it
155
publishAsset(templateParent,req);
156
157                 //gets all live container children
158
java.util.List JavaDoc identifiers = InodeFactory.getChildrenClass(templateParent, Identifier.class);
159                 java.util.Iterator JavaDoc 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 JavaDoc categories = InodeFactory.getParentsOfClass(container, Category.class);
167                     List JavaDoc 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 JavaDoc catsIter = categories.iterator();
179                         Set JavaDoc contentletSet = new HashSet JavaDoc();
180
181                         String JavaDoc condition = "working=" + com.dotmarketing.db.DbConnectionFactory.getDBTrue() + " and deleted=" + com.dotmarketing.db.DbConnectionFactory.getDBFalse();
182                         String JavaDoc sort = (container.getSortContentletsBy() == null) ? "sort_order" : container.getSortContentletsBy();
183
184                         while (catsIter.hasNext()) {
185                             Category category = (Category) catsIter.next();
186                             List JavaDoc 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 JavaDoc();
193                         contentlets.addAll(contentletSet);
194                     }
195                     //gets all not live contentlet children
196
java.util.Iterator JavaDoc contentletsIter = contentlets.iterator();
197                     while (contentletsIter.hasNext()) {
198                         //publishes each one
199
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             //writes the htmlpage to a live directory under velocity folder
212
PageServices.publishPageToFile((HTMLPage)webAsset);
213
214             //Refreshing the menues
215
RefreshMenus.deleteMenus();
216
217             //If the webassets published is an HTML page, we have to remove
218
//it from the PageNotFoundCache
219
PageNotFoundCache.removePageFromCache(htmlPage);
220             if(htmlPage.getURI().indexOf("index") != -1){
221                 String JavaDoc 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 JavaDoc condition = "working=" + com.dotmarketing.db.DbConnectionFactory.getDBTrue() + " and deleted=" + com.dotmarketing.db.DbConnectionFactory.getDBFalse();
234
235             //gets all links for this folder
236
java.util.List JavaDoc foldersListSubChildren = InodeFactory.getChildrenClass(parentFolder,Folder.class);
237             //gets all links for this folder
238
java.util.List JavaDoc linksListSubChildren = InodeFactory.getChildrenClassByCondition(parentFolder,Link.class,condition);
239             //gets all html pages for this folder
240
java.util.List JavaDoc htmlPagesSubListChildren = InodeFactory.getChildrenClassByCondition(parentFolder,HTMLPage.class,condition);
241             //gets all files for this folder
242
java.util.List JavaDoc filesListSubChildren = InodeFactory.getChildrenClassByCondition(parentFolder,File.class,condition);
243             //gets all templates for this folder
244
java.util.List JavaDoc templatesListSubChildren = InodeFactory.getChildrenClassByCondition(parentFolder,Template.class,condition);
245             //gets all containers for this folder
246
java.util.List JavaDoc containersListSubChildren = InodeFactory.getChildrenClassByCondition(parentFolder,Container.class,condition);
247
248             //gets all subitems
249
java.util.List JavaDoc elements = new java.util.ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc contentlets = InodeFactory.getParentsOfClass(webAsset, Contentlet.class);
267             Iterator JavaDoc 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 JavaDoc list = HTMLPageFactory.getLiveHTMLPages();
284
285         Iterator JavaDoc 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 JavaDoc getRelatedAssets(Inode webAsset,java.util.List JavaDoc relatedAssets) {
303
304         if (webAsset instanceof Contentlet) {
305
306             Logger.debug(PublishFactory.class, "*****I'm a Contentlet -- PrePublishing");
307
308             String JavaDoc condition = "working=" + com.dotmarketing.db.DbConnectionFactory.getDBTrue();
309
310             //gets all not live file children
311
java.util.List JavaDoc files = InodeFactory.getChildrenClassByCondition(webAsset, File.class, condition);
312             java.util.Iterator JavaDoc 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             //gets all not live link children
321
java.util.List JavaDoc links = InodeFactory.getChildrenClassByCondition(webAsset, Link.class, condition);
322             java.util.Iterator JavaDoc 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             //gets all identifier children
337
java.util.List JavaDoc identifiers = InodeFactory.getChildrenClass(webAsset, Identifier.class);
338             java.util.Iterator JavaDoc 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             //gets working (not published) template parent for this html page
355
Template templateParent = HTMLPageFactory.getHTMLPageTemplate(webAsset,true);
356
357             if (templateParent.getInode()>0) {
358
359                 if (!templateParent.isLive()) {
360                     relatedAssets.add(templateParent);
361                 }
362                 //if it exists it prepublishes it
363
relatedAssets = getRelatedAssets(templateParent,relatedAssets);
364
365                 //gets all live container children
366
java.util.List JavaDoc identifiers = InodeFactory.getChildrenClass(templateParent, Identifier.class);
367                 java.util.Iterator JavaDoc 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 JavaDoc categories = InodeFactory.getParentsOfClass(container, Category.class);
374                     List JavaDoc 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 JavaDoc catsIter = categories.iterator();
386                         Set JavaDoc contentletSet = new HashSet JavaDoc();
387
388                         String JavaDoc condition = "working=" + com.dotmarketing.db.DbConnectionFactory.getDBTrue() + " and deleted=" + com.dotmarketing.db.DbConnectionFactory.getDBFalse();
389                         String JavaDoc sort = (container.getSortContentletsBy() == null) ? "sort_order" : container.getSortContentletsBy();
390
391                         while (catsIter.hasNext()) {
392                             Category category = (Category) catsIter.next();
393                             List JavaDoc 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 JavaDoc();
400                         contentlets.addAll(contentletSet);
401                     }
402                     java.util.Iterator JavaDoc contentletsIter = contentlets.iterator();
403                     while (contentletsIter.hasNext()) {
404                         //publishes each one
405
Contentlet contentlet = (Contentlet)contentletsIter.next();
406                         if (!contentlet.isLive()) {
407                             relatedAssets.add(contentlet);
408                         }
409                         //if it exists it prepublishes it
410
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 JavaDoc condition = "working=" + com.dotmarketing.db.DbConnectionFactory.getDBTrue() + " and deleted=" + com.dotmarketing.db.DbConnectionFactory.getDBFalse();
425
426             //gets all links for this folder
427
java.util.List JavaDoc foldersListSubChildren = InodeFactory.getChildrenClass(parentFolder,Folder.class);
428             //gets all links for this folder
429
java.util.List JavaDoc linksListSubChildren = InodeFactory.getChildrenClassByCondition(parentFolder,Link.class,condition);
430             //gets all html pages for this folder
431
java.util.List JavaDoc htmlPagesSubListChildren = InodeFactory.getChildrenClassByCondition(parentFolder,HTMLPage.class,condition);
432             //gets all files for this folder
433
java.util.List JavaDoc filesListSubChildren = InodeFactory.getChildrenClassByCondition(parentFolder,File.class,condition);
434             //gets all templates for this folder
435
java.util.List JavaDoc templatesListSubChildren = InodeFactory.getChildrenClassByCondition(parentFolder,Template.class,condition);
436             //gets all containers for this folder
437
java.util.List JavaDoc containersListSubChildren = InodeFactory.getChildrenClassByCondition(parentFolder,Container.class,condition);
438
439             //gets all subitems
440
java.util.List JavaDoc elements = new java.util.ArrayList JavaDoc();
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 JavaDoc 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                 //if it exists it prepublishes it
459
relatedAssets = getRelatedAssets(asset,relatedAssets);
460             }
461
462         }
463
464         return relatedAssets;
465
466     }
467
468 }
469
Popular Tags