/**********************************************************
 * This file has been automatically created by "typemaker2"
 * from the file "user.xml".
 * Please do not edit this file, all changes will be lost.
 * Better edit the mentioned source file instead.
 **********************************************************/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "./user_p.h"

#include <gwenhywfar/misc.h>
#include <gwenhywfar/debug.h>

/* code headers */

/* macro functions */
GWEN_LIST_FUNCTIONS(AB_USER, AB_User)
GWEN_LIST2_FUNCTIONS(AB_USER, AB_User)
GWEN_INHERIT_FUNCTIONS(AB_USER)


AB_USER *AB_User_new(void) {
  AB_USER *p_struct;

  GWEN_NEW_OBJECT(AB_USER, p_struct)
  p_struct->_refCount=1;
  GWEN_INHERIT_INIT(AB_USER, p_struct)
  GWEN_LIST_INIT(AB_USER, p_struct)
  /* members */
  p_struct->uniqueId=0;
  p_struct->backendName=NULL;
  p_struct->userName=NULL;
  p_struct->userId=NULL;
  p_struct->customerId=NULL;
  p_struct->country=NULL;
  p_struct->bankCode=NULL;
  p_struct->provider=NULL;
  p_struct->certDb=NULL;
  p_struct->lastSessionId=0;
  /* virtual functions */
  p_struct->readFromDbFn=AB_User__ReadFromDb;
  p_struct->writeToDbFn=AB_User__WriteToDb;

  return p_struct;
}

void AB_User_free(AB_USER *p_struct) {
  if (p_struct) {
  assert(p_struct->_refCount);
  if (p_struct->_refCount==1) {
    GWEN_INHERIT_FINI(AB_USER, p_struct)
    GWEN_LIST_FINI(AB_USER, p_struct)
  /* members */
    free(p_struct->backendName); p_struct->backendName=NULL;
    free(p_struct->userName); p_struct->userName=NULL;
    free(p_struct->userId); p_struct->userId=NULL;
    free(p_struct->customerId); p_struct->customerId=NULL;
    free(p_struct->country); p_struct->country=NULL;
    free(p_struct->bankCode); p_struct->bankCode=NULL;
    GWEN_DB_Group_free(p_struct->certDb); p_struct->certDb=NULL;
    p_struct->_refCount=0;
    GWEN_FREE_OBJECT(p_struct);
  }
  else
    p_struct->_refCount--;
  }
}

void AB_User_Attach(AB_USER *p_struct) {
  assert(p_struct);
  assert(p_struct->_refCount);
  p_struct->_refCount++;
}

uint32_t AB_User_GetUniqueId(const AB_USER *p_struct) {
  assert(p_struct);
  return p_struct->uniqueId;
}

const char *AB_User_GetBackendName(const AB_USER *p_struct) {
  assert(p_struct);
  return p_struct->backendName;
}

const char *AB_User_GetUserName(const AB_USER *p_struct) {
  assert(p_struct);
  return p_struct->userName;
}

const char *AB_User_GetUserId(const AB_USER *p_struct) {
  assert(p_struct);
  return p_struct->userId;
}

const char *AB_User_GetCustomerId(const AB_USER *p_struct) {
  assert(p_struct);
  return p_struct->customerId;
}

const char *AB_User_GetCountry(const AB_USER *p_struct) {
  assert(p_struct);
  return p_struct->country;
}

const char *AB_User_GetBankCode(const AB_USER *p_struct) {
  assert(p_struct);
  return p_struct->bankCode;
}

AB_PROVIDER *AB_User_GetProvider(const AB_USER *p_struct) {
  assert(p_struct);
  return p_struct->provider;
}

GWEN_DB_NODE *AB_User_GetCertDb(const AB_USER *p_struct) {
  assert(p_struct);
  return p_struct->certDb;
}

uint32_t AB_User_GetLastSessionId(const AB_USER *p_struct) {
  assert(p_struct);
  return p_struct->lastSessionId;
}

void AB_User_SetUniqueId(AB_USER *p_struct, uint32_t p_src) {
  assert(p_struct);
  p_struct->uniqueId=p_src;
}

void AB_User_SetBackendName(AB_USER *p_struct, const char *p_src) {
  assert(p_struct);
  if (p_struct->backendName) {
    free(p_struct->backendName); p_struct->backendName=NULL;
  }
  if (p_src) {
    p_struct->backendName=strdup(p_src);
  }
  else {
    p_struct->backendName=NULL;
  }
}

void AB_User_SetUserName(AB_USER *p_struct, const char *p_src) {
  assert(p_struct);
  if (p_struct->userName) {
    free(p_struct->userName); p_struct->userName=NULL;
  }
  if (p_src) {
    p_struct->userName=strdup(p_src);
  }
  else {
    p_struct->userName=NULL;
  }
}

void AB_User_SetUserId(AB_USER *p_struct, const char *p_src) {
  assert(p_struct);
  if (p_struct->userId) {
    free(p_struct->userId); p_struct->userId=NULL;
  }
  if (p_src) {
    p_struct->userId=strdup(p_src);
  }
  else {
    p_struct->userId=NULL;
  }
}

void AB_User_SetCustomerId(AB_USER *p_struct, const char *p_src) {
  assert(p_struct);
  if (p_struct->customerId) {
    free(p_struct->customerId); p_struct->customerId=NULL;
  }
  if (p_src) {
    p_struct->customerId=strdup(p_src);
  }
  else {
    p_struct->customerId=NULL;
  }
}

void AB_User_SetCountry(AB_USER *p_struct, const char *p_src) {
  assert(p_struct);
  if (p_struct->country) {
    free(p_struct->country); p_struct->country=NULL;
  }
  if (p_src) {
    p_struct->country=strdup(p_src);
  }
  else {
    p_struct->country=NULL;
  }
}

void AB_User_SetBankCode(AB_USER *p_struct, const char *p_src) {
  assert(p_struct);
  if (p_struct->bankCode) {
    free(p_struct->bankCode); p_struct->bankCode=NULL;
  }
  if (p_src) {
    p_struct->bankCode=strdup(p_src);
  }
  else {
    p_struct->bankCode=NULL;
  }
}

void AB_User_SetProvider(AB_USER *p_struct, AB_PROVIDER *p_src) {
  assert(p_struct);
  p_struct->provider=p_src;
}

void AB_User_SetCertDb(AB_USER *p_struct, GWEN_DB_NODE *p_src) {
  assert(p_struct);
  if (p_struct->certDb) {
    GWEN_DB_Group_free(p_struct->certDb); p_struct->certDb=NULL;
  }
  p_struct->certDb=p_src;
}

void AB_User_SetLastSessionId(AB_USER *p_struct, uint32_t p_src) {
  assert(p_struct);
  p_struct->lastSessionId=p_src;
}

/* code for virtual functions */
int AB_User_ReadFromDb(AB_USER *p_struct, GWEN_DB_NODE* dbData) {
  assert(p_struct);
  if (p_struct->readFromDbFn)
    return p_struct->readFromDbFn(p_struct, dbData);
  else
    return GWEN_ERROR_NOT_IMPLEMENTED;
}

int AB_User_WriteToDb(const AB_USER *p_struct, GWEN_DB_NODE* dbData) {
  assert(p_struct);
  if (p_struct->writeToDbFn)
    return p_struct->writeToDbFn(p_struct, dbData);
  else
    return GWEN_ERROR_NOT_IMPLEMENTED;
}

/* setters for virtual functions */
AB_USER_READFROMDB_FN AB_User_SetReadFromDbFn(AB_USER *p_struct, AB_USER_READFROMDB_FN fn){
  AB_USER_READFROMDB_FN oldFn;

  assert(p_struct);
  oldFn=p_struct->readFromDbFn;
  p_struct->readFromDbFn=fn;
  return oldFn;
}

AB_USER_WRITETODB_FN AB_User_SetWriteToDbFn(AB_USER *p_struct, AB_USER_WRITETODB_FN fn){
  AB_USER_WRITETODB_FN oldFn;

  assert(p_struct);
  oldFn=p_struct->writeToDbFn;
  p_struct->writeToDbFn=fn;
  return oldFn;
}

void AB_User_ReadDb(AB_USER *p_struct, GWEN_DB_NODE *p_db) {
  assert(p_struct);
  /* member "uniqueId" */
  p_struct->uniqueId=GWEN_DB_GetIntValue(p_db, "uniqueId", 0, 0);

  /* member "backendName" */
  if (p_struct->backendName) {
    free(p_struct->backendName); p_struct->backendName=NULL;
  }
  { const char *s; s=GWEN_DB_GetCharValue(p_db, "backendName", 0, NULL); if (s) p_struct->backendName=strdup(s); }
  if (p_struct->backendName==NULL) {  p_struct->backendName=NULL;
  }

  /* member "userName" */
  if (p_struct->userName) {
    free(p_struct->userName); p_struct->userName=NULL;
  }
  { const char *s; s=GWEN_DB_GetCharValue(p_db, "userName", 0, NULL); if (s) p_struct->userName=strdup(s); }
  if (p_struct->userName==NULL) {  p_struct->userName=NULL;
  }

  /* member "userId" */
  if (p_struct->userId) {
    free(p_struct->userId); p_struct->userId=NULL;
  }
  { const char *s; s=GWEN_DB_GetCharValue(p_db, "userId", 0, NULL); if (s) p_struct->userId=strdup(s); }
  if (p_struct->userId==NULL) {  p_struct->userId=NULL;
  }

  /* member "customerId" */
  if (p_struct->customerId) {
    free(p_struct->customerId); p_struct->customerId=NULL;
  }
  { const char *s; s=GWEN_DB_GetCharValue(p_db, "customerId", 0, NULL); if (s) p_struct->customerId=strdup(s); }
  if (p_struct->customerId==NULL) {  p_struct->customerId=NULL;
  }

  /* member "country" */
  if (p_struct->country) {
    free(p_struct->country); p_struct->country=NULL;
  }
  { const char *s; s=GWEN_DB_GetCharValue(p_db, "country", 0, NULL); if (s) p_struct->country=strdup(s); }
  if (p_struct->country==NULL) {  p_struct->country=NULL;
  }

  /* member "bankCode" */
  if (p_struct->bankCode) {
    free(p_struct->bankCode); p_struct->bankCode=NULL;
  }
  { const char *s; s=GWEN_DB_GetCharValue(p_db, "bankCode", 0, NULL); if (s) p_struct->bankCode=strdup(s); }
  if (p_struct->bankCode==NULL) {  p_struct->bankCode=NULL;
  }

  /* member "provider" */
  /* member "provider" is volatile, just presetting */
  p_struct->provider=NULL;

  /* member "certDb" */
  if (p_struct->certDb) {
    GWEN_DB_Group_free(p_struct->certDb); p_struct->certDb=NULL;
  }
  { GWEN_DB_NODE *dbSrc; dbSrc=GWEN_DB_GetGroup(p_db, GWEN_PATH_FLAGS_NAMEMUSTEXIST, "certDb"); if (dbSrc) { p_struct->certDb=GWEN_DB_Group_dup(dbSrc); } else p_struct->certDb=NULL; }
  if (p_struct->certDb==NULL) {  p_struct->certDb=NULL;
  }

  /* member "lastSessionId" */
  p_struct->lastSessionId=GWEN_DB_GetIntValue(p_db, "lastSessionId", 0, 0);

}

int AB_User_WriteDb(const AB_USER *p_struct, GWEN_DB_NODE *p_db) {
  int p_rv;

  assert(p_struct);
  /* member "uniqueId" */
  p_rv=GWEN_DB_SetIntValue(p_db, GWEN_DB_FLAGS_OVERWRITE_VARS, "uniqueId", p_struct->uniqueId);
  if (p_rv<0) {
    DBG_INFO(GWEN_LOGDOMAIN, "here (%d)\n", p_rv);
    return p_rv;
  }

  /* member "backendName" */
  if (p_struct->backendName) p_rv=GWEN_DB_SetCharValue(p_db, GWEN_DB_FLAGS_OVERWRITE_VARS, "backendName", p_struct->backendName); else { GWEN_DB_DeleteVar(p_db, "backendName"); p_rv=0; }
  if (p_rv<0) {
    DBG_INFO(GWEN_LOGDOMAIN, "here (%d)\n", p_rv);
    return p_rv;
  }

  /* member "userName" */
  if (p_struct->userName) p_rv=GWEN_DB_SetCharValue(p_db, GWEN_DB_FLAGS_OVERWRITE_VARS, "userName", p_struct->userName); else { GWEN_DB_DeleteVar(p_db, "userName"); p_rv=0; }
  if (p_rv<0) {
    DBG_INFO(GWEN_LOGDOMAIN, "here (%d)\n", p_rv);
    return p_rv;
  }

  /* member "userId" */
  if (p_struct->userId) p_rv=GWEN_DB_SetCharValue(p_db, GWEN_DB_FLAGS_OVERWRITE_VARS, "userId", p_struct->userId); else { GWEN_DB_DeleteVar(p_db, "userId"); p_rv=0; }
  if (p_rv<0) {
    DBG_INFO(GWEN_LOGDOMAIN, "here (%d)\n", p_rv);
    return p_rv;
  }

  /* member "customerId" */
  if (p_struct->customerId) p_rv=GWEN_DB_SetCharValue(p_db, GWEN_DB_FLAGS_OVERWRITE_VARS, "customerId", p_struct->customerId); else { GWEN_DB_DeleteVar(p_db, "customerId"); p_rv=0; }
  if (p_rv<0) {
    DBG_INFO(GWEN_LOGDOMAIN, "here (%d)\n", p_rv);
    return p_rv;
  }

  /* member "country" */
  if (p_struct->country) p_rv=GWEN_DB_SetCharValue(p_db, GWEN_DB_FLAGS_OVERWRITE_VARS, "country", p_struct->country); else { GWEN_DB_DeleteVar(p_db, "country"); p_rv=0; }
  if (p_rv<0) {
    DBG_INFO(GWEN_LOGDOMAIN, "here (%d)\n", p_rv);
    return p_rv;
  }

  /* member "bankCode" */
  if (p_struct->bankCode) p_rv=GWEN_DB_SetCharValue(p_db, GWEN_DB_FLAGS_OVERWRITE_VARS, "bankCode", p_struct->bankCode); else { GWEN_DB_DeleteVar(p_db, "bankCode"); p_rv=0; }
  if (p_rv<0) {
    DBG_INFO(GWEN_LOGDOMAIN, "here (%d)\n", p_rv);
    return p_rv;
  }

  /* member "provider" is volatile, not writing to db */

  /* member "certDb" */
  if (p_struct->certDb){ GWEN_DB_NODE *dbCopy; dbCopy=GWEN_DB_GetGroup(p_db, GWEN_DB_FLAGS_DEFAULT, "certDb"); assert(dbCopy); p_rv=GWEN_DB_AddGroupChildren(dbCopy, p_struct->certDb); } else { GWEN_DB_DeleteGroup(p_db, "certDb"); p_rv=0; }
  if (p_rv<0) {
    DBG_INFO(GWEN_LOGDOMAIN, "here (%d)\n", p_rv);
    return p_rv;
  }

  /* member "lastSessionId" */
  p_rv=GWEN_DB_SetIntValue(p_db, GWEN_DB_FLAGS_OVERWRITE_VARS, "lastSessionId", p_struct->lastSessionId);
  if (p_rv<0) {
    DBG_INFO(GWEN_LOGDOMAIN, "here (%d)\n", p_rv);
    return p_rv;
  }

  return 0;
}

AB_USER *AB_User_fromDb(GWEN_DB_NODE *p_db) {
  AB_USER *p_struct;
  p_struct=AB_User_new();
  AB_User_ReadDb(p_struct, p_db);
  return p_struct;
}

int AB_User_toDb(const AB_USER *p_struct, GWEN_DB_NODE *p_db) {
  return AB_User_WriteDb(p_struct, p_db);
}

void AB_User_ReadXml(AB_USER *p_struct, GWEN_XMLNODE *p_db) {
  assert(p_struct);
  /* member "uniqueId" */
  p_struct->uniqueId=GWEN_XMLNode_GetIntValue(p_db, "uniqueId", 0);

  /* member "backendName" */
  if (p_struct->backendName) {
    free(p_struct->backendName); p_struct->backendName=NULL;
  }
  { const char *s; s=GWEN_XMLNode_GetCharValue(p_db, "backendName", NULL); if (s) p_struct->backendName=strdup(s); }
  if (p_struct->backendName==NULL) {  /* preset member "backendName" if empty */
  p_struct->backendName=NULL;
  }

  /* member "userName" */
  if (p_struct->userName) {
    free(p_struct->userName); p_struct->userName=NULL;
  }
  { const char *s; s=GWEN_XMLNode_GetCharValue(p_db, "userName", NULL); if (s) p_struct->userName=strdup(s); }
  if (p_struct->userName==NULL) {  /* preset member "userName" if empty */
  p_struct->userName=NULL;
  }

  /* member "userId" */
  if (p_struct->userId) {
    free(p_struct->userId); p_struct->userId=NULL;
  }
  { const char *s; s=GWEN_XMLNode_GetCharValue(p_db, "userId", NULL); if (s) p_struct->userId=strdup(s); }
  if (p_struct->userId==NULL) {  /* preset member "userId" if empty */
  p_struct->userId=NULL;
  }

  /* member "customerId" */
  if (p_struct->customerId) {
    free(p_struct->customerId); p_struct->customerId=NULL;
  }
  { const char *s; s=GWEN_XMLNode_GetCharValue(p_db, "customerId", NULL); if (s) p_struct->customerId=strdup(s); }
  if (p_struct->customerId==NULL) {  /* preset member "customerId" if empty */
  p_struct->customerId=NULL;
  }

  /* member "country" */
  if (p_struct->country) {
    free(p_struct->country); p_struct->country=NULL;
  }
  { const char *s; s=GWEN_XMLNode_GetCharValue(p_db, "country", NULL); if (s) p_struct->country=strdup(s); }
  if (p_struct->country==NULL) {  /* preset member "country" if empty */
  p_struct->country=NULL;
  }

  /* member "bankCode" */
  if (p_struct->bankCode) {
    free(p_struct->bankCode); p_struct->bankCode=NULL;
  }
  { const char *s; s=GWEN_XMLNode_GetCharValue(p_db, "bankCode", NULL); if (s) p_struct->bankCode=strdup(s); }
  if (p_struct->bankCode==NULL) {  /* preset member "bankCode" if empty */
  p_struct->bankCode=NULL;
  }

  /* member "provider" */
  /* member "provider" is volatile, just presetting */
  p_struct->provider=NULL;

  /* member "certDb" */
  if (p_struct->certDb) {
    GWEN_DB_Group_free(p_struct->certDb); p_struct->certDb=NULL;
  }
  
  if (p_struct->certDb==NULL) {  /* preset member "certDb" if empty */
  p_struct->certDb=NULL;
  }

  /* member "lastSessionId" */
  p_struct->lastSessionId=GWEN_XMLNode_GetIntValue(p_db, "lastSessionId", 0);

}

void AB_User_WriteXml(const AB_USER *p_struct, GWEN_XMLNODE *p_db) {
  assert(p_struct);
  /* member "uniqueId" */
  GWEN_XMLNode_SetIntValue(p_db, "uniqueId", p_struct->uniqueId);

  /* member "backendName" */
  GWEN_XMLNode_SetCharValue(p_db, "backendName", p_struct->backendName);

  /* member "userName" */
  GWEN_XMLNode_SetCharValue(p_db, "userName", p_struct->userName);

  /* member "userId" */
  GWEN_XMLNode_SetCharValue(p_db, "userId", p_struct->userId);

  /* member "customerId" */
  GWEN_XMLNode_SetCharValue(p_db, "customerId", p_struct->customerId);

  /* member "country" */
  GWEN_XMLNode_SetCharValue(p_db, "country", p_struct->country);

  /* member "bankCode" */
  GWEN_XMLNode_SetCharValue(p_db, "bankCode", p_struct->bankCode);

  /* member "provider" is volatile, not writing to xml */

  /* member "certDb" */
  

  /* member "lastSessionId" */
  GWEN_XMLNode_SetIntValue(p_db, "lastSessionId", p_struct->lastSessionId);

}

void AB_User_toXml(const AB_USER *p_struct, GWEN_XMLNODE *p_db) {
  AB_User_WriteXml(p_struct, p_db);
}

AB_USER *AB_User_fromXml(GWEN_XMLNODE *p_db) {
  AB_USER *p_struct;
  p_struct=AB_User_new();
  AB_User_ReadXml(p_struct, p_db);
  return p_struct;
}

AB_USER *AB_User_List_GetByUniqueId(const AB_USER_LIST *p_list, uint32_t p_cmp) {
  AB_USER *p_struct;

  assert(p_list);
  p_struct = AB_User_List_First(p_list);
  while(p_struct) {
    int p_rv;

    if (p_struct->uniqueId==p_cmp) p_rv=0; else if (p_cmp<p_struct->uniqueId) p_rv=-1; else p_rv=1;
    if (p_rv == 0)
      return p_struct;
    p_struct = AB_User_List_Next(p_struct);
  }
  return NULL;
}

AB_USER *AB_User_List_GetByLastSessionId(const AB_USER_LIST *p_list, uint32_t p_cmp) {
  AB_USER *p_struct;

  assert(p_list);
  p_struct = AB_User_List_First(p_list);
  while(p_struct) {
    int p_rv;

    if (p_struct->lastSessionId==p_cmp) p_rv=0; else if (p_cmp<p_struct->lastSessionId) p_rv=-1; else p_rv=1;
    if (p_rv == 0)
      return p_struct;
    p_struct = AB_User_List_Next(p_struct);
  }
  return NULL;
}

int AB_User__ReadFromDb(AB_USER *a, GWEN_DB_NODE *db) { assert(a); AB_User_ReadDb(a, db); return 0; }
int AB_User__WriteToDb(const AB_USER *a, GWEN_DB_NODE *db) { assert(a); AB_User_WriteDb(a, db); return 0; }

/* code headers */

