LibOFX
ofx_container_main.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  ofx_container_main.cpp
3  -------------------
4  copyright : (C) 2002 by Benoit GrĂ©goire
5  email : benoitg@coeus.ca
6 ***************************************************************************/
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22 
23 #include <string>
24 #include <iostream>
25 #include "ParserEventGeneratorKit.h"
26 #include "messages.hh"
27 #include "libofx.h"
28 #include "ofx_containers.hh"
29 
30 OfxMainContainer::OfxMainContainer(LibofxContext *p_libofx_context, OfxGenericContainer *para_parentcontainer, std::string para_tag_identifier):
31  OfxGenericContainer(p_libofx_context, para_parentcontainer, para_tag_identifier)
32 {
33 
34 //statement_tree_top=statement_tree.insert(statement_tree_top, NULL);
35 //security_tree_top=security_tree.insert(security_tree_top, NULL);
36 
37 }
38 OfxMainContainer::~OfxMainContainer()
39 {
40  message_out(DEBUG, "Entering the main container's destructor");
41  tree<OfxGenericContainer *>::iterator tmp = security_tree.begin();
42 
43  while (tmp != security_tree.end())
44  {
45  message_out(DEBUG, "Deleting " + (*tmp)->type);
46  delete (*tmp);
47  ++tmp;
48  }
49  tmp = account_tree.begin();
50  while (tmp != account_tree.end())
51  {
52  message_out(DEBUG, "Deleting " + (*tmp)->type);
53  delete (*tmp);
54  ++tmp;
55  }
56 }
57 int OfxMainContainer::add_container(OfxGenericContainer * container)
58 {
59  message_out(DEBUG, "OfxMainContainer::add_container for element " + container->tag_identifier + "; destroying the generic container");
60  /* Call gen_event anyway, it could be a status container or similar */
61  container->gen_event();
62  if (container != this) {
63  delete container;
64  }
65  return 0;
66 }
67 
68 int OfxMainContainer::add_container(OfxSecurityContainer * container)
69 {
70  message_out(DEBUG, "OfxMainContainer::add_container, adding a security");
71  security_tree.insert(security_tree.begin(), container);
72  return true;
73 
74 
75 }
76 
77 int OfxMainContainer::add_container(OfxAccountContainer * container)
78 {
79  message_out(DEBUG, "OfxMainContainer::add_container, adding an account");
80  if ( account_tree.size() == 0)
81  {
82  message_out(DEBUG, "OfxMainContainer::add_container, account is the first account");
83  account_tree.insert(account_tree.begin(), container);
84  }
85  else
86  {
87  message_out(DEBUG, "OfxMainContainer::add_container, account is not the first account");
89  tmp += (account_tree.number_of_siblings(tmp)); //Find last account
90  account_tree.insert_after(tmp, container);
91  }
92  return true;
93 }
94 
95 int OfxMainContainer::add_container(OfxStatementContainer * container)
96 {
97  message_out(DEBUG, "OfxMainContainer::add_container, adding a statement");
99  //cerr<< "size="<<account_tree.size()<<"; num_sibblings="<<account_tree.number_of_siblings(tmp)<<endl;
100  tmp += (account_tree.number_of_siblings(tmp)); //Find last account
101 
102  if (account_tree.is_valid(tmp))
103  {
104  message_out(DEBUG, "1: tmp is valid, Accounts are present");
105  tree<OfxGenericContainer *>::iterator child = account_tree.begin(tmp); // FIXME: Variable "child" is unused. Is there an error below?
106  if (account_tree.number_of_children(tmp) != 0)
107  {
108  message_out(DEBUG, "There are already children for this account");
109  account_tree.insert(tmp.begin(), container);
110 
111  }
112  else
113  {
114  message_out(DEBUG, "There are no children for this account");
115  account_tree.append_child(tmp, container);
116  }
117  container->add_account(&( ((OfxAccountContainer *)(*tmp))->data));
118  return true;
119  }
120  else
121  {
122  message_out(ERROR, "OfxMainContainer::add_container, no accounts are present (tmp is invalid)");
123  return false;
124  }
125 }
126 
127 int OfxMainContainer::add_container(OfxTransactionContainer * container)
128 {
129  message_out(DEBUG, "OfxMainContainer::add_container, adding a transaction");
130 
131  if ( account_tree.size() != 0)
132  {
134  //cerr<< "size="<<account_tree.size()<<"; num_sibblings="<<account_tree.number_of_siblings(tmp)<<endl;
135  tmp += (account_tree.number_of_siblings(tmp)); //Find last account
136  if (account_tree.is_valid(tmp))
137  {
138  message_out(DEBUG, "OfxMainContainer::add_container: tmp is valid, Accounts are present");
139  account_tree.append_child(tmp, container);
140  container->add_account(&(((OfxAccountContainer *)(*tmp))->data));
141  return true;
142  }
143  else
144  {
145  message_out(ERROR, "OfxMainContainer::add_container: tmp is invalid!");
146  return false;
147  }
148  }
149  else
150  {
151  message_out(ERROR, "OfxMainContainer::add_container: the tree is empty!");
152  return false;
153  }
154 }
155 
156 int OfxMainContainer::add_container(OfxPositionContainer * container)
157 {
158  message_out(DEBUG, "OfxMainContainer::add_container, adding a position");
159 
160  if ( account_tree.size() != 0)
161  {
163  //cerr<< "size="<<account_tree.size()<<"; num_sibblings="<<account_tree.number_of_siblings(tmp)<<endl;
164  tmp += (account_tree.number_of_siblings(tmp)); //Find last account
165  if (account_tree.is_valid(tmp))
166  {
167  message_out(DEBUG, "OfxMainContainer::add_container: tmp is valid, Accounts are present");
168  account_tree.append_child(tmp, container);
169  container->add_account(&(((OfxAccountContainer *)(*tmp))->data));
170  return true;
171  }
172  else
173  {
174  message_out(ERROR, "OfxMainContainer::add_container: tmp is invalid!");
175  return false;
176  }
177  }
178  else
179  {
180  message_out(ERROR, "OfxMainContainer::add_container: the tree is empty!");
181  return false;
182  }
183 }
184 
186 {
187  message_out(DEBUG, "Begin walking the trees of the main container to generate events");
188  tree<OfxGenericContainer *>::iterator tmp = security_tree.begin();
189  //cerr<<"security_tree.size(): "<<security_tree.size()<<endl;
190  int i = 0;
191  while (tmp != security_tree.end())
192  {
193  message_out(DEBUG, "Looping...");
194  //cerr <<i<<endl;
195  i++;
196  (*tmp)->gen_event();
197  ++tmp;
198  }
199  tmp = account_tree.begin();
200  //cerr<<account_tree.size()<<endl;
201  i = 0;
202  while (tmp != account_tree.end())
203  {
204  //cerr<< "i="<<i<<"; depth="<<account_tree.depth(tmp)<<endl;
205  i++;
206  (*tmp)->gen_event();
207  ++tmp;
208  }
209  message_out(DEBUG, "End walking the trees of the main container to generate events");
210 
211  return true;
212 }
213 
214 OfxSecurityData * OfxMainContainer::find_security(std::string unique_id)
215 {
216  message_out(DEBUG, "OfxMainContainer::find_security() Begin.");
217 
219  OfxSecurityData * retval = NULL;
220  while (tmp != security_tree.end() && retval == NULL)
221  {
222  if (((OfxSecurityContainer*)(*tmp))->data.unique_id == unique_id)
223  {
224  message_out(DEBUG, (std::string)"Security " + ((OfxSecurityContainer*)(*tmp))->data.unique_id + " found.");
225  retval = &((OfxSecurityContainer*)(*tmp))->data;
226  }
227  ++tmp;
228  }
229  return retval;
230 }
Represents a security, such as a stock or bond.
A generic container for an OFX SGML element. Every container inherits from OfxGenericContainer.
pre_order_iterator begin() const
Return iterator to the beginning of the tree.
Definition: tree.hh:581
Represents a generic transaction.
unsigned int number_of_siblings(const iterator_base &) const
Count the number of &#39;next&#39; siblings of node at iterator.
Definition: tree.hh:1458
std::string tag_identifier
int message_out(OfxMsgType error_type, const std::string message)
Message output function.
Definition: messages.cpp:67
Iterator which traverses only the nodes which are siblings of each other.
Definition: tree.hh:231
iter insert_after(iter position, const T &x)
Insert node as next sibling of node pointed to by position.
Definition: tree.hh:890
unsigned int number_of_children(const iterator_base &) const
Count the number of children of node at position.
Definition: tree.hh:1442
pre_order_iterator end() const
Return iterator to the end of the tree.
Definition: tree.hh:587
Represents a statement for either a bank account or a credit card account.
virtual int gen_event()
Generate libofx.h events.
iter append_child(iter position)
Insert empty node as last child of node pointed to by position.
Definition: tree.hh:742
iter insert(iter position, const T &x)
Insert node as previous sibling of node pointed to by position.
Definition: tree.hh:829
Represents an investment position, such as a stock or bond.
LibOFX internal object code.
Depth-first iterator, first accessing the node, then its children.
Definition: tree.hh:162
Message IO functionality.
bool is_valid(const iterator_base &) const
Determine whether the iterator is an &#39;end&#39; iterator and thus not actually pointing to a node...
Definition: tree.hh:1523
int size() const
Count the total number of nodes.
Definition: tree.hh:1408
Represents a bank account or a credit card account.
int gen_event()
Generate libofx.h events.