commsfwtools/preparedefaultcommsdatabase/src/MetaDatabase.cpp
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Implementation of MetaDatabase interface functions for CommsDat
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file 
       
    20  @publishedAll
       
    21  @released
       
    22 */
       
    23 
       
    24 
       
    25 #include <commsdattypesv1_1.h>
       
    26 
       
    27 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    28 #include <commsdat_partner.h>
       
    29 #endif
       
    30 
       
    31 #include "CommsDatInternalDefs.h"
       
    32 #include "CommsDatValidator.h"
       
    33 #include "CommsDatMapper.h"
       
    34 #include "MetaDatabaseVisitor.h"
       
    35 #include <commsdatutils.h>
       
    36 #include <comms-infras/commsdatschema.h>
       
    37 
       
    38 
       
    39 using namespace CommsDat;
       
    40 using namespace CommsDatInternal;
       
    41 using namespace Meta;
       
    42 
       
    43 /*  
       
    44 MMetaDatabase 
       
    45 
       
    46 Implementation of main high-level algorithm for each MMetaDatabase call.
       
    47 Detailed activity left to visitors for each field that needs to be handled.
       
    48 */
       
    49 
       
    50 
       
    51 EXPORT_C MMetaDatabase::MMetaDatabase()
       
    52 /**
       
    53  * Protected and explicit constructor - nothing to construct
       
    54  */
       
    55     {
       
    56     }
       
    57 
       
    58 
       
    59 
       
    60 EXPORT_C MMetaDatabase::~MMetaDatabase()
       
    61 /**
       
    62  * Virtual destructor - nothing to destroy
       
    63  */
       
    64     {
       
    65     }
       
    66 
       
    67 
       
    68 TBool CompareColumn(const TUint32& aFirst, const TUint32& aSecond)
       
    69 // Utility for RArray Find
       
    70     {
       
    71     TBool ret =  ((aFirst & KCDMaskShowType) == (aSecond & KCDMaskShowType));
       
    72     return ret;
       
    73     }
       
    74 
       
    75 //
       
    76 
       
    77 
       
    78 
       
    79 
       
    80 EXPORT_C void MMetaDatabase::LoadL(CMDBSession& aSession)
       
    81 /**
       
    82  * Loads each member of the MMetaDatabase object from the database.
       
    83  * Will (delete and) overwrite any data already in the container.
       
    84  **/
       
    85     {   
       
    86     CMDBSessionImpl* session = aSession.iMDBSessionImpl;
       
    87 
       
    88     // no need for a transaction for read, but do want the write lock.
       
    89    // TBool ownTransaction = session->MaybeOpenTransactionL();
       
    90     
       
    91     TAny* mem[KMMetaDBVisitorMaxSize];
       
    92     TMetaVTableIterator attribIter(this);
       
    93     SVDataTableEntry const* entry = NULL;
       
    94     TMDBVisitorBase* metaDBVisitor = NULL;
       
    95             
       
    96     CMDBElement* thisElement = static_cast<CMDBElement*>(this);
       
    97     
       
    98     __FLOG_STATIC1(KLogComponent, KCDInfoLog, _L("MMetaDatabase::LoadL() called for Element %08x..."), thisElement->ElementId());
       
    99 
       
   100     TInt err(KErrNotFound);
       
   101     
       
   102 #ifdef SYMBIAN_NON_SEAMLESS_NETWORK_BEARER_MOBILITY
       
   103     CCDMapper* mapper = CCDMapper::MaybeCreateMapperLC(*thisElement,*aSession.iMDBSessionImpl,ELoad);
       
   104 #else
       
   105     CCDMapper* mapper = NULL;
       
   106 #endif
       
   107     
       
   108        
       
   109     TUint32& elementId = *thisElement->Data();
       
   110         
       
   111     if ((elementId & KCDMaskShowRecordType) != 0) 
       
   112         {
       
   113         // Table id has been set
       
   114         err = session->LoadElementAttributesL(elementId);
       
   115         }        
       
   116     
       
   117     if (err == KErrNotFound)
       
   118         {
       
   119         if (mapper ||                                     //we have a mapper object  
       
   120             ! CommsDatSchema::IsNode(elementId)        || // Could be a generic record trying to load by name, so let it carry on.
       
   121             CommsDatSchema::IsGenericRecord(elementId)   )// let it find the template record
       
   122             {
       
   123 	        err = KErrNone;	
       
   124 	        }
       
   125         }
       
   126     
       
   127     if (err == KErrNone)
       
   128         {
       
   129         if (!mapper &&
       
   130             (CommsDatSchema::IsRecord(elementId) ) && 
       
   131             !CommsDatSchema::IsTemplate(elementId) 
       
   132             && !CommsDatSchema::IsGenericRecord(elementId))
       
   133             {
       
   134         
       
   135             // This section gets template field where appropriate and ensures minimal call to find in database
       
   136         
       
   137             RArray<TUint32> recordIds;
       
   138             CleanupClosePushL(recordIds);
       
   139             RArray<TUint32> templateIds;
       
   140             CleanupClosePushL(templateIds);
       
   141 
       
   142             // Find all entries in the record
       
   143             TUint32 mask = KCDMaskShowRecordType | KCDMaskShowRecordId | session->GetReadAttributeMask() | KCDUtilityFlag;
       
   144             TUint32 id = elementId & KCDMaskHideAttrAndRes;
       
   145 
       
   146             TInt err = session->StorageL()->FindL(id, mask, recordIds);  
       
   147 
       
   148             if (recordIds.Count())
       
   149                 {
       
   150                 // Find all entries for the template
       
   151                 mask = (KCDMaskShowRecordType | KCDMaskShowRecordId | session->GetReadAttributeMask() | KCDUtilityFlag) & ~ECDHidden;
       
   152             
       
   153                 session->StorageL()->FindL(id & ~KCDMaskShowRecordId, mask, templateIds); // there might not be a template don't need to check err
       
   154         
       
   155                 TInt loc(0);
       
   156                 TInt fieldId(0);
       
   157         
       
   158                 while ( (err == KErrNone || err == KErrNotFound)  &&  (entry = attribIter++) != NULL )
       
   159                     {
       
   160                     metaDBVisitor = static_cast<TMDBVisitorBase*>(entry->iMetaNewL(mem,GetAttribPtr(entry->iOffset))); // target
       
   161                     if (metaDBVisitor) // might be deprecated
       
   162                     	{
       
   163                         metaDBVisitor->SetOwner(thisElement);
       
   164                         // can optimise further by not doing array finds in this way
       
   165                         loc = recordIds.Find(metaDBVisitor->ElementId(), CompareColumn);
       
   166                         if (loc != KErrNotFound)
       
   167                             {
       
   168                             metaDBVisitor->SetElementId(recordIds[loc]);
       
   169                             // know the value is there so just get it
       
   170                             err = metaDBVisitor->GetL(session);
       
   171                 
       
   172                             if (err != KErrNone)
       
   173                                 {
       
   174                                 // oh dear, should have found this.  Something's gone horribly wrong
       
   175                                 __FLOG_STATIC1(KLogComponent, KCDErrLog, _L("MMetaDatabase::LoadL() VisitorGetL failed unexpectedly for Element %08x with err %d"), metaDBVisitor->ElementId());
       
   176                                 User::Leave(err);
       
   177                                 }
       
   178                 
       
   179                             recordIds.Remove(loc);
       
   180                             }
       
   181                         else if (templateIds.Count())
       
   182                             {
       
   183                             loc = templateIds.Find(metaDBVisitor->ElementId(), CompareColumn);
       
   184                             if (loc != KErrNotFound)
       
   185                                 {
       
   186                                 // get the template value instead
       
   187                     
       
   188                                 fieldId = metaDBVisitor->ElementId();
       
   189                                 metaDBVisitor->SetElementId(templateIds[loc]);
       
   190                                 err =metaDBVisitor->GetL(session);
       
   191                                 if (err != KErrNone)
       
   192                                     {
       
   193                                     // Should have found this too  Something else has gone horribly wrong 
       
   194                                     __FLOG_STATIC1(KLogComponent, KCDErrLog, _L("MMetaDatabase::LoadL() VisitorGetL failed unexpectedly for Element %08x with err %d"), metaDBVisitor->ElementId());
       
   195                                     metaDBVisitor->SetElementId(fieldId);
       
   196                                     User::Leave(err);
       
   197                                     }
       
   198                
       
   199                                 metaDBVisitor->SetElementId(fieldId);
       
   200                                 metaDBVisitor->FieldLoaded();
       
   201                                 
       
   202                                 templateIds.Remove(loc);
       
   203                                 }
       
   204                             }
       
   205 
       
   206                         if (loc == KErrNotFound)
       
   207                             {
       
   208                             metaDBVisitor->ClearValue();
       
   209                             }
       
   210                         }
       
   211                     }
       
   212                 }
       
   213             CleanupStack::PopAndDestroy(&templateIds);
       
   214             CleanupStack::PopAndDestroy(&recordIds);
       
   215             }
       
   216         else
       
   217             {
       
   218             DoLoadL(session, thisElement, mapper, err, thisElement->ElementId() & KCDMaskShowRecordId, thisElement->Attributes());
       
   219 
       
   220             /* This is a work around solution for the following case:
       
   221              * There is a table with legacy links. [IAP table, BearerType and Bearer fields in it]. With the
       
   222              * new CommsDat the Bearer field value is the fully resolved TMDBElementId of the linked record.
       
   223              * BUT there is a lot of code which expects this value to be only a record number (as it is 
       
   224              * originally in the config file) after the reading it.
       
   225              * Even worse there is code which tries to read this field via a CMDBField<TInt> while this field is
       
   226              * from now on a CMDBField<someLink>. So if somebody tries to read an integer field we have to check
       
   227              * whether is it a legacy link or not. If it is, then we have to do conversion on the value to contain
       
   228              * only the record number of the linked record and not the TMDBElementId.
       
   229              */
       
   230             if (CommsDatSchema::IsColumn(thisElement->ElementId() | KCDMaskShowRecordId) && 
       
   231              	(thisElement->Type() ==EInt || thisElement->Type()==EUint32))
       
   232              	{
       
   233              	CommsDatSchema::ChangeLegacyLinkFieldValues(thisElement);
       
   234              	}
       
   235             }
       
   236         
       
   237         /***********************************************************/
       
   238 #ifdef SYMBIAN_NON_SEAMLESS_NETWORK_BEARER_MOBILITY
       
   239         // Now load any mapped elements too 
       
   240         if (!mapper)
       
   241             {
       
   242             mapper = CCDMapper::MaybeCreateMapperLC(*thisElement,*aSession.iMDBSessionImpl,ELoad);
       
   243             }
       
   244 
       
   245 	    if ( mapper )
       
   246             {   
       
   247             DoLoadL(session, mapper, NULL, err, thisElement->ElementId() & KCDMaskShowRecordId, thisElement->Attributes(), ETrue);
       
   248             
       
   249             mapper->MapResultsL();
       
   250             
       
   251             /* This is a work around solution for the following case:
       
   252              * There is a table with legacy links. [IAP table, BearerType and Bearer fields in it]. With the
       
   253              * new CommsDat the Bearer field value is the fully resolved TMDBElementId of the linked record.
       
   254              * BUT there is a lot of code which expects this value to be only a record number (as it is 
       
   255              * originally in the config file) after the reading it.
       
   256              * Even worse there is code which tries to read this field via a CMDBField<TInt> while this field is
       
   257              * from now on a CMDBField<someLink>. So if somebody tries to read an integer field we have to check
       
   258              * whether is it a legacy link or not. If it is, then we have to do conversion on the value to contain
       
   259              * only the record number of the linked record and not the TMDBElementId.
       
   260              */
       
   261             if (CommsDatSchema::IsColumn(thisElement->ElementId() | KCDMaskShowRecordId) && 
       
   262              	(thisElement->Type() ==EInt || thisElement->Type()==EUint32))
       
   263              	{
       
   264              	CommsDatSchema::ChangeLegacyLinkFieldValues(thisElement);
       
   265              	}
       
   266         
       
   267             CleanupStack::PopAndDestroy(mapper);                
       
   268             }
       
   269 #endif
       
   270         }
       
   271         else
       
   272         {
       
   273         // Nothing at all found  or some other error
       
   274         __FLOG_STATIC1(KLogComponent, KCDErrLog, _L("MMetaDatabase::LoadL() failed for Element %08x as this node was not found in the database."), thisElement->ElementId() );
       
   275         User::Leave(err);
       
   276         }
       
   277     }    
       
   278     
       
   279 
       
   280 /*
       
   281  This DoLoadL is called from the LoadL method if the client tries to load a field and not a record.
       
   282 */
       
   283 void MMetaDatabase::DoLoadL(CMDBSessionImpl* aSession, 
       
   284                             CMDBElement* aElement, 
       
   285                             CMDBElement* aMapper, 
       
   286                             TInt &aErr, 
       
   287                             TMDBElementId aRecordId,
       
   288                             TMDBElementId aAttributes,
       
   289                             TBool isTheLoadForMapper)
       
   290     {
       
   291     TAny* mem[KMMetaDBVisitorMaxSize];
       
   292     TMetaVTableIterator attribIter(aElement);
       
   293     SVDataTableEntry const* entry = 0;
       
   294     TMDBVisitorBase* metaDBVisitor = 0;
       
   295  
       
   296     CCDMapper* mapper = static_cast<CCDMapper*>(aMapper);
       
   297 
       
   298     TBool loadTemplateField = !CommsDatSchema::IsTable(aElement->ElementId());
       
   299 
       
   300 
       
   301     // Apply Load function to each unmapped entry
       
   302     while ( (aErr == KErrNone  ||  aErr == KErrNotFound) &&  (entry = attribIter++) != NULL)
       
   303         {
       
   304         metaDBVisitor = static_cast<TMDBVisitorBase*>(entry->iMetaNewL(mem,aElement->GetAttribPtr(entry->iOffset))); // target
       
   305         if (metaDBVisitor) // might be deprecated
       
   306             {
       
   307             metaDBVisitor->SetOwner(aElement);
       
   308             if ( ! mapper  || ! mapper->Mapped(metaDBVisitor->ElementId()) )
       
   309                 {
       
   310                 aErr = metaDBVisitor->LoadL(aSession, aRecordId, aAttributes);
       
   311 
       
   312                 if ( aErr == KErrNotFound && loadTemplateField )
       
   313                     {
       
   314                     if (!aSession->IsSetAttributeMask(ECDHidden))  
       
   315                         {
       
   316                         aSession->SetAttributeMask(ECDHidden);
       
   317                         aErr = metaDBVisitor->LoadL(aSession, KCDDefaultRecord, ECDHidden);
       
   318                         aSession->ClearAttributeMask(ECDHidden); //need to cleanup if LoadL leaves
       
   319                         }
       
   320                     else
       
   321                         {
       
   322                         aErr = metaDBVisitor->LoadL(aSession, KCDDefaultRecord, ECDHidden);
       
   323                         }
       
   324                     
       
   325                     if (KCDInitialRecordId <= aRecordId && 
       
   326                 		KErrNone == aErr)
       
   327                 		{
       
   328                 		/* This means that the given field is read from the template record and the recordId of the
       
   329                 		 * record in question (on which the FindL is called) is not a template record as the aRecordId
       
   330                 		 * is not 0. This means that all of the flags should be removed from the elementId of the
       
   331                 		 * metaDBVisitor (hidden, modified) and the value should be given back to the client. In this 
       
   332                 		 * case the user gets the value from the template record, the TMDBElementId of the given field 
       
   333                 		 * is set so as the value would have been read from the record itself. When StoreL will be called
       
   334                 		 * on this record and this field is not changed it won't be stored once again in this record but
       
   335                 		 * remains only in the template record. Of course if the user changes the value of this field it
       
   336                 		 * will be stored into the given reocrd as the changed flag will be set.
       
   337                 		 */
       
   338                 		metaDBVisitor->ResetElementId(aRecordId);
       
   339                 		}
       
   340                     //We don't do this branch for mappers because it can just mix up the logic
       
   341                     //implemented in the PostMapping functions...
       
   342                     else if (KErrNone != aErr &&
       
   343                     		!isTheLoadForMapper)
       
   344                     	{
       
   345                     	/* The reading of the field is still erronous, and this means at this stage that it
       
   346                     	 * simply doesn't exist in the given record. What is important in this case is that
       
   347                     	 * the value of the given field should be set to NULL, because if it's a container
       
   348                     	 * re-use case, meaning that the container is re-used for reading from a record set
       
   349                     	 * for example, this is the way we can indicate that the given field is not existing
       
   350                     	 * in the record, nor in the template record...
       
   351                     	 * This is the 9.5 propagation of the INC123203 
       
   352                     	 */
       
   353                     	metaDBVisitor->ResetElementId(aRecordId);
       
   354                     	metaDBVisitor->ClearValue();
       
   355                     	}
       
   356                     }
       
   357                 }
       
   358             }
       
   359         }
       
   360         
       
   361     if (aErr == KErrNotFound)
       
   362         {
       
   363         if ( aElement->ElementId() &&
       
   364              !CommsDatSchema::IsNode(aElement->ElementId()) )
       
   365          //When the client tried to load a field explicitly and the given one cant be found we leave.
       
   366             {
       
   367             User::Leave(aErr);
       
   368             }
       
   369         else
       
   370             { 
       
   371             aErr = KErrNone;
       
   372             }
       
   373         }
       
   374     }
       
   375     
       
   376 /*
       
   377  This DoLoadL is called from the FindL method.
       
   378 */
       
   379 void MMetaDatabase::DoLoadL(CMDBSessionImpl* aSession, 
       
   380                             CMDBElement* aElement, 
       
   381                             CMDBElement* aMapper,
       
   382                             TInt &aErr, 
       
   383                             TMDBElementId aRecordId,
       
   384                             TMDBElementId aAttributes,
       
   385                             RArray<TUint32>& aMatches,
       
   386                             TBool isTheLoadForMapper)
       
   387     {
       
   388     TAny* mem[KMMetaDBVisitorMaxSize];
       
   389     TMetaVTableIterator attribIter(aElement);
       
   390     SVDataTableEntry const* entry = 0;
       
   391     TMDBVisitorBase* metaDBVisitor = 0;
       
   392  
       
   393     CCDMapper* mapper = static_cast<CCDMapper*>(aMapper);
       
   394 
       
   395     TBool loadTemplateField = !CommsDatSchema::IsTable(aElement->ElementId());
       
   396 
       
   397 
       
   398     // Apply Load function to each unmapped entry
       
   399     while ( (aErr == KErrNone  ||  aErr == KErrNotFound) &&  (entry = attribIter++) != NULL)
       
   400         {
       
   401         metaDBVisitor = static_cast<TMDBVisitorBase*>(entry->iMetaNewL(mem,aElement->GetAttribPtr(entry->iOffset))); // target
       
   402         if (metaDBVisitor) // might be deprecated
       
   403             {
       
   404             metaDBVisitor->SetOwner(aElement);
       
   405             if ( ! mapper  || ! mapper->Mapped(metaDBVisitor->ElementId()) )
       
   406                 {
       
   407                 aErr = metaDBVisitor->LoadL(aSession, aRecordId, aAttributes, aMatches);
       
   408 
       
   409                 if ( aErr == KErrNotFound && loadTemplateField )
       
   410                     {
       
   411                     if (!aSession->IsSetAttributeMask(ECDHidden))  
       
   412                         {
       
   413                         aSession->SetAttributeMask(ECDHidden);
       
   414                         aErr = metaDBVisitor->LoadL(aSession, KCDDefaultRecord, ECDHidden);
       
   415                         aSession->ClearAttributeMask(ECDHidden); //need to cleanup if LoadL leaves
       
   416                         }
       
   417                     else
       
   418                         {
       
   419                         aErr = metaDBVisitor->LoadL(aSession, KCDDefaultRecord, ECDHidden);
       
   420                         }
       
   421                     
       
   422                     if (KCDInitialRecordId <= aRecordId &&
       
   423 	                    KErrNone == aErr)
       
   424 	                    {
       
   425 	                    /* This means that the given field is read from the template record and the recordId of the
       
   426 	                     * record in question (on which the FindL is called) is not a template record as the aRecordId
       
   427 	                     * is not 0. This means that all of the flags should be removed from the elementId of the
       
   428 	                     * metaDBVisitor (hidden, modified) and the value should be given back to the client. In this 
       
   429 	                     * case the user gets the value from the template record, the TMDBElementId of the given field 
       
   430 	                     * is set so as the value would have been read from the record itself. When StoreL will be called
       
   431 	                     * on this record and this field is not changed it won't be stored once again in this record but
       
   432 	                     * remains only in the template record. Of course if the user changes the value of this field it
       
   433 	                     * will be stored into the given reocrd as the changed flag will be set.
       
   434 	                     */
       
   435 	                    metaDBVisitor->ResetElementId(aRecordId);
       
   436 	                    }
       
   437                     //We don't do this branch for mappers because it can just mix up the logic
       
   438                     //implemented in the PostMapping functions...
       
   439                     else if (KErrNone != aErr &&
       
   440                     		!isTheLoadForMapper)
       
   441                     	{
       
   442                     	/* The reading of the field is still erronous, and this means at this stage that it
       
   443                     	 * simply doesn't exist in the given record. What is important in this case is that
       
   444                     	 * the value of the given field should be set to NULL, because if it's a container
       
   445                     	 * re-use case, meaning that the container is re-used for reading from a record set
       
   446                     	 * for example, this is the way we can indicate that the given field is not existing
       
   447                     	 * in the record, nor in the template record...
       
   448                     	 * This is the 9.5 propagation of the INC123203 
       
   449                     	 */
       
   450                     	metaDBVisitor->ResetElementId(aRecordId);
       
   451                     	metaDBVisitor->ClearValue();
       
   452                     	}
       
   453                     }
       
   454                 }
       
   455             }
       
   456         }
       
   457         
       
   458     if (aErr == KErrNotFound)
       
   459         {
       
   460         aErr = KErrNone;
       
   461         }
       
   462     }
       
   463 
       
   464 
       
   465 
       
   466 EXPORT_C TBool MMetaDatabase::FindL(CMDBSession& aSession)
       
   467 /*
       
   468 Loads the MMetaDatabase object from the database where match is found for fields primed by the client 
       
   469     
       
   470 NB this is a depth-first search that assumes that caller will only instantiate links to records that contain 
       
   471 primed fields that need to be matched.  
       
   472 
       
   473 This is not attempting to be a generic optimised search solution, but assumes that clients will only search 
       
   474 down short trees.
       
   475 
       
   476 Searching time can be significantly reduced by appropriate choice of data container by client.
       
   477 */
       
   478 {
       
   479     CMDBSessionImpl* session = aSession.iMDBSessionImpl; 
       
   480                             
       
   481     RArray<TUint32> candidates;
       
   482     RArray<TUint32> matches;
       
   483     
       
   484     CleanupClosePushL(candidates);	
       
   485     CleanupClosePushL(matches);	
       
   486 
       
   487     TBool retval = EFalse;
       
   488   	TInt err = KErrNone;
       
   489 
       
   490     CMDBElement* thisElement = static_cast<CMDBElement*>(this);
       
   491     
       
   492     __FLOG_STATIC1(KLogComponent, KCDInfoLog, _L("MMetaDatabase::FindL() called for Element %08x..."), thisElement->ElementId());
       
   493 
       
   494 #ifdef SYMBIAN_NON_SEAMLESS_NETWORK_BEARER_MOBILITY
       
   495     CCDMapper* mapper = CCDMapper::MaybeCreateMapperLC(*thisElement,*aSession.iMDBSessionImpl,EFind);    
       
   496 
       
   497     if (mapper)
       
   498         {   
       
   499         //  LOG( ECommsDatLog::Printf(_L8("MMetaDBVisitor %08x:\tFindL() Mapping"), this););
       
   500                     
       
   501          // Find primed fields that don't need mapping
       
   502         DoFindL(session, thisElement, mapper, err, candidates, matches);
       
   503         
       
   504         if (err == KErrNone)
       
   505             {
       
   506             //do some pre-mappings
       
   507             mapper->PreMappingsL();
       
   508             
       
   509             // Then find any mapped primed fields
       
   510             DoFindL(session, mapper, NULL, err, candidates, matches);
       
   511         
       
   512             if (err == KErrNone  &&  matches.Count() > 0 )
       
   513                 {           		
       
   514                 DoLoadL(session, thisElement, mapper, err, matches[0] & KCDMaskShowRecordId, matches[0] & session->GetReadAttributeMask(), matches);
       
   515         
       
   516                 if (err == KErrNone || err == KErrNotFound)
       
   517 			        {
       
   518                     // and then Load each mapped entry
       
   519                     DoLoadL(session, mapper, NULL, err, matches[0] & KCDMaskShowRecordId, matches[0] & session->GetReadAttributeMask(), matches, ETrue);
       
   520             
       
   521                     if (err == KErrNone)
       
   522                         {
       
   523                         retval = ETrue;
       
   524                      
       
   525                         if ( matches.Count() > 0 && thisElement->RecordId() == 0 )
       
   526                             {
       
   527                             thisElement->SetElementId(thisElement->ElementId() | (matches[0] & (KCDMaskShowRecordId | KCDMaskShowAttributes)));
       
   528                             }
       
   529                         }
       
   530                     }
       
   531                 }
       
   532             }
       
   533         
       
   534         mapper->MapResultsL();
       
   535 
       
   536         CleanupStack::PopAndDestroy(mapper);
       
   537         }   
       
   538     else
       
   539 #endif
       
   540         {
       
   541         // No mapper...
       
   542 
       
   543 		// find all possible matches
       
   544 		DoFindL(session, thisElement, NULL, err, candidates, matches);
       
   545         
       
   546 		__FLOG_STATIC1(KLogComponent, KCDInfoLog, _L("MMetaDatabase::FindL() - the 'err' after the DoFindL: %d"), err);
       
   547 		
       
   548         if (err == KErrNone  &&  matches.Count() > 0 )
       
   549             {            
       
   550             //Case: FindL for TableIDs, when primed with a TableName
       
   551        		if(!thisElement->TableId() && 
       
   552                 matches.Count()==1)
       
   553        			{
       
   554        			thisElement->SetElementId((thisElement->ElementId()) | (matches[0] & KCDMaskShowRecordType));
       
   555        			}           			
       
   556            			
       
   557             // and load the elements
       
   558 		    DoLoadL(session, thisElement, NULL, err, matches[0] & KCDMaskShowRecordId, matches[0] & session->GetReadAttributeMask(), matches);
       
   559             
       
   560             /* This is a work around solution for the following case:
       
   561              * There is a table with legacy links. [IAP table, BearerType and Bearer fields in it]. With the
       
   562              * new CommsDat the Bearer field value is the fully resolved TMDBElementId of the linked record.
       
   563              * BUT there is a lot of code which expects this value to be only a record number (as it is 
       
   564              * originally in the config file) after the reading it.
       
   565              * Even worse there is code which tries to read this field via a CMDBField<TInt> while this field is
       
   566              * from now on a CMDBField<someLink>. So if somebody tries to read an integer field we have to check
       
   567              * whether is it a legacy link or not. If it is, then we have to do conversion on the value to contain
       
   568              * only the record number of the linked record and not the TMDBElementId.
       
   569              */
       
   570              if (CommsDatSchema::IsColumn(thisElement->ElementId() | KCDMaskShowRecordId) && 
       
   571              	(thisElement->Type() ==EInt || thisElement->Type()==EUint32))
       
   572              	{
       
   573              	CommsDatSchema::ChangeLegacyLinkFieldValues(thisElement);
       
   574              	}
       
   575 		    
       
   576             if (err == KErrNone)
       
   577                 {
       
   578                 retval = ETrue;
       
   579             
       
   580                 if ( matches.Count() > 0 && thisElement->RecordId() == 0 )
       
   581                     {
       
   582                     thisElement->SetElementId(thisElement->ElementId() | (matches[0] & (KCDMaskShowRecordId | KCDMaskShowAttributes)));
       
   583                     }
       
   584                 }
       
   585  	        }    
       
   586 	    }
       
   587     
       
   588     CleanupStack::PopAndDestroy(&matches); 
       
   589 	CleanupStack::PopAndDestroy(&candidates);
       
   590    
       
   591     return retval;
       
   592     }
       
   593 
       
   594 
       
   595 
       
   596 void MMetaDatabase::DoFindL(CMDBSessionImpl* aSession, CMDBElement* aElement, CMDBElement* aMapper, TInt& aErr, RArray<TUint32>& aCandidates, RArray<TUint32>& aMatches)
       
   597     {
       
   598     TAny* mem[KMMetaDBVisitorMaxSize];
       
   599     TMDBVisitorBase*  metaDBVisitor = 0;
       
   600     SVDataTableEntry const* entry = 0;    
       
   601     TMetaVTableIterator attribIter(aElement); 
       
   602 
       
   603     CCDMapper* mapper = static_cast<CCDMapper*>(aMapper);
       
   604 
       
   605     while ( aErr == KErrNone  &&  (entry = attribIter++) != NULL ) 
       
   606         {
       
   607         metaDBVisitor = static_cast<TMDBVisitorBase*>(entry->iMetaNewL(mem,aElement->GetAttribPtr(entry->iOffset))); // target
       
   608         if (metaDBVisitor) // might be deprecated
       
   609             {
       
   610             metaDBVisitor->SetOwner(aElement);
       
   611             if ( metaDBVisitor->Changed()  &&  
       
   612                 ( !mapper || ! mapper->Mapped(metaDBVisitor->ElementId()) ) )
       
   613                 {  
       
   614                 aErr = metaDBVisitor->FindL(aSession, aCandidates);
       
   615 
       
   616                 if (aErr == KErrNone )
       
   617                     {
       
   618                     aErr = aSession->FindMatchL(aCandidates, aMatches);
       
   619                     }
       
   620                 }
       
   621             }
       
   622         }
       
   623     }
       
   624 
       
   625     
       
   626 
       
   627 EXPORT_C void MMetaDatabase::RefreshL(CMDBSession& aSession)
       
   628 /**
       
   629  * Updates each unchanged member of the MMetaDatabase object from the database
       
   630  */
       
   631     {
       
   632     CMDBSessionImpl* session = aSession.iMDBSessionImpl;
       
   633      
       
   634     CMDBElement* thisElement = static_cast<CMDBElement*>(this);
       
   635 
       
   636     __FLOG_STATIC1(KLogComponent, KCDInfoLog, _L("MMetaDatabase::RefreshL() called for Element %08x..."), thisElement->ElementId());
       
   637 
       
   638     TInt err = KErrNotFound;
       
   639         
       
   640 #ifdef SYMBIAN_NON_SEAMLESS_NETWORK_BEARER_MOBILITY
       
   641     CCDMapper* mapper = CCDMapper::MaybeCreateMapperLC(*thisElement,*aSession.iMDBSessionImpl,ERefresh);     	
       
   642 #else
       
   643     CCDMapper* mapper = NULL;
       
   644 #endif
       
   645     
       
   646     err = session->LoadElementAttributesL(*thisElement->Data());
       
   647 
       
   648     if (err == KErrNone)
       
   649         {
       
   650         if (mapper) 
       
   651         {   
       
   652             DoRefreshL(session, thisElement, mapper, err );
       
   653             
       
   654 		    //if (err == KErrNone )
       
   655 		    if (mapper)
       
   656 		        {
       
   657 		        DoRefreshL(session, mapper, NULL, err);
       
   658             
       
   659                 if (err == KErrNone)
       
   660                     {
       
   661                     mapper->MapResultsL();
       
   662                     }
       
   663                 }
       
   664             CleanupStack::PopAndDestroy(mapper);
       
   665             }
       
   666         else
       
   667             {
       
   668             DoRefreshL(session, thisElement, mapper, err);
       
   669             }
       
   670         }
       
   671     }
       
   672 
       
   673 
       
   674 void MMetaDatabase::DoRefreshL(CMDBSessionImpl* aSession, CMDBElement* aElement, CMDBElement* aMapper, TInt &aErr)
       
   675     {
       
   676     CCDMapper* mapper = static_cast<CCDMapper*>(aMapper);
       
   677 
       
   678     TAny* mem[KMMetaDBVisitorMaxSize];
       
   679     TMetaVTableIterator attribIter(aElement);
       
   680     SVDataTableEntry const* entry = 0;
       
   681     TMDBVisitorBase* metaDBVisitor = 0;
       
   682 
       
   683     while ( (aErr == KErrNone  ||  aErr == KErrNotFound)  &&  (entry = attribIter++) != NULL)
       
   684         {
       
   685         metaDBVisitor = static_cast<TMDBVisitorBase*>(entry->iMetaNewL(mem,aElement->GetAttribPtr(entry->iOffset)));
       
   686         if (metaDBVisitor) // might be deprecated
       
   687             {
       
   688             metaDBVisitor->SetOwner(aElement);
       
   689             if ( (! mapper || ! mapper->Mapped(metaDBVisitor->ElementId()) ) &&
       
   690                  ! metaDBVisitor->Changed() || 
       
   691                  (metaDBVisitor->ElementId() & KCDMaskShowFieldType) == KCDMaskShowFieldType )   
       
   692                 {
       
   693                 aErr = metaDBVisitor->RefreshL(aSession);
       
   694                 }
       
   695             }
       
   696         }
       
   697 
       
   698     if (aErr == KErrNotFound)
       
   699         {
       
   700         aErr = KErrNone; 
       
   701         }
       
   702     }
       
   703 
       
   704 
       
   705 EXPORT_C void MMetaDatabase::StoreL(CMDBSession& aSession)
       
   706 /*
       
   707 	Store all initialised fields in the database - this will create new entries in the database
       
   708 		Client can request to ignore access control flags by setting the attribute mask,
       
   709 		but only fields that the client has capabilities to set will be created
       
   710 
       
   711 		A request to store data in an unauthorised area will cause the function to leave.
       
   712 		A request to create data in an already populated field will cause the function to leave
       
   713 
       
   714 		Store will be atomic - all fields stored after success.  No fields stored if the function leaves
       
   715 
       
   716 	Will return
       
   717 		KErrNone on success
       
   718         KErrNotFound if data cannot be found
       
   719 		KErrPermissionDenied if client not authorised to read or write the data
       
   720 
       
   721 	@publishedAll
       
   722 	*/
       
   723     {
       
   724 	TInt err = KErrNone;
       
   725 
       
   726     CMDBSessionImpl* session = aSession.iMDBSessionImpl;
       
   727 
       
   728     CMDBElement* thisElement = static_cast<CMDBElement*>(this);
       
   729  
       
   730     __FLOG_STATIC1(KLogComponent, KCDInfoLog, _L("MMetaDatabase::StoreL() called for Element %08x..."), thisElement->ElementId());
       
   731 
       
   732     TBool ownTransaction = session->MaybeOpenTransactionL();    
       
   733     
       
   734 #ifdef SYMBIAN_NON_SEAMLESS_NETWORK_BEARER_MOBILITY
       
   735     CCDMapper* mapper = CCDMapper::MaybeCreateMapperLC(*thisElement,*aSession.iMDBSessionImpl,EStore);    
       
   736  
       
   737     if ( mapper )
       
   738         {   
       
   739         mapper->PrepareToStoreL();
       
   740         
       
   741         CCDValidator::ValidateL(thisElement,*aSession.iMDBSessionImpl,EStore);
       
   742         
       
   743         mapper->PreMappingsL();
       
   744         
       
   745         DoStoreL(session, thisElement, mapper, err);
       
   746 		  
       
   747      	if ( err == KErrNone)
       
   748 		    {
       
   749             // Now Store mapped entries all together	
       
   750             DoStoreL(session, mapper, NULL, err);
       
   751             
       
   752             if ( err == KErrNone)
       
   753                 {
       
   754                 mapper->PostMappingsL();
       
   755                 }
       
   756 		    }
       
   757         
       
   758         CleanupStack::PopAndDestroy(mapper);
       
   759         }
       
   760     else 
       
   761 #endif // SYMBIAN_NON_SEAMLESS_NETWORK_BEARER_MOBILITY
       
   762         {   
       
   763         session->MaybeCreateNodeL(*thisElement);
       
   764         
       
   765 #ifdef SYMBIAN_NON_SEAMLESS_NETWORK_BEARER_MOBILITY
       
   766         CCDValidator::ValidateL(thisElement,*aSession.iMDBSessionImpl,EStore);
       
   767 #else
       
   768         CCDValidator::ValidateL(thisElement,*aSession.iMDBSessionImpl);
       
   769 #endif // SYMBIAN_NON_SEAMLESS_NETWORK_BEARER_MOBILITY
       
   770         
       
   771         DoStoreL(session, thisElement, NULL, err);
       
   772         }
       
   773    
       
   774   	session->MaybeCommitTransactionL(ownTransaction, thisElement, err);
       
   775     }
       
   776 
       
   777 
       
   778 void MMetaDatabase::DoStoreL(CMDBSessionImpl* aSession, CMDBElement* aElement, CMDBElement* aMapper, TInt &aErr)
       
   779     {
       
   780     TAny* mem[KMMetaDBVisitorMaxSize];
       
   781     TMetaVTableIterator attribIter(aElement);
       
   782     SVDataTableEntry const* entry = 0;
       
   783     TMDBVisitorBase* metaDBVisitor = 0;
       
   784  
       
   785     CCDMapper* mapper = static_cast<CCDMapper*>(aMapper);
       
   786 
       
   787     while ( aErr == KErrNone  &&  (entry = attribIter++) != NULL )
       
   788         {
       
   789         metaDBVisitor = static_cast<TMDBVisitorBase*>(entry->iMetaNewL(mem,aElement->GetAttribPtr(entry->iOffset))); 
       
   790         if (metaDBVisitor) // might be deprecated
       
   791             {
       
   792             metaDBVisitor->SetOwner(aElement);
       
   793             if ( metaDBVisitor->Changed() &&  
       
   794                  ( ! mapper  || !mapper->Mapped(metaDBVisitor->ElementId())) )
       
   795                 {
       
   796                 aErr = metaDBVisitor->StoreL(aSession, (aElement->ElementId() & KCDMaskShowRecordId), aElement->Attributes());                         
       
   797                 if (aErr == KErrNone)
       
   798                     {
       
   799                     metaDBVisitor->Sync();
       
   800 #ifndef __TOOLS2__
       
   801                     aSession->NotifierL()->MaybeNotifyChange(metaDBVisitor->ElementId());       
       
   802 #endif
       
   803                     }
       
   804                 else
       
   805                     {
       
   806                     __FLOG_STATIC2(KLogComponent, KCDErrLog, _L("MMetaDatabase::DoStoreL() StoreL failed with err <%d> for Element <%08x>"), aErr, metaDBVisitor->ElementId());
       
   807 
       
   808                     }
       
   809                 }
       
   810             }
       
   811         }
       
   812     }
       
   813 
       
   814 
       
   815 EXPORT_C void MMetaDatabase::ModifyL(CMDBSession& aSession)
       
   816 /**
       
   817  * Modifies each changed member of the MMetaDatabase object in the database
       
   818  */
       
   819     {   
       
   820     CMDBSessionImpl* session = aSession.iMDBSessionImpl;
       
   821     
       
   822     TBool ownTransaction = session->MaybeOpenTransactionL();
       
   823 
       
   824     CMDBElement* thisElement = static_cast<CMDBElement*>(this);
       
   825 
       
   826     __FLOG_STATIC1(KLogComponent, KCDInfoLog, _L("MMetaDatabase::ModifyL() called for Element %08x..."), thisElement->ElementId());
       
   827 
       
   828 	// ensure do not enter wrong type of data against this element
       
   829 #ifdef SYMBIAN_NON_SEAMLESS_NETWORK_BEARER_MOBILITY
       
   830     CCDValidator::ValidateL(thisElement,*aSession.iMDBSessionImpl,EModify);
       
   831 #else
       
   832     CCDValidator::ValidateL(thisElement,*aSession.iMDBSessionImpl);
       
   833 #endif // SYMBIAN_NON_SEAMLESS_NETWORK_BEARER_MOBILITY
       
   834     
       
   835     TInt err = KErrNone;
       
   836     TBool modifiedNode(0);
       
   837 
       
   838 #ifdef SYMBIAN_NON_SEAMLESS_NETWORK_BEARER_MOBILITY
       
   839     CCDMapper* mapper = CCDMapper::MaybeCreateMapperLC(*thisElement,*aSession.iMDBSessionImpl,EModify);    
       
   840  
       
   841     if ( mapper )
       
   842         { 	    
       
   843         mapper->PrepareToStoreL();
       
   844 
       
   845         DoModifyL(session, thisElement, mapper, err);
       
   846 	
       
   847         if (err == KErrNone )
       
   848 		    {
       
   849 		    //before doing the modifications on the mapped object do some pre mappings...
       
   850 		    
       
   851 		    mapper->PreMappingsL();
       
   852 		    
       
   853 	        // Now Modify mapped entries all together
       
   854 			DoModifyL(session, mapper, NULL, err);
       
   855 			
       
   856 			if ( err == KErrNone)
       
   857                 {
       
   858                 mapper->PostMappingsL();
       
   859                 }
       
   860             }
       
   861        CleanupStack::PopAndDestroy(mapper);
       
   862         }
       
   863     else
       
   864 #endif
       
   865         {   
       
   866         // No mapping.  just get on with it.
       
   867 
       
   868         // First, move the elements if there are any attribute changes 
       
   869         // this is only performed once - at the highest level in the case of a recursive walk through a record set
       
   870         modifiedNode = session->MaybeModifyNodeL(*thisElement->Data());
       
   871         
       
   872         DoModifyL(session, thisElement, NULL, err);         	;
       
   873 	
       
   874 /*
       
   875         if (err == KErrNone )
       
   876             {
       
   877             // make any general attribute changes for every item in the node
       
   878             err = session->MaybeModifyNodeL(*thisElement->Data());
       
   879             }
       
   880 */	    }
       
   881 
       
   882     if (modifiedNode)
       
   883         {
       
   884         session->EndModification();
       
   885         }
       
   886      
       
   887     session->MaybeCommitTransactionL(ownTransaction, thisElement, err);
       
   888     }
       
   889 
       
   890 
       
   891 void MMetaDatabase::DoModifyL(CMDBSessionImpl* aSession, CMDBElement* aElement, CMDBElement* aMapper, TInt &aErr)
       
   892     {
       
   893 
       
   894     CCDMapper* mapper = static_cast<CCDMapper*>(aMapper); 
       
   895 
       
   896     TAny* mem[KMMetaDBVisitorMaxSize];
       
   897     TMetaVTableIterator attribIter(aElement);
       
   898     SVDataTableEntry const* entry = 0;
       
   899     TMDBVisitorBase* metaDBVisitor = 0;
       
   900     
       
   901     // apply the Modify function to each changed entry that isn't mapped
       
   902     while ( (aErr == KErrNone  || aErr == KErrNotFound) &&  
       
   903             (entry = attribIter++) != NULL )
       
   904         {
       
   905         metaDBVisitor = static_cast<TMDBVisitorBase*>(entry->iMetaNewL(mem,aElement->GetAttribPtr(entry->iOffset)));
       
   906         if (metaDBVisitor) // might be deprecated
       
   907             {
       
   908             metaDBVisitor->SetOwner(aElement);
       
   909             TMDBElementId id = metaDBVisitor->ElementId();
       
   910             aErr = aSession->LoadElementAttributesL(id);
       
   911       
       
   912             if (aErr == KErrNone && ( id & (aSession->GetReadAttributeMask() | aSession->GetWriteAttributeMask()) ) )
       
   913                 {
       
   914                 aErr = KErrAccessDenied;
       
   915                 }
       
   916             else
       
   917             {
       
   918 	            if ( metaDBVisitor->Changed()  && 
       
   919                      ( ! mapper || ! mapper->Mapped(metaDBVisitor->ElementId()) ) &&
       
   920                     ( aErr == KErrNotFound  ||  aErr == KErrNone ) )
       
   921 	                {
       
   922 	                metaDBVisitor->SetElementId(&id);
       
   923 	                aErr = metaDBVisitor->ModifyL(aSession, aElement->Attributes());
       
   924 	                if (aErr == KErrNone)
       
   925 	                 {
       
   926 	                 metaDBVisitor->Sync();
       
   927 #ifndef __TOOLS2__
       
   928 	            	    aSession->NotifierL()->MaybeNotifyChange(metaDBVisitor->ElementId());
       
   929 #endif       
       
   930 	                    }
       
   931 	                }
       
   932                 }
       
   933             }
       
   934         }
       
   935 
       
   936     if (aErr == KErrNotFound)
       
   937         {
       
   938         aErr = KErrNone;
       
   939         }
       
   940     }
       
   941 
       
   942 
       
   943 
       
   944   
       
   945 EXPORT_C void MMetaDatabase::DeleteL(CMDBSession& aSession)
       
   946 /*
       
   947 Deletes each member of the MMetaDatabase object from the database
       
   948 
       
   949 @publishedAll
       
   950 */
       
   951     {
       
   952     CMDBSessionImpl* session = aSession.iMDBSessionImpl;
       
   953     
       
   954     TBool resetReadOnly = ! session->IsSetAttributeMask(ECDNoWriteButDelete);
       
   955     if (resetReadOnly)
       
   956         {
       
   957         session->SetAttributeMask(ECDNoWriteButDelete);
       
   958         }
       
   959 
       
   960     TBool ownTransaction = session->MaybeOpenTransactionL();
       
   961     
       
   962     CMDBElement* thisElement = static_cast<CMDBElement*>(this);
       
   963 
       
   964     __FLOG_STATIC1(KLogComponent, KCDInfoLog, _L("MMetaDatabase::DeleteL() called for Element %08x..."), thisElement->ElementId());
       
   965 
       
   966     TInt err = KErrNone;
       
   967 
       
   968 #ifdef SYMBIAN_NON_SEAMLESS_NETWORK_BEARER_MOBILITY    
       
   969     CCDValidator::ValidateL(thisElement,*aSession.iMDBSessionImpl, EDelete);
       
   970     
       
   971     CCDMapper* mapper = CCDMapper::MaybeCreateMapperLC(*thisElement,*aSession.iMDBSessionImpl,EDelete);    
       
   972     if (mapper)
       
   973         {
       
   974         //before doing the delete on the mapped object do some pre mappings...
       
   975         mapper->PreMappingsL();
       
   976         
       
   977         err = session->MaybeDeleteNodeL(mapper, *(mapper->Data()));
       
   978         
       
   979         mapper->PostMappingsL();
       
   980 
       
   981         CleanupStack::PopAndDestroy(mapper);
       
   982         }
       
   983     else
       
   984 #endif //SYMBIAN_NON_SEAMLESS_NETWORK_BEARER_MOBILITY 
       
   985         {
       
   986         err = session->MaybeDeleteNodeL(thisElement, *thisElement->Data());
       
   987         }
       
   988 
       
   989     if ( err == KErrNone )
       
   990         {
       
   991         TAny* mem[KMMetaDBVisitorMaxSize];
       
   992         SVDataTableEntry const* entry = NULL;
       
   993         TMDBVisitorBase* metaDBVisitor = NULL;
       
   994         TMetaVTableIterator attribIter(this);     
       
   995 		while ((entry = attribIter++) != NULL)
       
   996 			{
       
   997 			metaDBVisitor = static_cast<TMDBVisitorBase*>(entry->iMetaNewL(mem,GetAttribPtr(entry->iOffset)));
       
   998  			if ( metaDBVisitor && metaDBVisitor->IsFieldLoaded() )
       
   999                 {
       
  1000                 metaDBVisitor->Change();
       
  1001                 }
       
  1002 			}
       
  1003         }        
       
  1004 
       
  1005        
       
  1006     if (resetReadOnly)
       
  1007         {
       
  1008         session->ClearAttributeMask(ECDNoWriteButDelete);
       
  1009         }
       
  1010 
       
  1011     session->MaybeCommitTransactionL(ownTransaction, NULL, err);
       
  1012     }
       
  1013 
       
  1014 
       
  1015 #ifndef __TOOLS2__
       
  1016 EXPORT_C TInt MMetaDatabase::RequestNotification(CMDBSession& aSession, TRequestStatus& aStatus)
       
  1017 /*
       
  1018 Registers each set member of the MMetaDatabase object for notification of change in the database
       
  1019 If this function leaves, no effort made to unregister fields..  
       
  1020  
       
  1021 FUTURE - At the moment CommsDat makes a simple general request for notification on the whole repository.
       
  1022 Because this is all that Commdb does.  It would be good to change this to register for individual areas of change.
       
  1023 
       
  1024 @publishedAll
       
  1025 */  
       
  1026     {
       
  1027     if (aSession.iMDBSessionImpl)
       
  1028         {
       
  1029         __FLOG_STATIC(KLogComponent, KCDInfoLog, _L("MMetaDatabase::RequestNotification() called"));
       
  1030     
       
  1031         TRAPD(retval, aSession.iMDBSessionImpl->NotifierL()->RegisterNotifyL(aStatus));
       
  1032         return retval;
       
  1033         }
       
  1034     
       
  1035     // LOG error
       
  1036     return KErrNotFound;
       
  1037     }
       
  1038    
       
  1039    
       
  1040 EXPORT_C TInt MMetaDatabase::CancelNotification(CMDBSession& aSession, TRequestStatus& aStatus)
       
  1041 /*
       
  1042 Cancels Registration of change notification 
       
  1043  
       
  1044 FUTURE - At the moment CommsDat makes a simple general request for notification on the whole repository.
       
  1045 Because this is all that Commdb does.  It would be good to change this to register for individual areas of change.
       
  1046 
       
  1047 @publishedAll
       
  1048 */  
       
  1049     {
       
  1050     if (aSession.iMDBSessionImpl)
       
  1051         {
       
  1052         __FLOG_STATIC(KLogComponent, KCDInfoLog, _L("MMetaDatabase::CancelNotification() called"));
       
  1053         TRAPD(retval, aSession.iMDBSessionImpl->NotifierL()->CancelNotify(aStatus));
       
  1054         return retval;
       
  1055         }
       
  1056     
       
  1057     // LOG error
       
  1058     return KErrNotFound;
       
  1059     } 
       
  1060 #endif // __TOOLS2__
       
  1061 
       
  1062 
       
  1063 
       
  1064 
       
  1065 
       
  1066 
       
  1067 //
       
  1068 // Session object used by MMetaDatabase
       
  1069 //
       
  1070 
       
  1071 EXPORT_C CMDBSession* CMDBSession::NewL(::TVersion aRequiredVersion)
       
  1072 /*
       
  1073 NewL
       
  1074 
       
  1075 @publishedAll
       
  1076 */
       
  1077     {
       
  1078 	CMDBSession* r=NewLC(aRequiredVersion);
       
  1079 	CleanupStack::Pop(r);
       
  1080 	return r;
       
  1081     }
       
  1082 
       
  1083 
       
  1084 EXPORT_C CMDBSession* CMDBSession::NewLC(::TVersion aRequiredVersion)
       
  1085 /*
       
  1086 NewLC
       
  1087 
       
  1088 @publishedAll
       
  1089 */
       
  1090     {
       
  1091 	CMDBSession* r=new(ELeave) CMDBSession();
       
  1092 	CleanupStack::PushL(r);
       
  1093 	r->ConstructL(aRequiredVersion);
       
  1094 	return r;
       
  1095     }
       
  1096 
       
  1097 
       
  1098 EXPORT_C CMDBSession::CMDBSession() 
       
  1099 /*
       
  1100 Constructor
       
  1101 
       
  1102 @publishedAll
       
  1103 */
       
  1104     {
       
  1105     }
       
  1106 
       
  1107 
       
  1108 
       
  1109 EXPORT_C CMDBSession::~CMDBSession()
       
  1110 /*
       
  1111 Constructor
       
  1112 
       
  1113 @publishedAll
       
  1114 */
       
  1115     {
       
  1116     delete iMDBSessionImpl;
       
  1117     }
       
  1118     
       
  1119     
       
  1120 
       
  1121 EXPORT_C TInt CMDBSession::ConstructL(TVersion  aRequiredVersion)
       
  1122 /*
       
  1123 Open a session with the storage server and compare data versions
       
  1124         
       
  1125 The MetaDatabase always attempts to support the required version.  If the versions match,
       
  1126 KErrNone will be returned.  If the versions are not the same, but the requested version
       
  1127 is supported, a warning code will be returned. Otherwise an error will be 
       
  1128 returned.  The latest version is returned in the aLatestVersion parameter for information
       
  1129 
       
  1130 @publishedAll
       
  1131 */
       
  1132     {
       
  1133     if ( ! iMDBSessionImpl )
       
  1134         {
       
  1135 	    iMDBSessionImpl = new(ELeave)CMDBSessionImpl(*this);
       
  1136         }
       
  1137     
       
  1138     return iMDBSessionImpl->ConstructL(aRequiredVersion);
       
  1139     } 
       
  1140 
       
  1141 
       
  1142 /* 
       
  1143 Close session with storage server.
       
  1144 */
       
  1145 EXPORT_C void CMDBSession::Close()
       
  1146     {
       
  1147     if (iMDBSessionImpl)
       
  1148         {
       
  1149         iMDBSessionImpl->Close();
       
  1150         }
       
  1151     }
       
  1152 
       
  1153 
       
  1154 
       
  1155 EXPORT_C TVersion CMDBSession::LatestVersion()
       
  1156 /*
       
  1157 Static Lookup of latest data format version
       
  1158 
       
  1159 @publishedAll
       
  1160 */
       
  1161     {
       
  1162     return KCDLatestVersion;
       
  1163     }
       
  1164 
       
  1165 
       
  1166 
       
  1167 EXPORT_C TVersion CMDBSession::VersionInUse()
       
  1168 /*
       
  1169 Lookup data format version currently in use by client
       
  1170 
       
  1171 @publishedAll
       
  1172 */
       
  1173     {
       
  1174     if ( iMDBSessionImpl )
       
  1175         {
       
  1176         return iMDBSessionImpl->VersionInUse();
       
  1177         }
       
  1178 
       
  1179     return TVersion(0,0,0);
       
  1180     }
       
  1181     
       
  1182 
       
  1183 
       
  1184 EXPORT_C void CMDBSession::OpenTransactionL()
       
  1185 /*
       
  1186 Open Transaction with storage server and obtain write lock - if not already set
       
  1187 
       
  1188 Will fail with KErrNotAvailable if Database write locked by another process
       
  1189 
       
  1190 Will fail if client does not have capabilities to obtain write lock on storage server.
       
  1191 
       
  1192 Will report KErrAlreadyInTransaction if transaction already open in this process.
       
  1193 
       
  1194 @publishedAll
       
  1195 */
       
  1196     {
       
  1197     if (! iMDBSessionImpl )
       
  1198         {
       
  1199         User::Leave(KErrNotFound);
       
  1200         }
       
  1201     
       
  1202     iMDBSessionImpl->MaybeOpenTransactionL();
       
  1203 
       
  1204     __FLOG_STATIC(KLogComponent, KCDInfoLog, _L("CMDBSession::OpenTransactionL - Transaction is opened"));
       
  1205     }
       
  1206 
       
  1207 
       
  1208 
       
  1209 EXPORT_C void CMDBSession::CommitTransactionL()
       
  1210 /*
       
  1211 Commit Transaction in database.
       
  1212 
       
  1213 Will fail with KErrNotFound if not in transaction
       
  1214 
       
  1215 Will leave if error occurs during commit.
       
  1216 
       
  1217 @publishedAll
       
  1218 */
       
  1219     {
       
  1220     if (! iMDBSessionImpl)
       
  1221         {
       
  1222         __FLOG_STATIC(KLogComponent, KCDErrLog, _L("CMDBSession::CommitTransactionL - No Session; commit failed!!!"));
       
  1223         
       
  1224         User::Leave(KErrNotFound);
       
  1225         }   
       
  1226     
       
  1227     __ASSERT_DEBUG(iMDBSessionImpl->IsInTransaction(), User::Panic(KCommsDatName,ECommitBeforeOpenTransaction));
       
  1228     
       
  1229     // If client requests commit they take control of any existing transaction
       
  1230     iMDBSessionImpl->MaybeCommitTransactionL(ETrue, 0, KErrNone);
       
  1231     
       
  1232     __FLOG_STATIC(KLogComponent, KCDInfoLog, _L("CMDBSession::CommitTransactionL - Transaction is comitted"));
       
  1233     }
       
  1234 
       
  1235 
       
  1236 
       
  1237 EXPORT_C void CMDBSession::RollbackTransactionL()
       
  1238 /*
       
  1239 Cancel Transaction with database and rollback all associated changes
       
  1240 
       
  1241 Will fail with KErrNotFound if not in transaction
       
  1242 
       
  1243 @publishedAll
       
  1244 */
       
  1245     {
       
  1246     if( ! iMDBSessionImpl  ||  ! iMDBSessionImpl->IsInTransaction() )
       
  1247     	{
       
  1248     	__FLOG_STATIC(KLogComponent, KCDErrLog, _L("CMDBSession::RollbackTransactionL - No Session, or no open Transaction; commit failed!!!"));
       
  1249     	
       
  1250     	User::Leave(KErrNotFound);
       
  1251     	}
       
  1252  
       
  1253     // If client requests rollback they take control of any existing transaction
       
  1254     iMDBSessionImpl->RollbackTransactionL();
       
  1255     
       
  1256     __FLOG_STATIC(KLogComponent, KCDInfoLog, _L("CMDBSession::RollbackTransactionL - Transaction is rolled back"));
       
  1257     }
       
  1258 
       
  1259 
       
  1260 
       
  1261 EXPORT_C TInt CMDBSession::IsInTransaction()
       
  1262 /*
       
  1263 Query if in transaction with database
       
  1264 Return code will indicate 
       
  1265     Not in transaction, 
       
  1266     Already in transaction for this session, 
       
  1267 
       
  1268 @publishedAll
       
  1269 */
       
  1270     {
       
  1271     if( iMDBSessionImpl)
       
  1272         {
       
  1273         return iMDBSessionImpl->IsInTransaction();        
       
  1274         }
       
  1275 
       
  1276     return KErrNotFound;    
       
  1277     }
       
  1278 
       
  1279 
       
  1280 EXPORT_C void CMDBSession::SetAttributeMask(TMDBAttributeFlags aAttributeFlags)
       
  1281 /*
       
  1282 Set the mask the user wishes to use for access control attributes
       
  1283 some attributes are governed by platform security capabilities and can therefore
       
  1284 only be obeyed if user has appropriate capabilities.  
       
  1285 
       
  1286 @publishedAll
       
  1287 */
       
  1288     {
       
  1289     if( iMDBSessionImpl)
       
  1290         {
       
  1291         iMDBSessionImpl->SetAttributeMask(aAttributeFlags);        
       
  1292         }
       
  1293     }
       
  1294 
       
  1295 
       
  1296 EXPORT_C TBool CMDBSession::IsSetAttributeMask(TMDBAttributeFlags aAttributeFlags)
       
  1297 /*
       
  1298 Check attribute mask flags
       
  1299 
       
  1300 @publishedAll
       
  1301 */
       
  1302     {
       
  1303     if( iMDBSessionImpl)
       
  1304         {
       
  1305         return iMDBSessionImpl->IsSetAttributeMask(aAttributeFlags);        
       
  1306         }
       
  1307     return EFalse;
       
  1308     }
       
  1309 
       
  1310 
       
  1311 EXPORT_C void CMDBSession::ClearAttributeMask(TMDBAttributeFlags aAttributeFlags)
       
  1312 /*
       
  1313 Clear the flags from the mask to use for access control attributes
       
  1314 
       
  1315 @publishedAll
       
  1316 */
       
  1317     {
       
  1318     if( iMDBSessionImpl)
       
  1319         {
       
  1320         iMDBSessionImpl->ClearAttributeMask(aAttributeFlags);        
       
  1321         }
       
  1322     }
       
  1323 
       
  1324 #ifndef __TOOLS2__
       
  1325 EXPORT_C TInt CMDBSession::CancelAllNotifications()
       
  1326 /*
       
  1327 Set the mask the user wishes to use for access control attributes
       
  1328 some attributes are governed by platform security capabilities and can therefore
       
  1329 only be obeyed if user has appropriate capabilities.  
       
  1330 
       
  1331 @publishedAll
       
  1332 */
       
  1333     {
       
  1334     if( iMDBSessionImpl)
       
  1335         {
       
  1336         TRAPD(retval, iMDBSessionImpl->NotifierL()->CancelAllNotifications());
       
  1337         return retval;
       
  1338         }
       
  1339     else
       
  1340         return KErrNone; // No notifications extant
       
  1341     }
       
  1342 
       
  1343 
       
  1344 EXPORT_C TInt CMDBSession::PublishProperties()
       
  1345 /*
       
  1346 Attempt to create publish/subscribe properties if not allready defined. The client calling this function 
       
  1347 needs WriteDeviceData capability since these properties are either created in KUidSystemCategory category or
       
  1348 otherwise require protection against spoofing
       
  1349 @publishedAll
       
  1350 */
       
  1351 	{
       
  1352     __FLOG_STATIC0(KLogComponent, KCDInfoLog, _L("CMDBSession::PublishProperties()"));
       
  1353 
       
  1354 	TInt result(KErrNone);
       
  1355 	TInt propval(KErrNone);
       
  1356 	TSecurityPolicy readPolicy(ECapability_None);
       
  1357 	TSecurityPolicy writePolicy(ECapabilityWriteDeviceData);
       
  1358     TSecurityPolicy notificationWritePolicy(KCommsDatNotificationSetCapability);
       
  1359 
       
  1360  	result = RProperty::Get(KUidSystemCategory, KUidCommDbProxiesRecordChange.iUid, propval);
       
  1361 
       
  1362     if (result == KErrNotFound)
       
  1363 		{
       
  1364 		result = RProperty::Define(KUidSystemCategory,KUidCommDbSMSBearerChange.iUid, RProperty::EInt, readPolicy, writePolicy);
       
  1365 		__ASSERT_DEBUG(result == KErrPermissionDenied || result == KErrNone,User::Invariant());
       
  1366 
       
  1367         result = RProperty::Define(KUidSystemCategory,KUidCommDbNotificationEvent.iUid, RProperty::EInt, readPolicy, notificationWritePolicy);
       
  1368 		__ASSERT_DEBUG(result == KErrPermissionDenied || result == KErrNone,User::Invariant());
       
  1369 
       
  1370 		result = RProperty::Define(KUidSystemCategory,KUidCommDbSMSReceiveModeChange.iUid, RProperty::EInt, readPolicy, writePolicy);
       
  1371 		__ASSERT_DEBUG(result == KErrPermissionDenied || result == KErrNone,User::Invariant());
       
  1372 		result = RProperty::Define(KUidSystemCategory,KUidCommDbGPRSAttachModeChange.iUid, RProperty::EInt, readPolicy, writePolicy);
       
  1373 		__ASSERT_DEBUG(result == KErrPermissionDenied || result == KErrNone,User::Invariant());
       
  1374 		result = RProperty::Define(KUidSystemCategory,KUidCommDbModemTsyNameChange.iUid, RProperty::EInt, readPolicy, writePolicy);
       
  1375 		__ASSERT_DEBUG(result == KErrPermissionDenied || result == KErrNone,User::Invariant());
       
  1376 		result = RProperty::Define(KUidSystemCategory,KUidCommDbModemDataAndFaxChange.iUid, RProperty::EInt, readPolicy, writePolicy);
       
  1377 		__ASSERT_DEBUG(result == KErrPermissionDenied || result == KErrNone,User::Invariant());
       
  1378 
       
  1379 		result = RProperty::Define(KUidSystemCategory,KUidCommDbModemPhoneServicesAndSMSChange.iUid, RProperty::EInt, readPolicy, writePolicy);
       
  1380 		__ASSERT_DEBUG(result == KErrPermissionDenied || result == KErrNone,User::Invariant());
       
  1381 		result = RProperty::Define(KUidSystemCategory,KUidCommDbGPRSDefaultParamsChange.iUid, RProperty::EInt, readPolicy, writePolicy);
       
  1382 		__ASSERT_DEBUG(result == KErrPermissionDenied || result == KErrNone,User::Invariant());
       
  1383 		result = RProperty::Define(KUidSystemCategory,KUidCommDbModemRecordChange.iUid, RProperty::EInt, readPolicy, writePolicy);
       
  1384 		__ASSERT_DEBUG(result == KErrPermissionDenied || result == KErrNone,User::Invariant());
       
  1385 		result = RProperty::Define(KUidSystemCategory,KUidCommDbProxiesRecordChange.iUid, RProperty::EInt, readPolicy, writePolicy);
       
  1386 		__ASSERT_DEBUG(result == KErrPermissionDenied || result == KErrNone,User::Invariant());
       
  1387 
       
  1388 		}
       
  1389 
       
  1390  	result = RProperty::Get(KUidCommsDatStatusEvent, KCommsDatStatusEventCommitSeq, propval);
       
  1391     if (result == KErrNotFound)
       
  1392 		{
       
  1393 		result = RProperty::Define(KUidCommsDatStatusEvent, KCommsDatStatusEventCommitSeq, RProperty::EInt, readPolicy, writePolicy);
       
  1394 		__ASSERT_DEBUG(result == KErrPermissionDenied || result == KErrNone,User::Invariant());
       
  1395 		}
       
  1396 	
       
  1397 	return result;
       
  1398 	}	
       
  1399 #endif // __TOOLS2__
       
  1400 
       
  1401 //EOF
       
  1402 
       
  1403