LibOFX
ofx_container_main.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002          ofx_container_main.cpp
00003                              -------------------
00004     copyright            : (C) 2002 by Benoit Grégoire
00005     email                : benoitg@coeus.ca
00006 ***************************************************************************/
00010 /***************************************************************************
00011  *                                                                         *
00012  *   This program is free software; you can redistribute it and/or modify  *
00013  *   it under the terms of the GNU General Public License as published by  *
00014  *   the Free Software Foundation; either version 2 of the License, or     *
00015  *   (at your option) any later version.                                   *
00016  *                                                                         *
00017  ***************************************************************************/
00018 
00019 #ifdef HAVE_CONFIG_H
00020 #include <config.h>
00021 #endif
00022 
00023 #include <string>
00024 #include <iostream>
00025 #include "ParserEventGeneratorKit.h"
00026 #include "messages.hh"
00027 #include "libofx.h"
00028 #include "ofx_containers.hh"
00029 
00030 OfxMainContainer::OfxMainContainer(LibofxContext *p_libofx_context, OfxGenericContainer *para_parentcontainer, string para_tag_identifier):
00031   OfxGenericContainer(p_libofx_context, para_parentcontainer, para_tag_identifier)
00032 {
00033 
00034 //statement_tree_top=statement_tree.insert(statement_tree_top, NULL);
00035 //security_tree_top=security_tree.insert(security_tree_top, NULL);
00036 
00037 }
00038 OfxMainContainer::~OfxMainContainer()
00039 {
00040   message_out(DEBUG, "Entering the main container's destructor");
00041   tree<OfxGenericContainer *>::iterator tmp = security_tree.begin();
00042 
00043   while (tmp != security_tree.end())
00044   {
00045     message_out(DEBUG, "Deleting " + (*tmp)->type);
00046     delete (*tmp);
00047     ++tmp;
00048   }
00049   tmp = account_tree.begin();
00050   while (tmp != account_tree.end())
00051   {
00052     message_out(DEBUG, "Deleting " + (*tmp)->type);
00053     delete (*tmp);
00054     ++tmp;
00055   }
00056 }
00057 int OfxMainContainer::add_container(OfxGenericContainer * container)
00058 {
00059   message_out(DEBUG, "OfxMainContainer::add_container for element " + container->tag_identifier + "; destroying the generic container");
00060   /* Call gen_event anyway, it could be a status container or similar */
00061   container->gen_event();
00062   delete container;
00063   return 0;
00064 }
00065 
00066 int OfxMainContainer::add_container(OfxSecurityContainer * container)
00067 {
00068   message_out(DEBUG, "OfxMainContainer::add_container, adding a security");
00069   security_tree.insert(security_tree.begin(), container);
00070   return true;
00071 
00072 
00073 }
00074 
00075 int OfxMainContainer::add_container(OfxAccountContainer * container)
00076 {
00077   message_out(DEBUG, "OfxMainContainer::add_container, adding an account");
00078   if ( account_tree.size() == 0)
00079   {
00080     message_out(DEBUG, "OfxMainContainer::add_container, account is the first account");
00081     account_tree.insert(account_tree.begin(), container);
00082   }
00083   else
00084   {
00085     message_out(DEBUG, "OfxMainContainer::add_container, account is not the first account");
00086     tree<OfxGenericContainer *>::sibling_iterator tmp =  account_tree.begin();
00087     tmp += (account_tree.number_of_siblings(tmp)); //Find last account
00088     account_tree.insert_after(tmp, container);
00089   }
00090   return true;
00091 }
00092 
00093 int OfxMainContainer::add_container(OfxStatementContainer * container)
00094 {
00095   message_out(DEBUG, "OfxMainContainer::add_container, adding a statement");
00096   tree<OfxGenericContainer *>::sibling_iterator tmp =  account_tree.begin();
00097   //cerr<< "size="<<account_tree.size()<<"; num_sibblings="<<account_tree.number_of_siblings(tmp)<<endl;
00098   tmp += (account_tree.number_of_siblings(tmp)); //Find last account
00099 
00100   if (account_tree.is_valid(tmp))
00101   {
00102     message_out(DEBUG, "1: tmp is valid, Accounts are present");
00103     tree<OfxGenericContainer *>::iterator child = account_tree.begin(tmp);
00104     if (account_tree.number_of_children(tmp) != 0)
00105     {
00106       message_out(DEBUG, "There are already children for this account");
00107       account_tree.insert(tmp.begin(), container);
00108 
00109     }
00110     else
00111     {
00112       message_out(DEBUG, "There are no children for this account");
00113       account_tree.append_child(tmp, container);
00114     }
00115     container->add_account(&( ((OfxAccountContainer *)(*tmp))->data));
00116     return true;
00117   }
00118   else
00119   {
00120     message_out(ERROR, "OfxMainContainer::add_container, no accounts are present (tmp is invalid)");
00121     return false;
00122   }
00123 }
00124 
00125 int OfxMainContainer::add_container(OfxTransactionContainer * container)
00126 {
00127   message_out(DEBUG, "OfxMainContainer::add_container, adding a transaction");
00128 
00129   if ( account_tree.size() != 0)
00130   {
00131     tree<OfxGenericContainer *>::sibling_iterator tmp =  account_tree.begin();
00132     //cerr<< "size="<<account_tree.size()<<"; num_sibblings="<<account_tree.number_of_siblings(tmp)<<endl;
00133     tmp += (account_tree.number_of_siblings(tmp)); //Find last account
00134     if (account_tree.is_valid(tmp))
00135     {
00136       message_out(DEBUG, "OfxMainContainer::add_container: tmp is valid, Accounts are present");
00137       account_tree.append_child(tmp, container);
00138       container->add_account(&(((OfxAccountContainer *)(*tmp))->data));
00139       return true;
00140     }
00141     else
00142     {
00143       message_out(ERROR, "OfxMainContainer::add_container: tmp is invalid!");
00144       return false;
00145     }
00146   }
00147   else
00148   {
00149     message_out(ERROR, "OfxMainContainer::add_container: the tree is empty!");
00150     return false;
00151   }
00152 }
00153 
00154 int  OfxMainContainer::gen_event()
00155 {
00156   message_out(DEBUG, "Begin walking the trees of the main container to generate events");
00157   tree<OfxGenericContainer *>::iterator tmp = security_tree.begin();
00158   //cerr<<"security_tree.size(): "<<security_tree.size()<<endl;
00159   int i = 0;
00160   while (tmp != security_tree.end())
00161   {
00162     message_out(DEBUG, "Looping...");
00163     //cerr <<i<<endl;
00164     i++;
00165     (*tmp)->gen_event();
00166     ++tmp;
00167   }
00168   tmp = account_tree.begin();
00169   //cerr<<account_tree.size()<<endl;
00170   i = 0;
00171   while (tmp != account_tree.end())
00172   {
00173     //cerr<< "i="<<i<<"; depth="<<account_tree.depth(tmp)<<endl;
00174     i++;
00175     (*tmp)->gen_event();
00176     ++tmp;
00177   }
00178   message_out(DEBUG, "End walking the trees of the main container to generate events");
00179 
00180   return true;
00181 }
00182 
00183 OfxSecurityData *  OfxMainContainer::find_security(string unique_id)
00184 {
00185   message_out(DEBUG, "OfxMainContainer::find_security() Begin.");
00186 
00187   tree<OfxGenericContainer *>::sibling_iterator tmp = security_tree.begin();
00188   OfxSecurityData * retval = NULL;
00189   while (tmp != security_tree.end() && retval == NULL)
00190   {
00191     if (((OfxSecurityContainer*)(*tmp))->data.unique_id == unique_id)
00192     {
00193       message_out(DEBUG, (string)"Security " + ((OfxSecurityContainer*)(*tmp))->data.unique_id + " found.");
00194       retval = &((OfxSecurityContainer*)(*tmp))->data;
00195     }
00196     ++tmp;
00197   }
00198   return retval;
00199 }