from B01Common import CommonSql

class FillInProvince(CommonSql):
    def __init__(self,pgconn):
        CommonSql.__init__(self,pgconn)

    def executeQuery(self):

        # get  a collection of all CCAA by its foreing Key as ccaa_id
        ccaa_col = self.getRegionsByAdministrativeLevel(34,1)
        provIdLevel = self.getAdminRegionIdLevel(34,2)
        # iterate for each CCAA searching by its natcode in table provincias and then insert to administrative_regions 
        for row in ccaa_col:
            print(row[0])
            id_ca = row[1][:4]
            query = "SELECT id, ST_AsEWKT(geom) as wkt, natcode,nameunit FROM spain.provincias WHERE natcode LIKE '{}%';".format(id_ca)
            self._cursor.execute(query)
            provinces = self._cursor.fetchall()
            for prov  in provinces:
                print(prov[0])
                # parent_id, natcode, name, geom,region_level_id
                self.insertIntoAdministrativeRegion(row[0],prov[2],prov[3],prov[1],provIdLevel)