File: mg-context.c

package info (click to toggle)
mergeant 0.52-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 12,872 kB
  • ctags: 6,584
  • sloc: ansic: 63,372; xml: 23,218; sh: 10,895; makefile: 612; sql: 237
file content (1243 lines) | stat: -rw-r--r-- 36,405 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
/* mg-context.c
 *
 * Copyright (C) 2003 Vivien Malerba
 *
 * 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 the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#include "mg-context.h"
#include "mg-query.h"
#include "marshal.h"
#include <string.h>
#include "mg-parameter.h"
#include "mg-field.h"
#include "mg-referer.h"
#include "mg-entity.h"
#include "mg-server-data-type.h"
#include "mg-qfield.h"

/* 
 * Main static functions 
 */
static void mg_context_class_init (MgContextClass * class);
static void mg_context_init (MgContext * srv);
static void mg_context_dispose (GObject   * object);
static void mg_context_finalize (GObject   * object);

static void mg_context_set_property (GObject              *object,
				    guint                 param_id,
				    const GValue         *value,
				    GParamSpec           *pspec);
static void mg_context_get_property (GObject              *object,
				    guint                 param_id,
				    GValue               *value,
				    GParamSpec           *pspec);

static void mg_context_free_node (MgContext *context, MgContextNode *node);
static void nullified_param_cb (MgParameter *param, MgContext *context);
static void changed_param_cb (MgParameter *param, MgContext *context);

#ifdef debug
static void mg_context_dump                     (MgContext *context, guint offset);
#endif

/* get a pointer to the parents to be able to call their destructor */
static GObjectClass  *parent_class = NULL;

/* signals */
enum
{
	PARAM_CHANGED,
	LAST_SIGNAL
};

static gint mg_context_signals[LAST_SIGNAL] = { 0 };

/* properties */
enum
{
	PROP_0,
	PROP
};


/* private structure */
struct _MgContextPrivate
{
	GHashTable *param_default_values;      /* key = param, value = new GdaValue */
	GHashTable *param_default_aliases;     /* key = param, value = alias parameter */
	GHashTable *aliases_default_param;     /* key = alias parameter, value = param */

	GHashTable *param_repl;                /* key = parameter, value = parameter used instead because of similarity */
};



/* module error */
GQuark mg_context_error_quark (void)
{
	static GQuark quark;
	if (!quark)
		quark = g_quark_from_static_string ("mg_context_error");
	return quark;
}


guint
mg_context_get_type (void)
{
	static GType type = 0;

	if (!type) {
		static const GTypeInfo info = {
			sizeof (MgContextClass),
			(GBaseInitFunc) NULL,
			(GBaseFinalizeFunc) NULL,
			(GClassInitFunc) mg_context_class_init,
			NULL,
			NULL,
			sizeof (MgContext),
			0,
			(GInstanceInitFunc) mg_context_init
		};
		
		type = g_type_register_static (MG_BASE_TYPE, "MgContext", &info, 0);
	}
	return type;
}


static void
mg_context_class_init (MgContextClass *class)
{
	GObjectClass   *object_class = G_OBJECT_CLASS (class);

	parent_class = g_type_class_peek_parent (class);

	mg_context_signals[PARAM_CHANGED] =
		g_signal_new ("param_changed",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_FIRST,
			      G_STRUCT_OFFSET (MgContextClass, param_changed),
			      NULL, NULL,
			      marshal_VOID__OBJECT, G_TYPE_NONE, 1,
			      G_TYPE_OBJECT);

	class->param_changed = NULL;

	object_class->dispose = mg_context_dispose;
	object_class->finalize = mg_context_finalize;

	/* Properties */
	object_class->set_property = mg_context_set_property;
	object_class->get_property = mg_context_get_property;
	g_object_class_install_property (object_class, PROP,
					 g_param_spec_pointer ("prop", NULL, NULL, 
							       (G_PARAM_READABLE | G_PARAM_WRITABLE)));
	/* virtual functions */
#ifdef debug
        MG_BASE_CLASS (class)->dump = (void (*)(MgBase *, guint)) mg_context_dump;
#endif
}

static void
mg_context_init (MgContext *mg_context)
{
	mg_context->priv = g_new0 (MgContextPrivate, 1);
	mg_context->parameters = NULL;
	mg_context->nodes = NULL;

	mg_context->priv->param_default_values = g_hash_table_new_full (NULL, NULL, 
									NULL, (GDestroyNotify) gda_value_free);
	mg_context->priv->param_default_aliases = g_hash_table_new (NULL, NULL);
	mg_context->priv->aliases_default_param = g_hash_table_new (NULL, NULL);

	mg_context->priv->param_repl = g_hash_table_new (NULL, NULL);
}

static void compute_context_nodes (MgContext *context);
static void mg_context_real_add_param (MgContext *context, MgParameter *param);

/**
 * mg_context_new
 * @conf: a #MgConf object
 * @params: a list of #MgParameter objects
 *
 * Creates a new #MgContext object, and populates it with the list given as argument.
 * The list can then be freed as it gets copied. All the parameters in @params are referenced counted
 * and modified, so they should not be used anymore afterwards, and the @params list gets copied
 * (so it should be freed by the caller).
 *
 * Returns: a new #MgContext object
 */
GObject *
mg_context_new (MgConf *conf, GSList *params)
{
	GObject *obj;
	MgContext *mg_context;

	g_return_val_if_fail (conf && IS_MG_CONF (conf), NULL);

	obj = g_object_new (MG_CONTEXT_TYPE, "conf", conf, NULL);
        mg_context = MG_CONTEXT (obj);
        mg_base_set_id (MG_BASE (mg_context), 0);

	/* add the parameters */
	while (params) {
		mg_context_real_add_param (mg_context, MG_PARAMETER (params->data));
		params = g_slist_next (params);
	}
	compute_context_nodes (mg_context);

	return obj;
}


/**
 * mg_context_new_copy
 * @orig: a #MgContext to make a copy of
 * @copy_params: TRUE to also make a copy of the #MgParameter objects within @orig
 * @replacements: a place where to store the parameters and queries replacements, or %NULL
 * 
 * Copy constructor. @replacements is used and modified only if @copy_params is TRUE.
 *
 * Returns: a the new copy of @orig
 */
GObject *
mg_context_new_copy (MgContext *orig, gboolean copy_params, GHashTable *replacements)
{
	GObject   *obj;
	MgContext *mg_context;
	MgConf *conf;
	GSList *list;

	g_return_val_if_fail (orig && IS_MG_CONTEXT (orig), NULL);

	conf = mg_base_get_conf (MG_BASE (orig));
	obj = g_object_new (MG_CONTEXT_TYPE, "conf", conf, NULL);
	mg_context = MG_CONTEXT (obj);
	mg_base_set_id (MG_BASE (mg_context), 0);

	if (! copy_params) {
		GSList *olist;
		/* copy parameters */
		if (orig->parameters)
			mg_context->parameters = g_slist_copy (orig->parameters);
		
		olist = orig->parameters;
		list = mg_context->parameters;
		while (list) {
			g_signal_connect (G_OBJECT (list->data), "nullified",
					  G_CALLBACK (nullified_param_cb), mg_context);
			g_signal_connect (G_OBJECT (list->data), "changed",
					  G_CALLBACK (changed_param_cb), mg_context);
			g_object_ref (G_OBJECT (list->data));

			if (replacements)
				g_hash_table_insert (replacements, olist->data, list->data);

			list = g_slist_next (list);
			olist = g_slist_next (olist);
		}

		/* copy nodes */
		list = orig->nodes;
		while (list) {
			MgContextNode *node, *orig;
			
			orig = MG_CONTEXT_NODE (list->data);
			node = g_new0 (MgContextNode, 1);
			node->param = orig->param;
			if (orig->query) {
				node->query = orig->query;
				g_object_ref (G_OBJECT (node->query));
			}
			if (orig->params)
				node->params = g_slist_copy (orig->params);
			
			mg_context->nodes = g_slist_append (mg_context->nodes, node);
			
			if (replacements)
				g_hash_table_insert (replacements, list->data, node);

			list = g_slist_next (list);
		}
	}
	else {
		GHashTable *repl;
		GSList *list;
		MgParameter *param;

		if (replacements)
			repl = replacements;
		else
			repl = g_hash_table_new (NULL, NULL);
		/* copy parameters */
		if (orig->parameters) {
			list = orig->parameters;
			while (list) {
				param = MG_PARAMETER (mg_parameter_new_copy (MG_PARAMETER (list->data)));
				g_hash_table_insert (repl, list->data, param);
				mg_context->parameters = g_slist_append (mg_context->parameters, param);

				g_signal_connect (G_OBJECT (param), "nullified",
						  G_CALLBACK (nullified_param_cb), mg_context);
				g_signal_connect (G_OBJECT (param), "changed",
						  G_CALLBACK (changed_param_cb), mg_context);

				list = g_slist_next (list);
			}
		}
		
		/* finish nodes copy */
		list = orig->nodes;
		while (list) {
			MgContextNode *node, *orig;
			
			orig = MG_CONTEXT_NODE (list->data);
			node = g_new0 (MgContextNode, 1);

			node->param = g_hash_table_lookup (repl, orig->param);
			
			if (orig->params) {
				list = orig->params;
				while (list) {
					node->params = g_slist_append (node->params, g_hash_table_lookup (repl, list->data));
					list = g_slist_next (list);
				}
			}

			if (orig->query) 
				node->query = MG_QUERY (mg_query_new_copy (orig->query, repl));
			
			mg_context->nodes = g_slist_append (mg_context->nodes, node);
			g_hash_table_insert (repl, orig, node);

			list = g_slist_next (list);
		}

		/* make the replacements be taken into account */
		list = mg_context->parameters;
		while (list) {
			mg_referer_replace_refs (MG_REFERER (list->data), repl);
			list = g_slist_next (list);
		}
		
		if (!replacements)
			g_hash_table_destroy (repl);
	}

	return obj;
}

static void
nullified_param_cb (MgParameter *param, MgContext *context)
{
	g_assert (g_slist_find (context->parameters, param));
	context->parameters = g_slist_remove (context->parameters, param);
	g_signal_handlers_disconnect_by_func (G_OBJECT (param),
					      G_CALLBACK (nullified_param_cb), context);
	g_signal_handlers_disconnect_by_func (G_OBJECT (param),
					      G_CALLBACK (changed_param_cb), context);

	if (context->nodes) {
		MgContextNode *node;
		gboolean finished = FALSE;
		GSList *list = context->nodes;

		while (list && !finished) {
			node = MG_CONTEXT_NODE (list->data);
			if (node->params && g_slist_find (node->params, param)) {
				node->params = g_slist_remove (node->params, param);
				if (!node->params) {
					g_object_unref (node->query);
					context->nodes = g_slist_remove (context->nodes, node);
					g_free (node);
				}
				finished = TRUE;
			}
			list = g_slist_next (list);
		}
	}
	g_object_unref (G_OBJECT (param));
}

static void
changed_param_cb (MgParameter *param, MgContext *context)
{
	/* signal the parameter change */
	mg_base_changed (MG_BASE (context));
#ifdef debug_signal
	g_print (">> 'PARAM_CHANGED' from %s\n", __FUNCTION__);
#endif
	g_signal_emit (G_OBJECT (context), mg_context_signals[PARAM_CHANGED], 0, param);
#ifdef debug_signal
	g_print ("<< 'PARAM_CHANGED' from %s\n", __FUNCTION__);
#endif
}

static void
mg_context_dispose (GObject *object)
{
	MgContext *mg_context;

	g_return_if_fail (object != NULL);
	g_return_if_fail (IS_MG_CONTEXT (object));

	mg_context = MG_CONTEXT (object);
	if (mg_context->priv) {
		mg_base_nullify_check (MG_BASE (object));
	}
	
	/* free the nodes if there are some */
	while (mg_context->nodes)
		mg_context_free_node (mg_context, MG_CONTEXT_NODE (mg_context->nodes->data));

	/* free the parameters list */
	while (mg_context->parameters)
		nullified_param_cb (MG_PARAMETER (mg_context->parameters->data), mg_context);


	/* parent class */
	parent_class->dispose (object);
}

static void foreach_finalize_alias (MgParameter *param, MgParameter *alias, MgContext *context);
static void
mg_context_finalize (GObject *object)
{
	MgContext *mg_context;

	g_return_if_fail (object != NULL);
	g_return_if_fail (IS_MG_CONTEXT (object));

	mg_context = MG_CONTEXT (object);
	if (mg_context->priv) {
		g_hash_table_destroy (mg_context->priv->param_default_values);
		g_hash_table_foreach (mg_context->priv->param_default_aliases,
				      (GHFunc) foreach_finalize_alias, mg_context);
		g_hash_table_destroy (mg_context->priv->param_default_aliases);
		g_hash_table_destroy (mg_context->priv->aliases_default_param);
		g_hash_table_destroy (mg_context->priv->param_repl);

		g_free (mg_context->priv);
		mg_context->priv = NULL;
	}

	/* parent class */
	parent_class->finalize (object);
}


static void 
mg_context_set_property (GObject              *object,
			guint                 param_id,
			const GValue         *value,
			GParamSpec           *pspec)
{
	gpointer ptr;
	MgContext *mg_context;

	mg_context = MG_CONTEXT (object);
	if (mg_context->priv) {
		switch (param_id) {
		case PROP:
			ptr = g_value_get_pointer (value);
			break;
		}
	}
}

static void
mg_context_get_property (GObject              *object,
			  guint                 param_id,
			  GValue               *value,
			  GParamSpec           *pspec)
{
	MgContext *mg_context;
	mg_context = MG_CONTEXT (object);
	
	if (mg_context->priv) {
		switch (param_id) {
		case PROP:
			break;
		}	
	}
}

/*
 * Computes context->nodes, and if some nodes already exist, they are previously discarded
 */
static void 
compute_context_nodes (MgContext *context)
{
	gboolean err = FALSE;
	GSList *list;
	GHashTable *hash;       /* key = query referenced by a param, value= corresponding node */
	GHashTable *node_hashs; /* key = node, value = corresponding replacements hash */

	/* free the nodes if there are some */
	while (context->nodes)
		mg_context_free_node (context, MG_CONTEXT_NODE (context->nodes->data));

	hash = g_hash_table_new (NULL, NULL);
	node_hashs = g_hash_table_new (NULL, NULL);
	list = context->parameters;

	while (list && !err) {
		MgQfield *ref_field;
		
		ref_field = mg_parameter_get_source_field (MG_PARAMETER (list->data));
		if (ref_field) {
			/* the parameter has a value constrained by a query */
			MgEntity *ent;
			ent = mg_field_get_entity (MG_FIELD (ref_field));
			if (!ent || !IS_MG_QUERY (ent))
				err = TRUE;
			else {
				MgContextNode *enode = g_hash_table_lookup (hash, ent);
				if (!enode) {
					MgContextNode *node;
					GHashTable *repl;       /* replacements for query copying */
					
					repl = g_hash_table_new (NULL, NULL);
					node = g_new0 (MgContextNode, 1);
					node->query = MG_QUERY (mg_query_new_copy (MG_QUERY (ent), repl));
					node->params = g_slist_append (NULL, list->data);
					g_hash_table_insert (hash, ent, node);
					g_hash_table_insert (node_hashs, node, repl);

					context->nodes = g_slist_append (context->nodes, node);
				}
				else 
					enode->params = g_slist_append (enode->params, list->data);
			}
		}
		else {
			/* the parameter is free fill */
			MgContextNode *node;
			
			node = g_new0 (MgContextNode, 1);
			node->param = list->data;
			context->nodes = g_slist_append (context->nodes, node);
		}

		list = g_slist_next (list);
	}

	/* taking into account all the replacements */
	list = context->nodes;
	while (list) {
		GHashTable *node_repl, *dep_repl;
		MgQfield *ref_field;
		MgContextNode *node = MG_CONTEXT_NODE (list->data);
		GSList *params;

		node_repl = g_hash_table_lookup (node_hashs, node);
		
		if (node->param) {
			/* for field replacements if necessary */
			GSList *dest_fields, *tmplist;

			dest_fields = mg_parameter_get_dest_fields (node->param);
			tmplist = dest_fields;
			while (tmplist) {
				MgContextNode *enode;
				MgEntity *ent;
				ent = mg_field_get_entity (MG_FIELD (tmplist->data));
				enode = g_hash_table_lookup (hash, ent);
				if (enode) {
					dep_repl = g_hash_table_lookup (node_hashs, enode);
					mg_parameter_replace_ref (node->param, dep_repl);
				}
				tmplist = g_slist_next (tmplist);
			}
		}

		params = node->params;
		while (params) {
			GSList *dest_fields, *tmplist;

			/* dependency node's replacements */
			ref_field = mg_parameter_get_source_field (MG_PARAMETER (params->data));
			if (ref_field) {
				MgContextNode *enode;
				MgEntity *ent;
				ent = mg_field_get_entity (MG_FIELD (ref_field));
				enode = g_hash_table_lookup (hash, ent);
				g_assert (enode);
				dep_repl = g_hash_table_lookup (node_hashs, enode);
				mg_parameter_replace_ref (MG_PARAMETER (params->data), dep_repl);
			}
				
			/* this node's replacements */
			if (node_repl)
				mg_parameter_replace_ref (MG_PARAMETER (params->data), node_repl);

			/* for field replacements if necessary */
			dest_fields = mg_parameter_get_dest_fields (MG_PARAMETER (params->data));
			tmplist = dest_fields;
			while (tmplist) {
				MgContextNode *enode;
				MgEntity *ent;
				ent = mg_field_get_entity (MG_FIELD (tmplist->data));
				enode = g_hash_table_lookup (hash, ent);
				if (enode) {
					dep_repl = g_hash_table_lookup (node_hashs, enode);
					mg_parameter_replace_ref (MG_PARAMETER (params->data), dep_repl);
				}
				tmplist = g_slist_next (tmplist);
			}

			params = g_slist_next (params);
		}
		
		if (node->query) {
			/* replacements for the query */
			GSList *sublist = context->nodes;

			while (sublist) {
				if (MG_CONTEXT_NODE (sublist->data)->query)
					mg_referer_replace_refs (MG_REFERER (node->query), g_hash_table_lookup (node_hashs, sublist->data));
				sublist = g_slist_next (sublist);
			}
		}
		
		list = g_slist_next (list);
	}

	/* creating params_pos_in_query in each node */
	list = context->nodes;
	while (list) {
		GSList *params;
		GHashTable *hash_pos;
		gint pos;
		MgQfield *field;

		hash_pos = g_hash_table_new (NULL, NULL);
		MG_CONTEXT_NODE (list->data)->params_pos_in_query = hash_pos;
		
		params = MG_CONTEXT_NODE (list->data)->params;
		while (params) {
			field = mg_parameter_get_source_field (MG_PARAMETER (params->data));
			pos = mg_entity_get_field_index (MG_ENTITY (MG_CONTEXT_NODE (list->data)->query), MG_FIELD (field));
			if (pos < 0) {
				err = TRUE;
				g_warning ("Can't find position of field %p into query %p for parameter %p\n",
					   field, MG_CONTEXT_NODE (list->data)->query, params->data);
			}

			g_hash_table_insert (hash_pos, params->data, GINT_TO_POINTER (pos));
			params = g_slist_next (params);
		}

		list = g_slist_next (list);
	}

	/* memory de-allocations */
	list = context->nodes;
	while (list) {
		GHashTable *node_repl;
		node_repl = g_hash_table_lookup (node_hashs, list->data);
		if (node_repl) {
			g_hash_table_remove (node_hashs, list->data);
			g_hash_table_destroy (node_repl);
		}
		list = g_slist_next (list);
	}
	g_hash_table_destroy (hash);
	g_hash_table_destroy (node_hashs);

#ifdef debug_NO
	mg_context_dump (context, 0);
#endif
}

/**
 * mg_context_add_param
 * @context: a #MgContext object
 * @param: a #MgParameter object
 *
 * Adds @param to the list of parameters managed within @context.
 * <b>WARNING:</b> the context may decide not to use the @param parameter, but to
 * modify another parameter already present within the context. The publicly available
 * lists from the @context object may also be changed in the process.
 */
void
mg_context_add_param (MgContext *context, MgParameter *param)
{
	g_return_if_fail (context && IS_MG_CONTEXT (context));
	g_return_if_fail (param && IS_MG_PARAMETER (param));

	mg_context_real_add_param (context, param);
	compute_context_nodes (context);
}

static void
mg_context_real_add_param (MgContext *context, MgParameter *param)
{
	GSList *params;
	MgParameter *similar = NULL;
	GSList *param_dest_fields;

	if (g_slist_find (context->parameters, param))
		return;

	/* try to find a similar parameter in the context->parameters */
	param_dest_fields = mg_parameter_get_dest_fields (param);
	params = context->parameters;
	while (params && !similar) {
		GSList *list1;
		list1 = mg_parameter_get_dest_fields (MG_PARAMETER (params->data));

		while (list1 && !similar) {
			if (g_slist_find (param_dest_fields, list1->data))
				similar = MG_PARAMETER (params->data);
			list1 = g_slist_next (list1);
		}
		params = g_slist_next (params);
	}
	
	if (similar) {
#ifdef debug_NO
		g_print ("Param %p and %p are similar, keeping %p only\n", similar, param, similar);
#endif
		g_hash_table_insert (context->priv->param_repl, param, similar);
		/* don't use @param, but instead use similar and update the list of fields
		   similar is for */
		while (param_dest_fields) {
			mg_parameter_add_dest_field (similar, MG_QFIELD (param_dest_fields->data));
			param_dest_fields = g_slist_next (param_dest_fields);
		}
	}
	else {
		/* really add @param to the context */
		context->parameters = g_slist_append (context->parameters, param);
		g_signal_connect (G_OBJECT (param), "nullified",
				  G_CALLBACK (nullified_param_cb), context);
		g_signal_connect (G_OBJECT (param), "changed",
				  G_CALLBACK (changed_param_cb), context);

		g_object_ref (G_OBJECT (param));
		mg_parameter_replace_ref (param, context->priv->param_repl);
	}
}

/**
 * mg_context_merge_context_params
 * @context: a #MgContext object
 * @context_to_merge: a #MgContext object
 *
 * Add to @context all the parameters of @context_to_merge.
 */
void
mg_context_merge_context_params (MgContext *context, MgContext *context_to_merge)
{
	GSList *params = context_to_merge->parameters;
	g_return_if_fail (context && IS_MG_CONTEXT (context));
	g_return_if_fail (context_to_merge && IS_MG_CONTEXT (context_to_merge));

	while (params) {
		mg_context_add_param (context, MG_PARAMETER (params->data));
		params = g_slist_next (params);
	}
}

/**
 * mg__context_is_coherent
 * @context: a #MgContext object
 * @error:
 *
 * Checks that @context has a coherent structure
 *
 * Returns: TRUE if @context is coherent
 */
gboolean
mg_context_is_coherent (MgContext *context, GError **error)
{
	GSList *list;
	gint current_pos = 0;
	/* To test:
	   - For each parameter, check that the value provider is a field of the same type and is visible
	   - For each parameter, check that the value provider belongs to a SELECT query
	   - For each parameter, check that all the dependancies parameter are also in the context
	   - the nodes settings are correct
	*/
	list = context->parameters;
	while (list) {
		MgParameter *param = MG_PARAMETER (list->data);
		MgQfield *value_prov;
		MgContextNode *node;
		GSList *depend;
		
		node = mg_context_find_node_for_param (context, param);
		if (!node) {
			g_set_error (error, MG_CONTEXT_ERROR, MG_CONTEXT_NO_NODE_ERROR,
				     _("Value provider for parameter '%s' is not listed in any context node "
				       "(internal error)"),
				     mg_base_get_name (MG_BASE (param)));
			return FALSE;
		}

		/*
		 * testing the value providers
		 */
		value_prov = mg_parameter_get_source_field (param);
		if (value_prov) {
			MgQuery *query;

			/* type of object referenced by the "value_prov" property */
			if (! IS_MG_QFIELD (value_prov)) {
				g_set_error (error, MG_CONTEXT_ERROR, MG_CONTEXT_VALUE_PROV_OBJ_TYPE_ERROR,
					     _("Value provider for parameter '%s' is not a query field"),
					     mg_base_get_name (MG_BASE (param)));
				return FALSE;
			}
			
			/* data type of value_prov */
			if (mg_parameter_get_data_type (param) != mg_field_get_data_type (MG_FIELD (value_prov))) {
				g_set_error (error, MG_CONTEXT_ERROR, MG_CONTEXT_VALUE_PROV_DATA_TYPE_ERROR,
					     _("Data types for parameter '%s' and value provider differ "
					       "(respectively %s and %s)"),
					     mg_base_get_name (MG_BASE (param)),
					     mg_base_get_name (MG_BASE (mg_parameter_get_data_type (param))),
					     mg_base_get_name (MG_BASE (mg_field_get_data_type (MG_FIELD (value_prov)))));
				return FALSE;
			}

			/* value_prov is visible */
			if (!mg_qfield_is_visible (MG_QFIELD (value_prov))) {
				g_set_error (error, MG_CONTEXT_ERROR, MG_CONTEXT_VALUE_PROV_INVISIBLE_ERROR,
					     _("Value provider for parameter '%s' is not visible "
					       "(won't appear in the result set when query is executed)"),
					     mg_base_get_name (MG_BASE (param)));
				return FALSE;
			}

			/* type of query to which value_prov belongs */
			query = MG_QUERY (mg_field_get_entity (MG_FIELD (value_prov)));
			if (! mg_query_is_select_query (query)) {
				g_set_error (error, MG_CONTEXT_ERROR, MG_CONTEXT_VALUE_PROV_QUERY_TYPE_ERROR,
					     _("Value provider for parameter '%s' does not belong to a SELECT query"),
					     mg_base_get_name (MG_BASE (param)));
				return FALSE;
			}

			/* MgContextNode for this param is correct */
			if (node->param ||
			    (node->query != query) ||
			    (GPOINTER_TO_INT (g_hash_table_lookup (node->params_pos_in_query, param)) < 0)) {
				g_set_error (error, MG_CONTEXT_ERROR, MG_CONTEXT_NODE_OUTDATED_ERROR,
					     _("Parameter '%s' has changed since insertion in the context"),
					     mg_base_get_name (MG_BASE (param)));
				return FALSE;
			}
		}
		else {
			/* MgContextNode for this param is correct */	
			if ((node->param != param) ||
			    node->query ||
			    node->params) {
				g_set_error (error, MG_CONTEXT_ERROR, MG_CONTEXT_NODE_OUTDATED_ERROR,
					     _("Parameter '%s' has changed since insertion in the context"),
					     mg_base_get_name (MG_BASE (param)));
				return FALSE;
			}
		}

		/*
		 * testing the parameter dependencies which must be within the context and before
		 * param in the parameters list
		 */
		depend = mg_parameter_get_dependencies (param);
		while (depend) {
			gint i = g_slist_index (context->parameters, depend->data);

			/* dependency param in context */
			if (i < 0) {
				g_set_error (error, MG_CONTEXT_ERROR, MG_CONTEXT_DEPENDENCY_NOT_IN_CONTEXT_ERROR,
					     _("Parameter '%s' has a dependency parameter ('%s') which is "
					       "not handled in the context"),
					     mg_base_get_name (MG_BASE (param)),
					     mg_base_get_name (MG_BASE (depend->data)));
				return FALSE;
			}
			
			/* dependency param before tested param in the parameters list */
			if (i >= current_pos) {
				g_set_error (error, MG_CONTEXT_ERROR, MG_CONTEXT_DEPENDENCY_POSITION_ERROR,
					     _("Parameter '%s' has a dependency parameter ('%s') which is "
					       "after itself the context"),
					     mg_base_get_name (MG_BASE (param)),
					     mg_base_get_name (MG_BASE (depend->data)));
				return FALSE;
			}

			depend = g_slist_next (depend);
		}

		list = g_slist_next (list);
		current_pos++;
	}
	
	return TRUE;
}


/**
 * mg_context_free_node
 * @context: a #MgContext object
 * @node: a #MgContextNode structure
 *
 * Removes @node from @context and de-allocates any memory in the process.
 */
static void
mg_context_free_node (MgContext *context, MgContextNode *node)
{
	g_return_if_fail (context && IS_MG_CONTEXT (context));
	g_return_if_fail (node);
	g_return_if_fail (context->priv);
	g_return_if_fail (g_slist_find (context->nodes, node));

	if (node->param)
		node->param = NULL;
	if (node->query)
		g_object_unref (G_OBJECT (node->query));
	if (node->params)
		g_slist_free (node->params);
	if (node->params_pos_in_query)
		g_hash_table_destroy (node->params_pos_in_query);

	context->nodes = g_slist_remove (context->nodes, node);
	g_free (node);
}

/**
 * mg_context_is_valid
 * @context: a #MgContext object
 *
 * Tells if all the context's parameters have valid data
 *
 * Returns: TRUE if the context is valid
 */
gboolean
mg_context_is_valid (MgContext *context)
{
	GSList *params;
	gboolean retval = TRUE;

	g_return_val_if_fail (context && IS_MG_CONTEXT (context), FALSE);
	g_return_val_if_fail (context->priv, FALSE);

	params = context->parameters;
	while (params && retval) {
		if (!mg_parameter_is_valid (MG_PARAMETER (params->data))) 
			retval = FALSE;
		
		/* g_print ("== PARAM %p: valid= %d, value=%s\n", params->data, mg_parameter_is_valid (MG_PARAMETER (params->data)), */
/* 			 mg_parameter_get_value (MG_PARAMETER (params->data)) ?  */
/* 			 gda_value_stringify (mg_parameter_get_value (MG_PARAMETER (params->data))) : "Null"); */
		params = g_slist_next (params);
	}

	return retval;
}

/**
 * mg_context_needs_user_input
 * @context: a #MgContext object
 *
 * Tells if the context needs the user to provide some values for some parameters.
 * This is the case when either the context is invalid, or the context is valid and
 * contains some #MgParameter objects which are valid but are defined to require 
 * a user input.
 *
 * Note: this function may return TRUE even though @context is valid.
 *
 * Returns: TRUE if a user input is required for the context
 */
gboolean
mg_context_needs_user_input (MgContext *context)
{
	GSList *params;
	gboolean retval = FALSE;

	g_return_val_if_fail (context && IS_MG_CONTEXT (context), FALSE);
	g_return_val_if_fail (context->priv, FALSE);

	if (!mg_context_is_valid (context))
		return TRUE;

	params = context->parameters;
	while (params && !retval) {
		if (mg_parameter_requires_user_input (MG_PARAMETER (params->data)))
			retval = TRUE;
		
		params = g_slist_next (params);
	}

	return retval;
}


/**
 * mg_context_find_parameter_for_field
 * @context: a #MgContext object
 * @for_field: a #MgQfield object
 *
 * Finds a #MgParameter which is to be used by @for_field
 *
 * Returns: a #MgParameter or %NULL
 */
MgParameter *
mg_context_find_parameter_for_field (MgContext *context, MgQfield *for_field)
{
	MgParameter *param = NULL;
	GSList *list;

	g_return_val_if_fail (context && IS_MG_CONTEXT (context), NULL);
	g_return_val_if_fail (context->priv, NULL);

	list = context->parameters;
	while (list && !param) {
		GSList *for_fields = mg_parameter_get_dest_fields (MG_PARAMETER (list->data));
		if (for_fields &&  g_slist_find (for_fields, for_field))
			param = MG_PARAMETER (list->data);
		list = g_slist_next (list);
	}

	return param;
}

/**
 * mg_context_find_node_for_param
 * @context: a #MgContext object
 * @param: a #MgParameter object
 *
 * Finds a #MgContextNode which is for @param
 *
 * Returns: a #MgContextNode or %NULL
 */
MgContextNode *
mg_context_find_node_for_param (MgContext *context, MgParameter *param)
{
	MgContextNode *retval = NULL, *node;
	GSList *list;

	g_return_val_if_fail (context && IS_MG_CONTEXT (context), NULL);
	g_return_val_if_fail (context->priv, NULL);
	g_return_val_if_fail (param && IS_MG_PARAMETER (param), NULL);
	g_return_val_if_fail (g_slist_find (context->parameters, param), NULL);

	list = context->nodes;
	while (list && !retval) {
		node = MG_CONTEXT_NODE (list->data);

		if (node->param) {
			if (node->param == param)
				retval = node;
		}
		else {
			if (g_slist_find (node->params, param))
				retval = node;
		}

		list = g_slist_next (list);
	}

	return retval;
}


/**
 * mg_context_set_param_default_value
 * @context: a #MgContext object
 * @param: a #MgParameter object, managed by @context
 * @value: a #GdaValue, of the same type as @param, or %NULL
 *
 * Stores @value in @context to make it possible for @context's users to find a default value
 * for @param when one is required, instead of %NULL.
 *
 * @context only provides a storage functionnality, the way the value obtained with 
 * mg_context_get_param_default_value() is used is up to @context's user.
 */
void
mg_context_set_param_default_value (MgContext *context, MgParameter *param, const GdaValue *value)
{
	g_return_if_fail (context && IS_MG_CONTEXT (context));
	g_return_if_fail (param && IS_MG_PARAMETER (param));
	g_return_if_fail (g_slist_find (context->parameters, param));

	if (value && ! gda_value_is_null (value)) {
		g_return_if_fail (gda_value_get_type (value) ==
				  mg_server_data_type_get_gda_type (mg_parameter_get_data_type (param)));
		g_hash_table_insert (context->priv->param_default_values, param, gda_value_copy (value));
	}
	else
		g_hash_table_remove (context->priv->param_default_values, param);
}

static void nullified_alias_cb (MgParameter *alias, MgContext *context);

/*
 * mg_context_set_param_default_alias
 * @context: a #MgContext object
 * @param: a #MgParameter object, managed by @context
 * @alias: a #MgParameter object, of the same type as @param, or %NULL
 *
 * Stores a reference to @alias in @context to make it possible for @context's users to find a default value
 * for @param when one is required, instead of %NULL.
 *
 * @context only provides a storage functionnality, the way the value obtained with 
 * mg_context_get_param_default_value() is used is up to @context's user.
 */
void
mg_context_set_param_default_alias (MgContext *context, MgParameter *param, MgParameter *alias)
{
	MgParameter *oldalias;

	g_return_if_fail (context && IS_MG_CONTEXT (context));
	g_return_if_fail (param && IS_MG_PARAMETER (param));
	g_return_if_fail (g_slist_find (context->parameters, param));

	/* remove the old alias if there was one */
	oldalias = g_hash_table_lookup (context->priv->param_default_aliases, param);
	if (oldalias)
		nullified_alias_cb (oldalias, context);

	/* take care of new alias if there is one */
	if (alias) {
		g_return_if_fail (alias != param);
		g_return_if_fail (alias && IS_MG_PARAMETER (alias));
		g_return_if_fail (mg_parameter_get_data_type (param) == 
				  mg_parameter_get_data_type (alias));
		g_hash_table_insert (context->priv->param_default_aliases, param, alias);
		g_hash_table_insert (context->priv->aliases_default_param, alias, param);
		g_signal_connect (G_OBJECT (alias), "nullified",
				  G_CALLBACK (nullified_alias_cb), context);
		g_object_ref (G_OBJECT (alias));
	}
}

static void
nullified_alias_cb (MgParameter *alias, MgContext *context)
{
	MgParameter *param;

	g_signal_handlers_disconnect_by_func (G_OBJECT (alias),
					      G_CALLBACK (nullified_alias_cb), context);
	param = g_hash_table_lookup (context->priv->aliases_default_param, alias);
	g_hash_table_remove (context->priv->param_default_aliases, param);
	g_hash_table_remove (context->priv->aliases_default_param, alias);
	g_object_unref (G_OBJECT (alias));
}

static void
foreach_finalize_alias (MgParameter *param, MgParameter *alias, MgContext *context)
{
	g_signal_handlers_disconnect_by_func (G_OBJECT (alias),
					      G_CALLBACK (nullified_alias_cb), context);
	g_object_unref (G_OBJECT (alias));
}

/**
 * mg_context_get_param_default_value
 * @context: a #MgContext object
 * @param: a #MgParameter object, managed by @context
 *
 * Retreives a prefered default value to be used by @context's users when necessary.
 * The usage of such values is decided by @context's users.
 * 
 * @context only offers to store the value
 * using mg_context_set_param_default_value() or to store a #MgParameter reference from which to get
 * a value using mg_context_set_param_default_alias().
 *
 * Returns: a #GdaValue, or %NULL.
 */
const GdaValue *
mg_context_get_param_default_value  (MgContext *context, MgParameter *param)
{
	const GdaValue *value;
	g_return_val_if_fail (context && IS_MG_CONTEXT (context), NULL);
	g_return_val_if_fail (context->priv, NULL);
	g_return_val_if_fail (param && IS_MG_PARAMETER (param), NULL);

	value = g_hash_table_lookup (context->priv->param_default_values, param);
	if (value)
		return value;
	else {
		MgParameter *alias;
		
		alias = g_hash_table_lookup (context->priv->param_default_aliases, param);
		if (alias && mg_parameter_is_valid (alias))
			return mg_parameter_get_value (alias);
	}

	return NULL;
}

#ifdef debug
static void
mg_context_dump (MgContext *context, guint offset)
{
	gchar *str;
        guint i;
	
	g_return_if_fail (context && IS_MG_CONTEXT (context));

        /* string for the offset */
        str = g_new0 (gchar, offset+1);
        for (i=0; i<offset; i++)
                str[i] = ' ';
        str[offset] = 0;

        /* dump */
        if (context->priv) {
		GSList *list;
                g_print ("%s" D_COL_H1 "MgContext" D_COL_NOR " %p (id=%d)\n",
                         str, context, mg_base_get_id (MG_BASE (context)));

		g_print ("%s     ***** Parameters:\n", str);
		list = context->parameters;
		while (list) {
			mg_base_dump (MG_BASE (list->data), offset+5);
			list = g_slist_next (list);
		}

		g_print ("\n%s     ***** Nodes:\n", str);
		list = context->nodes;
		while (list) {
			MgContextNode *node = MG_CONTEXT_NODE (list->data);
			if (node->param) 
				g_print ("%s     * " D_COL_H1 "MgContextNode" D_COL_NOR " for param %p\n", str, node->param);
			else {
				GSList *params = node->params;
				g_print ("%s     * " D_COL_H1 "MgContextNode" D_COL_NOR "\n", str);
				mg_base_dump (MG_BASE (node->query), offset+10);
				while (params) {
					g_print ("%s       -> param %p (%s)\n", str, params->data, 
						 mg_base_get_name (MG_BASE (params->data)));
					params = g_slist_next (params);
				}
			}
			list = g_slist_next (list);
		}
	}
        else
                g_print ("%s" D_COL_ERR "Using finalized object %p" D_COL_NOR, str, context);
}
#endif