]> saetta.ns0.it Git - solipa/territorio/commitdiff
Inizio importazione dei file dell'istat (ma non funziona; sto aspettando
authorAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Tue, 26 Oct 2010 11:40:07 +0000 (13:40 +0200)
committerAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Tue, 26 Oct 2010 11:40:07 +0000 (13:40 +0200)
una rispota dagli sviluppatori di libgda).
Cambiamento nell'ottenimento di GUIDIR e FORMDIR sotto windows durante lo
sviluppo perché adesso essendo un eseguibile libtool il vero eseguibile si
trova nella directory src/.libs.

data/territorio/gui/territorio.gui
src/Makefile.am
src/importaregioniprovinceistat.c [new file with mode: 0644]
src/importaregioniprovinceistat.h [new file with mode: 0644]
src/main.c

index 78add341d5edc58b443d7824ccbbbc9a6ad7ee34..72e1a49f0e5e04f6858aa047df23f1f491551904 100644 (file)
                 </child>
               </object>
             </child>
+            <child>
+              <object class="GtkMenuItem" id="menuitem12">
+                <property name="visible">True</property>
+                <property name="label" translatable="yes">Utilit&#xE0;</property>
+                <property name="use_underline">True</property>
+                <child type="submenu">
+                  <object class="GtkMenu" id="menu5">
+                    <property name="visible">True</property>
+                    <child>
+                      <object class="GtkMenuItem" id="menuitem13">
+                        <property name="visible">True</property>
+                        <property name="label" translatable="yes">_Importa</property>
+                        <property name="use_underline">True</property>
+                        <child type="submenu">
+                          <object class="GtkMenu" id="menu6">
+                            <property name="visible">True</property>
+                            <child>
+                              <object class="GtkMenuItem" id="menuitem14">
+                                <property name="visible">True</property>
+                                <property name="label" translatable="yes">_Regioni e province da Istat</property>
+                                <property name="use_underline">True</property>
+                                <signal name="activate" handler="on_mnu_utilita_importa_regioniprovinceistat_activate"/>
+                              </object>
+                            </child>
+                          </object>
+                        </child>
+                      </object>
+                    </child>
+                  </object>
+                </child>
+              </object>
+            </child>
             <child>
               <object class="GtkMenuItem" id="menuitem4">
                 <property name="visible">True</property>
index 0fa55d7ec3bbb93b555ae24f872e4e9a202e335c..150878b8c8264b88c0ddb9b56237ba843419a28c 100644 (file)
@@ -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 (file)
index 0000000..28f77fe
--- /dev/null
@@ -0,0 +1,312 @@
+/* 
+ * Copyright (C) 2010 Andrea Zagli <azagli@libero.it>
+ *
+ * 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 (file)
index 0000000..b5079cc
--- /dev/null
@@ -0,0 +1,62 @@
+/* 
+ * Copyright (C) 2010 Andrea Zagli <azagli@libero.it>
+ *
+ * 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 <glib.h>
+#include <glib-object.h>
+
+#include <gtk/gtk.h>
+#include <libgdaex.h>
+
+#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__ */
index 5363c1af06e2276fa8b6e2247f7cd5e2f3cb37a8..fc40ba393f0e6d0b35a0da262c030e0cb9993995 100644 (file)
@@ -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';