More information is at http://code.neil.williamsleesmill.me.uk/
Each foreach function uses g_return_if_fail checks to protect the target book. If any essential data is missing, the loop returns without changing the target book. Note that this will not set or return an error value. However, g_return is only used for critical errors that arise from programming errors, not for invalid import data which should be cleaned up before creating the import QofBook.
Only qof_book_merge_update_result and qof_book_merge_commit return any error values to the calling process. qof_book_merge_init returns a pointer to the QofBookMergeData struct - the calling process needs to make sure this is non-NULL to know that the Init has been successful.
Files | |
| file | qofbookmerge.h |
API for merging two QofBook structures with collision handling. | |
Data Structures | |
| struct | QofBookMergeRule |
| One rule per entity, built into a single GList for the entire merge. More... | |
| struct | QofBookMergeData |
| mergeData contains the essential context data for any merge. More... | |
qof_book_merge API | |
| typedef void(*) | QofBookMergeRuleForeachCB (QofBookMergeData *, QofBookMergeRule *, guint) |
| Definition of the dialogue control callback routine. | |
| QofBookMergeData * | qof_book_merge_init (QofBook *importBook, QofBook *targetBook) |
| Initialise the QofBookMerge process. | |
| void | qof_book_merge_rule_foreach (QofBookMergeData *mergeData, QofBookMergeRuleForeachCB callback, QofBookMergeResult mergeResult) |
| Dialogue Control Callback. | |
| gchar * | qof_book_merge_param_as_string (QofParam *qtparam, QofInstance *qtEnt) |
| provides easy string access to parameter data for dialogue use | |
| QofBookMergeData * | qof_book_merge_update_result (QofBookMergeData *mergeData, QofBookMergeResult tag) |
| called by dialogue callback to set the result of user intervention | |
| gint | qof_book_merge_commit (QofBookMergeData *mergeData) |
| Commits the import data to the target book. | |
| void | qof_book_merge_abort (QofBookMergeData *mergeData) |
| Abort the merge and free all memory allocated by the merge. | |
Enumerations | |
| enum | QofBookMergeResult { MERGE_UNDEF, MERGE_ABSOLUTE, MERGE_NEW, MERGE_REPORT, MERGE_DUPLICATE, MERGE_UPDATE, MERGE_INVALID } |
| Results of collisions and user resolution. More... | |
| typedef void(* ) QofBookMergeRuleForeachCB(QofBookMergeData *, QofBookMergeRule *, guint) |
Definition of the dialogue control callback routine.
All MERGE_REPORT rules must be offered for user intervention using this template.
Commit will fail if any rules are still tagged as MERGE_REPORT.
Calling processes are free to also offer MERGE_NEW, MERGE_UPDATE, MERGE_DUPLICATE and MERGE_ABSOLUTE for user intervention. Attempting to query MERGE_INVALID rules will cause an error.
For an example, consider test_rule_loop, declared as:
void test_rule_loop(QofBookMergeData *mergeData, QofBookMergeRule *rule, guint remainder);
void test_rule_loop(QofBookMergeData *mergeData, QofBookMergeRule *rule, guint remainder)
{
g_return_if_fail(rule != NULL);
g_return_if_fail(mergeData != NULL); printf("Rule Result %s", rule->mergeType);
qof_book_merge_update_result(mergeData, rule, MERGE_UPDATE);
}
The dialogue is free to call qof_book_merge_update_result in the loop or at the end as long as the link between the rule and the result is maintained, e.g. by using a GHashTable.
The parameters are:
If the dialogue sets any rule result to MERGE_INVALID, the import will abort when qof_book_merge_commit is called. It is the responsibility of the calling function to handle the error code from qof_book_merge_commit, close the dialogue and return. The merge routines in these files will already have halted the merge operation and freed any memory allocated to merge structures before returning the error code. There is no need for the dialogue process to report back to QofBookMerge in this situation.
Definition at line 318 of file qofbookmerge.h.
| enum QofBookMergeResult |
Results of collisions and user resolution.
All rules are initialised as MERGE_UNDEF. Once the comparison is complete, each object within the import will be updated.
MERGE_ABSOLUTE, MERGE_NEW, MERGE_DUPLICATE and MERGE_UPDATE can be reported to the user along with all MERGE_REPORT objects for confirmation. It may be useful later to allow MERGE_ABSOLUTE, MERGE_NEW, MERGE_DUPLICATE and MERGE_UPDATE to not be reported, if the user sets a preferences option for each result. (Always accept new items: Y/N default NO, ignores all MERGE_NEW if set to Y etc.) This option would not require any changes to qofbookmerge.
MERGE_NEW, MERGE_DUPLICATE and MERGE_UPDATE are only actioned after conflicts are resolved by the user using a dialog and all MERGE_REPORT objects are re-assigned to one of MERGE_NEW, MERGE_DUPLICATE or MERGE_UPDATE. There is no automatic merge, even if no entities are tagged as MERGE_REPORT, the calling process must still check for REPORT items using qof_book_merge_rule_foreach and call qof_book_merge_commit.
MERGE_INVALID data should be rare and allows for user-abort - the imported file/source may be corrupted and the prescence of invalid data should raise concerns that the rest of the data may be corrupted, damaged or otherwise altered. If any entity is tagged as MERGE_INVALID, the merge operation will abort and leave the target book completely unchanged.
MERGE_ABSOLUTE is only used for a complete match. The import object contains the same data in the same parameters with no omissions or amendments. If any data is missing, amended or added, the data is labelled MERGE_UPDATE.
Every piece of data has a corresponding result. Only when the count of items labelled MERGE_REPORT is equal to zero are MERGE_NEW and MERGE_UPDATE items added to the existing book.
MERGE_DUPLICATE items are silently ignored. Aborting the dialogue/process (by the user or in a program crash) at any point before the final commit leaves the existing book completely untouched.
Definition at line 123 of file qofbookmerge.h.
00123 { 00124 MERGE_UNDEF, 00125 MERGE_ABSOLUTE, 00126 MERGE_NEW, 00128 MERGE_REPORT, 00129 MERGE_DUPLICATE, 00131 MERGE_UPDATE, 00133 MERGE_INVALID 00135 }QofBookMergeResult;
| void qof_book_merge_abort | ( | QofBookMergeData * | mergeData | ) |
Abort the merge and free all memory allocated by the merge.
Sometimes, setting MERGE_INVALID is insufficient: e.g. if the user aborts the merge from outside the functions dealing with the merge ruleset. This function causes an immediate abort - the calling process must start again at Init if a new merge is required.
Definition at line 884 of file qofbookmerge.c.
00885 { 00886 QofBookMergeRule *currentRule; 00887 00888 g_return_if_fail(mergeData != NULL); 00889 while(mergeData->mergeList != NULL) { 00890 currentRule = mergeData->mergeList->data; 00891 g_slist_free(currentRule->linkedEntList); 00892 g_slist_free(currentRule->mergeParam); 00893 g_free(mergeData->mergeList->data); 00894 if(currentRule) { 00895 g_slist_free(currentRule->linkedEntList); 00896 g_slist_free(currentRule->mergeParam); 00897 g_free(currentRule); 00898 } 00899 mergeData->mergeList = g_list_next(mergeData->mergeList); 00900 } 00901 g_list_free(mergeData->mergeList); 00902 g_slist_free(mergeData->mergeObjectParams); 00903 g_slist_free(mergeData->targetList); 00904 if(mergeData->orphan_list != NULL) { g_slist_free(mergeData->orphan_list); } 00905 g_hash_table_destroy(mergeData->target_table); 00906 g_free(mergeData); 00907 }
| gint qof_book_merge_commit | ( | QofBookMergeData * | mergeData | ) |
Commits the import data to the target book.
The last function in the API and the final part of any QofBookMerge operation.
qof_book_merge_commit will abort the entire merge operation if any rule is set to MERGE_INVALID. It is the responsibility of the calling function to handle the error code from qof_book_merge_commit, close the dialogue and return. qof_book_merge_commit will already have halted the merge operation and freed any memory allocated to all merge structures before returning the error code. There is no way for the dialogue process to report back to qof_book_merge in this situation.
qof_book_merge_commit checks for any entities still tagged as MERGE_REPORT and then proceeds to import all entities tagged as MERGE_UPDATE or MERGE_NEW into the target book.
This final process cannot be UNDONE!
| mergeData | the merge context, QofBookMergeData* |
Definition at line 1044 of file qofbookmerge.c.
01045 { 01046 QofBookMergeRule *currentRule; 01047 GList *check, *node; 01048 01049 ENTER (" "); 01050 01051 g_return_val_if_fail(mergeData != NULL, -10); 01052 g_return_val_if_fail(mergeData->mergeList != NULL, -11); 01053 g_return_val_if_fail(mergeData->targetBook != NULL, -12); 01054 if(mergeData->abort == TRUE) return -13; 01055 check = g_list_copy(mergeData->mergeList); 01056 g_return_val_if_fail(check != NULL, -14); 01057 for (node = check; node != NULL; node = node->next) { 01058 currentRule = node->data; 01059 01060 if(currentRule->mergeResult == MERGE_INVALID) { 01061 qof_book_merge_abort(mergeData); 01062 g_list_free(check); 01063 return(-2); 01064 } 01065 if(currentRule->mergeResult == MERGE_REPORT) { 01066 g_list_free(check); 01067 return 1; 01068 } 01069 } 01070 g_list_free(check); 01071 qof_book_merge_commit_foreach(qof_book_merge_commit_rule_create_objects, 01072 MERGE_NEW, mergeData); 01073 qof_book_merge_commit_foreach(qof_book_merge_commit_rule_loop, 01074 MERGE_NEW, mergeData); 01075 qof_book_merge_commit_foreach(qof_book_merge_commit_rule_loop, 01076 MERGE_UPDATE, mergeData); 01077 01078 /* Placeholder for QofObject merge_helper_cb - all objects 01079 and all parameters set */ 01080 while(mergeData->mergeList != NULL) { 01081 currentRule = mergeData->mergeList->data; 01082 g_slist_free(currentRule->mergeParam); 01083 g_slist_free(currentRule->linkedEntList); 01084 mergeData->mergeList = g_list_next(mergeData->mergeList); 01085 } 01086 g_list_free(mergeData->mergeList); 01087 g_slist_free(mergeData->mergeObjectParams); 01088 g_slist_free(mergeData->targetList); 01089 if(mergeData->orphan_list != NULL) { g_slist_free(mergeData->orphan_list); } 01090 g_hash_table_destroy(mergeData->target_table); 01091 g_free(mergeData); 01092 01093 LEAVE (" "); 01094 return 0; 01095 }
| QofBookMergeData* qof_book_merge_init | ( | QofBook * | importBook, | |
| QofBook * | targetBook | |||
| ) |
Initialise the QofBookMerge process.
First function of the QofBookMerge API. Every merge must begin with init.
Requires the book to import (QofBook *) and the book to receive the import, the target book (QofBook *). Returns a pointer to QofBookMergeData which must be checked for a NULL before continuing.
Process:
Definition at line 845 of file qofbookmerge.c.
00846 { 00847 QofBookMergeData *mergeData; 00848 QofBookMergeRule *currentRule; 00849 GList *node; 00850 00851 ENTER (" "); 00852 00853 g_return_val_if_fail((importBook != NULL)&&(targetBook != NULL), NULL); 00854 mergeData = g_new0(QofBookMergeData, 1); 00855 mergeData->abort = FALSE; 00856 mergeData->mergeList = NULL; 00857 mergeData->targetList = NULL; 00858 mergeData->mergeBook = importBook; 00859 mergeData->targetBook = targetBook; 00860 mergeData->mergeObjectParams = NULL; 00861 mergeData->orphan_list = NULL; 00862 mergeData->target_table = g_hash_table_new( g_direct_hash, qof_book_merge_rule_cmp); 00863 currentRule = g_new0(QofBookMergeRule, 1); 00864 mergeData->currentRule = currentRule; 00865 qof_object_foreach_type(qof_book_merge_foreach_type, mergeData); 00866 g_return_val_if_fail(mergeData->mergeObjectParams, NULL); 00867 if(mergeData->orphan_list != NULL) { 00868 qof_book_merge_match_orphans(mergeData); 00869 } 00870 00871 for (node = mergeData->mergeList; node != NULL; node = node->next) { 00872 currentRule = node->data; 00873 if(currentRule->mergeResult == MERGE_INVALID) { 00874 mergeData->abort = TRUE; 00875 return(NULL); 00876 } 00877 } 00878 00879 LEAVE (" "); 00880 return mergeData; 00881 }
| gchar* qof_book_merge_param_as_string | ( | QofParam * | qtparam, | |
| QofInstance * | qtEnt | |||
| ) |
provides easy string access to parameter data for dialogue use
Uses the param_getfcn to retrieve the parameter value as a string, suitable for display in dialogues and user intervention output. Within a QofBookMerge context, only the parameters used in the merge are available, i.e. parameters where both param_getfcn and param_setfcn are not NULL.
Note that the object type description (a full text version of the object name) is also available to the dialogue as QofBookMergeRule::mergeLabel.
This allows the dialog to display the description of the object and all parameter data.
Definition at line 925 of file qofbookmerge.c.
00926 { 00927 gchar *param_string, param_date[QOF_DATE_STRING_LENGTH]; 00928 gchar param_sa[GUID_ENCODING_LENGTH + 1]; 00929 QofType paramType; 00930 const GUID *param_guid; 00931 time_t param_t; 00932 gnc_numeric param_numeric, (*numeric_getter) (QofInstance*, QofParam*); 00933 Timespec param_ts, (*date_getter) (QofInstance*, QofParam*); 00934 double param_double, (*double_getter) (QofInstance*, QofParam*); 00935 gboolean param_boolean, (*boolean_getter) (QofInstance*, QofParam*); 00936 gint32 param_i32, (*int32_getter) (QofInstance*, QofParam*); 00937 gint64 param_i64, (*int64_getter) (QofInstance*, QofParam*); 00938 gchar param_char, (*char_getter) (QofInstance*, QofParam*); 00939 00940 param_string = NULL; 00941 paramType = qtparam->param_type; 00942 if(safe_strcmp(paramType, QOF_TYPE_STRING) == 0) { 00943 param_string = g_strdup(qtparam->param_getfcn(qtEnt,qtparam)); 00944 if(param_string == NULL) { param_string = ""; } 00945 return param_string; 00946 } 00947 if(safe_strcmp(paramType, QOF_TYPE_DATE) == 0) { 00948 date_getter = (Timespec (*)(QofInstance*, QofParam*))qtparam->param_getfcn; 00949 param_ts = date_getter(qtEnt, qtparam); 00950 param_t = timespecToTime_t(param_ts); 00951 qof_strftime(param_date, QOF_DATE_STRING_LENGTH, QOF_UTC_DATE_FORMAT, gmtime(¶m_t)); 00952 param_string = g_strdup(param_date); 00953 return param_string; 00954 } 00955 if((safe_strcmp(paramType, QOF_TYPE_NUMERIC) == 0) || 00956 (safe_strcmp(paramType, QOF_TYPE_DEBCRED) == 0)) { 00957 numeric_getter = (gnc_numeric (*)(QofInstance*, QofParam*)) qtparam->param_getfcn; 00958 param_numeric = numeric_getter(qtEnt,qtparam); 00959 param_string = g_strdup(gnc_numeric_to_string(param_numeric)); 00960 return param_string; 00961 } 00962 if(safe_strcmp(paramType, QOF_TYPE_GUID) == 0) { 00963 param_guid = qtparam->param_getfcn(qtEnt,qtparam); 00964 guid_to_string_buff(param_guid, param_sa); 00965 param_string = g_strdup(param_sa); 00966 return param_string; 00967 } 00968 if(safe_strcmp(paramType, QOF_TYPE_INT32) == 0) { 00969 int32_getter = (gint32 (*)(QofInstance*, QofParam*)) qtparam->param_getfcn; 00970 param_i32 = int32_getter(qtEnt, qtparam); 00971 param_string = g_strdup_printf("%d", param_i32); 00972 return param_string; 00973 } 00974 if(safe_strcmp(paramType, QOF_TYPE_INT64) == 0) { 00975 int64_getter = (gint64 (*)(QofInstance*, QofParam*)) qtparam->param_getfcn; 00976 param_i64 = int64_getter(qtEnt, qtparam); 00977 param_string = g_strdup_printf("%" G_GINT64_FORMAT, param_i64); 00978 return param_string; 00979 } 00980 if(safe_strcmp(paramType, QOF_TYPE_DOUBLE) == 0) { 00981 double_getter = (double (*)(QofInstance*, QofParam*)) qtparam->param_getfcn; 00982 param_double = double_getter(qtEnt, qtparam); 00983 param_string = g_strdup_printf("%f", param_double); 00984 return param_string; 00985 } 00986 if(safe_strcmp(paramType, QOF_TYPE_BOOLEAN) == 0){ 00987 boolean_getter = (gboolean (*)(QofInstance*, QofParam*)) qtparam->param_getfcn; 00988 param_boolean = boolean_getter(qtEnt, qtparam); 00989 /* Boolean values need to be lowercase for QSF validation. */ 00990 if(param_boolean == TRUE) { param_string = g_strdup("true"); } 00991 else { param_string = g_strdup("false"); } 00992 return param_string; 00993 } 00994 /* "kvp" contains repeating values, cannot be a single string for the frame. */ 00995 if(safe_strcmp(paramType, QOF_TYPE_KVP) == 0) { return param_string; } 00996 if(safe_strcmp(paramType, QOF_TYPE_CHAR) == 0) { 00997 char_getter = (gchar (*)(QofInstance*, QofParam*)) qtparam->param_getfcn; 00998 param_char = char_getter(qtEnt, qtparam); 00999 param_string = g_strdup_printf("%c", param_char); 01000 return param_string; 01001 } 01002 return NULL; 01003 }
| void qof_book_merge_rule_foreach | ( | QofBookMergeData * | mergeData, | |
| QofBookMergeRuleForeachCB | callback, | |||
| QofBookMergeResult | mergeResult | |||
| ) |
Dialogue Control Callback.
This function is designed to be used to iterate over all rules tagged with a specific QofBookMergeResult value.
| callback | external loop of type QofBookMergeRuleForeachCB | |
| mergeResult | QofBookMergeResult value to look up. | |
| mergeData | QofBookMergeData merge context. |
Uses qof_book_get_collection with the QofBookMergeRule::mergeType object type to return a collection of QofInstance entities from either the QofBookMergeData::mergeBook or QofBookMergeData::targetBook. Then uses qof_collection_lookup_entity to lookup the QofBookMergeRule::importEnt and again the QofBookMergeRule::targetEnt to return the two specific entities.
Definition at line 1098 of file qofbookmerge.c.
01101 { 01102 struct QofBookMergeRuleIterate iter; 01103 QofBookMergeRule *currentRule; 01104 GList *matching_rules, *node; 01105 01106 g_return_if_fail(cb != NULL); 01107 g_return_if_fail(mergeData != NULL); 01108 currentRule = mergeData->currentRule; 01109 g_return_if_fail(mergeResult > 0); 01110 g_return_if_fail(mergeResult != MERGE_INVALID); 01111 g_return_if_fail(mergeData->abort == FALSE); 01112 iter.fcn = cb; 01113 iter.data = mergeData; 01114 matching_rules = NULL; 01115 iter.ruleList = NULL; 01116 for (node = mergeData->mergeList; node != NULL; node = node->next) { 01117 currentRule = node->data; 01118 if(currentRule->mergeResult == mergeResult) { 01119 matching_rules = g_list_prepend(matching_rules, currentRule); 01120 } 01121 } 01122 iter.remainder = g_list_length(matching_rules); 01123 g_list_foreach (matching_rules, qof_book_merge_rule_cb, &iter); 01124 g_list_free(matching_rules); 01125 }
| QofBookMergeData* qof_book_merge_update_result | ( | QofBookMergeData * | mergeData, | |
| QofBookMergeResult | tag | |||
| ) |
called by dialogue callback to set the result of user intervention
Set any rule result to MERGE_INVALID to abort the import when qof_book_merge_commit is called, without changing the target book.
The calling process should make it absolutely clear that a merge operation cannot be undone and that a backup copy should always be available before a merge is initialised.
Recommended method: Only offer three options to the user per rule:
Handle the required result changes in code: Check the value of QofBookMergeRule::mergeAbsolute and use these principles:
To ignore entities tagged as:
To merge entities that are not pre-set to MERGE_NEW, set MERGE_UPDATE.
Attempting to merge an entity when the pre-set value was MERGE_NEW will force a change back to MERGE_NEW because no suitable target exists for the merge.
To add entities, check mergeAbsolute is FALSE and set MERGE_NEW.
An entity only be added if mergeAbsolute is FALSE. Attempting to add an entity when mergeAbsolute is TRUE will always force a MERGE_UPDATE.
It is not possible to update the same rule more than once.
qof_book_merge_commit only commits entities tagged with MERGE_NEW and MERGE_UPDATE results.
Entities tagged with MERGE_ABSOLUTE and MERGE_DUPLICATE results are ignored.
The calling process must check the return value and call qof_book_merge_abort(mergeData) if non-zero.
| mergeData | the merge context, QofBookMergeData* | |
| tag | the result to attempt to set, QofBookMergeResult |
Definition at line 1006 of file qofbookmerge.c.
01008 { 01009 QofBookMergeRule *resolved; 01010 01011 g_return_val_if_fail((mergeData != NULL), NULL); 01012 g_return_val_if_fail((tag > 0), NULL); 01013 g_return_val_if_fail((tag != MERGE_REPORT), NULL); 01014 resolved = mergeData->currentRule; 01015 g_return_val_if_fail((resolved != NULL), NULL); 01016 if((resolved->mergeAbsolute == TRUE)&&(tag == MERGE_DUPLICATE)) 01017 { 01018 tag = MERGE_ABSOLUTE; 01019 } 01020 if((resolved->mergeAbsolute == TRUE)&&(tag == MERGE_NEW)) 01021 { 01022 tag = MERGE_UPDATE; 01023 } 01024 if((resolved->mergeAbsolute == FALSE)&& (tag == MERGE_ABSOLUTE)) 01025 { 01026 tag = MERGE_DUPLICATE; 01027 } 01028 if((resolved->mergeResult == MERGE_NEW)&&(tag == MERGE_UPDATE)) 01029 { 01030 tag = MERGE_NEW; 01031 } 01032 if(resolved->updated == FALSE) { resolved->mergeResult = tag; } 01033 resolved->updated = TRUE; 01034 if(tag >= MERGE_INVALID) { 01035 mergeData->abort = TRUE; 01036 mergeData->currentRule = resolved; 01037 return NULL; 01038 } 01039 mergeData->currentRule = resolved; 01040 return mergeData; 01041 }
1.5.2