import logging

def releaseApplication(component, metadata, args, releaseIns, referencecell):
    service = AppReleaseService(component)
    return service.releaseApplication(metadata, args, releaseIns, referencecell)

def releaseModel(component, metadata, args, releaseIns, referencecell):
    service = AppReleaseService(component)
    return service.releaseModel(metadata, args, releaseIns, referencecell)

class AppReleaseService():

    session=None
    component=None

    def __init__(self, component):
        self.component = component
        self.session = self.component.getSession()

    def getMetadata(self, metadataName):
        return self.session.getApplication().getSheetMetadata(metadataName)

    def releaseApplication(self, releaseMetadata, args, releaseIns, referencecell):
        from orangescape.model.server.util.privateserializer import SheetSerializer
        from build import VersionNumber, BuildNumber, BuildDate
        logging.info('Serializing application data')
        appMetadata = self.getMetadata('Application')
        logging.info('Creating release')
        application = self.session.get(releaseIns.getValue('Application'), appMetadata)

        appData = SheetSerializer(self.session, application, {'Sheet008' : ['CellMetadata_99dcc7f8_f829_4da1_8d4f_a00c57684e45','Cell_001_002_006', 'Ced2a6420a28ae43539a38ced226abc6de', 'CellMetadata_c9e14075_5719_42b0_b62a_217477894f41', 'Cell_016_002_006']}, True).serializeApplication()
        import cPickle as pickle
        appPck = pickle.dumps(appData, 0)

        if releaseIns.getValue('Application_Name') and releaseIns.getValue('Attachment'):
            raise Exception('Already released or release in progress...')
        self.component.update(releaseIns, {'Application_Name':application.getSheetName(), 'VersionNumber':'%s.%s.v%s'%(VersionNumber, BuildNumber, BuildDate)})
        logging.info('Updating app data with release')
        self.component.updateAttachmentWithId(releaseIns, releaseIns.getSheetId()+'.pri', appPck)
        return releaseIns.getSheetId()

    def releaseModel(self, releaseMetadata, args, releaseIns, referencecell):
        modelId = args.get('modelid')

        appMetadata = self.getMetadata('Application')
        sheetMetadata = self.getMetadata('SheetMetadata')
        sheetInstanceMetadata = self.getMetadata('SheetInstance')
        instanceReleaseMetadata = self.getMetadata('InstanceRelease')

        instanceAppRefCell = sheetInstanceMetadata.getPublicCellMetadata('Application')
        instanceReleaseRefCell = instanceReleaseMetadata.getPublicCellMetadata('Release')

        application = self.session.get(releaseIns.getValue('Application'), appMetadata)
        model = self.session.get(modelId, sheetMetadata)
        logging.info('Tagging model %s(%s) for the release %s for the application %s'%(model.getSheetName(), model.getSheetId(), releaseIns.getSheetId(), releaseIns.getValue('Application')))

        modelReleaseIns = self.component.copyAttachmentWithId(model, releaseIns, referencecell)
        self.component.update(modelReleaseIns, {'ModifiedBy':None})
        self.component.copyAttachment(application, instanceAppRefCell, releaseIns, instanceReleaseRefCell, model.getSheetId()+'.sec')


