QOF: qofreference.c Source File

QOF 0.7.5

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

qofreference.c
1 /***************************************************************************
2 * qofreference.c
3 *
4 * Mon Feb 13 21:06:44 2006
5 * Copyright 2006 Neil Williams
6 * linux@codehelp.co.uk
7 ****************************************************************************/
8 /*
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 */
23
24 #include "config.h"
25 #include <glib.h>
26 #include "qofreference.h"
27
28 static void
29 entity_set_reference_cb (QofEntity * ent, gpointer user_data)
30 {
31 void (*reference_setter) (QofEntity *, QofEntity *);
32 void (*choice_setter) (QofEntity *, QofEntity *);
33 void (*collect_setter) (QofEntity *, QofCollection *);
34 QofEntityReference *ref;
35 GList *book_ref_list;
36 QofCollection *coll;
37 QofIdType type;
38 QofEntity *reference;
39 QofBook *partial_book;
40
41 partial_book = (QofBook *) user_data;
42 g_return_if_fail (partial_book || ent);
43 reference = NULL;
44 coll = NULL;
45 book_ref_list = qof_book_get_data (partial_book, ENTITYREFERENCE);
46 while (book_ref_list)
47 {
48 ref = (QofEntityReference *) book_ref_list->data;
49 if (0 == guid_compare (ref->ref_guid, qof_entity_get_guid (ent)))
50 {
51 /* avoid setting the entity's own guid as a reference. */
52 book_ref_list = g_list_next (book_ref_list);
53 continue;
54 }
55 if (qof_object_is_choice (ent->e_type))
56 {
57 type = ref->choice_type;
58 }
59 type = ref->param->param_type;
60 coll = qof_book_get_collection (partial_book, type);
61 reference = qof_collection_lookup_entity (coll, ref->ref_guid);
62 reference_setter =
63 (void (*)(QofEntity *, QofEntity *)) ref->param->param_setfcn;
64 if ((reference) && (reference_setter))
65 {
66 qof_util_param_edit ((QofInstance *) ent, ref->param);
67 qof_util_param_edit ((QofInstance *) reference, ref->param);
68 reference_setter (ent, reference);
69 qof_util_param_commit ((QofInstance *) ent, ref->param);
70 qof_util_param_commit ((QofInstance *) reference, ref->param);
71 }
72 /* collect and choice handling */
73 collect_setter =
74 (void (*)(QofEntity *,
75 QofCollection *)) ref->param->param_setfcn;
76 choice_setter =
77 (void (*)(QofEntity *, QofEntity *)) ref->param->param_setfcn;
78 if ((0 == safe_strcmp (ref->param->param_type, QOF_TYPE_COLLECT))
79 && (0 == guid_compare (qof_entity_get_guid (ent),
80 ref->ent_guid))
81 && (0 == safe_strcmp (ref->type, ent->e_type)))
82 {
83 QofCollection *temp_col;
84 gchar cm_sa[GUID_ENCODING_LENGTH + 1];
85
86 temp_col = ref->param->param_getfcn (ent, ref->param);
87 coll = qof_book_get_collection (partial_book,
88 qof_collection_get_type (temp_col));
89 guid_to_string_buff (ref->ref_guid, cm_sa);
90 reference = qof_collection_lookup_entity (coll, ref->ref_guid);
91 if (reference)
92 {
93 qof_collection_add_entity (temp_col, reference);
94 qof_util_param_edit ((QofInstance *) ent, ref->param);
95 qof_util_param_edit ((QofInstance *) reference, ref->param);
96 if (collect_setter)
97 {
98 collect_setter (ent, temp_col);
99 }
100 qof_util_param_commit ((QofInstance *) ent, ref->param);
101 qof_util_param_commit ((QofInstance *) reference, ref->param);
102 qof_collection_destroy (temp_col);
103 }
104 }
105 if (0 == safe_strcmp (ref->param->param_type, QOF_TYPE_CHOICE))
106 {
107 coll = qof_book_get_collection (partial_book, ref->type);
108 reference = qof_collection_lookup_entity (coll, ref->ref_guid);
109 qof_util_param_edit ((QofInstance *) ent, ref->param);
110 qof_util_param_edit ((QofInstance *) reference, ref->param);
111 if (choice_setter)
112 {
113 choice_setter (ent, reference);
114 }
115 qof_util_param_commit ((QofInstance *) ent, ref->param);
116 qof_util_param_commit ((QofInstance *) reference, ref->param);
117 }
118 book_ref_list = g_list_next (book_ref_list);
119 }
120 }
121
122 static void
123 set_each_type (QofObject * obj, gpointer user_data)
124 {
125 QofBook *book;
126
127 book = (QofBook *) user_data;
128 qof_object_foreach (obj->e_type, book, entity_set_reference_cb, book);
129 }
130
131 static QofEntityReference *
132 create_reference (QofEntity * ent, const QofParam * param)
133 {
134 QofEntityReference *reference;
135 QofEntity *ref_ent;
136 const GUID *cm_guid;
137 char cm_sa[GUID_ENCODING_LENGTH + 1];
138 gchar *cm_string;
139
140 ref_ent = (QofEntity *) param->param_getfcn (ent, param);
141 if (!ref_ent)
142 {
143 return NULL;
144 }
145 reference = g_new0 (QofEntityReference, 1);
146 reference->type = ent->e_type;
147 reference->ref_guid = g_new (GUID, 1);
148 reference->ent_guid = &ent->guid;
149 if (qof_object_is_choice (ent->e_type))
150 {
151 reference->choice_type = ref_ent->e_type;
152 }
153 reference->param = param;
154 cm_guid = qof_entity_get_guid (ref_ent);
155 guid_to_string_buff (cm_guid, cm_sa);
156 cm_string = g_strdup (cm_sa);
157 if (TRUE == string_to_guid (cm_string, reference->ref_guid))
158 {
159 g_free (cm_string);
160 return reference;
161 }
162 g_free (cm_string);
163 return NULL;
164 }
165
166 QofEntityReference *
167 qof_entity_get_reference_from (QofEntity * ent, const QofParam * param)
168 {
169 g_return_val_if_fail (param, NULL);
170 param = qof_class_get_parameter (ent->e_type, param->param_name);
171 g_return_val_if_fail (0 !=
172 safe_strcmp (param->param_type, QOF_TYPE_COLLECT), NULL);
173 return create_reference (ent, param);
174 }
175
176 void
177 qof_book_set_references (QofBook * book)
178 {
179 gboolean partial;
180
181 partial =
182 (gboolean)
183 GPOINTER_TO_INT (qof_book_get_data (book, PARTIAL_QOFBOOK));
184 g_return_if_fail (partial);
185 qof_object_foreach_type (set_each_type, book);
186 }
qof_collection_add_entity
gboolean qof_collection_add_entity(QofCollection *coll, QofEntity *ent)
Add an entity to a QOF_TYPE_COLLECT.
Definition: qofid.c:211
qof_entity_reference::ent_guid
const GUID * ent_guid
Definition: qofreference.h:115
QOF_TYPE_COLLECT
#define QOF_TYPE_COLLECT
Definition: qofclass.h:93
qof_entity_reference::type
QofIdType type
Definition: qofreference.h:109
QofIdType
const gchar * QofIdType
Definition: qofid.h:81
QofInstance_s
Definition: qofinstance-p.h:35
qof_book_get_collection
QofCollection * qof_book_get_collection(QofBook *book, QofIdType entity_type)
Definition: qofbook.c:220
_QofParam
Definition: qofclass.h:195
QofEntity_s
Definition: qofid.h:147
qof_object_foreach
void qof_object_foreach(QofIdTypeConst type_name, QofBook *book, QofEntityForeachCB cb, gpointer user_data)
Definition: qofobject.c:174
string_to_guid
gboolean string_to_guid(const gchar *string, GUID *guid)
qof_book_get_data
gpointer qof_book_get_data(QofBook *book, const gchar *key)
Definition: qofbook.c:212
qof_entity_reference::ref_guid
GUID * ref_guid
Definition: qofreference.h:112
GUID_ENCODING_LENGTH
#define GUID_ENCODING_LENGTH
Definition: guid.h:64
QofCollection
struct QofCollection_s QofCollection
Definition: qofid.h:138
qof_class_get_parameter
const QofParam * qof_class_get_parameter(QofIdTypeConst obj_name, const gchar *parameter)
Definition: qofclass.c:146
qof_util_param_edit
gboolean qof_util_param_edit(QofInstance *inst, const QofParam *param)
Prepare to edit a parameter.
Definition: qofutil.c:278
qof_entity_reference::choice_type
QofIdType choice_type
Definition: qofreference.h:107
qof_book_set_references
void qof_book_set_references(QofBook *book)
Read QofEntityReference data for this book and set values.
Definition: qofreference.c:177
qof_collection_lookup_entity
QofEntity * qof_collection_lookup_entity(QofCollection *col, const GUID *guid)
Definition: qofid.c:321
qof_entity_get_reference_from
QofEntityReference * qof_entity_get_reference_from(QofEntity *ent, const QofParam *param)
Get a reference from this entity to another entity.
Definition: qofreference.c:167
_QofObject
Definition: qofobject.h:70
guid_to_string_buff
gchar * guid_to_string_buff(const GUID *guid, gchar *buff)
qof_util_param_commit
gboolean qof_util_param_commit(QofInstance *inst, const QofParam *param)
Commit this parameter change, with undo support.
Definition: qofutil.c:306
qof_entity_reference::param
const QofParam * param
Definition: qofreference.h:113
ENTITYREFERENCE
#define ENTITYREFERENCE
Definition: qofreference.h:137
qof_collection_get_type
QofIdType qof_collection_get_type(QofCollection *col)
Definition: qofid.c:175
_GUID
Definition: guid.h:53
qof_collection_destroy
void qof_collection_destroy(QofCollection *col)
Definition: qofid.c:161
qof_entity_get_guid
const GUID * qof_entity_get_guid(QofEntity *ent)
Definition: qofid.c:105
qofreference.h
Dealing with relationships between entities in partial books.
qof_entity_reference
External references in a partial QofBook.
Definition: qofreference.h:105
qof_object_is_choice
gboolean qof_object_is_choice(QofIdType type)
Does this object contain a choice parameter?
Definition: qofchoice.c:45
_QofBook
Definition: qofbook-p.h:48
safe_strcmp
gint safe_strcmp(const gchar *da, const gchar *db)
Definition: qofutil.c:72
QOF_TYPE_CHOICE
#define QOF_TYPE_CHOICE
Identify an object as containing a choice.
Definition: qofchoice.h:103
qof_object_foreach_type
void qof_object_foreach_type(QofForeachTypeCB cb, gpointer user_data)
Definition: qofobject.c:140
PARTIAL_QOFBOOK
#define PARTIAL_QOFBOOK
Flag indicating a partial QofBook.
Definition: qofreference.h:146