# Path: farmsList/settings.py
# class ProdConfig(Config):
#     """Production configuration."""
#     ENV = 'prod'
#     DEBUG = False
#     SQLALCHEMY_DATABASE_URI = os_env.get('HEROKU_POSTGRESQL_IVORY_URL')
#     DEBUG_TB_ENABLED = False  # Disable Debug toolbar
#     HTTP_SERVER = "acres.herokuapp.com"
# 
# class DevConfig(Config):
#     """Development configuration."""
#     ENV = 'dev'
#     DEBUG = True
#     MAIL_DEBUG = True
#     DB_NAME = 'dev.db'
#     # Put the db file in project root
#     # DB_PATH = os.path.join(Config.PROJECT_ROOT, DB_NAME)
#     SQLALCHEMY_DATABASE_URI = 'postgresql://urbanlandlocatoradmin:@localhost/urban_land_locator' #'sqlite:///{0}'.format(DB_PATH)
#     DEBUG_TB_ENABLED = True
#     ASSETS_DEBUG = True  # Don't bundle/minify static assets
#     HTTP_SERVER = "localhost:5001"
# 
# Path: farmsList/modules/importers.py
# def findExistingMatchingParcels(parcels):
# 	for parcel in parcels:
# 		if "*|*" in parcel['id'] + parcel['jurisdiction']:
# 			raise ValueError('parcel id or jurisdiction contains delimeter *|*: {} {}'.format(parcel['id'], parcel['jurisdiction']))
# 	timestamp = int(time.time())
# 	conn.execute("CREATE TEMPORARY TABLE temp_parcels_{} (internal_id INTEGER, id VARCHAR(800))".format(timestamp))
# 	conn.execute("INSERT INTO temp_parcels_{} (SELECT id AS internal_id, apn || '*|*' || jurisdiction AS id FROM parcels)".format(timestamp))
# 	conn.execute("CREATE TEMPORARY TABLE temp_parcels_new_{} (id VARCHAR(800))".format(timestamp))
# 	sqlCommandsString = "INSERT INTO temp_parcels_new_{} (id) VALUES ".format(timestamp)
# 	for parcel in parcels:
# 		sqlCommandsString += "('{}'), ".format(parcel['id'] + "*|*" + parcel['jurisdiction'])
# 	sqlCommandsString = sqlCommandsString[:-2]
# 	conn.execute(sqlCommandsString)
# 	results = conn.execute("SELECT apn, jurisdiction, geometry, water, zoning, soil, address FROM parcels WHERE id IN (SELECT internal_id AS id FROM temp_parcels_{} WHERE id IN (SELECT id FROM temp_parcels_new_{}))".format(timestamp, timestamp)).fetchall()
# 	conn.execute('DROP TABLE temp_parcels_{}'.format(timestamp))
# 	conn.execute('DROP TABLE temp_parcels_new_{}'.format(timestamp))
# 	return results


import os
import json
import time
from sqlalchemy import create_engine
from farmsList.settings import ProdConfig, DevConfig
from farmsList.modules.importers import findExistingMatchingParcels
			parcel1, parcel2 = overlap
			parts1 = parcel1.split(DELIMETER)
			parts2 = parcel2.split(DELIMETER)
			parcelId1 = parts1[0]
			jurisdiction1 = parts1[1]
			parcelId2 = parts2[0]
			jurisdiction2 = parts2[1]
			issues += '\nID: {} Jurisdiction: {} overlaps with ID: {} Jurisdiction: {}'.format(parcelId1, jurisdiction1, parcelId2, jurisdiction2)

		existingGeometryConflicts = self.validateGeometryAgainstExisting(parcels)
		if len(existingGeometryConflicts) > 0:
			issues += '\n\r\n\rThese parcels overlap with existing data:\n'
		for overlap in existingGeometryConflicts:
			parts = overlap.split(DELIMETER)
			parcelId = parts[0]
			jurisdiction = parts[1]
			issues += '\nID: {}  Jurisdiction: {}'.format(parcelId, jurisdiction)

		if len(issues) < 1:
			issues = 'No issues!'
		else:
			issues += '\n\r\n\rPlease address this issues and run the import again.\nFor additional help, contact robertm@cityofwestsacramento.org or grantrobertsmith@gmail.com.'
		return issues

	def findUpdates(self, parcels):
		indexedParcels = {}
		for parcel in parcels:
			index = parcel['id'] + DELIMETER + parcel['jurisdiction']
			indexedParcels[index] = parcel
		updatedParcels = {}

# Path: farmsList/settings.py
# class ProdConfig(Config):
#     """Production configuration."""
#     ENV = 'prod'
#     DEBUG = False
#     SQLALCHEMY_DATABASE_URI = os_env.get('HEROKU_POSTGRESQL_IVORY_URL')
#     DEBUG_TB_ENABLED = False  # Disable Debug toolbar
#     HTTP_SERVER = "acres.herokuapp.com"
# 
# class DevConfig(Config):
#     """Development configuration."""
#     ENV = 'dev'
#     DEBUG = True
#     MAIL_DEBUG = True
#     DB_NAME = 'dev.db'
#     # Put the db file in project root
#     # DB_PATH = os.path.join(Config.PROJECT_ROOT, DB_NAME)
#     SQLALCHEMY_DATABASE_URI = 'postgresql://urbanlandlocatoradmin:@localhost/urban_land_locator' #'sqlite:///{0}'.format(DB_PATH)
#     DEBUG_TB_ENABLED = True
#     ASSETS_DEBUG = True  # Don't bundle/minify static assets
#     HTTP_SERVER = "localhost:5001"
# 
# Path: farmsList/modules/importers.py
# def findExistingMatchingParcels(parcels):
# 	for parcel in parcels:
# 		if "*|*" in parcel['id'] + parcel['jurisdiction']:
# 			raise ValueError('parcel id or jurisdiction contains delimeter *|*: {} {}'.format(parcel['id'], parcel['jurisdiction']))
# 	timestamp = int(time.time())
# 	conn.execute("CREATE TEMPORARY TABLE temp_parcels_{} (internal_id INTEGER, id VARCHAR(800))".format(timestamp))
# 	conn.execute("INSERT INTO temp_parcels_{} (SELECT id AS internal_id, apn || '*|*' || jurisdiction AS id FROM parcels)".format(timestamp))
# 	conn.execute("CREATE TEMPORARY TABLE temp_parcels_new_{} (id VARCHAR(800))".format(timestamp))
# 	sqlCommandsString = "INSERT INTO temp_parcels_new_{} (id) VALUES ".format(timestamp)
# 	for parcel in parcels:
# 		sqlCommandsString += "('{}'), ".format(parcel['id'] + "*|*" + parcel['jurisdiction'])
# 	sqlCommandsString = sqlCommandsString[:-2]
# 	conn.execute(sqlCommandsString)
# 	results = conn.execute("SELECT apn, jurisdiction, geometry, water, zoning, soil, address FROM parcels WHERE id IN (SELECT internal_id AS id FROM temp_parcels_{} WHERE id IN (SELECT id FROM temp_parcels_new_{}))".format(timestamp, timestamp)).fetchall()
# 	conn.execute('DROP TABLE temp_parcels_{}'.format(timestamp))
# 	conn.execute('DROP TABLE temp_parcels_new_{}'.format(timestamp))
# 	return results


# Path: farmsList/modules/importCheckers.py
import os
import json
import time
from sqlalchemy import create_engine
from farmsList.settings import ProdConfig, DevConfig
from farmsList.modules.importers import findExistingMatchingParcels

			parcel1, parcel2 = overlap
			parts1 = parcel1.split(DELIMETER)
			parts2 = parcel2.split(DELIMETER)
			parcelId1 = parts1[0]
			jurisdiction1 = parts1[1]
			parcelId2 = parts2[0]
			jurisdiction2 = parts2[1]
			issues += '\nID: {} Jurisdiction: {} overlaps with ID: {} Jurisdiction: {}'.format(parcelId1, jurisdiction1, parcelId2, jurisdiction2)

		existingGeometryConflicts = self.validateGeometryAgainstExisting(parcels)
		if len(existingGeometryConflicts) > 0:
			issues += '\n\r\n\rThese parcels overlap with existing data:\n'
		for overlap in existingGeometryConflicts:
			parts = overlap.split(DELIMETER)
			parcelId = parts[0]
			jurisdiction = parts[1]
			issues += '\nID: {}  Jurisdiction: {}'.format(parcelId, jurisdiction)

		if len(issues) < 1:
			issues = 'No issues!'
		else:
			issues += '\n\r\n\rPlease address this issues and run the import again.\nFor additional help, contact robertm@cityofwestsacramento.org or grantrobertsmith@gmail.com.'
		return issues

	def findUpdates(self, parcels):
		indexedParcels = {}
		for parcel in parcels:
			index = parcel['id'] + DELIMETER + parcel['jurisdiction']
			indexedParcels[index] = parcel
		updatedParcels = {}

		matchingParcels = findExistingMatchingParcels(parcels)