Example #1
0
void
GncSqlBillTermBackend::load_all (GncSqlBackend* sql_be)
{

    g_return_if_fail (sql_be != NULL);

    std::stringstream sql;
    sql << "SELECT * FROM " << TABLE_NAME;
    auto stmt = sql_be->create_statement_from_sql(sql.str());
    auto result = sql_be->execute_select_statement(stmt);
    InstanceVec instances;
    BillTermParentGuidVec l_billterms_needing_parents;

    for (auto row : *result)
    {
        auto pBillTerm =
            load_single_billterm (sql_be, row, l_billterms_needing_parents);
        if (pBillTerm != nullptr)
            instances.push_back(QOF_INSTANCE(pBillTerm));
    }

    if (!instances.empty())
        gnc_sql_slots_load_for_instancevec (sql_be, instances);

    /* While there are items on the list of billterms needing parents,
       try to see if the parent has now been loaded.  Theory says that if
       items are removed from the front and added to the back if the
       parent is still not available, then eventually, the list will
       shrink to size 0. */
    if (!l_billterms_needing_parents.empty())
    {
        bool progress_made = true;
	std::reverse(l_billterms_needing_parents.begin(),
		     l_billterms_needing_parents.end());
	auto end = l_billterms_needing_parents.end();
        while (progress_made)
        {
            progress_made = false;
            end = std::remove_if(l_billterms_needing_parents.begin(), end,
				 [&](BillTermParentGuidPtr s)
				 {
				     auto pBook = qof_instance_get_book (QOF_INSTANCE (s->billterm));
				     auto parent = gncBillTermLookup (pBook,
								      &s->guid);
				     if (parent != nullptr)
				     {
					 gncBillTermSetParent (s->billterm, parent);
					 gncBillTermSetChild (parent, s->billterm);
					 progress_made = true;
					 delete s;
					 return true;
				     }
				     return false;
				 });
        }
    }
}
Example #2
0
GncBillTerm *gncBillTermReturnChild (GncBillTerm *term, gboolean make_new)
{
    GncBillTerm *child = NULL;

    if (!term) return NULL;
    if (term->child) return term->child;
    if (term->parent || term->invisible) return term;
    if (make_new)
    {
        child = gncBillTermCopy (term);
        gncBillTermSetChild (term, child);
        gncBillTermSetParent (child, term);
    }
    return child;
}
static void
billterm_scrub (QofBook *book)
{
    GList *list = NULL;
    GList *node;
    GncBillTerm *parent, *term;
    GHashTable *ht = g_hash_table_new(g_direct_hash, g_direct_equal);

    DEBUG("scrubbing billterms...");
    qof_object_foreach (GNC_ID_INVOICE,  book, billterm_scrub_invoices, ht);
    qof_object_foreach (GNC_ID_CUSTOMER, book, billterm_scrub_cust, ht);
    qof_object_foreach (GNC_ID_VENDOR,   book, billterm_scrub_vendor, ht);
    qof_object_foreach (GNC_ID_BILLTERM, book, billterm_scrub_cb, &list);

    /* destroy the list of "grandchildren" bill terms */
    for (node = list; node; node = node->next)
    {
        gchar termstr[GUID_ENCODING_LENGTH+1];
        term = node->data;

        guid_to_string_buff(qof_instance_get_guid(QOF_INSTANCE(term)), termstr);
        PWARN ("deleting grandchild billterm: %s\n", termstr);

        /* Make sure the parent has no children */
        parent = gncBillTermGetParent(term);
        gncBillTermSetChild(parent, NULL);

        /* Destroy this bill term */
        gncBillTermBeginEdit(term);
        gncBillTermDestroy(term);
    }

    /* reset the refcounts as necessary */
    g_hash_table_foreach(ht, billterm_reset_refcount, NULL);

    g_list_free(list);
    g_hash_table_destroy(ht);
}
Example #4
0
static void
bt_set_parent (gpointer data, gpointer value)
{
    GncBillTerm* billterm;
    GncBillTerm* parent;
    QofBook* pBook;
    GncGUID* guid = (GncGUID*)value;

    g_return_if_fail (data != NULL);
    g_return_if_fail (GNC_IS_BILLTERM (data));

    billterm = GNC_BILLTERM (data);
    pBook = qof_instance_get_book (QOF_INSTANCE (billterm));
    if (guid != NULL)
    {
        parent = gncBillTermLookup (pBook, guid);
        if (parent != NULL)
        {
            gncBillTermSetParent (billterm, parent);
            gncBillTermSetChild (parent, billterm);
        }
    }
}