public static void list(String unitId) {

    Http.Header hd = new Http.Header();

    hd.name = "Access-Control-Allow-Origin";
    hd.values = new ArrayList<String>();
    hd.values.add("*");

    Http.Response.current().headers.put("Access-Control-Allow-Origin", hd);

    if (unitId == null) badRequest();

    Phone p = Phone.find("unitId = ?", unitId).first();

    if (p == null) badRequest();

    List<TripPattern> patterns = TripPattern.find("route.phone = ?", p).fetch();

    Gson gson =
        new GsonBuilder()
            .registerTypeAdapter(TripPattern.class, new TripPatternSerializer())
            .serializeSpecialFloatingPointValues()
            .serializeNulls()
            .create();

    renderJSON(gson.toJson(patterns));
  }
  public static void exportAll() throws InterruptedException {

    List<TripPattern> tripPatterns = TripPattern.findAll();
    /*for(TripPattern tp: tripPatterns) {
    	ProcessGisExport gisExport = new ProcessGisExport(tp.id);
    	gisExport.doJob();
    }*/

    ok();
  }
  public static void view(String unitId) {

    if (unitId == null) index(true);

    Phone p = Phone.find("unitId = ?", unitId).first();

    if (p == null) index(true);

    List<TripPattern> patterns = TripPattern.find("route.phone = ?", p).fetch();

    render(p, patterns);
  }
  public static void export(String unitId) throws InterruptedException {

    Phone p = Phone.find("unitId = ?", unitId).first();

    List<TripPattern> tripPatterns = TripPattern.find("route.phone = ?", p).fetch();

    ArrayList<Long> patternIds = new ArrayList<Long>();

    for (TripPattern pattern : tripPatterns) {
      patternIds.add(pattern.id);
    }

    ProcessGisExport gisExport = new ProcessGisExport(patternIds, unitId.toString());
    gisExport.doJob();
    ok();
  }
  public static void pattern(Long patternId) {

    Http.Header hd = new Http.Header();

    hd.name = "Access-Control-Allow-Origin";
    hd.values = new ArrayList<String>();
    hd.values.add("*");

    Http.Response.current().headers.put("Access-Control-Allow-Origin", hd);

    TripPattern pattern = TripPattern.findById(patternId);

    Gson gson =
        new GsonBuilder()
            .registerTypeAdapter(TripPattern.class, new TripPatternShapeSerializer())
            .serializeSpecialFloatingPointValues()
            .serializeNulls()
            .create();

    renderJSON(gson.toJson(pattern));
  }
  public static void upload(String imei, File data) {

    try {

      File pbFile =
          new File(
              Play.configuration.getProperty("application.uploadDataDirectory"),
              imei + "_" + new Date().getTime() + ".pb");
      Logger.info(pbFile.toString());
      data.renameTo(pbFile);

      byte[] dataFrame = new byte[(int) pbFile.length()];
      ;
      DataInputStream dataInputStream =
          new DataInputStream(new BufferedInputStream(new FileInputStream(pbFile)));

      dataInputStream.read(dataFrame);
      Upload upload = Upload.parseFrom(dataFrame);

      Phone phone = Phone.find("imei = ?", imei).first();
      if (phone == null) badRequest();

      for (Upload.Route r : upload.getRouteList()) {

        if (r.getPointList().size() <= 1) continue;

        Agency a = Agency.find("gtfsAgencyId = ?", "DEFAULT").first();
        Route route = new Route("", r.getRouteName(), RouteType.BUS, r.getRouteDescription(), a);
        route.phone = phone;
        route.routeNotes = r.getRouteNotes();
        route.vehicleCapacity = r.getVehicleCapacity();
        route.vehicleType = r.getVehicleType();
        route.captureTime = new Date(r.getStartTime());
        route.save();

        List<String> points = new ArrayList<String>();

        Integer pointSequence = 1;
        for (Upload.Route.Point p : r.getPointList()) {
          points.add(new Double(p.getLon()).toString() + " " + new Double(p.getLat()).toString());
          RoutePoint.addRoutePoint(p, route.id, pointSequence);
          pointSequence++;
        }

        String linestring = "LINESTRING(" + StringUtils.join(points, ", ") + ")";

        BigInteger tripShapeId = TripShape.nativeInsert(TripShape.em(), "", linestring, 0.0);

        TripPattern tp = new TripPattern();
        tp.route = route;
        tp.headsign = r.getRouteName();
        tp.shape = TripShape.findById(tripShapeId.longValue());
        tp.save();

        Integer sequenceId = 0;

        for (Upload.Route.Stop s : r.getStopList()) {
          BigInteger stopId = Stop.nativeInsert(Stop.em(), s);

          TripPatternStop tps = new TripPatternStop();
          tps.stop = Stop.findById(stopId.longValue());
          tps.stopSequence = sequenceId;
          tps.defaultTravelTime = s.getArrivalTimeoffset();
          tps.defaultDwellTime = s.getDepartureTimeoffset() - s.getArrivalTimeoffset();
          tps.pattern = tp;
          tps.board = s.getBoard();
          tps.alight = s.getAlight();
          tps.save();

          sequenceId++;
        }

        // ProcessGisExport gisExport = new ProcessGisExport(tp.id);
        // gisExport.doJob();
      }

      Logger.info("Routes uploaded: " + upload.getRouteList().size());

      dataInputStream.close();

      ok();
    } catch (Exception e) {
      e.printStackTrace();
      badRequest();
    }
  }