qofreference.c

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)) { type = ref->choice_type; }
00056                 type = ref->param->param_type;
00057                 coll = qof_book_get_collection(partial_book, type);
00058                 reference = qof_collection_lookup_entity(coll, ref->ref_guid);
00059                 reference_setter = (void(*)(QofEntity*, QofEntity*))ref->param->param_setfcn;
00060                 if((reference) && (reference_setter))
00061                 {
00062                         qof_begin_edit((QofInstance*)ent);
00063                         qof_begin_edit((QofInstance*)reference);
00064                         reference_setter(ent, reference);
00065                         qof_commit_edit((QofInstance*)ent);
00066                         qof_commit_edit((QofInstance*)reference);
00067                 }
00068                 /* collect and choice handling */
00069                 collect_setter = (void(*)(QofEntity*, QofCollection*))ref->param->param_setfcn;
00070                 choice_setter = (void(*)(QofEntity*, QofEntity*))ref->param->param_setfcn;
00071                 if ((0 == safe_strcmp(ref->param->param_type, QOF_TYPE_COLLECT)) &&
00072                         (0 == guid_compare(qof_entity_get_guid(ent), ref->ent_guid)) &&
00073                         (0 == safe_strcmp(ref->type, ent->e_type)))
00074                 {
00075                         QofCollection *temp_col;
00076                         char cm_sa[GUID_ENCODING_LENGTH + 1];
00077                         
00078                         temp_col = ref->param->param_getfcn(ent, ref->param);
00079                         coll = qof_book_get_collection(partial_book, 
00080                                 qof_collection_get_type(temp_col));
00081                         guid_to_string_buff(ref->ref_guid, cm_sa);
00082                         reference = qof_collection_lookup_entity(coll, ref->ref_guid);
00083                         if(reference) {
00084                                 qof_collection_add_entity(temp_col, reference);
00085                                 qof_begin_edit((QofInstance*)ent);
00086                                 qof_begin_edit((QofInstance*)reference);
00087                                 if(collect_setter) { collect_setter(ent, temp_col); }
00088                                 qof_commit_edit((QofInstance*)ent);
00089                                 qof_commit_edit((QofInstance*)reference);
00090                                 qof_collection_destroy(temp_col);
00091                         }
00092                 }
00093                 if(0 == safe_strcmp(ref->param->param_type, QOF_TYPE_CHOICE))
00094                 {
00095                         coll = qof_book_get_collection(partial_book, ref->type);
00096                         reference = qof_collection_lookup_entity(coll, ref->ref_guid);
00097                         qof_begin_edit((QofInstance*)ent);
00098                         qof_begin_edit((QofInstance*)reference);
00099                         if(choice_setter) { choice_setter(ent, reference); }
00100                         qof_commit_edit((QofInstance*)ent);
00101                         qof_commit_edit((QofInstance*)reference);
00102                 }
00103                 book_ref_list = g_list_next(book_ref_list);
00104         }
00105 }
00106 
00107 static void
00108 set_each_type(QofObject *obj, gpointer user_data)
00109 {
00110         QofBook *book;
00111 
00112         book = (QofBook*)user_data;
00113         qof_object_foreach(obj->e_type, book, entity_set_reference_cb, book);
00114 }
00115 
00116 static QofEntityReference*
00117 create_reference(QofEntity *ent, const QofParam *param)
00118 {
00119         QofEntityReference *reference;
00120         QofEntity          *ref_ent;
00121         const GUID         *cm_guid;
00122         char                cm_sa[GUID_ENCODING_LENGTH + 1];
00123         gchar              *cm_string;
00124 
00125         ref_ent = (QofEntity*)param->param_getfcn(ent, param);
00126         if(!ref_ent) { return NULL; }
00127         reference = g_new0(QofEntityReference, 1);
00128         reference->type = ent->e_type;
00129         reference->ref_guid = g_new(GUID, 1);
00130         reference->ent_guid = &ent->guid;
00131         if(qof_object_is_choice(ent->e_type)) 
00132         { 
00133                 reference->choice_type = ref_ent->e_type; 
00134         }
00135         reference->param = param;
00136         cm_guid = qof_entity_get_guid(ref_ent);
00137         guid_to_string_buff(cm_guid, cm_sa);
00138         cm_string = g_strdup(cm_sa);
00139         if(TRUE == string_to_guid(cm_string, reference->ref_guid)) {
00140                 g_free(cm_string);
00141                 return reference;
00142         }
00143         g_free(cm_string);
00144         return NULL;
00145 }
00146 
00147 QofEntityReference*
00148 qof_entity_get_reference_from(QofEntity *ent, const QofParam *param)
00149 {
00150         g_return_val_if_fail(param, NULL);
00151         param = qof_class_get_parameter(ent->e_type, param->param_name);
00152         g_return_val_if_fail(0 != safe_strcmp(param->param_type, QOF_TYPE_COLLECT), NULL);
00153         return create_reference(ent, param);
00154 }
00155 
00156 void qof_book_set_references(QofBook *book)
00157 {
00158         gboolean partial;
00159 
00160         partial =
00161           (gboolean)GPOINTER_TO_INT(qof_book_get_data(book, PARTIAL_QOFBOOK));
00162         g_return_if_fail(partial);
00163         qof_object_foreach_type(set_each_type, book);
00164 }

Generated on Fri May 12 18:00:33 2006 for QOF by  doxygen 1.4.4