/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java
ViewVC logotype

Diff of /android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 801 by torben, Mon Jun 7 11:29:55 2010 UTC revision 806 by torben, Mon Jun 7 18:35:40 2010 UTC
# Line 10  import java.text.SimpleDateFormat; Line 10  import java.text.SimpleDateFormat;
10  import java.util.ArrayList;  import java.util.ArrayList;
11  import java.util.Date;  import java.util.Date;
12  import java.util.List;  import java.util.List;
13    import java.util.Map;
14  import java.util.logging.Level;  import java.util.logging.Level;
15  import java.util.logging.Logger;  import java.util.logging.Logger;
16  import java.util.zip.ZipEntry;  import java.util.zip.ZipEntry;
# Line 20  import javax.servlet.http.HttpServlet; Line 21  import javax.servlet.http.HttpServlet;
21  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;  import javax.servlet.http.HttpServletResponse;
23    
24    import dk.thoerup.traininfoservice.banedk.TimeoutMap;
25    
26  public class RequestPlotter extends HttpServlet {  public class RequestPlotter extends HttpServlet {
27          private static final long serialVersionUID = 1L;          private static final long serialVersionUID = 1L;
28    
# Line 27  public class RequestPlotter extends Http Line 30  public class RequestPlotter extends Http
30    
31          static final String KML = "application/vnd.google-earth.kml";          static final String KML = "application/vnd.google-earth.kml";
32          static final String KMZ = "application/vnd.google-earth.kmz";          static final String KMZ = "application/vnd.google-earth.kmz";
33            
34            Map<String,String> cache = new TimeoutMap<String,String>(2*60*1000);
35    
36          class RequestPosition {          class RequestPosition {
37                  public String ip;                  public String ip;
# Line 121  public class RequestPlotter extends Http Line 126  public class RequestPlotter extends Http
126          */                */      
127                                    
128    
129                    final String STYLE_GREEN = "green";
130                    final String STYLE_YELLOW = "yellow";
131                    final String STYLE_RED = "red";
132    
133                    String oldstyle = null;
   
   
134                  Date now = new Date();                  Date now = new Date();
135                  for(RequestPosition current : list) {                  for(RequestPosition current : list) {
136                                                    
# Line 132  public class RequestPlotter extends Http Line 138  public class RequestPlotter extends Http
138                          long timediff = now.getTime() - current.time.getTime();                          long timediff = now.getTime() - current.time.getTime();
139                                                    
140                          if ( timediff < (3*60*60*1000) ) {                          if ( timediff < (3*60*60*1000) ) {
141                                  style = "#red";                                  style = STYLE_RED;
142                          } else if ( timediff < (24*60*60*1000)) {                          } else if ( timediff < (24*60*60*1000)) {
143                                  style = "#yellow";                                  style = STYLE_YELLOW;
144                          } else {                          } else {
145                                  style = "#green";                                  style = STYLE_GREEN;
146                            }
147                            
148                            if (  !style.equals(oldstyle) ) {
149                                    if (oldstyle != null)                          
150                                            sb.append( "</Folder>\n");
151                                    sb.append( "<Folder>\n");
152                                    sb.append( " <name>" ).append(style).append("</name>\n");
153                                    sb.append( " <open>0</open>\n" );
154                                    
155                                    oldstyle = style;
156                          }                          }
157                                                    
158                          sb.append( " <Placemark>\n" );                          sb.append( " <Placemark>\n" );
159                          sb.append( "  <styleUrl>" + style + "</styleUrl>\n" );                          sb.append( "  <styleUrl>#" + style + "</styleUrl>\n" );
160                          sb.append( "  <description>IP=" + current.ip + "  Time=" + current.time + "</description>\n" );                          sb.append( "  <description>IP=" + current.ip + "  Time=" + current.time + "</description>\n" );
161                          sb.append( "  <Point><coordinates>" + current.lng + "," + current.lat + ",0</coordinates></Point>\n" );                          sb.append( "  <Point><coordinates>" + current.lng + "," + current.lat + ",0</coordinates></Point>\n" );
162                          sb.append( " </Placemark>\n" );                                          sb.append( " </Placemark>\n" );                
163                  }                  }
164                    sb.append( "</Folder>\n" );
165                  sb.append(  "</Document>\n" );                  sb.append( "</Document>\n" );
166                  sb.append( "</kml>\n" );                  sb.append( "</kml>\n" );
167    
168                  return sb.toString();                  return sb.toString();
# Line 154  public class RequestPlotter extends Http Line 170  public class RequestPlotter extends Http
170    
171          @Override          @Override
172          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
173                    final String KEY = "kmldata";
174                    
175                    String kmlData = cache.get(KEY);
176                    
177                    if (kmlData == null) {
178                            kmlData = formatXml( getRequestsFromFile() );
179                            cache.put(KEY, kmlData);
180                            kmlData += "<!-- from source -->";                      
181                    } else {
182                            kmlData += "<!-- cached -->";
183                    }
184    
185                  String data = formatXml( getRequestsFromFile() );                  
186    
187                  if (req.getParameter("zip") != null) {                  if (req.getParameter("zip") != null) {
188    
# Line 163  public class RequestPlotter extends Http Line 190  public class RequestPlotter extends Http
190    
191                          ZipOutputStream zip = new ZipOutputStream(baos);                          ZipOutputStream zip = new ZipOutputStream(baos);
192                          zip.putNextEntry( new ZipEntry("trains.kml") );                          zip.putNextEntry( new ZipEntry("trains.kml") );
193                          zip.write( data.getBytes() );                          zip.write( kmlData.getBytes() );
194                          zip.closeEntry();                          zip.closeEntry();
195                          zip.close();                          zip.close();
196    
# Line 175  public class RequestPlotter extends Http Line 202  public class RequestPlotter extends Http
202    
203                  } else {                  } else {
204                          resp.setContentType(KML);                          resp.setContentType(KML);
205                          resp.getWriter().print( data );                          resp.getWriter().print( kmlData );
206                  }                  }
207          }          }
208  }  }

Legend:
Removed from v.801  
changed lines
  Added in v.806

  ViewVC Help
Powered by ViewVC 1.1.20