/[projects]/android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/LoadStations.java
ViewVC logotype

Diff of /android/TrainInfoServiceGoogle/src/dk/thoerup/traininfoservice/LoadStations.java

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

revision 1104 by torben, Mon Sep 20 20:11:55 2010 UTC revision 1105 by torben, Wed Sep 22 21:09:39 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfoservice;  package dk.thoerup.traininfoservice;
2    
3  import java.io.IOException;  import java.io.IOException;
 import java.io.PrintWriter;  
 import java.sql.Connection;  
 import java.sql.PreparedStatement;  
 import java.sql.ResultSet;  
 import java.sql.SQLException;  
4    
5  import javax.servlet.ServletException;  import javax.servlet.ServletException;
6  import javax.servlet.http.HttpServlet;  import javax.servlet.http.HttpServlet;
7  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpServletRequest;
8  import javax.servlet.http.HttpServletResponse;  import javax.servlet.http.HttpServletResponse;
9    
10  @Deprecated  import org.simpleframework.xml.Serializer;
11    import org.simpleframework.xml.core.Persister;
12    
13    import dk.thoerup.android.traininfo.common.StationBean;
14    
15    
16  //@WebServlet(urlPatterns={"/LoadStations"})  //@WebServlet(urlPatterns={"/LoadStations"})
17    
18  public class LoadStations extends HttpServlet {  public class LoadStations extends HttpServlet {
# Line 22  public class LoadStations extends HttpSe Line 22  public class LoadStations extends HttpSe
22                  super();                  super();
23          }          }
24    
         protected void insertOrUpdate(Connection conn, String type, String code, String name)  throws SQLException{  
                 String sql = "SELECT id FROM trainstations WHERE stationcode=?";  
                 PreparedStatement stmt = conn.prepareStatement(sql);  
                 stmt.setString(1, code);  
                   
                 ResultSet rs = stmt.executeQuery();  
                   
                 boolean stog = type.equalsIgnoreCase("S2");  
                   
                 if (rs.next()) {  
                         int id = rs.getInt(1);  
                         stmt = conn.prepareStatement("UPDATE trainstations SET name=?, stog=?, updated=now() WHERE id=?");  
                         stmt.setString(1, name);  
                         stmt.setBoolean(2, stog);  
                         stmt.setInt(3, id);  
                         stmt.execute();  
                 } else {  
                         stmt = conn.prepareStatement("INSERT INTO trainstations (name,stationcode,stog,updated) VALUES (?,?,?,now())");  
                         stmt.setString(1, name);  
                         stmt.setString(2, code);  
                         stmt.setBoolean(3, stog);  
                         stmt.execute();  
                 }  
                   
         }  
25    
26          @Override          @Override
27          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
28                  String url = "http://rafiki.t-hoerup.dk/tog/stations.php";                  String url = "http://app.t-hoerup.dk/TrainInfoService/LocateStations?dump";
29                    
30                  String stationStr = DownloadUtil.getContentString(url, 5000, "ISO-8859-1");                  String xml = DownloadUtil.getContentString(url, 5000, "ISO-8859-1");
31                    
32                  String stations[] = stationStr.split("\n");                  Serializer serializer = new Persister();
33                  Connection conn = null;                  StationBean stations;
34                  try {                  try {
35                          conn = DBConnection.getConnection();                                      stations = serializer.read(StationBean.class, xml);
                         for (String stationLine : stations) {  
                                 String fields[] = stationLine.split(";");  
                                   
                                   
                                 insertOrUpdate(conn, fields[0], fields[1], fields[2]);  
                         }  
36                  } catch (Exception e) {                  } catch (Exception e) {
37                          throw new ServletException(e);                          throw new ServletException(e);
38                  } finally {                  }
39                          try {                  
40                                  if (conn != null && !conn.isClosed())                  StationDAO stationdao = new StationDAO();
41                                          conn.close();                  int count = stationdao.saveStations(stations);
42                          } catch (SQLException e) {}                  
43                  }                                        resp.getWriter().print("Saved " + count + " entries");
44                  PrintWriter out = resp.getWriter();  
                 out.print("ok");  
45          }          }
46    
47  }  }

Legend:
Removed from v.1104  
changed lines
  Added in v.1105

  ViewVC Help
Powered by ViewVC 1.1.20