# 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
			if 'geometry' in parcel:
				sqlCommandsString += "('{}', ST_GeomFromGeoJson('{}')), ".format(parcel['id'] + DELIMETER + parcel['jurisdiction'], json.dumps(parcel['geometry']))
		sqlCommandsString = sqlCommandsString[:-2]
		conn.execute(sqlCommandsString)
		result = conn.execute("SELECT n.id FROM temp_single_poly_{} p JOIN temp_new_parcels_{} n ON ST_Intersects(n.geometry, p.geom)".format(timestamp, timestamp)).fetchall()
		conn.execute('DROP TABLE temp_single_poly_{}'.format(timestamp))
		conn.execute('DROP TABLE temp_new_parcels_{}'.format(timestamp))
		for row in result:
			parcelId = row[0]
			if parcelId not in knownConflicts:
				hasGeom = 'geometry' in indexedParcels[index]
				if hasGeom:
					result = conn.execute("SELECT count(*) FROM parcels WHERE geometry = {}".format(indexedParcels[index]['geometry'])).fetchone()
					isNewParcel = result[0] == 0
				if hasGeom and isNewParcel:
					knownConflicts.append(parcelId)
		conn.execute('DROP TABLE temp_raw_parcels_{}'.format(timestamp))
		conn.execute('DROP TABLE temp_rings_{}'.format(timestamp))
		conn.execute('DROP TABLE temp_boundaries_{}'.format(timestamp))
		conn.execute('DROP TABLE temp_parcels_polys_{}'.format(timestamp))
		conn.execute('DROP SEQUENCE temp_polyseq_{}'.format(timestamp))
		conn.execute('DROP TABLE temp_polys_{}'.format(timestamp))
		return knownConflicts

	# This is formatting stuff right now. Ideally, this data would be formatted client side and be made more useful.
	def validateParcels(self, parcels):
		for parcel in parcels:
			if 'id' not in parcel or 'jurisdiction' not in parcel:
				raise IndexError('all parcels must have both an id and a jurisdiction')
			if DELIMETER in parcel['id'] + parcel['jurisdiction']:

# 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

			if 'geometry' in parcel:
				sqlCommandsString += "('{}', ST_GeomFromGeoJson('{}')), ".format(parcel['id'] + DELIMETER + parcel['jurisdiction'], json.dumps(parcel['geometry']))
		sqlCommandsString = sqlCommandsString[:-2]
		conn.execute(sqlCommandsString)
		result = conn.execute("SELECT n.id FROM temp_single_poly_{} p JOIN temp_new_parcels_{} n ON ST_Intersects(n.geometry, p.geom)".format(timestamp, timestamp)).fetchall()
		conn.execute('DROP TABLE temp_single_poly_{}'.format(timestamp))
		conn.execute('DROP TABLE temp_new_parcels_{}'.format(timestamp))
		for row in result:
			parcelId = row[0]
			if parcelId not in knownConflicts:
				hasGeom = 'geometry' in indexedParcels[index]
				if hasGeom:
					result = conn.execute("SELECT count(*) FROM parcels WHERE geometry = {}".format(indexedParcels[index]['geometry'])).fetchone()
					isNewParcel = result[0] == 0
				if hasGeom and isNewParcel:
					knownConflicts.append(parcelId)
		conn.execute('DROP TABLE temp_raw_parcels_{}'.format(timestamp))
		conn.execute('DROP TABLE temp_rings_{}'.format(timestamp))
		conn.execute('DROP TABLE temp_boundaries_{}'.format(timestamp))
		conn.execute('DROP TABLE temp_parcels_polys_{}'.format(timestamp))
		conn.execute('DROP SEQUENCE temp_polyseq_{}'.format(timestamp))
		conn.execute('DROP TABLE temp_polys_{}'.format(timestamp))
		return knownConflicts

	# This is formatting stuff right now. Ideally, this data would be formatted client side and be made more useful.
	def validateParcels(self, parcels):
		for parcel in parcels:
			if 'id' not in parcel or 'jurisdiction' not in parcel:
				raise IndexError('all parcels must have both an id and a jurisdiction')
			if DELIMETER in parcel['id'] + parcel['jurisdiction']:

				raise ValueError('parcel id or jurisdiction contains delimeter *|*: {} {}'.format(parcel['id'], parcel['jurisdiction']))