#!/usr/bin/env jython

import sys
import stilts
from java.lang import *
from java.sql import *


from ipptopsps import IppToPsps
from scratchdb import ScratchDb

'''
SetupScratchDb class
'''
class SetupScratchDb(IppToPsps):

    '''
    Constructor
    '''
    def __init__(self, argv):
        super(SetupScratchDb, self).__init__(argv)

        if len(argv) < 3:
            self.printUsage()
            self.exitProgram("incorrect args")

        try:
            self.scratchDb = ScratchDb(self.logger, self.config, argv[2])
        except:
            self.exitProgram("Could not connect to a scratch Db")
            raise


    def run(self):

        self.logger.infoSeparator()

        # install stored procedure
        self.logger.infoPair("Installing",  "psfLikelihood() stored procedure")
        self.scratchDb.createPsfLikelihoodFunction()

        # install IN tables
        self.logger.infoPair("Installing", "initialization tables")
        tables = stilts.treads("../config/IN/tables.vot")
        for table in tables:
            self.logger.debug("Creating IN table: " + table.name)
            table.write(self.scratchDb.url + '#' + table.name)

    '''
    Overrides base-class version
    '''
    def printUsage(self):
        super(SetupScratchDb, self).printUsage("<scratchdb_name>")

'''
Program starts here
'''
try:
    setupScratchDb = SetupScratchDb(sys.argv)
    setupScratchDb.run()
    setupScratchDb.exitProgram("completed")
except: pass


