#!/usr/bin/python

# Copyright (C) 2012-2015  ASTRON (Netherlands Institute for Radio Astronomy)
# P.O. Box 2, 7990 AA Dwingeloo, The Netherlands
#
# This file is part of the LOFAR software suite.
# The LOFAR software suite is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# The LOFAR software suite is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.

# $Id: radb.py 33394 2016-01-25 15:53:55Z schaap $

'''
TODO: documentation
'''
import logging

from lofar.common.postgres import makePostgresNotificationQueries

logger = logging.getLogger(__name__)

if __name__ == '__main__':
    with open('add_notifications.sql', 'wt') as f:
        f.write('--this file was generated by create_add_notifications.sql.py\n')
        f.write('--it creates triggers and functions which fire postgres notify events upon the given table actions\n')
        f.write('--these notify events can be listened to implenting a subclass of the PostgresListener in the lofar.common.postgres python module\n')
        f.write('--for the radb such a subclass has been made, which listens specifically to the notifications defined below\n')
        f.write('--RADBPGListener in module lofar.sas.resourceassignment.database.radbpglistener\n')
        f.write('--this RADBPGListener then broadcasts the event on the lofar bus.\n')
        f.write('\n')

        f.write('\nBEGIN;\n\n')

        f.write('-- only issue >warnings log messages. (only during this transaction)\n')
        f.write('SET LOCAL client_min_messages=warning;\n\n')

        f.writelines(makePostgresNotificationQueries('resource_allocation', 'task', 'INSERT'))
        f.writelines(makePostgresNotificationQueries('resource_allocation', 'task', 'UPDATE'))
        f.writelines(makePostgresNotificationQueries('resource_allocation', 'task', 'DELETE'))
        f.writelines(makePostgresNotificationQueries('resource_allocation', 'task_predecessor', 'INSERT', column_name='task_id'))
        f.writelines(makePostgresNotificationQueries('resource_allocation', 'task_predecessor', 'UPDATE', column_name='task_id'))
        f.writelines(makePostgresNotificationQueries('resource_allocation', 'task_predecessor', 'DELETE', column_name='task_id'))
        f.writelines(makePostgresNotificationQueries('resource_allocation', 'task_predecessor', 'INSERT', column_name='predecessor_id'))
        f.writelines(makePostgresNotificationQueries('resource_allocation', 'task_predecessor', 'UPDATE', column_name='predecessor_id'))
        f.writelines(makePostgresNotificationQueries('resource_allocation', 'task_predecessor', 'DELETE', column_name='predecessor_id'))
        f.writelines(makePostgresNotificationQueries('resource_allocation', 'specification', 'UPDATE'))
        f.writelines(makePostgresNotificationQueries('resource_allocation', 'resource_claim', 'INSERT'))
        f.writelines(makePostgresNotificationQueries('resource_allocation', 'resource_claim', 'UPDATE'))
        f.writelines(makePostgresNotificationQueries('resource_allocation', 'resource_claim', 'DELETE'))
        f.writelines(makePostgresNotificationQueries('resource_monitoring', 'resource_availability', 'UPDATE', column_name='resource_id'))
        f.writelines(makePostgresNotificationQueries('resource_monitoring', 'resource_capacity', 'UPDATE', column_name='resource_id'))

        f.write('\nCOMMIT;\n')