/* This file is auto-generated from "message.xml" by the typemaker
   tool of Gwenhywfar. 
   Do not edit this file -- all changes will be lost! */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "message_p.h"
#include <gwenhywfar/misc.h>
#include <gwenhywfar/db.h>
#include <gwenhywfar/debug.h>
#include <assert.h>
#include <stdlib.h>
#include <strings.h>

#include <gwenhywfar/types.h>
#include <gwenhywfar/gwentime.h>
#include <aqbanking/error.h>


GWEN_LIST_FUNCTIONS(AB_MESSAGE, AB_Message)
GWEN_LIST2_FUNCTIONS(AB_MESSAGE, AB_Message)




AB_MESSAGE *AB_Message_new(void) {
  AB_MESSAGE *st;

  GWEN_NEW_OBJECT(AB_MESSAGE, st)
  st->_usage=1;
  GWEN_LIST_INIT(AB_MESSAGE, st)
  return st;
}


void AB_Message_free(AB_MESSAGE *st) {
  if (st) {
    assert(st->_usage);
    if (--(st->_usage)==0) {
  if (st->subject)
    free(st->subject);
  if (st->text)
    free(st->text);
  if (st->dateReceived)
    GWEN_Time_free(st->dateReceived);
  GWEN_LIST_FINI(AB_MESSAGE, st)
  GWEN_FREE_OBJECT(st);
    }
  }

}


AB_MESSAGE *AB_Message_dup(const AB_MESSAGE *d) {
  AB_MESSAGE *st;

  assert(d);
  st=AB_Message_new();
  st->userId=d->userId;
  st->accountId=d->accountId;
  if (d->subject)
    st->subject=strdup(d->subject);
  if (d->text)
    st->text=strdup(d->text);
  if (d->dateReceived)
    st->dateReceived=GWEN_Time_dup(d->dateReceived);
  return st;
}


int AB_Message_toDb(const AB_MESSAGE *st, GWEN_DB_NODE *db) {
  assert(st);
  assert(db);
  if (GWEN_DB_SetIntValue(db, GWEN_DB_FLAGS_OVERWRITE_VARS, "userId", st->userId))
    return -1;
  if (GWEN_DB_SetIntValue(db, GWEN_DB_FLAGS_OVERWRITE_VARS, "accountId", st->accountId))
    return -1;
  if (st->subject)
    if (GWEN_DB_SetCharValue(db, GWEN_DB_FLAGS_OVERWRITE_VARS, "subject", st->subject))
      return -1;
  if (st->text)
    if (GWEN_DB_SetCharValue(db, GWEN_DB_FLAGS_OVERWRITE_VARS, "text", st->text))
      return -1;
  if (st->dateReceived)
    if (GWEN_Time_toDb(st->dateReceived, GWEN_DB_GetGroup(db, GWEN_DB_FLAGS_DEFAULT, "dateReceived")))
      return -1;
  return 0;
}


int AB_Message_ReadDb(AB_MESSAGE *st, GWEN_DB_NODE *db) {
  assert(st);
  assert(db);
  AB_Message_SetUserId(st, GWEN_DB_GetIntValue(db, "userId", 0, 0));
  AB_Message_SetAccountId(st, GWEN_DB_GetIntValue(db, "accountId", 0, 0));
  AB_Message_SetSubject(st, GWEN_DB_GetCharValue(db, "subject", 0, 0));
  AB_Message_SetText(st, GWEN_DB_GetCharValue(db, "text", 0, 0));
  if (1) { /* for local vars */
    GWEN_DB_NODE *dbT;

    dbT=GWEN_DB_GetGroup(db, GWEN_PATH_FLAGS_NAMEMUSTEXIST, "dateReceived");
    if (dbT) {
  if (st->dateReceived)
    GWEN_Time_free(st->dateReceived);
  st->dateReceived=GWEN_Time_fromDb(dbT);
}
  }
  return 0;
}


AB_MESSAGE *AB_Message_fromDb(GWEN_DB_NODE *db) {
  AB_MESSAGE *st;

  assert(db);
  st=AB_Message_new();
  AB_Message_ReadDb(st, db);
  st->_modified=0;
  return st;
}




uint32_t AB_Message_GetUserId(const AB_MESSAGE *st) {
  assert(st);
  return st->userId;
}


void AB_Message_SetUserId(AB_MESSAGE *st, uint32_t d) {
  assert(st);
  st->userId=d;
  st->_modified=1;
}




uint32_t AB_Message_GetAccountId(const AB_MESSAGE *st) {
  assert(st);
  return st->accountId;
}


void AB_Message_SetAccountId(AB_MESSAGE *st, uint32_t d) {
  assert(st);
  st->accountId=d;
  st->_modified=1;
}




const char *AB_Message_GetSubject(const AB_MESSAGE *st) {
  assert(st);
  return st->subject;
}


void AB_Message_SetSubject(AB_MESSAGE *st, const char *d) {
  assert(st);
  if (st->subject)
    free(st->subject);
  if (d && *d)
    st->subject=strdup(d);
  else
    st->subject=0;
  st->_modified=1;
}




const char *AB_Message_GetText(const AB_MESSAGE *st) {
  assert(st);
  return st->text;
}


void AB_Message_SetText(AB_MESSAGE *st, const char *d) {
  assert(st);
  if (st->text)
    free(st->text);
  if (d && *d)
    st->text=strdup(d);
  else
    st->text=0;
  st->_modified=1;
}




const GWEN_TIME *AB_Message_GetDateReceived(const AB_MESSAGE *st) {
  assert(st);
  return st->dateReceived;
}


void AB_Message_SetDateReceived(AB_MESSAGE *st, const GWEN_TIME *d) {
  assert(st);
  if (st->dateReceived)
    GWEN_Time_free(st->dateReceived);
  if (d)
    st->dateReceived=GWEN_Time_dup(d);
  else
    st->dateReceived=0;
  st->_modified=1;
}




int AB_Message_IsModified(const AB_MESSAGE *st) {
  assert(st);
  return st->_modified;
}


void AB_Message_SetModified(AB_MESSAGE *st, int i) {
  assert(st);
  st->_modified=i;
}


void AB_Message_Attach(AB_MESSAGE *st) {
  assert(st);
  st->_usage++;
}
AB_MESSAGE *AB_Message_List2__freeAll_cb(AB_MESSAGE *st, void *user_data) {
  AB_Message_free(st);
return 0;
}


void AB_Message_List2_freeAll(AB_MESSAGE_LIST2 *stl) {
  if (stl) {
    AB_Message_List2_ForEach(stl, AB_Message_List2__freeAll_cb, 0);
    AB_Message_List2_free(stl); 
  }
}


AB_MESSAGE_LIST *AB_Message_List_dup(const AB_MESSAGE_LIST *stl) {
  if (stl) {
    AB_MESSAGE_LIST *nl;
    AB_MESSAGE *e;

    nl=AB_Message_List_new();
    e=AB_Message_List_First(stl);
    while(e) {
      AB_MESSAGE *ne;

      ne=AB_Message_dup(e);
      assert(ne);
      AB_Message_List_Add(ne, nl);
      e=AB_Message_List_Next(e);
    } /* while (e) */
    return nl;
  }
  else
    return 0;
}




