#! /usr/bin/env python
#############################################################
#                                                           #
#   Author: Herve Menager                                   #
#   Organization:'Biological Software and Databases' Group, #
#                Institut Pasteur, Paris.                   #
#   Distributed under GPLv2 Licence. Please refer to the    #
#   COPYING.LIB document.                                   #
#                                                           #
#############################################################
import mb_cgi
from Mobyle.DataInputsIndex import DataInputsIndex
from  Mobyle.Classes.DataType import DataTypeFactory
from Mobyle.MobyleError import MobyleError

def process( self ):
    bc = self.cfg.getDatabanksConfig()
    pdi = DataInputsIndex("program")
    wdi = DataInputsIndex("workflow")
    vdi = DataInputsIndex("viewer")
    datatypes = {}
    df = DataTypeFactory()
    dataInputsList = pdi.getList().values() + vdi.getList().values()
    for parameter in dataInputsList:
        if not(datatypes.has_key(parameter.get('dataTypeClass'))):
            datatypes[parameter.get('dataTypeClass')]={}
            if parameter.get('dataTypeSuperClass'):
                dt = df.newDataType(parameter.get('dataTypeSuperClass'),parameter.get('dataTypeClass'))
            else:
                try:
                    dt = df.newDataType(parameter.get('dataTypeClass'))
                except MobyleError , err:
                    mb_cgi.c_log.critical( "%s is corrupted : parameter %s : %s" %( parameter[ 'programPID' ] ,
                                                                                    parameter[ 'name' ] , 
                                                                                    err ) )
                    continue
            datatypes[parameter.get('dataTypeClass')]['ancestorTypes']=dt.ancestors
    self.jsonMap = {'portal_name': self.cfg.portal_name(),
                    'conversions':self.cfg.data_conversions(),
                    'banks': bc,
                    'program_workflow_inputs': dict(pdi.getList().items() + wdi.getList().items()),
                    'viewer_inputs': vdi.getList(),
                    'datatypes':datatypes,
                    'htbase':'/'+self.cfg.portal_url(True)+"/"}

if __name__ == "__main__":
    mb_cgi.JSONCGI(processFunction=process,mime_type="application/javascript",wrapper="var portalProperties = %s;")