| 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 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 String 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 f = new java.io.File (Config.CONTEXT.getRealPath(filePath)); 35 36 try { 37 38 java.io.BufferedOutputStream bout = new java.io.BufferedOutputStream ( new java.io.FileOutputStream (f)); 40 41 StringBuffer sb = new StringBuffer (); 42 sb.append("<%@ include file=\"/jsp/html/common/cms_template_scriptlets.jsp\" %>"); 43 44 46 sb.append(template.getBody()); 47 48 java.io.StringReader strReader = new java.io.StringReader (sb.toString()); 49 50 int bytedata; 51 while ((bytedata = strReader.read()) != -1) { 53 bout.write(bytedata); 54 } 55 56 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 (); 64 sb.append("<%@ include file=\"/jsp/html/common/cms_template_scriptlets.jsp\" %>"); 65 66 String textHTTP = 67 Http.URLtoString("http://" + PublicCompanyFactory.getDefaultCompany().getPortalURL() + "/portal" + filePath); 68 Logger.debug(TemplateServices.class, "Got the page!!!!"); 69 70 bout = new java.io.BufferedOutputStream (new java.io.FileOutputStream (f)); 72 73 strReader = new java.io.StringReader (sb.append(textHTTP).toString()); 74 75 while ((bytedata = strReader.read()) != -1) { 77 bout.write(bytedata); 78 } 79 Logger.debug(TemplateServices.class, "wrote:" + f.getAbsoluteFile()); 80 81 bout.close(); 83 strReader.close(); 84 bout = null; 85 strReader = null; 86 f = null; 87 88 89 } 90 catch (java.io.FileNotFoundException e) { 91 Logger.error(TemplateServices.class, "File Not Found!", e); 92 } 93 catch (Exception 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 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 folderPath = (!EDIT_MODE) ? "live" + java.io.File.separator: "working" + java.io.File.separator; 127 128 java.io.BufferedOutputStream tmpOut = new java.io.BufferedOutputStream (new java.io.FileOutputStream (new java.io.File ( 129 velocityRootPath + folderPath + identifier.getInode() + "." + Config.getStringProperty("VELOCITY_TEMPLATE_EXTENSION")))); 130 131 StringBuffer templateBody = new StringBuffer (); 132 templateBody.append(Constants.TEMPLATE_PREPROCESS); 133 templateBody.append(template.getBody()); 134 templateBody.append(Constants.TEMPLATE_POSTPROCESS); 135 136 java.io.StringReader strReader = new java.io.StringReader (templateBody.toString()); 137 138 while ((bytedata = strReader.read()) != -1) { 140 tmpOut.write(bytedata); 141 } 142 tmpOut.close(); 143 strReader.close(); 144 145 } catch (Exception 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 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 folderPath = (!EDIT_MODE) ? "live" + java.io.File.separator: "working" + java.io.File.separator; 172 173 java.io.File f = new java.io.File (velocityRootPath + folderPath + 174 identifier.getInode() + "." + 175 Config.getStringProperty("VELOCITY_TEMPLATE_EXTENSION")); 176 f.delete(); 177 } 178 } 179 | Popular Tags |