KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > cms > virtualtour > factories > VirtualTourFactory


1 package com.dotmarketing.cms.virtualtour.factories;
2
3 import java.io.File JavaDoc;
4 import java.io.FileReader JavaDoc;
5 import java.io.FileWriter JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.util.Date JavaDoc;
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 JavaDoc getBuildingTypesXML() {
29         Structure st = StructureFactory.getStructureByType(Config.getStringProperty("V_TOUR_BUILDINGS_STRUCT"));
30         Field f = FieldFactory.getFieldByName(st.getInode(), "Building Type");
31         String JavaDoc values = f.getValues();
32         String JavaDoc[] types = values.split("\n");
33         StringBuffer JavaDoc xml = new StringBuffer JavaDoc();
34
35         String JavaDoc 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 JavaDoc type : types) {
42             String JavaDoc[] 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 JavaDoc getBuildingListXML(String JavaDoc type) {
54         
55         Logger.debug(VirtualTourFactory.class, "getBuildingListXML: type = " + type);
56         String JavaDoc pathToDir = Config.CONTEXT.getRealPath("/WEB-INF/velocity/static/vt/");
57         File JavaDoc dirFile = new File JavaDoc(pathToDir);
58         if (!dirFile.exists()) {
59             dirFile.mkdir();
60         }
61         String JavaDoc fileName = "";
62         if (UtilMethods.isSet(type)) {
63             fileName = "buildings-" + type + ".xml";
64         } else {
65             fileName = "buildings.xml";
66         }
67         String JavaDoc path = dirFile.getAbsolutePath() + File.separator + fileName;
68         Logger.debug(VirtualTourFactory.class, "getBuildingListXML: file path = " + path);
69         File JavaDoc file = new File JavaDoc(path);
70         String JavaDoc 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 JavaDoc fw = null;
78                 try {
79                     boolean success = file.createNewFile();
80                     if (success) {
81                         fw = new FileWriter JavaDoc (file);
82                         text = buildBuildingListXML(type);
83                         fw.write(text);
84                     }
85                 } catch (IOException JavaDoc 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 JavaDoc 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 JavaDoc fr = null;
98                 try {
99                     fr = new FileReader JavaDoc(file);
100                     int filechar = fr.read();
101                     while (filechar != -1) {
102                         text += (char) filechar;
103                         filechar = fr.read();
104                     }
105                 } catch (IOException JavaDoc 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 JavaDoc 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 JavaDoc fw = null;
120             try {
121                 success = file.createNewFile();
122                 fw = new FileWriter JavaDoc (file);
123                 if (success) {
124                     text = buildBuildingListXML(type);
125                     fw.write(text);
126                 }
127             } catch (IOException JavaDoc 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 JavaDoc 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 JavaDoc buildBuildingListXML(String JavaDoc type) {
143         Structure st = StructureFactory.getStructureByType(Config.getStringProperty("V_TOUR_BUILDINGS_STRUCT"));
144         String JavaDoc 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 JavaDoc xml = new StringBuffer JavaDoc();
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 JavaDoc name = (String JavaDoc)doc.get("text1");
158             String JavaDoc identifier = (String JavaDoc)doc.get("identifier");
159             long xCoord = (Long JavaDoc)Long.parseLong((String JavaDoc)doc.get("integer6"));
160             long yCoord = (Long JavaDoc)Long.parseLong((String JavaDoc)doc.get("integer7"));
161             String JavaDoc 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 JavaDoc getBuildingDetailXML(String JavaDoc buildingIdentifier) {
173         String JavaDoc pathToDir = Config.CONTEXT.getRealPath("/WEB-INF/velocity/static/vt/");
174         File JavaDoc dirFile = new File JavaDoc(pathToDir);
175         if (!dirFile.exists()) {
176             dirFile.mkdir();
177         }
178         if (!UtilMethods.isSet(buildingIdentifier))
179             return "";
180         String JavaDoc fileName = "building-" + buildingIdentifier + ".xml";
181         String JavaDoc path = dirFile.getAbsolutePath() + File.separator + fileName;
182         File JavaDoc file = new File JavaDoc(path);
183         String JavaDoc text = "";
184         if (file.exists()) {
185             long fileModification = file.lastModified();
186             if (tooOld(fileModification)) {
187                 file.delete();
188                 FileWriter JavaDoc fw = null;
189                 try {
190                     boolean success = file.createNewFile();
191                     if (success) {
192                         fw = new FileWriter JavaDoc (file);
193                         text = buildBuildingDetailXML(buildingIdentifier);
194                         fw.write(text);
195                     }
196                 } catch (IOException JavaDoc 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 JavaDoc e) {
203                             Logger.error(VirtualTourFactory.class, "Error trying to close the file writer", e);
204                         }
205                 }
206             } else {
207                 FileReader JavaDoc fr = null;
208                 try {
209                     fr = new FileReader JavaDoc(file);
210                     int filechar = fr.read();
211                     while (filechar != -1) {
212                         text += (char) filechar;
213                         filechar = fr.read();
214                     }
215                 } catch (IOException JavaDoc 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 JavaDoc e) {
222                             Logger.error(VirtualTourFactory.class, "Error trying to close the file reader", e);
223                         }
224                 }
225             }
226         } else {
227             boolean success;
228             FileWriter JavaDoc fw = null;
229             try {
230                 success = file.createNewFile();
231                 fw = new FileWriter JavaDoc (file);
232                 if (success) {
233                     text = buildBuildingDetailXML(buildingIdentifier);
234                     fw.write(text);
235                 }
236             } catch (IOException JavaDoc 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 JavaDoc 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 JavaDoc buildBuildingDetailXML(String JavaDoc 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 JavaDoc body = (String JavaDoc)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 JavaDoc)ContentletFactory.getFieldValue(cont, "Building Image " + (i + 1));
261             if (imageInode != 0) {
262                 imgTotal++;
263                 imagesInodes[i] = imageInode;
264             }
265         }
266         StringBuffer JavaDoc xml = new StringBuffer JavaDoc ();
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc().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