class AssetManager(object):
	
	def __init__(self, db_path):
		self.db_access = DbAccess(db_path)

	def get_all_assets(self):
		cmd = 'select * from BANK_ASSET_VIEW;'
		output = self.db_access.execute(cmd)
		return self.get_assets_from_output(output)

	def get_assets_by_region(self, region):
		results = []
		cmd = 'select * from BANK_ASSET_VIEW where upper(REGION)="%s";'%(region.upper())
		output = self.db_access.execute(cmd)
		results.extend(self.get_assets_from_output(output))
		return results

	def get_assets_from_output(self, output):
		assets = []
		for row in output:
			assets.append(Asset(row.get('DISPLAY_NAME'),
								row.get('LATITUDE'),
								row.get('LONGITUDE'),
								row.get('COUNTRY'),
								row.get('SITE_TYPE'),
								row.get('REGION'),
								row.get('ADDRESS'),
								EMAILS.get(row.get('REGION'))))
		return assets
class EventManager(object):

	def __init__(self, db_path):
		self.db_access = DbAccess(db_path)

	def get_all_events(self):
		cmd = 'select * from incidents_view;'
		output = self.db_access.execute(cmd)
		return self.get_events_from_output(output)

	def get_events_by_country(self, country):
		cmd = 'select * from incidents_view where upper(country)="%s";'%(country.upper())
		output = self.db_access.execute(cmd)
		return self.get_events_from_output(output)

	def get_events_by_date_region(self, date, region):
		events = []
		for country in REGIONS.get(region):
			cmd = 'select * from incidents_view where upper(country)="%s" and event_date >= date("%s") AND event_date <  date("%s", "+1 day");'%(country.upper(), self.format_date(date), self.format_date(date))
			output = self.db_access.execute(cmd)
			events.extend(self.get_events_from_output(output))
		return events

	def format_date(self, date):
		try:
			return datetime.strptime(date, '%m/%d/%Y').strftime('%Y-%m-%d')
		except:
			return date

	def get_events_from_output(self, output):
		events = []
		for row in output:
			events.append(Event(row.get('EVENT_NAME'),
								row.get('EVENT_DATE'),
								row.get('LATITUDE'),
								row.get('LONGITUDE'),
								row.get('COUNTRY'),
								row.get('ROUTE_NAME'),
								row.get('LOCALITY'),
								row.get('NEIGHBOURHOOD'),
								row.get('ADMIN_AREA_LEVEL1'),
								row.get('FORMATTED_ADDRESS')))
		return events
	def __init__(self, db_path):
		self.db_access = DbAccess(db_path)