--- /dev/null
+/*
+ * 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 */