[storage] Direct non-monetary gains and losses (bug #123)
[abstract.git] / storage / aaLoadFlow.cpp
blobd3c4585afc90615246a246bba0c0f5134a19e1b8
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 "aaLoadFlow.h"
41 #define AA_LOADFLOW_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"
51 aaLoadFlow::aaLoadFlow(nsISupports *aOuter)
52 :aaLoadQuery(aOuter)
56 aaLoadFlow::~aaLoadFlow()
60 NS_IMPL_ISUPPORTS_INHERITED0(aaLoadFlow,
61 aaLoadQuery)
63 /* Virtual methods */
64 const char*
65 aaLoadFlow::getSelectQuery() const
67 return AA_LOADFLOW_SELECT_QUERY;
70 aaIDataNode*
71 aaLoadFlow::fetchRow()
73 nsresult rv;
74 PRInt64 flowId, entityId, giveId, takeId;
75 double rate;
76 nsEmbedCString tag;
77 rv = mStatement->GetInt64(0, &flowId);
78 if (NS_FAILED(rv))
79 return nsnull;
81 rv = mStatement->GetUTF8String(1, tag);
82 if (NS_FAILED(rv))
83 return nsnull;
85 rv = mStatement->GetInt64(2, &entityId);
86 if (NS_FAILED(rv))
87 return nsnull;
89 nsEmbedCString entityTag;
90 rv = mStatement->GetUTF8String(3, entityTag);
91 if (NS_FAILED(rv))
92 return nsnull;
94 rv = mStatement->GetInt64(4, &giveId);
95 if (NS_FAILED(rv))
96 return nsnull;
98 nsEmbedCString giveTag;
99 rv = mStatement->GetUTF8String(5, giveTag);
100 if (NS_FAILED(rv))
101 return nsnull;
103 rv = mStatement->GetInt64(6, &takeId);
104 if (NS_FAILED(rv))
105 return nsnull;
107 nsEmbedCString takeTag;
108 rv = mStatement->GetUTF8String(7, takeTag);
109 if (NS_FAILED(rv))
110 return nsnull;
112 rv = mStatement->GetDouble(8, &rate);
113 if (NS_FAILED(rv))
114 return nsnull;
116 nsCOMPtr<aaIEntity> entity(do_CreateInstance("@aasii.org/base/entity;1"));
117 if (! entity)
118 return nsnull;
120 rv = entity->SetId(entityId);
121 if (NS_FAILED(rv))
122 return nsnull;
124 rv = entity->SetTag(NS_ConvertUTF8toUTF16(entityTag));
125 if (NS_FAILED(rv))
126 return nsnull;
128 nsCOMPtr<aaIResource> give(do_CreateInstance("@aasii.org/base/resource;1"));
129 if (! give)
130 return nsnull;
132 rv = give->SetId(giveId);
133 if (NS_FAILED(rv))
134 return nsnull;
136 rv = give->SetTag(NS_ConvertUTF8toUTF16(giveTag));
137 if (NS_FAILED(rv))
138 return nsnull;
140 nsCOMPtr<aaIResource> take(do_CreateInstance("@aasii.org/base/resource;1"));
141 if (! take)
142 return nsnull;
144 rv = take->SetId(takeId);
145 if (NS_FAILED(rv))
146 return nsnull;
148 rv = take->SetTag(NS_ConvertUTF8toUTF16(takeTag));
149 if (NS_FAILED(rv))
150 return nsnull;
152 aaIFlow* flow;
153 CallCreateInstance("@aasii.org/base/flow;1", nsnull, &flow);
154 if (! flow)
155 return nsnull;
157 flow->SetId(flowId);
158 flow->SetTag(NS_ConvertUTF8toUTF16(tag));
159 flow->SetEntity(entity);
160 flow->SetGiveResource(give);
161 flow->SetTakeResource(take);
162 flow->SetRate(rate);
164 return flow;