# -*- coding: utf-8 -*-

from storm.locals import *
import uuid
import sys
import datetime
from time import gmtime, mktime
sys.path.append("../storm/")
from StormClass import *


db_new = create_database("postgres://michel:8888sm@10.0.3.7:5432/dbpgalloa")
store_new = Store(db_new)

db_old = create_database("postgres://michel:8888sm@10.0.3.7:5432/alloadb")
store_old = Store(db_old)

def remplir_entete(classe, table):
    classe.z_nomtable = unicode(table)
    classe.z_semaphore = u"FREE"
    classe.z_datecreation = mktime(gmtime())
    classe.z_datemodification = classe.z_datecreation
    classe.z_dernier_temps_modif = u"0"
    classe.z_groupecreateur = u"root:root"
    classe.z_encourstraitement = False
    classe.z_datearevoirle = datetime.date(1970, 1, 1)
    classe.z_nombreimport = 0
    classe.z_modification = False
    classe.z_localesvn = int(datetime.datetime.now().strftime("%y%m%d%H%M%S"))
    classe.z_raisonrefus = u""
    classe.z_historique = u"root:" + datetime.datetime.now().strftime("%y%m%d%H%M%S") + ";"

#################################################################################################

def stripANous(champs):
    if champs == None:
        return u""
    else:
        return unicode(champs.strip())

def export_continents():
    global store_old, store_new
    res = store_old.execute("select * from cartographie.continents")
    for cont in res:
        new = Continent()
        new.uuidcontinents = unicode(uuid.uuid4())
        new.frlibcontinent = cont[2]
        new.gblibcontinent = cont[3]
        new.refcontinents = cont[1]
        new.refalaune = cont[16]
        new.refbaniere = cont[17]
        new.frdesccontinents = cont[18]
        new.gbdesccontinents = cont[19]
        new.ispublique = True
        new.is_thematique = False
        remplir_entete(new, "Continent")
        store_new.add(new)


def export_pays():
    global store_old, store_new
    res = store_old.execute("select * from cartographie.pays")
    uuiddevise = store_new.find(Ogdevise).one().uuidogdevise
    for pays in res:
        new = Pays()
        new.uuidpays = unicode(uuid.uuid4())
        new.refpays = pays[1] + u"-"
        new.frlibpays = stripANous(pays[2])
        new.gblibpays = stripANous(pays[3])
        new.frdescpays = stripANous(pays[4])
        new.gbdescpays = stripANous(pays[5])
        new.codedevise = stripANous(pays[6])
        new.lattitude_depart = stripANous(pays[7])
        new.longitude_depart = stripANous(pays[8])
        new.lattitude_arrivee = stripANous(pays[9])
        new.longitude_arrivee = stripANous(pays[10])
        if pays[24].strip() == "5":
            new.refcontinents = u"12"
        else : new.refcontinents = stripANous(pays[24])
        new.uuidogdevise = uuiddevise
        new.is_cee = False
        new.is_asean = False
        new.is_dangerous = False
        new.is_commonewealth = False
        new.ispublique = True
        new.is_thematique = False
        new.codeiso = stripANous(pays[1])
        remplir_entete(new, "Pays")
        store_new.add(new)

def export_region():
    global store_old, store_new
    res = store_old.execute("select * from cartographie.region")
    for cont in res:
        new = Region2()
        new.refregion = stripANous(cont[0])
        new.uuidregion = unicode(uuid.uuid4())
        new.frlibregion = stripANous(cont[2])
        new.gblibregion = stripANous(cont[3])
        new.frdescregion = u""
        new.gbdescregion = u""
        new.frarguments = u""
        new.gbarguments = u""
        new.frclimat = u""
        new.gbclimat = u""
        new.frsante = u""
        new.gbsante = u""
        new.fraccroche = u""
        new.gbaccroche = u""
        new.fra_voir_avivre = u""
        new.gba_voir_avivre = u""
        new.frcuisine = u""
        new.gbcuisine = u""
        new.frshopping = u""
        new.gbshopping = u""
        new.janvier = False
        new.fevrier = False
        new.mars = False
        new.avril = False
        new.mai = False
        new.juin = False
        new.juillet = False
        new.aout = False
        new.septembre = False
        new.octobre = False
        new.novembre = False
        new.decembre = False
        new.note_privee = u""
        new.refmageaccroche = u""
        new.lattitude_depart = u""
        new.longitude_depart = u""
        new.lattitude_arrivee = u""
        new.longitude_arrivee = u""
        new.principauxgatteways = u""
        new.ispublique = True
        tmp = store_new.find(Pays, Pays.refpays == stripANous(cont[39]) + u"-").one()
        if (tmp != None):
            new.refpays = stripANous(tmp.refpays)
        else :
            new.refpays = u"Z9"
        new.is_thematique = False
        remplir_entete(new, "Region")
        store_new.add(new)


def export_destination():
    global store_old, store_new
    res = store_old.execute("select * from cartographie.destination")
    compteur = 0
    for dest in res:
        new = Region2()
        new.uuidregion = unicode(uuid.uuid4())
        new.frlibregion = stripANous(dest[1])
        new.gblibregion = stripANous(dest[2])
        new.frdescregion = u""
        new.gbdescregion = u""
        new.frarguments = u""
        new.gbarguments = u""
        new.frclimat = u""
        new.gbclimat = u""
        new.frsante = u""
        new.gbsante = u""
        new.fraccroche = u""
        new.gbaccroche = u""
        new.fra_voir_avivre = u""
        new.gba_voir_avivre = u""
        new.frcuisine = u""
        new.gbcuisine = u""
        new.frshopping = u""
        new.gbshopping = u""
        new.janvier = False
        new.fevrier = False
        new.mars = False
        new.avril = False
        new.mai = False
        new.juin = False
        new.juillet = False
        new.aout = False
        new.septembre = False
        new.octobre = False
        new.novembre = False
        new.decembre = False
        new.note_privee = u""
        new.refmageaccroche = u""
        new.lattitude_depart = u""
        new.longitude_depart = u""
        new.lattitude_arrivee = u""
        new.longitude_arrivee = u""
        new.principauxgatteways = u""
        new.ispublique = True
        tmp = store_new.find(Region, Region.refregion == stripANous(dest[0])).one()
        if tmp != None:
            new.refregion = u"zzz%s" % compteur
        else:
            new.refregion = stripANous(dest[0])
        compteur += 1
        tmp = store_new.find(Pays, Pays.refpays == stripANous(dest[0])[0:3]).one()
        if (tmp != None):
            new.refpays = stripANous(tmp.refpays)
        else :
            new.refpays = u"Z9"
        new.is_thematique = True
        remplir_entete(new, "Region")
        store_new.add(new)

def export_ville():
    global store_old, store_new
    res = store_old.execute("select * from cartographie.ville")
    cpt = 0
    for ville in res:
        cpt += 1
        if cpt % 1000:
            print cpt
        new = Ville()
        new.refville = stripANous(ville[0])
        new.uuidville = unicode(uuid.uuid4())
        new.frlibville = stripANous(ville[1])
        new.gblibville = stripANous(ville[2])
        new.frdescville = u""
        new.gbdescville = u""
        new.code_postal = u""
        new.codeetat = stripANous(ville[23])
        new.gateway = ville[24]
        new.lattitude_depart = stripANous(ville[6])
        new.longitude_depart = stripANous(ville[7])
        new.lattitude_arrivee = u""
        new.longitude_arrivee = u""
        tmp = store_new.find(Region, Region.refregion == stripANous(ville[10])).one()
        if (tmp != None):
            new.refregion = stripANous(tmp.refregion)
        else: new.refregion = u"bidon"
        new.ispublique = True
        new.codeiso = u""
        remplir_entete(new, "Ville")
        store_new.add(new)

def export_gateway():
    global store_old, store_new
    res = store_old.execute("select * from cartographie.gateway")
    cpt = 0
    cptaffiche = 100
    for gat in res:
        new = Gateway()
        cpt += 1
        if (cpt == cptaffiche) or (cpt == 27846):
            print "%s/27846" % cpt
            cptaffiche += 100
        new.refgateway = stripANous(gat[1])
        new.uuidgateway = unicode(uuid.uuid4())
        new.codegalileo = stripANous(gat[2])
        new.frlibgateway = stripANous(gat[3])
        new.gblibgateway = stripANous(gat[4])
        new.isfer = gat[5]
        new.isroutiere = gat[6]
        new.isaeroportinter = gat[7]
        new.isaerodrome = gat[8]
        new.isferry = gat[9]
        new.isportcroisiere = gat[10]
        new.isportplaisance = gat[11]
        new.lattitude_depart = stripANous(gat[12])
        new.longitude_depart = stripANous(gat[13])
        new.lattitude_arrivee = stripANous(gat[14])
        new.longitude_arrivee = stripANous(gat[15])
        new.ispublique = True
        remplir_entete(new, "Gateway")
        store_new.add(new)




def export_z_villes_gateways():
    print "créations des listes"
    tmp = store_new.find(Ville, True)
    listeVille = []
    listeGateway = []
    for cont in tmp:
        listeVille.append(cont.refville)
    tmp = store_new.find(Gateway, True)
    for cont in tmp:
        listeGateway.append(cont.refgateway)
    
    print "bouclewhile"
    cpt = 0
    while len(listeGateway):
        tmpgateway = listeGateway[0]
        del listeGateway[0]
        res = store_old.execute("select * from cartographie.z_villes_gateways where refgateway='%s'" % tmpgateway)
        for cont in res:
            if cont[0] in listeVille:
                cpt += 1
                a = Zlien_villes_gateways()
                a.refville = cont[0]
                a.refgateway = tmpgateway
                store_new.add(a)
        print "commit", cpt
        store_new.commit()

    
    
    """global store_old,store_new
    print "récupération de la table en cours..."
    res=store_old.execute("select * from cartographie.z_villes_gateways ORDER BY refville asc limit 10000")
    print "récupération terminé"
    cpt=1
    villeAvant=None
    for lien in res:
        new=Zlien_villes_gateways()
        cpt+=1
        if cpt%1000==0:
            print "%s/12311488"%cpt
        tmp1=lien[0]
        tmp2=lien[1]
        if villeAvant==tmp2 :
            if tmp1 in listeGateway:
                new.refgateway=tmp1
                new.refville=tmp2
            else:
                continue
        else:
           if tmp2 in listeVille:
                villeAvant=tmp2
                new.refville=tmp2
                new.refgateway=tmp1
           else:
                villeAvant=None
                continue
        
        store_new.add(new)
        if cpt%150000==0:
            print "commit en cour..."
            store_new.commit()
            print "commit terminé"
            """
    
def creationObjetBidon():           
    # ## création d'un contineent bidon
    new = Continent()
    new.uuidcontinents = unicode(uuid.uuid4())
    new.frlibcontinent = u"Continent Bidon"
    new.gblibcontinent = u"Continent bidon"
    new.refcontinents = u"12"
    new.refalaune = u""
    new.refbaniere = u""
    new.frdesccontinents = u"un très joli continent"
    new.gbdesccontinents = u"a very beautiful continent"
    new.ispublique = False
    new.is_thematique = False
    remplir_entete(new, "Continent")
    store_new.add(new)
    store_new.commit()
    # ## création d'un pays bidon
    new = Pays()
    new.uuidpays = unicode(uuid.uuid4())
    new.frlibpays = u"PAYS BIDON"
    new.gblibpays = u"PAYS BIDON"
    new.refpays = u"Z9"
    new.refcontinents = u"12"
    new.frdescpays = u"un très joli pays"
    new.gbdescpays = u"a very beautiful pays"
    new.ispublique = False
    new.is_thematique = False
    remplir_entete(new, "Pays")
    new.uuidogdevise = store_new.find(Ogdevise).one().uuidogdevise
    store_new.add(new)
    store_new.commit()
    ### création d'une region bidon ###
    
    new = Region()
    new.refregion = u"bidon"
    new.uuidregion = unicode(uuid.uuid4())
    new.frlibregion = u"REGION BIDON"
    new.gblibregion = u"REGION BIDON"
    new.refpays = u"Z9"
    new.ispublique = False
    remplir_entete(new, "Region")
    store_new.add(new)
    store_new.commit()
    
    ### création d'une ville bidon ###
    
    new = Ville()
    new.refville = u"ZBidon"
    new.refregion = u"bidon"
    new.uuidville = unicode(uuid.uuid4())
    new.frlibville = u"VILLE BIDON"
    new.gblibville = u"VILLE BIDON"
    new.ispublique = False
    remplir_entete(new, "Region")
    store_new.add(new)
    store_new.commit()
    ### création d'un aéroport bidon ###
    
    new = Gateway()
    new.refgateway = u"ZZNAZ"
    new.uuidgateway = unicode(uuid.uuid4())
    new.frlibgateway = u"GATEWAY BIDON"
    new.gblibgateway = u"GATEWAY BIDON"
    new.ispublique = False
    remplir_entete(new, "Gateway")
    store_new.add(new)
    store_new.commit()


def importerAll ():

#===============================================================================
# #===============================================================================
# #    print"def export_continents():"
# #    export_continents()
# #    store_new.commit()
# #    print"def export_pays()"
# #    export_pays()
# #    store_new.commit()
# #    print "export_region()"
# #    export_region()
# #    store_new.commit()
# #    print "export_destination()"
# #    export_destination()
# #    store_new.commit()
# #===============================================================================
#    print "export_ville()"
#    export_ville()
#    print "commit en cours"
#    store_new.commit()
#===============================================================================
#===============================================================================
#    print "export_gateway()"
#    export_gateway()
#    store_new.commit()
#===============================================================================
    print "export_z_villes_gateways():"
    export_z_villes_gateways()





# creationObjetBidon()
export_region()
store_new.commit()
export_destination()
# export_z_villes_gateways()
print "commit en cour..."
store_new.commit()
print "commit terminé"
print "--terminé--"
