'''
Created on Jun 28, 2011

@author: Pravin
'''
from google.appengine.ext import webapp
from orangescape.model.server.commands.CommandFactory import CommandFactory
from RenderTemplate import renderTemplate

import logging

log = logging.getLogger(__name__)

class BaseMigrationCommand(webapp.RequestHandler, CommandFactory):

    ''' Setting the deployed application path '''
    def __init__(self):
        pass

    def application(self, applicationid, metadatatype):
        templateDict = dict()
        handler = self.getVersionHandler()
        mismatchModelDict = handler.getApplicationDiffList()
        templateDict['mismatchModelList'] = mismatchModelDict
        templateDict['applicationid'] = applicationid
        self.response.out.write(self.renderAppMismatchPage(templateDict))

    def setattachments(self, applicationid, metadatatype):
        handler = self.getVersionHandler()
        handler.setModelAttachments()

#    def generatescripts(self, applicationid, metadatatype):
#        self.response.out.write (self.renderAlterTableScriptsPage(self.generateAlterTableScripts()))

    def sheetmetadata(self, applicationid, metadatatype, url):
        urlList = self.getParams()
        handler = self.getVersionHandler()
        listOfModelLevelMismatches = handler.getSheetDiff(urlList[-1])
#        listOfModelLevelMismatches = handler.getModelDiff(urlList[-1])
        self.response.out.write (self.renderModelMismatchPage(listOfModelLevelMismatches))

    def executescripts(self, applicationid, metadatatype):
        handler = self.getVersionHandler()
#        handler.generateAlterScripts()
        self.response.out.write(handler.setModelAttachments())

    ''' Renders the listOfAppLevelMismatches for the browser by merging Cheetah template & the HTML structure '''         
    def renderAppMismatchPage(self, listOfAppLevelMismatches):
        return renderTemplate("appmismatchpage.html",listOfAppLevelMismatches, self)

    ''' Renders the listOfModelLevelMismatches for the browser by merging Cheetah template & the HTML structure '''
    def renderModelMismatchPage(self, listOfModelLevelMismatches):
        return renderTemplate("modelmismatchpage.html",listOfModelLevelMismatches, self) 

    def renderAlterTableScriptsPage(self, listOfAlterTableScripts):
        return renderTemplate("alterscriptspage.html",listOfAlterTableScripts, self)

    ''' Retrieves the list of Application Sheet metadata which has a mismatch between the deployed and migrated version. '''

    def getParams(self):
        import urllib
        url=self.request.path
        url=urllib.unquote(url)
        path=url[1:len(url)].split('/')
        return path

#    def generateAlterTableScripts(self):
#        alterScriptTemplateDict = dict()
#        handler = self.getVersionHandler()
#        alterScriptTemplateDict['alterModelList'] = handler.generateAlterTableScripts()
#        return alterScriptTemplateDict

    def getVersionHandler(self):
        raise NotImplementedError