KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.dotmarketing.services;
2
3 import com.dotmarketing.beans.Identifier;
4 import com.dotmarketing.cms.factories.PublicCompanyFactory;
5 import com.dotmarketing.factories.IdentifierFactory;
6 import com.dotmarketing.portlets.templates.model.Template;
7 import com.dotmarketing.util.Config;
8 import com.dotmarketing.util.Constants;
9 import com.dotmarketing.util.Logger;
10 import com.liferay.util.Http;
11
12 /**
13  * @author will
14  *
15  * To change this generated comment edit the template variable "typecomment":
16  * Window>Preferences>Java>Templates.
17  * To enable and disable the creation of type comments go to
18  * Window>Preferences>Java>Code Generation.
19  */

20 public class TemplateServices {
21
22     public static void previewTemplate(Template template, boolean temp, boolean previewMode) {
23
24         Logger.debug(TemplateServices.class, "template=" + template.getInode());
25
26         //gets the real path to the assets directory
27
String JavaDoc filePath =
28             (temp)
29                 ? Constants.HTML_PAGE_TMP_DIR + "/" + template.getInode() + ".jsp"
30                 : Constants.HTML_PAGE_DIR + "/" + template.getInode() + ".jsp";
31
32         Logger.debug(TemplateServices.class, "filePath:"+ filePath);
33
34         java.io.File JavaDoc f = new java.io.File JavaDoc(Config.CONTEXT.getRealPath(filePath));
35
36         try {
37
38             //gets buffered outputstrem to write the data in the file
39
java.io.BufferedOutputStream JavaDoc bout = new java.io.BufferedOutputStream JavaDoc( new java.io.FileOutputStream JavaDoc(f));
40
41             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
42             sb.append("<%@ include file=\"/jsp/html/common/cms_template_scriptlets.jsp\" %>");
43
44             //append the header information
45

46             sb.append(template.getBody());
47
48             java.io.StringReader JavaDoc strReader = new java.io.StringReader JavaDoc(sb.toString());
49
50             int bytedata;
51             //writes all data from input in output
52
while ((bytedata = strReader.read()) != -1) {
53                 bout.write(bytedata);
54             }
55
56             //closes all streams
57
bout.close();
58             strReader.close();
59             
60             Logger.debug(TemplateServices.class, "Finished generating the template file in tmp");
61
62             Logger.debug(TemplateServices.class, "Getting Page: http://" + PublicCompanyFactory.getDefaultCompany().getPortalURL() + "/portal" + filePath);
63             sb = new StringBuffer JavaDoc();
64             sb.append("<%@ include file=\"/jsp/html/common/cms_template_scriptlets.jsp\" %>");
65
66             String JavaDoc textHTTP =
67                 Http.URLtoString("http://" + PublicCompanyFactory.getDefaultCompany().getPortalURL() + "/portal" + filePath);
68             Logger.debug(TemplateServices.class, "Got the page!!!!");
69
70             //gets buffered outputstrem to write the data in the file
71
bout = new java.io.BufferedOutputStream JavaDoc(new java.io.FileOutputStream JavaDoc(f));
72
73             strReader = new java.io.StringReader JavaDoc(sb.append(textHTTP).toString());
74
75             //writes all data from input in output
76
while ((bytedata = strReader.read()) != -1) {
77                 bout.write(bytedata);
78             }
79             Logger.debug(TemplateServices.class, "wrote:" + f.getAbsoluteFile());
80
81             //closes all streams
82
bout.close();
83             strReader.close();
84             bout = null;
85             strReader = null;
86             f = null;
87             
88
89         }
90         catch (java.io.FileNotFoundException JavaDoc e) {
91             Logger.error(TemplateServices.class, "File Not Found!", e);
92         }
93         catch (Exception JavaDoc e) {
94             Logger.error(TemplateServices.class, e.toString(), e);
95         }
96
97     }
98     
99     public static void publishTemplateToFile(Template template) {
100
101         Identifier identifier = IdentifierFactory.getParentIdentifier(template);
102         writeTemplateToFile(template, identifier, false);
103
104     }
105
106     public static void writeTemplateToFile(Template template, boolean EDIT_MODE) {
107
108         Identifier identifier = IdentifierFactory.getParentIdentifier(template);
109         writeTemplateToFile(template, identifier, EDIT_MODE);
110
111     }
112     
113     
114     public static void writeTemplateToFile(Template template, Identifier identifier, boolean EDIT_MODE) {
115
116
117         try {
118             int bytedata;
119
120             String JavaDoc velocityRootPath = Config.getStringProperty("VELOCITY_ROOT");
121             if (velocityRootPath.startsWith("/WEB-INF")) {
122                 velocityRootPath = Config.CONTEXT.getRealPath(velocityRootPath);
123             }
124             velocityRootPath += java.io.File.separator;
125
126             String JavaDoc folderPath = (!EDIT_MODE) ? "live" + java.io.File.separator: "working" + java.io.File.separator;
127             
128             java.io.BufferedOutputStream JavaDoc tmpOut = new java.io.BufferedOutputStream JavaDoc(new java.io.FileOutputStream JavaDoc(new java.io.File JavaDoc(
129                     velocityRootPath + folderPath + identifier.getInode() + "." + Config.getStringProperty("VELOCITY_TEMPLATE_EXTENSION"))));
130
131             StringBuffer JavaDoc templateBody = new StringBuffer JavaDoc();
132             templateBody.append(Constants.TEMPLATE_PREPROCESS);
133             templateBody.append(template.getBody());
134             templateBody.append(Constants.TEMPLATE_POSTPROCESS);
135             
136             java.io.StringReader JavaDoc strReader = new java.io.StringReader JavaDoc(templateBody.toString());
137
138             //writes all data from input in output
139
while ((bytedata = strReader.read()) != -1) {
140                 tmpOut.write(bytedata);
141             }
142             tmpOut.close();
143             strReader.close();
144             
145         } catch (Exception JavaDoc e) {
146             Logger.error(TemplateServices.class, e.toString(), e);
147         }
148         
149     }
150
151     
152     public static void unpublishTemplateFile(Template asset) {
153
154         Identifier identifier = IdentifierFactory.getParentIdentifier(asset);
155         removeTemplateFile(asset, identifier, false);
156     }
157     
158     public static void removeTemplateFile(Template asset, boolean EDIT_MODE) {
159
160         Identifier identifier = IdentifierFactory.getParentIdentifier(asset);
161         removeTemplateFile(asset, identifier, EDIT_MODE);
162     }
163     
164     public static void removeTemplateFile (Template asset, Identifier identifier, boolean EDIT_MODE) {
165         String JavaDoc velocityRootPath = Config.getStringProperty("VELOCITY_ROOT");
166         if (velocityRootPath.startsWith("/WEB-INF")) {
167             velocityRootPath = Config.CONTEXT.getRealPath(velocityRootPath);
168         }
169         velocityRootPath += java.io.File.separator;
170
171         String JavaDoc folderPath = (!EDIT_MODE) ? "live" + java.io.File.separator: "working" + java.io.File.separator;
172
173         java.io.File JavaDoc f = new java.io.File JavaDoc(velocityRootPath + folderPath +
174                 identifier.getInode() + "." +
175                 Config.getStringProperty("VELOCITY_TEMPLATE_EXTENSION"));
176         f.delete();
177     }
178 }
179
Popular Tags