transport: pass correct channel
[abstract.git] / storage / base / aaSaveEntity.cpp
bloba5b5b8071338665c2d19ae8ab3cdf894797434be
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 "nsISupportsPrimitives.h"
26 #include "nsXPCOMCID.h"
27 #include "nsComponentManagerUtils.h"
28 #include "nsStringAPI.h"
30 /* Unfrozen API */
32 /* Project includes */
33 #include "aaIEntity.h"
34 #include "aaISqliteChannel.h"
35 #include "aaISqlRequest.h"
36 #include "aaBaseLoaders.h"
37 #include "aaSaveKeys.h"
38 #include "aaSaveEntity.h"
40 aaSaveEntity::aaSaveEntity()
44 NS_IMPL_ISUPPORTS1(aaSaveEntity,
45 aaISqlTransaction)
47 /* aaISqlTransaction */
48 NS_IMETHODIMP
49 aaSaveEntity::SetParam(nsISupports *aParam)
51 nsresult rv;
52 mEntity = do_QueryInterface(aParam, &rv);
53 NS_ENSURE_SUCCESS(rv, rv);
55 return NS_OK;
58 NS_IMETHODIMP
59 aaSaveEntity::Execute(aaISqliteChannel *aChannel, nsISupports * * _retval)
61 NS_ENSURE_ARG_POINTER(aChannel);
62 NS_ENSURE_TRUE(mEntity, NS_ERROR_NOT_INITIALIZED);
63 nsresult rv;
65 PRInt64 id;
66 rv = mEntity->GetId(&id);
67 NS_ENSURE_SUCCESS(rv, rv);
69 if (!id) {
70 rv = insert(aChannel);
71 NS_ENSURE_SUCCESS(rv, rv);
73 else {
74 rv = update(aChannel);
75 if (NS_FAILED(rv)) return rv;
78 if (_retval)
79 *_retval = nsnull;
81 return NS_OK;
84 /* private methods */
85 nsresult
86 aaSaveEntity::insert(aaISqliteChannel *aChannel)
88 nsresult rv;
89 if (!mInserter) {
90 mInserter = do_CreateInstance(AA_INSERT_ENTITY_CONTRACT, &rv);
91 NS_ENSURE_SUCCESS(rv, rv);
94 rv = mInserter->SetFilter(mEntity);
95 NS_ENSURE_SUCCESS(rv, rv);
97 rv = aChannel->Open(mInserter, nsnull);
98 NS_ENSURE_SUCCESS(rv, rv);
100 if (!mIdLoader) {
101 mIdLoader = do_CreateInstance(AA_LOADLASTID_CONTRACT, &rv);
102 NS_ENSURE_SUCCESS(rv, rv);
104 nsCOMPtr<nsISupportsCString> filter
105 = do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
106 NS_ENSURE_SUCCESS(rv, rv);
108 rv = filter->SetData(NS_LITERAL_CSTRING("entity"));
109 NS_ENSURE_SUCCESS(rv, rv);
111 rv = mIdLoader->SetFilter(filter);
112 NS_ENSURE_SUCCESS(rv, rv);
115 nsCOMPtr<aaILoadObserver> id
116 = do_CreateInstance(AA_SQLIDLISTENER_CONTRACT, mEntity, &rv);
117 NS_ENSURE_SUCCESS(rv, rv);
119 rv = aChannel->Open(mIdLoader, id);
120 NS_ENSURE_SUCCESS(rv, rv);
122 return NS_OK;
125 nsresult
126 aaSaveEntity::update(aaISqliteChannel *aChannel)
128 nsresult rv;
129 if (!mUpdater) {
130 mUpdater = do_CreateInstance(AA_UPDATE_ENTITY_CONTRACT, &rv);
131 NS_ENSURE_SUCCESS(rv, rv);
134 rv = mUpdater->SetFilter(mEntity);
135 NS_ENSURE_SUCCESS(rv, rv);
137 rv = aChannel->Open(mUpdater, nsnull);
138 if (NS_FAILED(rv)) return rv;
140 return NS_OK;