Advertisement
Guest User

Untitled

a guest
Sep 8th, 2012
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. package cz.fhejl.pubtran;
  2.  
  3. import java.io.Serializable;
  4. import java.text.SimpleDateFormat;
  5. import java.util.ArrayList;
  6. import java.util.Date;
  7. import java.util.Locale;
  8.  
  9. import cz.fhejl.pubtran.Departures.Platform;
  10. import cz.fhejl.pubtran.JourneyPart.TransportType;
  11. import cz.fhejl.pubtran.api.TtwsSoapParser.Elem;
  12. import cz.fhejl.pubtran.other.TimeUtils;
  13.  
  14. @SuppressWarnings("serial")
  15. public class Departures extends ArrayList<Platform> {
  16.     public Departures(Elem root) {
  17.         for (Elem elem : root.getElems("aoInfo")) {
  18.             int platformId = elem.getElem("oStation1").getInt("iStation");
  19.             String platformName = elem.getElem("oStation1").getString("sName");
  20.             Platform platform = new Platform(platformName, platformId);
  21.             for (int i = 0; i <= size(); i++) {
  22.                 if (i == size() || get(i).getId() > platformId) {
  23.                     add(i, platform);
  24.                     break;
  25.                 } else if (get(i).getId() == platformId) {
  26.                     platform = get(i);
  27.                     break;
  28.                 }
  29.             }
  30.  
  31.             String time = elem.getString("dtDateTime1");
  32.             String symbol = elem.getElem("oTrain").getString("sNum1");
  33.             String name = elem.getElem("oTrain").getString("sNum2");
  34.             String type = elem.getElem("oTrain").getString("sType");
  35.             TransportType transportType = TransportType.find(elem.getElem("oTrain").getInt("iID"));
  36.             String destination = elem.getElem("oStationTrainEnd").getString("sName");
  37.             Departure departure = new Departure(symbol, name, type, transportType, time, destination);
  38.             platform.getDepartures().add(departure);
  39.         }
  40.     }
  41.  
  42.     private static long parseDateTime(String s) {
  43.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US);
  44.         sdf.setTimeZone(TimeUtils.getCzechTimezone());
  45.         Date date = null;
  46.         try {
  47.             date = sdf.parse(s);
  48.         } catch (java.text.ParseException e) {
  49.             e.printStackTrace();
  50.         }
  51.  
  52.         return date.getTime();
  53.     }
  54.  
  55.     public static class Platform implements Serializable {
  56.         private int id;
  57.         private ArrayList<Departure> departures = new ArrayList<Departure>();
  58.         private String name;
  59.  
  60.         public Platform(String name, int id) {
  61.             this.name = name;
  62.             this.id = id;
  63.         }
  64.  
  65.         public ArrayList<Departure> getDepartures() {
  66.             return departures;
  67.         }
  68.  
  69.         public int getId() {
  70.             return id;
  71.         }
  72.  
  73.         public String getName() {
  74.             return name;
  75.         }
  76.     }
  77.  
  78.     public static class Departure implements Serializable {
  79.         private long time;
  80.         private String name;
  81.         private String destination;
  82.  
  83.         public Departure(String num1, String num2, String type, TransportType transportType, String time,
  84.                 String destination) {
  85.             this.time = parseDateTime(time);
  86.             this.destination = destination;
  87.  
  88.             name = num1;
  89.             if (transportType == TransportType.TRAIN || num1.length() > 4 || !num2.equals("")) {
  90.                 name = type + " " + num1;
  91.                 if (num2 != null) name += " " + num2;
  92.             }
  93.         }
  94.  
  95.         public long getTime() {
  96.             return time;
  97.         }
  98.  
  99.         public String getName() {
  100.             return name;
  101.         }
  102.  
  103.         public String getDestination() {
  104.             return destination;
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement