bool AssetContract::AddAccountRecord(const Account& theAccount) const // adds // the // account // to the // list. // (When // account // is // created.) { // Load up account list StringMap. Create it if doesn't already exist. // See if account is already there in the map. Add it otherwise. // Save the StringMap back again. (The account records list for a given // instrument definition.) const char* szFunc = "OTAssetContract::AddAccountRecord"; if (theAccount.GetInstrumentDefinitionID() != m_ID) { otErr << szFunc << ": Error: theAccount doesn't have the same asset " "type ID as *this does.\n"; return false; } const Identifier theAcctID(theAccount); const String strAcctID(theAcctID); String strInstrumentDefinitionID, strAcctRecordFile; GetIdentifier(strInstrumentDefinitionID); strAcctRecordFile.Format("%s.a", strInstrumentDefinitionID.Get()); OTDB::Storable* pStorable = nullptr; std::unique_ptr<OTDB::Storable> theAngel; OTDB::StringMap* pMap = nullptr; if (OTDB::Exists(OTFolders::Contract().Get(), strAcctRecordFile.Get())) // the file already exists; let's // try to load it up. pStorable = OTDB::QueryObject(OTDB::STORED_OBJ_STRING_MAP, OTFolders::Contract().Get(), strAcctRecordFile.Get()); else // the account records file (for this instrument definition) doesn't // exist. pStorable = OTDB::CreateObject( OTDB::STORED_OBJ_STRING_MAP); // this asserts already, on failure. theAngel.reset(pStorable); pMap = (nullptr == pStorable) ? nullptr : dynamic_cast<OTDB::StringMap*>(pStorable); // It exists. // if (nullptr == pMap) { otErr << szFunc << ": Error: failed trying to load or create the account records " "file for instrument definition: " << strInstrumentDefinitionID << "\n"; return false; } auto& theMap = pMap->the_map; auto map_it = theMap.find(strAcctID.Get()); if (theMap.end() != map_it) // we found it. { // We were ADDING IT, but it was ALREADY THERE. // (Thus, we're ALREADY DONE.) // Let's just make sure the right instrument definition ID is associated // with this // account // (it better be, since we loaded the account records file based on the // instrument definition ID as its filename...) // const std::string& str2 = map_it->second; // Containing the instrument // definition ID. (Just in // case // someone copied the wrong file here, // -------------------------------- // every account should map // to the SAME instrument definition id.) if (false == strInstrumentDefinitionID.Compare(str2.c_str())) // should // never // happen. { otErr << szFunc << ": Error: wrong instrument definition found in " "account records " "file...\n For instrument definition: " << strInstrumentDefinitionID << "\n " "For account: " << strAcctID << "\n Found wrong instrument definition: " << str2 << "\n"; return false; } return true; // already there (no need to add.) + the instrument // definition ID // matches. } // it wasn't already on the list... // ...so add it. // theMap[strAcctID.Get()] = strInstrumentDefinitionID.Get(); // Then save it back to local storage: // if (!OTDB::StoreObject(*pMap, OTFolders::Contract().Get(), strAcctRecordFile.Get())) { otErr << szFunc << ": Failed trying to StoreObject, while saving updated " "account records file for instrument definition: " << strInstrumentDefinitionID << "\n to contain account ID: " << strAcctID << "\n"; return false; } // Okay, we saved the updated file, with the account added. (done, success.) // return true; }