From c0eb350ceb81e2adba824b58144db1e29d980743 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Tue, 26 Oct 2010 13:40:07 +0200 Subject: [PATCH] =?utf8?q?Inizio=20importazione=20dei=20file=20dell'istat?= =?utf8?q?=20(ma=20non=20funziona;=20sto=20aspettando=20una=20rispota=20da?= =?utf8?q?gli=20sviluppatori=20di=20libgda).=20Cambiamento=20nell'ottenime?= =?utf8?q?nto=20di=20GUIDIR=20e=20FORMDIR=20sotto=20windows=20durante=20lo?= =?utf8?q?=20sviluppo=20perch=E9=20adesso=20essendo=20un=20eseguibile=20li?= =?utf8?q?btool=20il=20vero=20eseguibile=20si=20trova=20nella=20directory?= =?utf8?q?=20src/.libs.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- data/territorio/gui/territorio.gui | 32 +++ src/Makefile.am | 2 + src/importaregioniprovinceistat.c | 312 +++++++++++++++++++++++++++++ src/importaregioniprovinceistat.h | 62 ++++++ src/main.c | 13 +- 5 files changed, 420 insertions(+), 1 deletion(-) create mode 100644 src/importaregioniprovinceistat.c create mode 100644 src/importaregioniprovinceistat.h diff --git a/data/territorio/gui/territorio.gui b/data/territorio/gui/territorio.gui index 78add34..72e1a49 100644 --- a/data/territorio/gui/territorio.gui +++ b/data/territorio/gui/territorio.gui @@ -116,6 +116,38 @@ + + + True + Utilità + True + + + True + + + True + _Importa + True + + + True + + + True + _Regioni e province da Istat + True + + + + + + + + + + + True diff --git a/src/Makefile.am b/src/Makefile.am index 0fa55d7..150878b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -20,6 +20,8 @@ territorio_SOURCES = \ comune.h \ comuni.c \ comuni.h \ + importaregioniprovinceistat.c \ + importaregioniprovinceistat.h \ main.c \ province.c \ province.h \ diff --git a/src/importaregioniprovinceistat.c b/src/importaregioniprovinceistat.c new file mode 100644 index 0000000..28f77fe --- /dev/null +++ b/src/importaregioniprovinceistat.c @@ -0,0 +1,312 @@ +/* + * Copyright (C) 2010 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include "importaregioniprovinceistat.h" + +static void territorio_importa_regioniprovinceistat_class_init (TerritorioImportaRegioniProvinceIstatClass *klass); +static void territorio_importa_regioniprovinceistat_init (TerritorioImportaRegioniProvinceIstat *territorio_importa_regioniprovinceistat); + +static void territorio_importa_regioniprovinceistat_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void territorio_importa_regioniprovinceistat_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +#define TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT, TerritorioImportaRegioniProvinceIstatPrivate)) + +typedef struct _TerritorioImportaRegioniProvinceIstatPrivate TerritorioImportaRegioniProvinceIstatPrivate; +struct _TerritorioImportaRegioniProvinceIstatPrivate + { + TerritorioCommons *commons; + }; + +G_DEFINE_TYPE (TerritorioImportaRegioniProvinceIstat, territorio_importa_regioniprovinceistat, G_TYPE_OBJECT) + +static void +territorio_importa_regioniprovinceistat_class_init (TerritorioImportaRegioniProvinceIstatClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (object_class, sizeof (TerritorioImportaRegioniProvinceIstatPrivate)); + + object_class->set_property = territorio_importa_regioniprovinceistat_set_property; + object_class->get_property = territorio_importa_regioniprovinceistat_get_property; +} + +static void +territorio_importa_regioniprovinceistat_init (TerritorioImportaRegioniProvinceIstat *territorio_importa_regioniprovinceistat) +{ + TerritorioImportaRegioniProvinceIstatPrivate *priv = TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT_GET_PRIVATE (territorio_importa_regioniprovinceistat); +} + +/** + * territorio_importa_regioniprovinceistat_new: + * @commons: + * + * Returns: the newly created #TerritorioImportaRegioniProvinceIstat object. + */ +TerritorioImportaRegioniProvinceIstat +*territorio_importa_regioniprovinceistat_new (TerritorioCommons *commons) +{ + GtkWidget *fchooser; + + TerritorioImportaRegioniProvinceIstat *a = TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT (g_object_new (territorio_importa_regioniprovinceistat_get_type (), NULL)); + + TerritorioImportaRegioniProvinceIstatPrivate *priv = TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT_GET_PRIVATE (a); + + priv->commons = commons; + + GtkWindow *w_main = GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")); + + fchooser = gtk_file_chooser_dialog_new ("Scegli il file istat con le regioni e le province", + w_main, + GTK_FILE_CHOOSER_ACTION_OPEN, + GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, + GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, + NULL); + if (gtk_dialog_run (GTK_DIALOG (fchooser)) == GTK_RESPONSE_ACCEPT) + { + char *filename; + GdaDataModel *dm; + GError *error; + GSList *errors; + + GtkWidget *dialog; + + gint row; + gint rows; + + gchar *sql; + + GdaDataModel *dm_regioni; + guint id_regioni; + gchar *k_regioni_cod_istat; + + GdaDataModel *dm_province; + guint id_province; + + filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (fchooser)); + g_message (filename); + gtk_widget_destroy (fchooser); + + GdaSet *plist = gda_set_new_inline (14, + "SEPARATOR", G_TYPE_STRING, ";", + "ESCAPE_CHAR", G_TYPE_STRING, "", + "G_TYPE_0", G_TYPE_GTYPE, G_TYPE_INT, + "G_TYPE_1", G_TYPE_GTYPE, G_TYPE_STRING, + "G_TYPE_2", G_TYPE_GTYPE, G_TYPE_STRING, + "G_TYPE_3", G_TYPE_GTYPE, G_TYPE_STRING, + "G_TYPE_4", G_TYPE_GTYPE, G_TYPE_STRING, + "G_TYPE_5", G_TYPE_GTYPE, G_TYPE_STRING, + "G_TYPE_6", G_TYPE_GTYPE, G_TYPE_STRING, + "G_TYPE_7", G_TYPE_GTYPE, G_TYPE_STRING, + "G_TYPE_8", G_TYPE_GTYPE, G_TYPE_STRING, + "G_TYPE_9", G_TYPE_GTYPE, G_TYPE_STRING, + "G_TYPE_10", G_TYPE_GTYPE, G_TYPE_STRING, + "G_TYPE_11", G_TYPE_GTYPE, G_TYPE_STRING); + dm = gda_data_model_import_new_file (filename, TRUE, plist); + if (dm == NULL) + { + dialog = gtk_message_dialog_new (w_main, + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "Errore durante l'importazione del file: %s.", filename); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + return NULL; + } + + /*errors = gda_data_model_import_get_errors (GDA_DATA_MODEL_IMPORT (dm)); + if (errors != NULL) + { + while (errors != NULL) + { + error = (GError *)errors->data; + g_warning (error->message != NULL ? error->message : ""); + errors = g_slist_next (errors); + } + + dialog = gtk_message_dialog_new (w_main, + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "Errore durante l'importazione del file."); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + return NULL; + }*/ + + plist = gda_set_new_inline (1, "OVERWRITE", G_TYPE_BOOLEAN, TRUE); + gda_data_model_export_to_file (dm, GDA_DATA_MODEL_IO_TEXT_SEPARATED, "importato.csv", + NULL, 0, NULL, 0, plist, NULL); + + rows = gda_data_model_get_n_rows (dm); + if (rows < 1) + { + dialog = gtk_message_dialog_new (w_main, + GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, + "Nessuna informazione da importare nel file: %s.", filename); + gtk_dialog_run (GTK_DIALOG (dialog)); + gtk_widget_destroy (dialog); + return NULL; + } + g_message ("Numero di record: %d.", rows); + + /* TODO + * segnarsi i record aggiornati, perché alla fine del ciclo quelli + * non aggiornati vanni segnati eliminati + */ + + k_regioni_cod_istat = g_strdup (""); + for (row = 0; row < rows; row++) + { + if (g_strcmp0 (k_regioni_cod_istat, gdaex_data_model_get_value_stringify_at (dm, row, 4)) != 0) + { + /* nuova regione */ + k_regioni_cod_istat = gdaex_data_model_get_value_stringify_at (dm, row, 4); + sql = g_strdup_printf ("SELECT id FROM regioni WHERE cod_istat = '%s' AND status <> 'E'", + k_regioni_cod_istat); + dm_regioni = gdaex_query (priv->commons->gdaex, sql); + if (dm_regioni == NULL || gda_data_model_get_n_rows (dm_regioni) == 0) + { + g_object_unref (dm_regioni); + id_regioni = 0; + sql = g_strdup_printf ("SELECT COALESCE (MAX (id), 0) FROM regioni"); + dm_regioni = gdaex_query (priv->commons->gdaex, sql); + if (dm_regioni != NULL && gda_data_model_get_n_rows (dm_regioni) == 1) + { + id_regioni = gdaex_data_model_get_value_integer_at (dm_regioni, 0, 0); + } + id_regioni++; + + sql = g_strdup_printf ("INSERT INTO regioni (id, nome, cod_istat)" + " VALUES (%d, '%s', '%s')", + id_regioni, + gdaex_data_model_get_value_stringify_at (dm, row, 7), + k_regioni_cod_istat); + } + else + { + id_regioni = gdaex_data_model_get_value_integer_at (dm_regioni, 0, 0); + sql = g_strdup_printf ("UPDATE regioni SET nome = '%s' WHERE id = %d", + gdaex_data_model_get_value_stringify_at (dm, row, 7), + id_regioni); + } + if (gdaex_execute (priv->commons->gdaex, sql) < 1) + { + g_warning ("Si è verificato un errore nell'importazione della regione: %s - %s", + k_regioni_cod_istat, + gdaex_data_model_get_value_stringify_at (dm, row, 7)); + return NULL; + } + } + + /* provincia */ + sql = g_strdup_printf ("SELECT id FROM province" + " WHERE id_regioni = %d AND cod_istat = '%s'", + id_regioni, + gdaex_data_model_get_value_stringify_at (dm, row, 8)); + dm_province = gdaex_query (priv->commons->gdaex, sql); + if (dm_province == NULL || gda_data_model_get_n_rows (dm_province) == 0) + { + g_object_unref (dm_province); + id_province= 0; + sql = g_strdup_printf ("SELECT COALESCE (MAX (id), 0) FROM province"); + dm_province = gdaex_query (priv->commons->gdaex, sql); + if (dm_province != NULL && gda_data_model_get_n_rows (dm_province) == 1) + { + id_province = gdaex_data_model_get_value_integer_at (dm_province, 0, 0); + } + id_province++; + + sql = g_strdup_printf ("INSERT INTO province (id, nome, cod_istat, sigla, id_regioni)" + " VALUES (%d, '%s', '%s', '%s', %d)", + id_province, + gdaex_data_model_get_value_stringify_at (dm, row, 10), + gdaex_data_model_get_value_stringify_at (dm, row, 8), + gdaex_data_model_get_value_stringify_at (dm, row, 11), + id_regioni); + } + else + { + id_province = gdaex_data_model_get_value_integer_at (dm_province, 0, 0); + sql = g_strdup_printf ("UPDATE province SET nome = '%s'," + " sigla = '%s'" + " WHERE id = %d", + gdaex_data_model_get_value_stringify_at (dm, row, 10), + gdaex_data_model_get_value_stringify_at (dm, row, 11), + id_province); + } + if (gdaex_execute (priv->commons->gdaex, sql) < 1) + { + g_warning ("Si è verificato un errore nell'importazione della provincia: %s", + gdaex_data_model_get_value_stringify_at (dm, row, 10)); + return NULL; + } + } + + g_free (filename); + } + else + { + gtk_widget_destroy (fchooser); + } + g_message ("Fine import."); + + return a; +} + +/* PRIVATE */ +static void +territorio_importa_regioniprovinceistat_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) +{ + TerritorioImportaRegioniProvinceIstat *territorio_importa_regioniprovinceistat = TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT (object); + + TerritorioImportaRegioniProvinceIstatPrivate *priv = TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT_GET_PRIVATE (territorio_importa_regioniprovinceistat); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +territorio_importa_regioniprovinceistat_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) +{ + TerritorioImportaRegioniProvinceIstat *territorio_importa_regioniprovinceistat = TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT (object); + + TerritorioImportaRegioniProvinceIstatPrivate *priv = TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT_GET_PRIVATE (territorio_importa_regioniprovinceistat); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +/* CALLBACK */ diff --git a/src/importaregioniprovinceistat.h b/src/importaregioniprovinceistat.h new file mode 100644 index 0000000..b5079cc --- /dev/null +++ b/src/importaregioniprovinceistat.h @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2010 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef __TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT_H__ +#define __TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT_H__ + +#include +#include + +#include +#include + +#include "commons.h" + +G_BEGIN_DECLS + + +#define TYPE_TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT (territorio_importa_regioniprovinceistat_get_type ()) +#define TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT, TerritorioImportaRegioniProvinceIstat)) +#define TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT, TerritorioImportaRegioniProvinceIstatClass)) +#define IS_TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT)) +#define IS_TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT)) +#define TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT, TerritorioImportaRegioniProvinceIstatClass)) + + +typedef struct _TerritorioImportaRegioniProvinceIstat TerritorioImportaRegioniProvinceIstat; +typedef struct _TerritorioImportaRegioniProvinceIstatClass TerritorioImportaRegioniProvinceIstatClass; + +struct _TerritorioImportaRegioniProvinceIstat + { + GObject parent; + }; + +struct _TerritorioImportaRegioniProvinceIstatClass + { + GObjectClass parent_class; + }; + +GType territorio_importa_regioniprovinceistat_get_type (void) G_GNUC_CONST; + +TerritorioImportaRegioniProvinceIstat *territorio_importa_regioniprovinceistat_new (TerritorioCommons *commons); + + +G_END_DECLS + +#endif /* __TERRITORIO_IMPORTA_REGIONIPROVINCEISTAT_H__ */ diff --git a/src/main.c b/src/main.c index 5363c1a..fc40ba3 100644 --- a/src/main.c +++ b/src/main.c @@ -30,6 +30,7 @@ #include "aggregazioniterritoriali.h" #include "commons.h" #include "comuni.h" +#include "importaregioniprovinceistat.h" #include "province.h" #include "regioni.h" #include "stati.h" @@ -148,6 +149,15 @@ on_mnu_tables_aggregazioni_territoriali_activate (GtkMenuItem *menuitem, main_set_vbx_body_child (vbx); } +G_MODULE_EXPORT void +on_mnu_utilita_importa_regioniprovinceistat_activate (GtkMenuItem *menuitem, + gpointer user_data) +{ + TerritorioImportaRegioniProvinceIstat *m = territorio_importa_regioniprovinceistat_new (commons); + + g_object_unref (m); +} + G_MODULE_EXPORT void on_mnu_help_about_activate (GtkMenuItem *menuitem, gpointer user_data) @@ -254,7 +264,8 @@ main (int argc, char *argv[]) p = strrchr (moddir, G_DIR_SEPARATOR); if (p != NULL - && g_ascii_strcasecmp (p + 1, "src") == 0) + && (g_ascii_strcasecmp (p + 1, "src") == 0 + || g_ascii_strcasecmp (p + 1, ".libs") == 0)) { /* *p = '\0'; -- 2.49.0