From 4057637c935185204d75021cb85e537e3dfc7125 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Thu, 16 Jun 2011 17:41:41 +0200 Subject: [PATCH] Finito import vie da civilia. --- configure.ac | 3 +- data/territorio/gui/territorio.gui | 26 +++++++ src/commons.h | 5 +- src/importaviecivilia.c | 110 ++++++++++++++++++++++++++--- src/main.c | 3 + 5 files changed, 135 insertions(+), 12 deletions(-) diff --git a/configure.ac b/configure.ac index 19b7670..8afdd77 100644 --- a/configure.ac +++ b/configure.ac @@ -30,7 +30,8 @@ PKG_CHECK_MODULES(TERRITORIO, [gio-2.0 >= 2.20 libgdaex >= 0.3.0 libgtkform >= 0.1.3 libaute >= 0.0.2 - libautoz >= 0.0.1]) + libautoz >= 0.0.1 + libsolipa >= 0.3.1]) AC_SUBST(TERRITORIO_CFLAGS) AC_SUBST(TERRITORIO_LIBS) diff --git a/data/territorio/gui/territorio.gui b/data/territorio/gui/territorio.gui index ade36d4..d4fa37a 100644 --- a/data/territorio/gui/territorio.gui +++ b/data/territorio/gui/territorio.gui @@ -363,6 +363,7 @@ True True lstore_stati + True 1 @@ -539,6 +540,7 @@ True 255 + True 1 @@ -554,6 +556,7 @@ True 255 + True 1 @@ -582,6 +585,7 @@ True 5 + True 1 @@ -623,6 +627,7 @@ True 5 + True 1 @@ -730,6 +735,7 @@ True True lstore_regioni + True 1 @@ -893,6 +899,7 @@ True 255 + True 1 @@ -921,6 +928,7 @@ True 5 + True 1 @@ -1013,6 +1021,7 @@ True True lstore_province + True 1 @@ -1212,6 +1221,7 @@ True 255 + True 1 @@ -1240,6 +1250,7 @@ True 5 + True 1 @@ -1268,6 +1279,7 @@ True 5 + True 1 @@ -1418,6 +1430,7 @@ True True lstore_comuni + True 1 @@ -1617,6 +1630,7 @@ True 255 + True 1 @@ -1645,6 +1659,7 @@ True 5 + True 1 @@ -1673,6 +1688,7 @@ True 5 + True 1 @@ -1852,6 +1868,7 @@ True lstore_tipi_aggregazione_territoriale False + True 0 @@ -2015,6 +2032,7 @@ True 150 + True 1 @@ -2108,6 +2126,7 @@ True lstore_aggregazioni_territoriali False + True 0 @@ -2271,6 +2290,7 @@ True 150 + True uu @@ -2397,6 +2417,7 @@ True lstore_vie False + True 1 @@ -2607,6 +2628,7 @@ True 255 + True 1 @@ -2635,6 +2657,7 @@ True 5 + True 1 @@ -2783,6 +2806,7 @@ True True + True 1 @@ -2943,6 +2967,7 @@ True True + True 1 @@ -3103,6 +3128,7 @@ True True + True 1 diff --git a/src/commons.h b/src/commons.h index 18b0031..91b71af 100644 --- a/src/commons.h +++ b/src/commons.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Andrea Zagli + * Copyright (C) 2010-2011 Andrea Zagli * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,6 +28,7 @@ #include #include #include +#include typedef struct { @@ -43,6 +44,8 @@ typedef struct gchar *utente; Autoz *autoz; AutozIRole *role_utente; + + Solipa *solipa; } TerritorioCommons; #endif /* __TERRITORIO_COMMONS_H__ */ diff --git a/src/importaviecivilia.c b/src/importaviecivilia.c index d5d5bd4..90cd7c1 100644 --- a/src/importaviecivilia.c +++ b/src/importaviecivilia.c @@ -18,6 +18,7 @@ */ #include +#include #include "importaviecivilia.h" @@ -69,10 +70,13 @@ territorio_importa_viecivilia_init (TerritorioImportaVieCivilia *territorio_impo TerritorioImportaVieCivilia *territorio_importa_viecivilia_new (TerritorioCommons *commons) { + GtkWindow *w_main; GtkWidget *dialog; GtkWidget *carea; GtkWidget *connection; + SolipaProgressWindow *w_progress; + GdaEx *gdaex; gchar *sql; @@ -80,11 +84,18 @@ TerritorioImportaVieCivilia GdaDataModel *dm; GdaDataModelIter *iter; + guint rows; + gdouble fract; + gdouble fract_tot; + GdaDataModel *dm_territorio; const GdaDsnInfo *dsn; guint id_vie; + gchar *nome; + gchar *m1_comu_cod; + gchar *m1_via_cap; TerritorioImportaVieCivilia *a = TERRITORIO_IMPORTA_VIECIVILIA (g_object_new (territorio_importa_viecivilia_get_type (), NULL)); @@ -94,7 +105,7 @@ TerritorioImportaVieCivilia priv->commons = commons; - GtkWindow *w_main = GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")); + w_main = GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")); dialog = gtk_dialog_new_with_buttons ("Immettere i parametri di connessione al database Civilia dal quale importare", w_main, @@ -115,9 +126,9 @@ TerritorioImportaVieCivilia { dsn = gdaui_login_get_connection_information (GDAUI_LOGIN (connection)); gdaex = gdaex_new_from_string (g_strdup_printf ("%s://%s;%s", dsn->provider, dsn->cnc_string, dsn->auth_string)); + gtk_widget_destroy (dialog); if (gdaex == NULL) { - gtk_widget_destroy (dialog); dialog = gtk_message_dialog_new (w_main, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_WARNING, @@ -134,27 +145,64 @@ TerritorioImportaVieCivilia if (dm != NULL) { + rows = gda_data_model_get_n_rows (dm); + if (rows > 0) + { + w_progress = solipa_progress_window_new (priv->commons->solipa); + g_object_set (G_OBJECT (w_progress), + "title", "Importazione vie da Civilia", + "label", "", + "with-label", FALSE, + "with-cancel-button", FALSE, + "with-percentage", TRUE, + NULL); + solipa_progress_window_run (w_progress, w_main); + + fract = 1.0 / (gdouble)rows; + fract_tot = 0.0 - fract; + } + iter = gda_data_model_create_iter (dm); while (gda_data_model_iter_move_next (iter)) { + fract_tot += fract; + solipa_progress_window_set_fraction (w_progress, fract_tot); + while (gtk_events_pending ()) gtk_main_iteration (); + + nome = gdaex_strescape (gdaex_data_model_iter_get_field_value_stringify_at (iter, "M1_VIA_DESC"), NULL); + if (g_strcmp0 (nome, "") == 0) + { + g_free (nome); + continue; + } + + m1_comu_cod = g_strdup_printf ("%06d", gdaex_data_model_iter_get_field_value_integer_at (iter, "M1_COMU_COD")); + m1_via_cap = gdaex_strescape (gdaex_data_model_iter_get_field_value_stringify_at (iter, "M1_VIA_CAP"), NULL); + /* controllo se esiste */ sql = g_strdup_printf ("SELECT v.id" " FROM vie AS v INNER JOIN comuni AS c ON v.id_comuni = c.id" " INNER JOIN province AS p ON c.id_province = p.id" " WHERE p.cod_istat || c.cod_istat = '%s'" " AND v.nome = '%s'", - g_strdup_printf ("%06d", gdaex_data_model_iter_get_field_value_integer_at (iter, "M1_COMU_COD")), - gdaex_strescape (gdaex_data_model_iter_get_field_value_stringify_at (iter, "M1_VIA_DESC"), NULL)); + m1_comu_cod, + nome); dm_territorio = gdaex_query (priv->commons->gdaex, sql); + g_free (sql); if (dm_territorio == NULL || gda_data_model_get_n_rows (dm_territorio) < 1) { /* trovo un nuovo id */ id_vie = 0; sql = g_strdup_printf ("SELECT COALESCE (MAX (id), 0) FROM vie"); dm_territorio = gdaex_query (priv->commons->gdaex, sql); - if (dm_territorio != NULL && gda_data_model_get_n_rows (dm_territorio) > 0) + g_free (sql); + if (dm_territorio != NULL) { - id_vie = gdaex_data_model_get_value_integer_at (dm_territorio, 0, 0); + if (gda_data_model_get_n_rows (dm_territorio) > 0) + { + id_vie = gdaex_data_model_get_value_integer_at (dm_territorio, 0, 0); + } + g_object_unref (dm_territorio); } id_vie++; @@ -163,12 +211,13 @@ TerritorioImportaVieCivilia " (SELECT c.id FROM comuni AS c INNER JOIN province AS p ON c.id_province = p.id WHERE p.cod_istat || c.cod_istat = '%s')," " '%s')", id_vie, - gdaex_strescape (gdaex_data_model_iter_get_field_value_stringify_at (iter, "M1_VIA_DESC"), NULL), - g_strdup_printf ("%06d", gdaex_data_model_iter_get_field_value_integer_at (iter, "M1_COMU_COD")), - gdaex_strescape (gdaex_data_model_iter_get_field_value_stringify_at (iter, "M1_VIA_CAP"), NULL)); + nome, + m1_comu_cod, + m1_via_cap); if (gdaex_execute (priv->commons->gdaex, sql) == -1) { - gtk_widget_destroy (dialog); + g_free (sql); + g_object_unref (w_progress); dialog = gtk_message_dialog_new (w_main, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_WARNING, @@ -179,12 +228,51 @@ TerritorioImportaVieCivilia gtk_widget_destroy (dialog); return NULL; } + g_free (sql); } else { + if (dm_territorio != NULL) + { + g_object_unref (dm_territorio); + } + /* aggiorno la via */ id_vie = gdaex_data_model_get_value_integer_at (dm_territorio, 0, 0); + sql = g_strdup_printf ("UPDATE vie" + " SET nome = '%s'," + " id_comuni = (SELECT c.id FROM comuni AS c INNER JOIN province AS p ON c.id_province = p.id WHERE p.cod_istat || c.cod_istat = '%s')," + " cap = '%s'" + " WHERE id = %d", + nome, + m1_comu_cod, + m1_via_cap, + id_vie); + if (gdaex_execute (priv->commons->gdaex, sql) == -1) + { + g_free (sql); + g_object_unref (w_progress); + dialog = gtk_message_dialog_new (w_main, + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "Errore durante il salvataggio della via: %s.", + gdaex_data_model_iter_get_field_value_stringify_at (iter, "M1_VIA_DESC")); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + return NULL; + } + g_free (sql); } + + g_free (nome); + g_free (m1_comu_cod); + g_free (m1_via_cap); + } + + if (rows > 0) + { + g_object_unref (w_progress); } } else @@ -199,6 +287,8 @@ TerritorioImportaVieCivilia gtk_widget_destroy (dialog); return NULL; } + + gdaex_free (gdaex); } else { diff --git a/src/main.c b/src/main.c index 0201359..47f6161 100644 --- a/src/main.c +++ b/src/main.c @@ -373,6 +373,9 @@ main (int argc, char *argv[]) g_error ("Errore nella connessione al database: %s", cnc_string); } + /* inizializzo solipa */ + commons->solipa = solipa_new (); + commons->gtkbuilder = gtk_builder_new (); error = NULL; -- 2.49.0