KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > services > PageServices


1 package com.dotmarketing.services;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6
7 import com.dotmarketing.beans.Identifier;
8 import com.dotmarketing.factories.IdentifierFactory;
9 import com.dotmarketing.factories.InodeFactory;
10 import com.dotmarketing.portlets.containers.model.Container;
11 import com.dotmarketing.portlets.contentlet.model.Contentlet;
12 import com.dotmarketing.portlets.htmlpages.model.HTMLPage;
13 import com.dotmarketing.portlets.languagesmanager.factories.LanguageFactory;
14 import com.dotmarketing.portlets.languagesmanager.model.Language;
15 import com.dotmarketing.util.Config;
16 import com.dotmarketing.util.Logger;
17 import com.dotmarketing.util.UtilMethods;
18
19 /**
20  * @author will
21  *
22  * To change this generated comment edit the template variable "typecomment":
23  * Window>Preferences>Java>Templates.
24  * To enable and disable the creation of type comments go to
25  * Window>Preferences>Java>Code Generation.
26  */

27 public class PageServices {
28
29     public static void publishPageToFile(HTMLPage htmlPage) {
30
31         Identifier identifier = IdentifierFactory.getParentIdentifier(htmlPage);
32         writePageToFile(htmlPage, identifier, false);
33     }
34     
35     public static void writePageToFile(HTMLPage htmlPage, boolean EDIT_MODE) {
36
37         Identifier identifier = IdentifierFactory.getParentIdentifier(htmlPage);
38         writePageToFile(htmlPage, identifier, EDIT_MODE);
39     }
40
41     @SuppressWarnings JavaDoc("unchecked")
42     public static void writePageToFile(HTMLPage htmlPage, Identifier identifier, boolean EDIT_MODE) {
43
44         // let's write this puppy out to our file
45
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
46
47         com.dotmarketing.portlets.templates.model.Template cmsTemplate = com.dotmarketing.portlets.htmlpages.factories.HTMLPageFactory.getHTMLPageTemplate(htmlPage, EDIT_MODE);
48
49         //gets pageChannel for this path
50
java.util.StringTokenizer JavaDoc st = new java.util.StringTokenizer JavaDoc(String.valueOf(identifier.getURI()),"/");
51         String JavaDoc pageChannel = null;
52         if(st.hasMoreTokens()){
53             pageChannel = st.nextToken();
54         }
55         
56         //creates the context where to place the variables
57
// Build a context to pass to the page
58
sb.append("#set ( $quote = '\"' )\n");
59         sb.append("#set ($HTMLPAGE_INODE = \"" + String.valueOf(htmlPage.getInode()) + "\" )\n");
60         sb.append("#set ($HTMLPAGE_TITLE = \"" + UtilMethods.espaceForVelocity(htmlPage.getTitle()) + "\" )\n");
61         sb.append("#set ($TEMPLATE_INODE = \"" + String.valueOf(cmsTemplate.getInode()) + "\" )\n");
62         sb.append("#set ($HTMLPAGE_META = \"" + UtilMethods.espaceForVelocity(htmlPage.getMetadata()) + "\" )\n");
63         sb.append("#set ($HTMLPAGE_META = \"#fixBreaks($HTMLPAGE_META)\")\n");
64         sb.append("#set ($HTMLPAGE_SECURE = \"" + String.valueOf(htmlPage.isHttpsRequired()) + "\" )\n");
65         sb.append("#set ($VTLSERVLET_URI = \"" + identifier.getURI() + "\" )\n");
66         sb.append("#set ($HTMLPAGE_REDIRECT = \"" + UtilMethods.espaceForVelocity(htmlPage.getRedirect()) + "\" )\n");
67         sb.append("#set ($pageTitle = \"" + UtilMethods.espaceForVelocity(htmlPage.getTitle()) + "\" )\n");
68         sb.append("#set ($pageChannel = \"" + pageChannel + "\" )");
69
70
71
72         //get the containers for the page and stick them in context
73
List JavaDoc identifiers = InodeFactory.getChildrenClass(cmsTemplate, Identifier.class);
74
75         String JavaDoc folderPath = (!EDIT_MODE) ? "live/" : "working/";
76         
77         List JavaDoc languages = LanguageFactory.getLanguages();
78         
79         Iterator JavaDoc i = identifiers.iterator();
80         while(i.hasNext()){
81             Identifier ident = (Identifier) i.next();
82             Container c = null;
83             if (EDIT_MODE) {
84                 c = (Container) IdentifierFactory.getWorkingChildOfClass(ident,Container.class);
85             }
86             else {
87                 c = (Container) IdentifierFactory.getLiveChildOfClass(ident,Container.class);
88             }
89             //sets container to load the container file
90
sb.append("#set ($container"+ident.getInode() + " = \"" + folderPath + ident.getInode() + "." + Config.getStringProperty("VELOCITY_CONTAINER_EXTENSION") + "\" )");
91
92             String JavaDoc sort = (c.getSortContentletsBy() == null) ? "tree_order" : c.getSortContentletsBy();
93
94             boolean dynamicContainer = UtilMethods.isSet(c.getLuceneQuery());
95                 
96             int langCounter = 0;
97             
98             Iterator JavaDoc langIter = languages.iterator();
99             
100             while (langIter.hasNext()) {
101             
102                 Language language = (Language) langIter.next();
103
104                 List JavaDoc<Contentlet> contentlets = new ArrayList JavaDoc<Contentlet>();
105                 if (!dynamicContainer) {
106                     Identifier idenHtmlPage = IdentifierFactory.getIdentifierByWebAsset(htmlPage);
107                     Identifier idenContainer = IdentifierFactory.getIdentifierByWebAsset(c);
108                     //The container doesn't have categories
109
contentlets = com.dotmarketing.portlets.contentlet.factories.ContentletFactory
110                     .getContentletsByOrderAndParents(idenHtmlPage.getInode(), idenContainer.getInode(), sort, EDIT_MODE, language.getId()+"");
111                     Logger.debug(PageServices.class, "HTMLPage= " + htmlPage.getInode() + " Container=" + c.getInode() + " Language=" + language.getId() + " Contentlets=" + contentlets.size());
112                 }
113                 
114                 String JavaDoc contentletList = "";
115                 Iterator JavaDoc iter = contentlets.iterator();
116                 int count = 0;
117                 while (iter.hasNext() && count < c.getMaxContentlets()) {
118                     Contentlet contentlet = (Contentlet) iter.next();
119                     Identifier contentletIdentifier = IdentifierFactory.getParentIdentifier(contentlet);
120                     contentletList += "\"" + folderPath + contentletIdentifier.getInode() + "_" + language.getId() + "." + Config.getStringProperty("VELOCITY_CONTENT_EXTENSION") + "\"";
121                     count++;
122                     if (iter.hasNext() && count < c.getMaxContentlets()) {
123                         contentletList += ",";
124                     }
125                 }
126                 //sets contentletlist with all the files to load per container
127
if (langCounter==0) {
128                     sb.append("#if ($language==\"" + language.getId() + "\")");
129                 }
130                 else {
131                     sb.append("#elseif ($language==\"" + language.getId() + "\")");
132                 }
133                 sb.append("#set ($contentletList" + ident.getInode() + " = [" + contentletList + "] )");
134                 langCounter++;
135                 
136             }
137             sb.append("#end");
138         }
139         
140         Identifier iden = IdentifierFactory.getIdentifierByWebAsset(cmsTemplate);
141         sb.append("#parse('" + folderPath + iden.getInode() + "." + Config.getStringProperty("VELOCITY_TEMPLATE_EXTENSION") + "')");
142                 
143         try {
144             int bytedata;
145             String JavaDoc realFolderPath = (!EDIT_MODE) ? "live" + java.io.File.separator: "working" + java.io.File.separator;
146             String JavaDoc velocityRootPath = Config.getStringProperty("VELOCITY_ROOT");
147             if (velocityRootPath.startsWith("/WEB-INF")) {
148                 velocityRootPath = Config.CONTEXT.getRealPath(velocityRootPath);
149             }
150             velocityRootPath += java.io.File.separator;
151
152             java.io.BufferedOutputStream JavaDoc tmpOut = new java.io.BufferedOutputStream JavaDoc(new java.io.FileOutputStream JavaDoc(new java.io.File JavaDoc(
153                     velocityRootPath + realFolderPath + identifier.getInode() + "." + Config.getStringProperty("VELOCITY_HTMLPAGE_EXTENSION"))));
154
155             java.io.StringReader JavaDoc strReader = new java.io.StringReader JavaDoc(sb.toString());
156
157             //writes all data from input in output
158
while ((bytedata = strReader.read()) != -1) {
159                 tmpOut.write(bytedata);
160             }
161             tmpOut.close();
162             strReader.close();
163         } catch (Exception JavaDoc e) {
164             Logger.error(PageServices.class, e.toString(), e);
165         }
166         
167     }
168     
169     public static void unpublishPageFile(HTMLPage htmlPage) {
170
171         Identifier identifier = IdentifierFactory.getParentIdentifier(htmlPage);
172         removePageFile(htmlPage, identifier, false);
173     }
174     
175     public static void removePageFile(HTMLPage htmlPage, boolean EDIT_MODE) {
176
177         Identifier identifier = IdentifierFactory.getParentIdentifier(htmlPage);
178         removePageFile(htmlPage, identifier, EDIT_MODE);
179     }
180     
181     public static void removePageFile (HTMLPage htmlPage, Identifier identifier, boolean EDIT_MODE) {
182         String JavaDoc folderPath = (!EDIT_MODE) ? "live" + java.io.File.separator: "working" + java.io.File.separator;
183         String JavaDoc velocityRootPath = Config.getStringProperty("VELOCITY_ROOT");
184         if (velocityRootPath.startsWith("/WEB-INF")) {
185             velocityRootPath = Config.CONTEXT.getRealPath(velocityRootPath);
186         }
187         velocityRootPath += java.io.File.separator;
188         java.io.File JavaDoc f = new java.io.File JavaDoc(velocityRootPath + folderPath +
189                 identifier.getInode() + "." +
190                 Config.getStringProperty("VELOCITY_HTMLPAGE_EXTENSION"));
191         f.delete();
192     }
193     
194 }
195
Popular Tags