QOF
0.7.5
|
00001 /*************************************************************************** 00002 * qofreference.c 00003 * 00004 * Mon Feb 13 21:06:44 2006 00005 * Copyright 2006 Neil Williams 00006 * linux@codehelp.co.uk 00007 ****************************************************************************/ 00008 /* 00009 * This program is free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00022 */ 00023 00024 #include "config.h" 00025 #include <glib.h> 00026 #include "qofreference.h" 00027 00028 static void 00029 entity_set_reference_cb (QofEntity * ent, gpointer user_data) 00030 { 00031 void (*reference_setter) (QofEntity *, QofEntity *); 00032 void (*choice_setter) (QofEntity *, QofEntity *); 00033 void (*collect_setter) (QofEntity *, QofCollection *); 00034 QofEntityReference *ref; 00035 GList *book_ref_list; 00036 QofCollection *coll; 00037 QofIdType type; 00038 QofEntity *reference; 00039 QofBook *partial_book; 00040 00041 partial_book = (QofBook *) user_data; 00042 g_return_if_fail (partial_book || ent); 00043 reference = NULL; 00044 coll = NULL; 00045 book_ref_list = qof_book_get_data (partial_book, ENTITYREFERENCE); 00046 while (book_ref_list) 00047 { 00048 ref = (QofEntityReference *) book_ref_list->data; 00049 if (0 == guid_compare (ref->ref_guid, qof_entity_get_guid (ent))) 00050 { 00051 /* avoid setting the entity's own guid as a reference. */ 00052 book_ref_list = g_list_next (book_ref_list); 00053 continue; 00054 } 00055 if (qof_object_is_choice (ent->e_type)) 00056 { 00057 type = ref->choice_type; 00058 } 00059 type = ref->param->param_type; 00060 coll = qof_book_get_collection (partial_book, type); 00061 reference = qof_collection_lookup_entity (coll, ref->ref_guid); 00062 reference_setter = 00063 (void (*)(QofEntity *, QofEntity *)) ref->param->param_setfcn; 00064 if ((reference) && (reference_setter)) 00065 { 00066 qof_util_param_edit ((QofInstance *) ent, ref->param); 00067 qof_util_param_edit ((QofInstance *) reference, ref->param); 00068 reference_setter (ent, reference); 00069 qof_util_param_commit ((QofInstance *) ent, ref->param); 00070 qof_util_param_commit ((QofInstance *) reference, ref->param); 00071 } 00072 /* collect and choice handling */ 00073 collect_setter = 00074 (void (*)(QofEntity *, 00075 QofCollection *)) ref->param->param_setfcn; 00076 choice_setter = 00077 (void (*)(QofEntity *, QofEntity *)) ref->param->param_setfcn; 00078 if ((0 == safe_strcmp (ref->param->param_type, QOF_TYPE_COLLECT)) 00079 && (0 == guid_compare (qof_entity_get_guid (ent), 00080 ref->ent_guid)) 00081 && (0 == safe_strcmp (ref->type, ent->e_type))) 00082 { 00083 QofCollection *temp_col; 00084 gchar cm_sa[GUID_ENCODING_LENGTH + 1]; 00085 00086 temp_col = ref->param->param_getfcn (ent, ref->param); 00087 coll = qof_book_get_collection (partial_book, 00088 qof_collection_get_type (temp_col)); 00089 guid_to_string_buff (ref->ref_guid, cm_sa); 00090 reference = qof_collection_lookup_entity (coll, ref->ref_guid); 00091 if (reference) 00092 { 00093 qof_collection_add_entity (temp_col, reference); 00094 qof_util_param_edit ((QofInstance *) ent, ref->param); 00095 qof_util_param_edit ((QofInstance *) reference, ref->param); 00096 if (collect_setter) 00097 { 00098 collect_setter (ent, temp_col); 00099 } 00100 qof_util_param_commit ((QofInstance *) ent, ref->param); 00101 qof_util_param_commit ((QofInstance *) reference, ref->param); 00102 qof_collection_destroy (temp_col); 00103 } 00104 } 00105 if (0 == safe_strcmp (ref->param->param_type, QOF_TYPE_CHOICE)) 00106 { 00107 coll = qof_book_get_collection (partial_book, ref->type); 00108 reference = qof_collection_lookup_entity (coll, ref->ref_guid); 00109 qof_util_param_edit ((QofInstance *) ent, ref->param); 00110 qof_util_param_edit ((QofInstance *) reference, ref->param); 00111 if (choice_setter) 00112 { 00113 choice_setter (ent, reference); 00114 } 00115 qof_util_param_commit ((QofInstance *) ent, ref->param); 00116 qof_util_param_commit ((QofInstance *) reference, ref->param); 00117 } 00118 book_ref_list = g_list_next (book_ref_list); 00119 } 00120 } 00121 00122 static void 00123 set_each_type (QofObject * obj, gpointer user_data) 00124 { 00125 QofBook *book; 00126 00127 book = (QofBook *) user_data; 00128 qof_object_foreach (obj->e_type, book, entity_set_reference_cb, book); 00129 } 00130 00131 static QofEntityReference * 00132 create_reference (QofEntity * ent, const QofParam * param) 00133 { 00134 QofEntityReference *reference; 00135 QofEntity *ref_ent; 00136 const GUID *cm_guid; 00137 char cm_sa[GUID_ENCODING_LENGTH + 1]; 00138 gchar *cm_string; 00139 00140 ref_ent = (QofEntity *) param->param_getfcn (ent, param); 00141 if (!ref_ent) 00142 { 00143 return NULL; 00144 } 00145 reference = g_new0 (QofEntityReference, 1); 00146 reference->type = ent->e_type; 00147 reference->ref_guid = g_new (GUID, 1); 00148 reference->ent_guid = &ent->guid; 00149 if (qof_object_is_choice (ent->e_type)) 00150 { 00151 reference->choice_type = ref_ent->e_type; 00152 } 00153 reference->param = param; 00154 cm_guid = qof_entity_get_guid (ref_ent); 00155 guid_to_string_buff (cm_guid, cm_sa); 00156 cm_string = g_strdup (cm_sa); 00157 if (TRUE == string_to_guid (cm_string, reference->ref_guid)) 00158 { 00159 g_free (cm_string); 00160 return reference; 00161 } 00162 g_free (cm_string); 00163 return NULL; 00164 } 00165 00166 QofEntityReference * 00167 qof_entity_get_reference_from (QofEntity * ent, const QofParam * param) 00168 { 00169 g_return_val_if_fail (param, NULL); 00170 param = qof_class_get_parameter (ent->e_type, param->param_name); 00171 g_return_val_if_fail (0 != 00172 safe_strcmp (param->param_type, QOF_TYPE_COLLECT), NULL); 00173 return create_reference (ent, param); 00174 } 00175 00176 void 00177 qof_book_set_references (QofBook * book) 00178 { 00179 gboolean partial; 00180 00181 partial = 00182 (gboolean) 00183 GPOINTER_TO_INT (qof_book_get_data (book, PARTIAL_QOFBOOK)); 00184 g_return_if_fail (partial); 00185 qof_object_foreach_type (set_each_type, book); 00186 }