/**********************************************************
 * This file has been automatically created by "typemaker2"
 * from the file "balance.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 "./balance_p.h"

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

/* code headers */

/* macro functions */
GWEN_LIST_FUNCTIONS(AB_BALANCE, AB_Balance)


AB_BALANCE_TYPE AB_Balance_Type_fromString(const char *p_s) {
  if (p_s && *p_s) {
    if (strcasecmp(p_s, "none")==0)
      return AB_Balance_TypeNone;
    else if (strcasecmp(p_s, "noted")==0)
      return AB_Balance_TypeNoted;
    else if (strcasecmp(p_s, "booked")==0)
      return AB_Balance_TypeBooked;
    else if (strcasecmp(p_s, "bankLine")==0)
      return AB_Balance_TypeBankLine;
    else if (strcasecmp(p_s, "disposable")==0)
      return AB_Balance_TypeDisposable;
    else if (strcasecmp(p_s, "temporary")==0)
      return AB_Balance_TypeTemporary;
  }
  return AB_Balance_TypeUnknown;
}

const char *AB_Balance_Type_toString(AB_BALANCE_TYPE p_i) {
  switch(p_i) {
    case AB_Balance_TypeNone: return "none";
    case AB_Balance_TypeNoted: return "noted";
    case AB_Balance_TypeBooked: return "booked";
    case AB_Balance_TypeBankLine: return "bankLine";
    case AB_Balance_TypeDisposable: return "disposable";
    case AB_Balance_TypeTemporary: return "temporary";
    default: return "unknown";
  }
}

AB_BALANCE *AB_Balance_new(void) {
  AB_BALANCE *p_struct;

  GWEN_NEW_OBJECT(AB_BALANCE, p_struct)
  p_struct->_refCount=1;
  GWEN_LIST_INIT(AB_BALANCE, p_struct)
  /* members */
  p_struct->date=NULL;
  p_struct->value=AB_Value_new();
  p_struct->type=AB_Balance_TypeUnknown;
  /* virtual functions */

  return p_struct;
}

void AB_Balance_free(AB_BALANCE *p_struct) {
  if (p_struct) {
  assert(p_struct->_refCount);
  if (p_struct->_refCount==1) {
    GWEN_LIST_FINI(AB_BALANCE, p_struct)
  /* members */
    GWEN_Date_free(p_struct->date); p_struct->date=NULL;
    AB_Value_free(p_struct->value);
    p_struct->_refCount=0;
    GWEN_FREE_OBJECT(p_struct);
  }
  else
    p_struct->_refCount--;
  }
}

void AB_Balance_Attach(AB_BALANCE *p_struct) {
  assert(p_struct);
  assert(p_struct->_refCount);
  p_struct->_refCount++;
}

AB_BALANCE *AB_Balance_dup(const AB_BALANCE *p_src) {
  AB_BALANCE *p_struct;

  assert(p_src);
  p_struct=AB_Balance_new();
  /* member "date" */
  if (p_struct->date) {
    GWEN_Date_free(p_struct->date); p_struct->date=NULL;
    p_struct->date=NULL;
  }
  if (p_src->date) {
    p_struct->date=GWEN_Date_dup(p_src->date);
  }

  /* member "value" */
  if (p_struct->value) {
    AB_Value_free(p_struct->value);
    p_struct->value=NULL;
  }
  if (p_src->value) {
    p_struct->value=AB_Value_dup(p_src->value);
  }

  /* member "type" */
  p_struct->type=p_src->type;

  return p_struct;
}

AB_BALANCE *AB_Balance_copy(AB_BALANCE *p_struct, const AB_BALANCE *p_src) {
    assert(p_struct);
  assert(p_src);
  /* member "date" */
  if (p_struct->date) {
    GWEN_Date_free(p_struct->date); p_struct->date=NULL;
    p_struct->date=NULL;
  }
  if (p_src->date) {
    p_struct->date=GWEN_Date_dup(p_src->date);
  }

  /* member "value" */
  if (p_struct->value) {
    AB_Value_free(p_struct->value);
    p_struct->value=NULL;
  }
  if (p_src->value) {
    p_struct->value=AB_Value_dup(p_src->value);
  }

  /* member "type" */
  p_struct->type=p_src->type;

  return p_struct;
}

const GWEN_DATE *AB_Balance_GetDate(const AB_BALANCE *p_struct) {
  assert(p_struct);
  return p_struct->date;
}

const AB_VALUE *AB_Balance_GetValue(const AB_BALANCE *p_struct) {
  assert(p_struct);
  return p_struct->value;
}

AB_BALANCE_TYPE AB_Balance_GetType(const AB_BALANCE *p_struct) {
  assert(p_struct);
  return p_struct->type;
}

void AB_Balance_SetDate(AB_BALANCE *p_struct, const GWEN_DATE *p_src) {
  assert(p_struct);
  if (p_struct->date) {
    GWEN_Date_free(p_struct->date); p_struct->date=NULL;
  }
  if (p_src) {
    p_struct->date=GWEN_Date_dup(p_src);
  }
  else {
    p_struct->date=NULL;
  }
}

void AB_Balance_SetValue(AB_BALANCE *p_struct, const AB_VALUE *p_src) {
  assert(p_struct);
  if (p_struct->value) {
    AB_Value_free(p_struct->value);
  }
  if (p_src) {
    p_struct->value=AB_Value_dup(p_src);
  }
  else {
    p_struct->value=AB_Value_new();
  }
}

void AB_Balance_SetType(AB_BALANCE *p_struct, AB_BALANCE_TYPE  p_src) {
  assert(p_struct);
  p_struct->type=p_src;
}

/* list1 functions */
AB_BALANCE_LIST *AB_Balance_List_dup(const AB_BALANCE_LIST *p_src) {
  AB_BALANCE_LIST *p_dest;
  AB_BALANCE *p_elem;

  assert(p_src);
  p_dest=AB_Balance_List_new();
  p_elem=AB_Balance_List_First(p_src);
  while(p_elem) {
    AB_BALANCE *p_cpy;

    p_cpy=AB_Balance_dup(p_elem);
    AB_Balance_List_Add(p_cpy, p_dest);
    p_elem=AB_Balance_List_Next(p_elem);
  }

  return p_dest;
}

void AB_Balance_ReadDb(AB_BALANCE *p_struct, GWEN_DB_NODE *p_db) {
  assert(p_struct);
  /* member "date" */
  if (p_struct->date) {
    GWEN_Date_free(p_struct->date); p_struct->date=NULL;
  }
  { const char *s; s=GWEN_DB_GetCharValue(p_db, "date", 0, NULL); if (s && *s) p_struct->date=GWEN_Date_fromString(s); }
  if (p_struct->date==NULL) {  p_struct->date=NULL;
  }

  /* member "value" */
  if (p_struct->value) {
    AB_Value_free(p_struct->value);
  }
  { const char *s; s=GWEN_DB_GetCharValue(p_db, "value", 0, NULL); if (s) p_struct->value=AB_Value_fromString(s); else p_struct->value=NULL; }
  if (p_struct->value==NULL) {  p_struct->value=AB_Value_new();
  }

  /* member "type" */
  { const char *s; s=GWEN_DB_GetCharValue(p_db, "type", 0, NULL); if (s) p_struct->type=AB_Balance_Type_fromString(s); else p_struct->type=AB_Balance_TypeUnknown; }

}

int AB_Balance_WriteDb(const AB_BALANCE *p_struct, GWEN_DB_NODE *p_db) {
  int p_rv;

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

  /* member "value" */
  if (p_struct->value) { GWEN_BUFFER *tbuf=GWEN_Buffer_new(0, 64, 0, 1); AB_Value_toString(p_struct->value, tbuf); p_rv=GWEN_DB_SetCharValue(p_db, GWEN_DB_FLAGS_OVERWRITE_VARS, "value", GWEN_Buffer_GetStart(tbuf)); GWEN_Buffer_free(tbuf); } else { GWEN_DB_DeleteVar(p_db, "value"); p_rv=0; }
  if (p_rv<0) {
    DBG_INFO(GWEN_LOGDOMAIN, "here (%d)\n", p_rv);
    return p_rv;
  }

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

  return 0;
}

AB_BALANCE *AB_Balance_fromDb(GWEN_DB_NODE *p_db) {
  AB_BALANCE *p_struct;
  p_struct=AB_Balance_new();
  AB_Balance_ReadDb(p_struct, p_db);
  return p_struct;
}

int AB_Balance_toDb(const AB_BALANCE *p_struct, GWEN_DB_NODE *p_db) {
  return AB_Balance_WriteDb(p_struct, p_db);
}

void AB_Balance_ReadXml(AB_BALANCE *p_struct, GWEN_XMLNODE *p_db) {
  assert(p_struct);
  /* member "date" */
  if (p_struct->date) {
    GWEN_Date_free(p_struct->date); p_struct->date=NULL;
  }
  { const char *s; s=GWEN_XMLNode_GetCharValue(p_db, "date", NULL); if (s && *s) p_struct->date=GWEN_Date_fromString(s); }
  if (p_struct->date==NULL) {  /* preset member "date" if empty */
  p_struct->date=NULL;
  }

  /* member "value" */
  if (p_struct->value) {
    AB_Value_free(p_struct->value);
  }
  { const char *s; s=GWEN_XMLNode_GetCharValue(p_db, "value", NULL); if (s) p_struct->value=AB_Value_fromString(s); else p_struct->value=NULL; }
  if (p_struct->value==NULL) {  /* preset member "value" if empty */
  p_struct->value=AB_Value_new();
  }

  /* member "type" */
  { const char *s; s=GWEN_XMLNode_GetCharValue(p_db, "type", NULL); if (s) p_struct->type=AB_Balance_Type_fromString(s); else p_struct->type=AB_Balance_TypeUnknown; }

}

void AB_Balance_WriteXml(const AB_BALANCE *p_struct, GWEN_XMLNODE *p_db) {
  assert(p_struct);
  /* member "date" */
  if (p_struct->date){ const char *s=GWEN_Date_GetString(p_struct->date); GWEN_XMLNode_SetCharValue(p_db, "date", s); }

  /* member "value" */
  if (p_struct->value) { GWEN_BUFFER *tbuf=GWEN_Buffer_new(0, 64, 0, 1); AB_Value_toString(p_struct->value, tbuf); GWEN_XMLNode_SetCharValue(p_db, "value", GWEN_Buffer_GetStart(tbuf)); GWEN_Buffer_free(tbuf); }

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

}

void AB_Balance_toXml(const AB_BALANCE *p_struct, GWEN_XMLNODE *p_db) {
  AB_Balance_WriteXml(p_struct, p_db);
}

AB_BALANCE *AB_Balance_fromXml(GWEN_XMLNODE *p_db) {
  AB_BALANCE *p_struct;
  p_struct=AB_Balance_new();
  AB_Balance_ReadXml(p_struct, p_db);
  return p_struct;
}

AB_BALANCE *AB_Balance_List_GetByType(const AB_BALANCE_LIST *p_list, int p_cmp) {
  AB_BALANCE *p_struct;

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

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

AB_BALANCE *AB_Balance_List_FindFirstByType(const AB_BALANCE_LIST *bl, int ty) { AB_BALANCE *bal; assert(bl); bal=AB_Balance_List_First(bl); while(bal) { if (ty==AB_Balance_TypeNone || ty==bal->type) return bal; bal=AB_Balance_List_Next(bal); } return NULL; }
AB_BALANCE *AB_Balance_List_FindNextByType(AB_BALANCE *bal, int ty) { assert(bal); bal=AB_Balance_List_Next(bal); while(bal) { if (ty==AB_Balance_TypeNone || ty==bal->type) return bal; bal=AB_Balance_List_Next(bal); } return NULL; }
AB_BALANCE *AB_Balance_List_GetLatestByType(const AB_BALANCE_LIST *bl, int ty) { AB_BALANCE *lastBal=NULL; const GWEN_DATE *lastDt=NULL; AB_BALANCE *bal=NULL; bal=AB_Balance_List_First(bl); while(bal) { if (ty==0 || (AB_Balance_GetType(bal)==ty)) { const GWEN_DATE *dt; if (lastBal && lastDt && (dt=AB_Balance_GetDate(bal))) { if (GWEN_Date_Compare(dt, lastDt)>0) { lastBal=bal; lastDt=dt; } } else { lastBal=bal; lastDt=AB_Balance_GetDate(bal); } } bal=AB_Balance_List_Next(bal); } return lastBal; }

/* code headers */

