1 package com.dotmarketing.cms.virtualtour.factories; 2 3 import java.io.File ; 4 import java.io.FileReader ; 5 import java.io.FileWriter ; 6 import java.io.IOException ; 7 import java.util.Date ; 8 9 import org.apache.lucene.document.Document; 10 11 import com.dotmarketing.beans.Identifier; 12 import com.dotmarketing.factories.IdentifierFactory; 13 import com.dotmarketing.factories.InodeFactory; 14 import com.dotmarketing.portlets.contentlet.factories.ContentletFactory; 15 import com.dotmarketing.portlets.contentlet.model.Contentlet; 16 import com.dotmarketing.portlets.structure.factories.FieldFactory; 17 import com.dotmarketing.portlets.structure.factories.StructureFactory; 18 import com.dotmarketing.portlets.structure.model.Field; 19 import com.dotmarketing.portlets.structure.model.Structure; 20 import com.dotmarketing.util.Config; 21 import com.dotmarketing.util.Logger; 22 import com.dotmarketing.util.LuceneHits; 23 import com.dotmarketing.util.UtilMethods; 24 25 26 public class VirtualTourFactory { 27 28 public static String getBuildingTypesXML() { 29 Structure st = StructureFactory.getStructureByType(Config.getStringProperty("V_TOUR_BUILDINGS_STRUCT")); 30 Field f = FieldFactory.getFieldByName(st.getInode(), "Building Type"); 31 String values = f.getValues(); 32 String [] types = values.split("\n"); 33 StringBuffer xml = new StringBuffer (); 34 35 String buildingListPath = Config.getStringProperty("V_TOUR_BUILDING_LIST_PATH"); 36 37 xml.append("<?xml version='1.0'?>\n"); 38 xml.append("<xmlPath>\n"); 39 int i = 0; 40 xml.append(" <myPath categoryName=\"All\" xmlFile=\"" + buildingListPath + "\" />\n"); 41 for (String type : types) { 42 String [] typeSplitted = type.split("\\|"); 43 if (typeSplitted.length == 2) { 44 xml.append(" <myPath categoryName=\"" + typeSplitted[0].trim() + "\" xmlFile=\"" + 45 buildingListPath + "?type=" + typeSplitted[1].trim() + "\" />\n"); 46 } 47 i++; 48 } 49 xml.append("</xmlPath>\n"); 50 return xml.toString(); 51 } 52 53 public static String getBuildingListXML(String type) { 54 55 Logger.debug(VirtualTourFactory.class, "getBuildingListXML: type = " + type); 56 String pathToDir = Config.CONTEXT.getRealPath("/WEB-INF/velocity/static/vt/"); 57 File dirFile = new File (pathToDir); 58 if (!dirFile.exists()) { 59 dirFile.mkdir(); 60 } 61 String fileName = ""; 62 if (UtilMethods.isSet(type)) { 63 fileName = "buildings-" + type + ".xml"; 64 } else { 65 fileName = "buildings.xml"; 66 } 67 String path = dirFile.getAbsolutePath() + File.separator + fileName; 68 Logger.debug(VirtualTourFactory.class, "getBuildingListXML: file path = " + path); 69 File file = new File (path); 70 String text = ""; 71 if (file.exists()) { 72 Logger.debug(VirtualTourFactory.class, "getBuildingListXML: file path exits!"); 73 long fileModification = file.lastModified(); 74 if (tooOld(fileModification)) { 75 Logger.debug(VirtualTourFactory.class, "getBuildingListXML: file path exists but it's too old rebuilding the file!"); 76 file.delete(); 77 FileWriter fw = null; 78 try { 79 boolean success = file.createNewFile(); 80 if (success) { 81 fw = new FileWriter (file); 82 text = buildBuildingListXML(type); 83 fw.write(text); 84 } 85 } catch (IOException e) { 86 Logger.error(VirtualTourFactory.class, "Error trying to generate the index xml file for the buildings", e); 87 } finally { 88 if (fw != null) 89 try { 90 fw.close(); 91 } catch (IOException e) { 92 Logger.error(VirtualTourFactory.class, "Error trying to close the file writer", e); 93 } 94 } 95 } else { 96 Logger.debug(VirtualTourFactory.class, "getBuildingListXML: file path exists reading the file!"); 97 FileReader fr = null; 98 try { 99 fr = new FileReader (file); 100 int filechar = fr.read(); 101 while (filechar != -1) { 102 text += (char) filechar; 103 filechar = fr.read(); 104 } 105 } catch (IOException e) { 106 Logger.error(VirtualTourFactory.class, "Error trying to read the index xml file for the buildings", e); 107 } finally { 108 if (fr != null) 109 try { 110 fr.close(); 111 } catch (IOException e) { 112 Logger.error(VirtualTourFactory.class, "Error trying to close the file reader", e); 113 } 114 } 115 } 116 } else { 117 Logger.debug(VirtualTourFactory.class, "getBuildingListXML: file path doesn't exist, building the file"); 118 boolean success; 119 FileWriter fw = null; 120 try { 121 success = file.createNewFile(); 122 fw = new FileWriter (file); 123 if (success) { 124 text = buildBuildingListXML(type); 125 fw.write(text); 126 } 127 } catch (IOException e) { 128 Logger.error(VirtualTourFactory.class, "Error trying to generate the index xml file for the buildings", e); 129 } finally { 130 if (fw != null) 131 try { 132 fw.close(); 133 } catch (IOException e) { 134 Logger.error(VirtualTourFactory.class, "Error trying to close the file writer", e); 135 } 136 } 137 } 138 139 return text; 140 } 141 142 private static String buildBuildingListXML(String type) { 143 Structure st = StructureFactory.getStructureByType(Config.getStringProperty("V_TOUR_BUILDINGS_STRUCT")); 144 String luceneQuery = "+structureInode: " + st.getInode() + " +type:content +live:true +deleted:false +languageId:1 -text2:tour"; 145 if (UtilMethods.isSet(type)) 146 luceneQuery = "+structureInode: " + st.getInode() + " +type:content +live:true +deleted:false +languageId:1 +text2:" + type.toLowerCase(); 147 148 Logger.debug(VirtualTourFactory.class, "buildBuildingListXML: lucene query = " + luceneQuery); 149 150 LuceneHits hits = ContentletFactory.indexSearch(st, luceneQuery, false, 0, 1000, "text1"); 151 StringBuffer xml = new StringBuffer (); 152 xml.append("<?xml version='1.0'?>\n"); 153 xml.append("<database>\n"); 154 Logger.debug(VirtualTourFactory.class, "buildBuildingListXML: hits.getTotal() = " + hits.getTotal()); 155 for (int i = 0; i < hits.getTotal(); i++) { 156 Document doc = hits.doc(i); 157 String name = (String )doc.get("text1"); 158 String identifier = (String )doc.get("identifier"); 159 long xCoord = (Long )Long.parseLong((String )doc.get("integer6")); 160 long yCoord = (Long )Long.parseLong((String )doc.get("integer7")); 161 String refPage = Config.getStringProperty("V_TOUR_BUILDING_PATH") + "?id=" + 162 identifier; 163 if (xCoord > 0 && yCoord > 0) { 164 xml.append("<Index myListN=\"" + name + "\" myXcor=\"" + xCoord + "\" myYcor=\"" + yCoord + 165 "\" myStop=\"" + refPage + "\"></Index>\n"); 166 } 167 } 168 xml.append("</database>\n"); 169 return xml.toString(); 170 } 171 172 public static String getBuildingDetailXML(String buildingIdentifier) { 173 String pathToDir = Config.CONTEXT.getRealPath("/WEB-INF/velocity/static/vt/"); 174 File dirFile = new File (pathToDir); 175 if (!dirFile.exists()) { 176 dirFile.mkdir(); 177 } 178 if (!UtilMethods.isSet(buildingIdentifier)) 179 return ""; 180 String fileName = "building-" + buildingIdentifier + ".xml"; 181 String path = dirFile.getAbsolutePath() + File.separator + fileName; 182 File file = new File (path); 183 String text = ""; 184 if (file.exists()) { 185 long fileModification = file.lastModified(); 186 if (tooOld(fileModification)) { 187 file.delete(); 188 FileWriter fw = null; 189 try { 190 boolean success = file.createNewFile(); 191 if (success) { 192 fw = new FileWriter (file); 193 text = buildBuildingDetailXML(buildingIdentifier); 194 fw.write(text); 195 } 196 } catch (IOException e) { 197 Logger.error(VirtualTourFactory.class, "Error trying to generate the index xml file for the buildings", e); 198 } finally { 199 if (fw != null) 200 try { 201 fw.close(); 202 } catch (IOException e) { 203 Logger.error(VirtualTourFactory.class, "Error trying to close the file writer", e); 204 } 205 } 206 } else { 207 FileReader fr = null; 208 try { 209 fr = new FileReader (file); 210 int filechar = fr.read(); 211 while (filechar != -1) { 212 text += (char) filechar; 213 filechar = fr.read(); 214 } 215 } catch (IOException e) { 216 Logger.error(VirtualTourFactory.class, "Error trying to read the index xml file for the buildings", e); 217 } finally { 218 if (fr != null) 219 try { 220 fr.close(); 221 } catch (IOException e) { 222 Logger.error(VirtualTourFactory.class, "Error trying to close the file reader", e); 223 } 224 } 225 } 226 } else { 227 boolean success; 228 FileWriter fw = null; 229 try { 230 success = file.createNewFile(); 231 fw = new FileWriter (file); 232 if (success) { 233 text = buildBuildingDetailXML(buildingIdentifier); 234 fw.write(text); 235 } 236 } catch (IOException e) { 237 Logger.error(VirtualTourFactory.class, "Error trying to generate the index xml file for the buildings", e); 238 } finally { 239 if (fw != null) 240 try { 241 fw.close(); 242 } catch (IOException e) { 243 Logger.error(VirtualTourFactory.class, "Error trying to close the file writer", e); 244 } 245 } 246 } 247 248 return text; 249 } 250 251 private static String buildBuildingDetailXML(String identifier) { 252 Identifier id = (Identifier) InodeFactory.getInode(identifier, Identifier.class); 253 Contentlet cont = (Contentlet) IdentifierFactory.getLiveChildOfClass(id, Contentlet.class); 254 if (cont.getInode() == 0) 255 return ""; 256 String body = (String )ContentletFactory.getFieldValue(cont, "Short Description"); 257 int imgTotal = 0; 258 long[] imagesInodes = new long[5]; 259 for (int i = 0; i < 5; i++) { 260 long imageInode = (Long )ContentletFactory.getFieldValue(cont, "Building Image " + (i + 1)); 261 if (imageInode != 0) { 262 imgTotal++; 263 imagesInodes[i] = imageInode; 264 } 265 } 266 StringBuffer xml = new StringBuffer (); 267 xml.append("dummyVar=none&"); 268 xml.append("stopDescription=" + UtilMethods.encodeURL(body) + "&"); 269 xml.append("imgTotal=" + imgTotal + "&"); 270 for (int i = 0; i < 5; i++) { 271 if (imagesInodes[i] != 0) { 272 String url = "thumbfoto" + (i + 1) + "=" + UtilMethods.encodeURL("/thumbnail?inode=" + imagesInodes[i] + "&h=94&w=94"); 273 xml.append(url); 274 xml.append("&"); 275 } else { 276 String url = "thumbfoto" + (i + 1) + "=null"; 277 xml.append(url); 278 xml.append("&"); 279 } 280 } 281 int imgCounter = imgTotal; 282 for (int i = 0; i < 5; i++) { 283 if (imagesInodes[i] != 0) { 284 imgCounter--; 285 String url = "foto" + (i + 1) + "=" + UtilMethods.encodeURL("/thumbnail?inode=" + imagesInodes[i] + "&h=350&w=350"); 286 xml.append(url); 287 if (i < 4) 288 xml.append("&"); 289 } else { 290 imgCounter--; 291 String url = "foto" + (i + 1) + "=null"; 292 xml.append(url); 293 if (i < 4) 294 xml.append("&"); 295 } 296 } 297 return xml.toString(); 298 } 299 300 private static boolean tooOld(long fileModification) { 301 long now = new Date ().getTime(); 302 boolean returnValue = false; 303 int hoursToLive = Config.getIntProperty("V_TOUR_REFRESH_TIME", 15); 304 long millisToLive = hoursToLive * 60000; 305 if ((now - fileModification) > millisToLive) { 306 returnValue = true; 307 } 308 return returnValue; 309 } 310 311 } 312 | Popular Tags |