static void qof_instance_list_foreach(gpointer data, gpointer user_data) { QofInstanceCopyData *qecd; QofInstance *original; QofInstance *inst; QofBook *book; const GncGUID *g; g_return_if_fail(data != NULL); original = QOF_INSTANCE(data); g_return_if_fail(user_data != NULL); qecd = (QofInstanceCopyData*)user_data; if (qof_instance_guid_match(qecd->new_session, original)) { return; } qecd->from = original; if (!qof_object_compliance(original->e_type, FALSE)) { qecd->error = TRUE; return; } book = qof_session_get_book(qecd->new_session); inst = (QofInstance*)qof_object_new_instance(original->e_type, book); if (!inst) { PERR (" failed to create new entity type=%s.", original->e_type); qecd->error = TRUE; return; } qecd->to = inst; g = qof_instance_get_guid(original); qof_instance_set_guid(qecd->to, g); if (qecd->param_list != NULL) { g_slist_free(qecd->param_list); qecd->param_list = NULL; } qof_class_param_foreach(original->e_type, qof_instance_param_cb, qecd); qof_begin_edit(inst); g_slist_foreach(qecd->param_list, qof_instance_foreach_copy, qecd); qof_commit_edit(inst); }
gboolean qof_instance_copy_to_session(QofSession* new_session, QofInstance* original) { QofInstanceCopyData qecd; QofInstance *inst; QofBook *book; if (!new_session || !original) { return FALSE; } if (qof_instance_guid_match(new_session, original)) { return FALSE; } if (!qof_object_compliance(original->e_type, TRUE)) { return FALSE; } qof_event_suspend(); qecd.param_list = NULL; book = qof_session_get_book(new_session); qecd.new_session = new_session; qof_book_set_partial(book); inst = (QofInstance*)qof_object_new_instance(original->e_type, book); qecd.to = inst; qecd.from = original; qof_instance_set_guid(qecd.to, qof_instance_get_guid(original)); qof_begin_edit(inst); qof_class_param_foreach(original->e_type, qof_instance_param_cb, &qecd); qof_commit_edit(inst); if (g_slist_length(qecd.param_list) == 0) { return FALSE; } g_slist_foreach(qecd.param_list, qof_instance_foreach_copy, &qecd); g_slist_free(qecd.param_list); qof_event_resume(); return TRUE; }
static void create_data (QofSession *original, guint counter) { QofCollection *coll; QofBook *start; mygrand *grand1; myparent *parent1; mychild *child1; start = qof_session_get_book(original); grand1 = (mygrand*)qof_object_new_instance(GRAND_MODULE_NAME, start); do_test ((NULL != &grand1->inst), "instance init"); switch (counter) { case 0 : /* NULL tree */ { do_test((grand1 != NULL), "empty tree check"); coll = qof_book_get_collection(start, GRAND_MODULE_NAME); do_test((qof_collection_count(coll) == 1), "Too many grandparents found - should be 1"); coll = qof_book_get_collection(start, CHILD_MODULE_NAME); do_test((qof_collection_count(coll) == 0), "child found, should be empty"); coll = qof_book_get_collection(start, PARENT_MODULE_NAME); do_test((qof_collection_count(coll) == 0), "tree not empty: parent found"); break; } case 1 : /* one parent, no child */ { parent1 = (myparent*)qof_object_new_instance(PARENT_MODULE_NAME, start); grand_setChild(grand1, parent1); do_test((parent1 != NULL), "single parent check"); do_test((grand_getChild(grand1) == parent1), "set child in grandparent"); coll = qof_book_get_collection(start, GRAND_MODULE_NAME); do_test((qof_collection_count(coll) == 1), "Wrong number of grandparents, should be 1"); coll = qof_book_get_collection(start, CHILD_MODULE_NAME); do_test((qof_collection_count(coll) == 0), "Should be no child entities this iteration."); coll = qof_book_get_collection(start, PARENT_MODULE_NAME); do_test((qof_collection_count(coll) == 1), "Wrong number of parents found, should be 1"); break; } case 2 : /* one parent, one child */ { parent1 = (myparent*)qof_object_new_instance(PARENT_MODULE_NAME, start); grand_setChild(grand1, parent1); child1 = (mychild*)qof_object_new_instance(CHILD_MODULE_NAME, start); parent1 = grand_getChild(grand1); parent_setChild(parent1, child1); do_test((child1 != NULL), "one parent with one related child"); do_test((child1 == parent_getChild(parent1)), "child of single parent"); coll = qof_book_get_collection(start, GRAND_MODULE_NAME); do_test((qof_collection_count(coll) == 1), "Wrong number of grandparents. Should be 1"); coll = qof_book_get_collection(start, CHILD_MODULE_NAME); do_test((qof_collection_count(coll) == 1), "Wrong number of child entities, should be 1"); coll = qof_book_get_collection(start, PARENT_MODULE_NAME); do_test((qof_collection_count(coll) == 1), "Wrong number of parents. Should be 1"); break; } case 3 : /* same grand, new parent, same child */ { child1 = (mychild*)qof_object_new_instance(CHILD_MODULE_NAME, start); parent1 = (myparent*)qof_object_new_instance(PARENT_MODULE_NAME, start); grand_setChild(grand1, parent1); parent_setChild(parent1, child1); do_test((parent1 == grand_getChild(grand1)), "same grandparent, new parent"); do_test((child1 == parent_getChild(parent1)), "new parent, same child"); coll = qof_book_get_collection(start, GRAND_MODULE_NAME); do_test((qof_collection_count(coll) == 1), "Wrong number of grandparents. Should be 1, Iteration 3."); coll = qof_book_get_collection(start, CHILD_MODULE_NAME); do_test((qof_collection_count(coll) == 1), "Wrong number of child entities, should be 1. Iteration 3."); coll = qof_book_get_collection(start, PARENT_MODULE_NAME); do_test((qof_collection_count(coll) == 1), "Wrong number of parents. Should be 1. Iteration 3."); break; } case 4 : /* new grand, unrelated parent, child unrelated to grand */ { grand1 = (mygrand*)qof_object_new_instance(GRAND_MODULE_NAME, start); parent1 = (myparent*)qof_object_new_instance(PARENT_MODULE_NAME, start); child1 = (mychild*)qof_object_new_instance(CHILD_MODULE_NAME, start); parent_setChild(parent1, child1); do_test((NULL == grand_getChild(grand1)), "new grand, unrelated parent"); do_test((child1 == parent_getChild(parent1)), "child unrelated to grand"); coll = grand_getDescend(grand1); do_test((coll != NULL), "grandparent not valid"); if (coll) { QofInstance *ent; ent = QOF_INSTANCE(child1); qof_collection_add_entity(coll, ent); grand_setDescend(grand1, coll); qof_collection_destroy(coll); do_test((g_list_length(grand1->descend) > 0), "entity not added"); do_test((qof_collection_count(grand_getDescend(grand1)) > 0), "empty collection returned"); } break; } } }