kpilot
dataproxy.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "dataproxy.h"
00029 #include "record.h"
00030
00031 #include "options.h"
00032
00033 DataProxy::DataProxy() : fIterator( fRecords )
00034 {
00035 FUNCTIONSETUP;
00036 }
00037
00038 DataProxy::~DataProxy()
00039 {
00040 FUNCTIONSETUP;
00041
00042 qDeleteAll( fRecords );
00043 }
00044
00045 QString DataProxy::create( Record *record )
00046 {
00047 FUNCTIONSETUP;
00048
00049
00050 QString uniqueId = generateUniqueId();
00051
00052
00053 record->setId( uniqueId );
00054
00055 DEBUGKPILOT << "Record created with id: [" << uniqueId << "]";
00056
00057 fRecords.insert( uniqueId, record );
00058
00059
00060 fCreated.insert( uniqueId, false );
00061 fCounter.created();
00062
00063 return uniqueId;
00064 }
00065
00066 void DataProxy::remove( const QString &id )
00067 {
00068 FUNCTIONSETUP;
00069
00070 Record *rec = fRecords.value( id );
00071 if( rec == 0L )
00072 {
00073
00074 return;
00075 }
00076
00077 DEBUGKPILOT << "Removing record id: [" << id << "]";
00078
00079 fRecords.remove( id );
00080
00081
00082 fDeletedRecords.insert( rec->id(), rec );
00083 fDeleted.insert( rec->id(), false );
00084 fCounter.deleted();
00085 }
00086
00087 void DataProxy::update( const QString &id, Record *newRecord )
00088 {
00089 FUNCTIONSETUP;
00090
00091 Record *oldRecord = fRecords.value( id );
00092 if( oldRecord == 0L )
00093 {
00094
00095 DEBUGKPILOT << "There is no record with id: [" << id
00096 << "]. Record not updated and not added.";
00097 return;
00098 }
00099 DEBUGKPILOT << "Updating record id: [" << id << "]";
00100
00101 newRecord->setId( id );
00102 fRecords.insert( id, newRecord );
00103
00104
00105 fOldRecords.insert( id, oldRecord );
00106 fCounter.updated();
00107 }
00108
00109 QList<QString> DataProxy::ids() const
00110 {
00111 return fRecords.keys();
00112 }
00113
00114 const CUDCounter* DataProxy::counter() const
00115 {
00116 FUNCTIONSETUP;
00117
00118 return &fCounter;
00119 }
00120
00121 void DataProxy::setEndcount()
00122 {
00123 FUNCTIONSETUP;
00124
00125 fCounter.setEndCount( fRecords.size() );
00126 }
00127
00128 void DataProxy::setIterateMode( const Mode m )
00129 {
00130 FUNCTIONSETUP;
00131
00132 fMode = m;
00133 }
00134
00135 unsigned int DataProxy::recordCount() const
00136 {
00137 return fRecords.size();
00138 }
00139
00140 Record* DataProxy::find( const QString &id ) const
00141 {
00142 FUNCTIONSETUP;
00143 return fRecords.value( id );
00144 }
00145
00146 void DataProxy::resetIterator()
00147 {
00148 fIterator = QMapIterator<QString, Record*>( fRecords );
00149 }
00150
00151 bool DataProxy::hasNext() const
00152 {
00153 FUNCTIONSETUP;
00154
00155 if( fMode == All )
00156 {
00157 return fIterator.hasNext();
00158 }
00159 else
00160 {
00161 QMapIterator<QString, Record*> tmpIt = fIterator;
00162 while( tmpIt.hasNext() )
00163 {
00164 Record *rec = tmpIt.next().value();
00165 if( rec->isModified() )
00166 {
00167 return true;
00168 }
00169 }
00170 }
00171
00172 return false;
00173 }
00174
00175 Record* DataProxy::next()
00176 {
00177 FUNCTIONSETUP;
00178
00179 if( fMode == All )
00180 {
00181 return fIterator.next().value();
00182 }
00183 else
00184 {
00185 while( fIterator.hasNext() )
00186 {
00187 Record *rec = fIterator.next().value();
00188 if( rec->isModified() )
00189 {
00190 return rec;
00191 }
00192 }
00193 }
00194
00195 return 0L;
00196 }
00197
00198 bool DataProxy::commit()
00199 {
00200 FUNCTIONSETUP;
00201
00202
00203 QStringListIterator it( fCreated.keys() );
00204
00205 DEBUGKPILOT << "Committing: [" << fCreated.size() << "] records.";
00206
00207
00208 fCreated.clear();
00209
00210 while( it.hasNext() )
00211 {
00212 QString id = it.next();
00213
00214 DEBUGKPILOT << "Committing id: [" << id << "]";
00215
00216 Record *rec = find( id );
00217 if( rec )
00218 {
00219 if( !commitCreate( rec ) )
00220 {
00221
00222 return false;
00223 }
00224
00225
00226
00227
00228 if( rec->id() != id )
00229 {
00230 fCreated.remove( id );
00231 fCreated.insert( rec->id(), true );
00232
00233 fRecords.remove( id );
00234 fRecords.insert( rec->id(), rec );
00235
00236 fChangedIds.insert( id, rec->id() );
00237 }
00238 else
00239 {
00240 fCreated.insert( rec->id(), true );
00241 }
00242 }
00243 else
00244 {
00245 DEBUGKPILOT << "Record with id: [" << id << "] not found!";
00246 }
00247 }
00248
00249
00250 DEBUGKPILOT << "Updating: [" << fOldRecords.size() << "] records.";
00251
00252 QListIterator<Record*> i( fOldRecords.values() );
00253 while( i.hasNext() )
00254 {
00255
00256 Record *oldRec = i.next();
00257 QString id = oldRec->id();
00258
00259
00260 Record *rec = find( id );
00261
00262 if( rec && !fCreated.value( id ) )
00263 {
00264 if( !commitUpdate( rec ) )
00265 {
00266
00267 return false;
00268 }
00269
00270
00271
00272 QString newId = rec->id();
00273
00274 if( newId != id )
00275 {
00276 oldRec->setId( newId );
00277
00278 fChangedIds.insert( id, newId );
00279 }
00280 fUpdated.insert( rec->id(), true );
00281 }
00282 }
00283
00284
00285 DEBUGKPILOT << "Deleting: [" << fDeletedRecords.size() << "] records.";
00286
00287 i = QListIterator<Record*>( fDeletedRecords.values() );
00288 while( i.hasNext() )
00289 {
00290 Record *oldRec = i.next();
00291
00292 if( !fDeleted.value( oldRec->id() ) )
00293 {
00294 DEBUGKPILOT << "Deleting record: [" << oldRec->id() << "].";
00295
00296 if( !commitDelete( oldRec ) )
00297 {
00298
00299 return false;
00300 }
00301
00302
00303
00304 fDeleted.insert( oldRec->id(), true );
00305 }
00306 }
00307
00308
00309 return _commit();
00310 }
00311
00312 bool DataProxy::rollback()
00313 {
00314 FUNCTIONSETUP;
00315
00316
00317 QStringListIterator it( fCreated.keys() );
00318
00319 while( it.hasNext() )
00320 {
00321 QString id = it.next();
00322
00323
00324 Record *rec = find( id );
00325 if( rec && fCreated.value( id ) )
00326 {
00327 DEBUGKPILOT << "Deleting created record: [" << rec->id() << "].";
00328
00329 commitDelete( rec );
00330 fCreated.insert( rec->id(), false );
00331 }
00332 }
00333
00334
00335 fCreated.clear();
00336
00337
00338 QListIterator<Record*> i( fOldRecords.values() );
00339 while( i.hasNext() )
00340 {
00341 Record *oldRec = i.next();
00342 if( fUpdated.value( oldRec->id() ) )
00343 {
00344 DEBUGKPILOT << "Restoring changed record: [" << oldRec->id() << "].";
00345
00346 QString oldId = oldRec->id();
00347 commitUpdate( oldRec );
00348
00349
00350 if( oldRec->id() != oldId )
00351 {
00352 fUpdated.remove( oldId );
00353
00354 fChangedIds.insert( oldId, oldRec->id() );
00355 }
00356
00357 fUpdated.insert( oldRec->id(), false );
00358 }
00359 }
00360
00361
00362 i = QListIterator<Record*>( fDeletedRecords.values() );
00363 while( i.hasNext() )
00364 {
00365 Record *oldRec = i.next();
00366
00367 if( fDeleted.value( oldRec->id() ) )
00368 {
00369 DEBUGKPILOT << "Restoring deleted record: [" << oldRec->id() << "].";
00370
00371 QString oldId = oldRec->id();
00372 commitCreate( oldRec );
00373
00374
00375 if( oldRec->id() != oldId )
00376 {
00377 fDeleted.remove( oldId );
00378
00379 fChangedIds.insert( oldId, oldRec->id() );
00380 }
00381
00382 fDeleted.insert( oldRec->id(), false );
00383 }
00384 }
00385
00386
00387 return _rollback();
00388 }
00389
00390 QMap<QString,QString> DataProxy::changedIds()
00391 {
00392 return fChangedIds;
00393 }