[xpunit] Rename from 'cxxunit' and switch to mozbuild (bug #114)
[abstract.git] / storage / aaLoadFlowChart.cpp
blob86586599d01ebc8829f4a0ebd37c9442449e1818
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 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 <abstract/aacore.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 "unstable/mozIStorageConnection.h"
32 #include "unstable/mozIStorageStatement.h"
34 /* Project includes */
35 #include <abstract/base/aaIEntity.h>
36 #include <abstract/base/aaIResource.h>
37 #include <abstract/base/aaIFlow.h>
38 #include <abstract/storage/aaILoadQuery.h>
39 #include "aaLoadFlowChart.h"
41 #define AA_LOADFLOWEXT_SELECT_QUERY "SELECT flow.id, flow.tag, flow.entity_id,\
42 entity.tag, giveR.id, giveR.tag, takeR.id, takeR.tag, flow.rate\
43 FROM flow\
44 LEFT JOIN entity ON flow.entity_id == entity.id\
45 LEFT JOIN term AS giveT ON giveT.flow_id = flow.id AND giveT.side = 0\
46 LEFT JOIN resource AS giveR ON giveR.id = giveT.resource_id\
47 LEFT JOIN term AS takeT ON takeT.flow_id = flow.id AND takeT.side = 1\
48 LEFT JOIN resource AS takeR ON takeR.id = takeT.resource_id"
49 #define AA_FLOW_ITEM_CONTRACT "@aasii.org/base/flow;1"
50 #define AA_INCOMEFLOW_CONTRACT "@aasii.org/base/income-flow;1"
52 aaLoadFlowChart::aaLoadFlowChart(nsISupports *aOuter)
53 :aaLoadQuery(aOuter)
57 aaLoadFlowChart::~aaLoadFlowChart()
61 NS_IMPL_ISUPPORTS_INHERITED0(aaLoadFlowChart,
62 aaLoadQuery)
64 NS_IMETHODIMP
65 aaLoadFlowChart::Load()
67 nsresult rv;
68 rv = aaLoadQuery::Load();
69 NS_ENSURE_SUCCESS(rv, rv);
71 nsCOMPtr<aaIFlow> pl = do_CreateInstance(AA_INCOMEFLOW_CONTRACT, &rv);
72 NS_ENSURE_TRUE(pl, rv);
74 return mData->AppendElement(pl, PR_FALSE);
77 /* Virtual methods */
78 const char*
79 aaLoadFlowChart::getSelectQuery() const
81 return AA_LOADFLOWEXT_SELECT_QUERY;
84 aaIDataNode*
85 aaLoadFlowChart::fetchRow()
87 nsresult rv;
88 PRInt64 flowId, entityId, giveId, takeId;
89 double rate;
90 nsEmbedCString tag;
91 rv = mStatement->GetInt64(0, &flowId);
92 if (NS_FAILED(rv))
93 return nsnull;
95 rv = mStatement->GetUTF8String(1, tag);
96 if (NS_FAILED(rv))
97 return nsnull;
99 rv = mStatement->GetInt64(2, &entityId);
100 if (NS_FAILED(rv))
101 return nsnull;
103 nsEmbedCString entityTag;
104 rv = mStatement->GetUTF8String(3, entityTag);
105 if (NS_FAILED(rv))
106 return nsnull;
108 rv = mStatement->GetInt64(4, &giveId);
109 if (NS_FAILED(rv))
110 return nsnull;
112 nsEmbedCString giveTag;
113 rv = mStatement->GetUTF8String(5, giveTag);
114 if (NS_FAILED(rv))
115 return nsnull;
117 rv = mStatement->GetInt64(6, &takeId);
118 if (NS_FAILED(rv))
119 return nsnull;
121 nsEmbedCString takeTag;
122 rv = mStatement->GetUTF8String(7, takeTag);
123 if (NS_FAILED(rv))
124 return nsnull;
126 rv = mStatement->GetDouble(8, &rate);
127 if (NS_FAILED(rv))
128 return nsnull;
130 nsCOMPtr<aaIEntity> entity(do_CreateInstance("@aasii.org/base/entity;1"));
131 if (! entity)
132 return nsnull;
134 rv = entity->SetId(entityId);
135 if (NS_FAILED(rv))
136 return nsnull;
138 rv = entity->SetTag(NS_ConvertUTF8toUTF16(entityTag));
139 if (NS_FAILED(rv))
140 return nsnull;
142 nsCOMPtr<aaIResource> give(do_CreateInstance("@aasii.org/base/resource;1"));
143 if (! give)
144 return nsnull;
146 rv = give->SetId(giveId);
147 if (NS_FAILED(rv))
148 return nsnull;
150 rv = give->SetTag(NS_ConvertUTF8toUTF16(giveTag));
151 if (NS_FAILED(rv))
152 return nsnull;
154 nsCOMPtr<aaIResource> take(do_CreateInstance("@aasii.org/base/resource;1"));
155 if (! take)
156 return nsnull;
158 rv = take->SetId(takeId);
159 if (NS_FAILED(rv))
160 return nsnull;
162 rv = take->SetTag(NS_ConvertUTF8toUTF16(takeTag));
163 if (NS_FAILED(rv))
164 return nsnull;
166 aaIFlow* flow;
167 CallCreateInstance("@aasii.org/base/flow;1", nsnull, &flow);
168 if (! flow)
169 return nsnull;
171 flow->SetId(flowId);
172 flow->SetTag(NS_ConvertUTF8toUTF16(tag));
173 flow->SetEntity(entity);
174 flow->SetGiveResource(give);
175 flow->SetTakeResource(take);
176 flow->SetRate(rate);
178 return flow;