# Path: data_model.py
# class StopLocation(GeoModel):
#     stopID       = db.StringProperty()
#     intersection = db.StringProperty()
#     direction    = db.StringProperty()
#     description  = db.StringProperty(multiline=True)
# 
# Path: data_model.py
# class StopLocationLoader(db.Model):
#     stopID       = db.StringProperty()
#     name         = db.StringProperty()
#     description  = db.StringProperty(multiline=True)
#     lat          = db.FloatProperty()
#     lon          = db.FloatProperty()
#     direction    = db.StringProperty()
# 
# Path: data_model.py
# class RouteListing(db.Model):
#     route        = db.StringProperty()
#     routeCode    = db.StringProperty()
#     direction    = db.StringProperty()
#     stopID       = db.StringProperty()
#     stopCode     = db.StringProperty()
#     scheduleURL  = db.StringProperty(indexed=False)
#     stopLocation = db.ReferenceProperty(StopLocation,collection_name="stops")
# 
# Path: data_model.py
# class RouteListingLoader(db.Model):
#     routeID       = db.StringProperty()
#     routeCode     = db.StringProperty()
#     direction     = db.StringProperty()
#     directionCode = db.StringProperty()
#     stopID        = db.StringProperty()
#     stopCode      = db.StringProperty()
# 
# Path: data_model.py
# class DestinationListing(db.Model):
#     id    = db.StringProperty()
#     label = db.StringProperty()


import logging
from google.appengine.api.taskqueue import Task
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.db import GeoPt
from google.appengine.ext.db import TransactionFailedError
from google.appengine.ext.db import Timeout
from google.appengine.runtime import DeadlineExceededError
from data_model import StopLocation
from data_model import StopLocationLoader
from data_model import RouteListing
from data_model import RouteListingLoader
from data_model import DestinationListing
## end RouteTransformationParent

class RouteTransformationChildTask(webapp.RequestHandler):
    def post(self):
        route_loader_key = self.request.get('rll_key')
        logging.debug('work on %s' % self.request.get('rll_key'))
        route_loader = RouteListingLoader.get(route_loader_key)
        if route_loader is None:
            logging.error('total fail. unable to find %s' % route_loader_key)
        else:
            logging.debug(route_loader.routeID)
            # find the corresponding stop details
            stop = db.GqlQuery("SELECT * FROM StopLocation WHERE stopID = :1", route_loader.stopID).get()
            if stop is None:
              logging.error("Missing stop %s which should be impossible",route_loader.stopID);

            try:
                url = CRAWL_URLBASE + '?r=' + route_loader.routeCode + '&d=' + route_loader.directionCode + '&s=' + route_loader.stopCode
                logging.debug(url)
                route = RouteListing()
                route.route = route_loader.routeID
                route.routeCode = route_loader.routeCode
                route.direction = route_loader.directionCode
                route.stopID = route_loader.stopID
                route.stopCode = route_loader.stopCode
                route.scheduleURL = url
                route.stopLocation = stop
                route.put()
                logging.info("added new route listing entry to the database!")


# Path: data_model.py
# class StopLocation(GeoModel):
#     stopID       = db.StringProperty()
#     intersection = db.StringProperty()
#     direction    = db.StringProperty()
#     description  = db.StringProperty(multiline=True)
# 
# Path: data_model.py
# class StopLocationLoader(db.Model):
#     stopID       = db.StringProperty()
#     name         = db.StringProperty()
#     description  = db.StringProperty(multiline=True)
#     lat          = db.FloatProperty()
#     lon          = db.FloatProperty()
#     direction    = db.StringProperty()
# 
# Path: data_model.py
# class RouteListing(db.Model):
#     route        = db.StringProperty()
#     routeCode    = db.StringProperty()
#     direction    = db.StringProperty()
#     stopID       = db.StringProperty()
#     stopCode     = db.StringProperty()
#     scheduleURL  = db.StringProperty(indexed=False)
#     stopLocation = db.ReferenceProperty(StopLocation,collection_name="stops")
# 
# Path: data_model.py
# class RouteListingLoader(db.Model):
#     routeID       = db.StringProperty()
#     routeCode     = db.StringProperty()
#     direction     = db.StringProperty()
#     directionCode = db.StringProperty()
#     stopID        = db.StringProperty()
#     stopCode      = db.StringProperty()
# 
# Path: data_model.py
# class DestinationListing(db.Model):
#     id    = db.StringProperty()
#     label = db.StringProperty()


# Path: crawl/load_gtfs.py
import logging
from google.appengine.api.taskqueue import Task
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.db import GeoPt
from google.appengine.ext.db import TransactionFailedError
from google.appengine.ext.db import Timeout
from google.appengine.runtime import DeadlineExceededError
from data_model import StopLocation
from data_model import StopLocationLoader
from data_model import RouteListing
from data_model import RouteListingLoader
from data_model import DestinationListing

## end RouteTransformationParent

class RouteTransformationChildTask(webapp.RequestHandler):
    def post(self):
        route_loader_key = self.request.get('rll_key')
        logging.debug('work on %s' % self.request.get('rll_key'))
        route_loader = RouteListingLoader.get(route_loader_key)
        if route_loader is None:
            logging.error('total fail. unable to find %s' % route_loader_key)
        else:
            logging.debug(route_loader.routeID)
            # find the corresponding stop details
            stop = db.GqlQuery("SELECT * FROM StopLocation WHERE stopID = :1", route_loader.stopID).get()
            if stop is None:
              logging.error("Missing stop %s which should be impossible",route_loader.stopID);

            try:
                url = CRAWL_URLBASE + '?r=' + route_loader.routeCode + '&d=' + route_loader.directionCode + '&s=' + route_loader.stopCode
                logging.debug(url)
                route = RouteListing()
                route.route = route_loader.routeID
                route.routeCode = route_loader.routeCode
                route.direction = route_loader.directionCode
                route.stopID = route_loader.stopID
                route.stopCode = route_loader.stopCode
                route.scheduleURL = url
                route.stopLocation = stop
                route.put()
                logging.info("added new route listing entry to the database!")


                DestinationListing.get_or_insert(route_loader.direction, id=route_loader.directionCode, label=route_loader.direction)