[view] Populate three columns in general ledger
[abstract.git] / storage / base / aaLoadFactList.cpp
blob3c20917143c0ffafd7b521115cf06f66b497ba72
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent tw=79 ft=cpp: */
3 /*
4 * Copyright (C) 2007,2008 Sergey Yanovich <ynvich@gmail.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 #include "xpcom-config.h"
24 #include "nsCOMPtr.h"
25 #include "nsIMutableArray.h"
26 #include "nsComponentManagerUtils.h"
27 #include "nsStringAPI.h"
28 #include "nsEmbedString.h"
30 /* Unfrozen API */
31 #include "mozIStorageConnection.h"
32 #include "mozIStorageStatement.h"
34 /* Project includes */
35 #include "aaIResource.h"
36 #include "aaIFlow.h"
37 #include "aaIEvent.h"
38 #include "aaIFact.h"
39 #include "aaITransaction.h"
40 #include "aaLoadFactList.h"
42 #define AA_LOADFACTLIST_SELECT_QUERY "SELECT strftime('%s',transfer.day),\
43 transfer.event_id, transfer.id, fromF.flow_id, toF.flow_id, transfer.amount,\
44 txn.value, fromFl.tag, toFl.tag, txn.earnings, resource.id, resource.tag\
45 FROM transfer\
46 LEFT JOIN txn ON transfer.day == txn.day AND transfer.event_id == txn.event_id\
47 AND transfer.id == txn.transfer_id\
48 LEFT JOIN fact AS fromF ON transfer.day == fromF.day AND transfer.event_id\
49 == fromF.event_id AND transfer.id == fromF.transfer_id AND\
50 fromF.side == 1\
51 LEFT JOIN fact AS toF ON transfer.day == toF.day AND transfer.event_id ==\
52 toF.event_id AND transfer.id == toF.transfer_id AND toF.side == 0\
53 LEFT JOIN flow AS fromFl ON fromF.flow_id=fromFl.id\
54 LEFT JOIN flow AS toFl ON toF.flow_id=toFl.id\
55 LEFT JOIN term ON term.flow_id=IFNULL(toFl.id,fromFl.id) AND \
56 term.side=IFNULL(toF.side,fromF.side)\
57 LEFT JOIN resource ON resource.id=term.resource_id"
59 aaLoadFactList::aaLoadFactList(nsISupports *aOuter)
60 :aaLoadQuery(aOuter)
64 aaLoadFactList::~aaLoadFactList()
68 NS_IMPL_ISUPPORTS_INHERITED0(aaLoadFactList,
69 aaLoadQuery)
71 /* Virtual methods */
72 const char*
73 aaLoadFactList::getSelectQuery() const
75 return AA_LOADFACTLIST_SELECT_QUERY;
78 aaIDataNode*
79 aaLoadFactList::fetchRow()
81 nsresult rv;
82 PRInt64 id, eventId, fromId = 0, toId = 0;
83 PRTime time;
84 PRBool isNull;
85 double sum, value, earnings;
86 nsEmbedCString tag;
88 rv = mStatement->GetInt64(0, &time);
89 if (NS_FAILED(rv))
90 return nsnull;
91 time *= 1000000;
93 rv = mStatement->GetInt64(1, &eventId);
94 if (NS_FAILED(rv))
95 return nsnull;
97 nsCOMPtr<aaIEvent> event = do_CreateInstance("@aasii.org/base/event;1");
98 if (NS_UNLIKELY( ! event ))
99 return nsnull;
100 event->SetTime( time );
101 event->SetId( eventId );
103 rv = mStatement->GetInt64(2, &id);
104 NS_ENSURE_SUCCESS(rv, nsnull);
106 nsCOMPtr<aaIFlow> fromFlow(do_CreateInstance("@aasii.org/base/flow;1"));
107 nsCOMPtr<aaIFlow> toFlow(do_CreateInstance("@aasii.org/base/flow;1"));
108 nsCOMPtr<aaIFlow> finres(do_CreateInstance("@aasii.org/base/income-flow;1"));
109 NS_ENSURE_TRUE(fromFlow && toFlow, nsnull);
111 rv = mStatement->GetIsNull(3, &isNull);
112 NS_ENSURE_SUCCESS(rv, nsnull);
114 PRInt64 resourceId;
115 rv = mStatement->GetInt64(10, &resourceId);
116 NS_ENSURE_SUCCESS(rv, nsnull);
118 nsCOMPtr<aaIResource> resource
119 = do_CreateInstance("@aasii.org/base/resource;1");
120 NS_ENSURE_TRUE(resource, nsnull);
121 resource->SetId(resourceId);
123 rv = mStatement->GetUTF8String(11, tag);
124 NS_ENSURE_SUCCESS(rv, nsnull);
125 rv = resource->SetTag(NS_ConvertUTF8toUTF16(tag));
126 NS_ENSURE_SUCCESS(rv, nsnull);
128 if (!isNull) {
129 rv = mStatement->GetInt64(3, &fromId);
130 NS_ENSURE_SUCCESS(rv, nsnull);
131 fromFlow->SetId(fromId);
133 rv = mStatement->GetUTF8String(7, tag);
134 NS_ENSURE_SUCCESS(rv, nsnull);
135 rv = fromFlow->SetTag(NS_ConvertUTF8toUTF16(tag));
136 NS_ENSURE_SUCCESS(rv, nsnull);
137 rv = fromFlow->SetTakeResource(resource);
138 NS_ENSURE_SUCCESS(rv, nsnull);
139 } else {
140 fromFlow = finres;
143 rv = mStatement->GetIsNull(4, &isNull);
144 NS_ENSURE_SUCCESS(rv, nsnull);
146 if (!isNull) {
147 rv = mStatement->GetInt64(4, &toId);
148 NS_ENSURE_SUCCESS(rv, nsnull);
149 toFlow->SetId(toId);
151 rv = mStatement->GetUTF8String(8, tag);
152 NS_ENSURE_SUCCESS(rv, nsnull);
153 rv = toFlow->SetTag(NS_ConvertUTF8toUTF16(tag));
154 NS_ENSURE_SUCCESS(rv, nsnull);
155 rv = toFlow->SetGiveResource(resource);
156 NS_ENSURE_SUCCESS(rv, nsnull);
157 } else {
158 toFlow = finres;
161 NS_ENSURE_TRUE(toId || fromId, nsnull);
163 rv = mStatement->GetDouble(5, &sum);
164 NS_ENSURE_SUCCESS(rv, nsnull);
166 rv = mStatement->GetDouble(6, &value);
167 NS_ENSURE_SUCCESS(rv, nsnull);
169 rv = mStatement->GetIsNull(9, &isNull);
170 NS_ENSURE_SUCCESS(rv, nsnull);
172 if (!isNull) {
173 rv = mStatement->GetDouble(9, &earnings);
174 NS_ENSURE_SUCCESS(rv, nsnull);
177 nsCOMPtr<aaIFact> fact(do_CreateInstance("@aasii.org/base/fact;1"));
178 if (! fact)
179 return nsnull;
181 fact->SetId(id);
182 fact->SetEvent(event);
183 fact->SetTakeFrom(fromFlow);
184 fact->SetGiveTo(toFlow);
185 fact->SetAmount(sum);
187 aaITransaction *txn;
188 CallCreateInstance("@aasii.org/base/transaction;1", nsnull, &txn);
190 txn->SetFact(fact);
191 txn->SetValue(value);
192 if (!isNull)
193 txn->SetEarnings(earnings);
194 return txn;