# Path: OGRgeoConverter/models.py
# class LogEntry(models.Model):
#     session_key = models.CharField(max_length=50)
#     job_id = models.CharField(max_length=50)
# 
#     client_job_token = models.CharField(max_length=50)
#     client_ip = models.CharField(max_length=50)
#     client_language = models.CharField(max_length=50)
#     client_user_agent = models.CharField(max_length=256)
# 
#     input_type = models.CharField(
#         max_length=50, choices=(
#             ('files', 'Files'), ('webservice', 'Webservice')))
#     start_time = models.DateTimeField()
#     end_time = models.DateTimeField()
#     export_format = models.CharField(max_length=100)
#     source_srs = models.CharField(max_length=100)
#     target_srs = models.CharField(max_length=100)
#     simplify_parameter = models.CharField(max_length=100)
#     download_file_size = models.IntegerField(
#         verbose_name='Download file size (KB)')
# 
#     file_downloaded = models.BooleanField()
#     has_download_file = models.BooleanField()
#     all_files_converted = models.BooleanField()
#     has_no_error = models.BooleanField()
# 
#     def __unicode__(self):
#         return str(self.start_time)
# 
#     def duration(self):
#         difference = self.end_time - self.start_time
#         return str(difference)
# 
#     class Meta:
#         verbose_name_plural = "Log entries"
#         db_table = 'ogrgeoconverter_log_entries'
# 
#     @staticmethod
#     def get_log_entry(session_key, job_id):
#         return LogEntry.objects.filter(session_key=session_key, job_id=job_id)
# 
# Path: OGRgeoConverter/models.py
# class OGRlogEntry(models.Model):
#     log_entry = models.ForeignKey(LogEntry)
# 
#     sub_path = models.CharField(max_length=1000)
#     input = models.CharField(max_length=5000)
#     command = models.CharField(max_length=1000)
#     error_message = models.CharField(max_length=10000)
#     has_output_file = models.BooleanField()
# 
#     class Meta:
#         verbose_name = "OGR Log entry"
#         verbose_name_plural = "OGR Log entries"
#         db_table = 'ogrgeoconverter_ogr_log_entries'
# 
#     @staticmethod
#     def get_ogr_log_entry(log_entry):
#         return OGRlogEntry.objects.filter(log_entry=log_entry)
# 
#     @staticmethod
#     def cleanup_by_date(date_limit):
#         OGRlogEntry.objects.filter(
#             log_entry__start_time__lt=date_limit).delete()
# 
# Path: OGRgeoConverter/geoconverter/datetimehandler.py
# def get_default_datetime():
# def get_now():


from OGRgeoConverter.models import LogEntry
from OGRgeoConverter.models import OGRlogEntry
from OGRgeoConverter.geoconverter import datetimehandler


class LogHandler:

    '''
    Class to read and write log entries.
    '''

    def __init__(self, job_identifier):
        if job_identifier is not None:
            self.__job_identifier = job_identifier
        else:
            raise Exception("job_identifier may not be None")

    def __get_log_entry_query_set(self):
        log_entries = LogEntry.get_log_entry(
            self.__job_identifier.session_key,
            self.__job_identifier.job_id)

        if len(log_entries) == 0:
            # Creat log entry if it doesen't exist yet
            log_entry = LogEntry()
            log_entry.session_key = self.__job_identifier.session_key
            log_entry.job_id = self.__job_identifier.job_id
            log_entry.client_job_token = self.__job_identifier.client_job_token
            log_entry.start_time = datetimehandler.get_default_datetime()
            log_entry.end_time = datetimehandler.get_default_datetime()
            log_entry.download_file_size = -1
            log_entry.save()

# Path: OGRgeoConverter/models.py
# class LogEntry(models.Model):
#     session_key = models.CharField(max_length=50)
#     job_id = models.CharField(max_length=50)
# 
#     client_job_token = models.CharField(max_length=50)
#     client_ip = models.CharField(max_length=50)
#     client_language = models.CharField(max_length=50)
#     client_user_agent = models.CharField(max_length=256)
# 
#     input_type = models.CharField(
#         max_length=50, choices=(
#             ('files', 'Files'), ('webservice', 'Webservice')))
#     start_time = models.DateTimeField()
#     end_time = models.DateTimeField()
#     export_format = models.CharField(max_length=100)
#     source_srs = models.CharField(max_length=100)
#     target_srs = models.CharField(max_length=100)
#     simplify_parameter = models.CharField(max_length=100)
#     download_file_size = models.IntegerField(
#         verbose_name='Download file size (KB)')
# 
#     file_downloaded = models.BooleanField()
#     has_download_file = models.BooleanField()
#     all_files_converted = models.BooleanField()
#     has_no_error = models.BooleanField()
# 
#     def __unicode__(self):
#         return str(self.start_time)
# 
#     def duration(self):
#         difference = self.end_time - self.start_time
#         return str(difference)
# 
#     class Meta:
#         verbose_name_plural = "Log entries"
#         db_table = 'ogrgeoconverter_log_entries'
# 
#     @staticmethod
#     def get_log_entry(session_key, job_id):
#         return LogEntry.objects.filter(session_key=session_key, job_id=job_id)
# 
# Path: OGRgeoConverter/models.py
# class OGRlogEntry(models.Model):
#     log_entry = models.ForeignKey(LogEntry)
# 
#     sub_path = models.CharField(max_length=1000)
#     input = models.CharField(max_length=5000)
#     command = models.CharField(max_length=1000)
#     error_message = models.CharField(max_length=10000)
#     has_output_file = models.BooleanField()
# 
#     class Meta:
#         verbose_name = "OGR Log entry"
#         verbose_name_plural = "OGR Log entries"
#         db_table = 'ogrgeoconverter_ogr_log_entries'
# 
#     @staticmethod
#     def get_ogr_log_entry(log_entry):
#         return OGRlogEntry.objects.filter(log_entry=log_entry)
# 
#     @staticmethod
#     def cleanup_by_date(date_limit):
#         OGRlogEntry.objects.filter(
#             log_entry__start_time__lt=date_limit).delete()
# 
# Path: OGRgeoConverter/geoconverter/datetimehandler.py
# def get_default_datetime():
# def get_now():


# Path: OGRgeoConverter/modelhandlers/loghandler.py
from OGRgeoConverter.models import LogEntry
from OGRgeoConverter.models import OGRlogEntry
from OGRgeoConverter.geoconverter import datetimehandler



class LogHandler:

    '''
    Class to read and write log entries.
    '''

    def __init__(self, job_identifier):
        if job_identifier is not None:
            self.__job_identifier = job_identifier
        else:
            raise Exception("job_identifier may not be None")

    def __get_log_entry_query_set(self):
        log_entries = LogEntry.get_log_entry(
            self.__job_identifier.session_key,
            self.__job_identifier.job_id)

        if len(log_entries) == 0:
            # Creat log entry if it doesen't exist yet
            log_entry = LogEntry()
            log_entry.session_key = self.__job_identifier.session_key
            log_entry.job_id = self.__job_identifier.job_id
            log_entry.client_job_token = self.__job_identifier.client_job_token
            log_entry.start_time = datetimehandler.get_default_datetime()
            log_entry.end_time = datetimehandler.get_default_datetime()
            log_entry.download_file_size = -1
            log_entry.save()

            log_entries = LogEntry.get_log_entry(