|
20 | 20 | import javax.xml.transform.TransformerFactory;
|
21 | 21 | import javax.xml.transform.dom.DOMSource;
|
22 | 22 | import javax.xml.transform.stream.StreamResult;
|
| 23 | +import java.io.BufferedReader; |
| 24 | +import java.io.FileReader; |
| 25 | +import java.io.IOException; |
23 | 26 | import java.io.StringWriter;
|
24 | 27 | import java.util.ArrayList;
|
25 | 28 | import java.util.List;
|
@@ -65,6 +68,50 @@ public static String parseRestRequest (Map<?, ?> params)
|
65 | 68 | weatherLocation.getLocationName(), weatherDate.getMediumString()));
|
66 | 69 | }
|
67 | 70 |
|
| 71 | + if (properties.getProperty("useFlatFiles").equals("true")) { |
| 72 | + String flatFile = parseFile(weatherDate, weatherLocation); |
| 73 | + if (flatFile != null) { |
| 74 | + return flatFile; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return queryDB (responseType, weatherDate, weatherLocation); |
| 79 | + } |
| 80 | + |
| 81 | + private static String parseFile (WeatherDate weatherDate, Location location) |
| 82 | + { |
| 83 | + StringBuilder sb = null; |
| 84 | + BufferedReader br = null; |
| 85 | + try { |
| 86 | + String sCurrentLine; |
| 87 | + sb = new StringBuilder(); |
| 88 | + br = new BufferedReader( |
| 89 | + new FileReader(properties.getProperty("flatFileLocation") |
| 90 | + + weatherDate.getSmallString() + "." |
| 91 | + + location.getLocationName() + ".xml")); |
| 92 | + |
| 93 | + while ((sCurrentLine = br.readLine()) != null) { |
| 94 | + sb.append(sCurrentLine); |
| 95 | + } |
| 96 | + } catch (IOException ignored) { |
| 97 | + sb = null; |
| 98 | + } finally { |
| 99 | + try { |
| 100 | + if (br != null) { |
| 101 | + br.close(); |
| 102 | + } |
| 103 | + } catch (IOException ignored) {} |
| 104 | + } |
| 105 | + |
| 106 | + if (sb != null) { |
| 107 | + return sb.toString(); |
| 108 | + } |
| 109 | + return null; |
| 110 | + } |
| 111 | + |
| 112 | + private static String queryDB (String responseType, WeatherDate weatherDate, |
| 113 | + Location weatherLocation) |
| 114 | + { |
68 | 115 | List<Weather> reports = new ArrayList<Weather>();
|
69 | 116 | List<Forecast> forecasts = new ArrayList<Forecast>();
|
70 | 117 | List<Energy> energys = new ArrayList<Energy>();
|
@@ -105,7 +152,7 @@ else if (responseType.equalsIgnoreCase("energy")) {
|
105 | 152 | }
|
106 | 153 |
|
107 | 154 | return createXML(reports, forecasts, energys);
|
108 |
| - } |
| 155 | + } |
109 | 156 |
|
110 | 157 | private static String createXML (List<Weather> reports,
|
111 | 158 | List<Forecast> forecasts,
|
|
0 commit comments