contentpublishingsrv/contentpublishingserver/cpsqlitestorage/src/cpstorageengine.cpp
branchRCL_3
changeset 30 a5a39a295112
equal deleted inserted replaced
29:0efa10d348c0 30:a5a39a295112
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include "cpstorageengine.h"
       
    20 #include "cpliwmap.h"
       
    21 #include "ccontentmap.h"
       
    22 #include "cpdebug.h"
       
    23 #include "cpssqlconstants.h"
       
    24 #include "cpserverdef.h"
       
    25 #include <centralrepository.h>
       
    26 
       
    27 // ---------------------------------------------------------------------------
       
    28 // CCpStorageEngine::CCpStorageEngine()
       
    29 //
       
    30 // ---------------------------------------------------------------------------
       
    31 //
       
    32 CCpStorageEngine::CCpStorageEngine(TLiwVariant& aDataMapCache) :
       
    33     iDataMapCache(aDataMapCache)
       
    34     {
       
    35     }
       
    36 
       
    37 // ---------------------------------------------------------------------------
       
    38 // CCpStorageEngine::ConstructL()
       
    39 //
       
    40 // ---------------------------------------------------------------------------
       
    41 //
       
    42 void CCpStorageEngine::ConstructL()
       
    43     {
       
    44 #ifdef CONTENT_PUBLISHER_DEBUG
       
    45     if(!CCPDebug::Enable())
       
    46         {
       
    47         iDebug = CCPDebug::NewL( KCPDebugFileName2 );
       
    48         }
       
    49 #endif
       
    50     CP_DEBUG( _L8("CCpStorageEngine::ConstructL()") );
       
    51     
       
    52     iRepository = CRepository::NewL( KServerUid );
       
    53     TBool createNew( EFalse );
       
    54     TInt err = iSqlDb.Open( KCPdatabase, &KMdsSqlDbaConfig );
       
    55     if( err == KSqlErrCorrupt )
       
    56     	{
       
    57     	DeleteCorruptedDbL();
       
    58     	createNew = ETrue;
       
    59     	}
       
    60     else if( err/* == KErrNotFound */)
       
    61     	{
       
    62     	createNew = ETrue;
       
    63     	}
       
    64     
       
    65     if ( createNew )
       
    66         {
       
    67         CreateNewDbL();
       
    68         }
       
    69     }
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // CCpStorageEngine::NewL()
       
    73 //
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 CCpStorageEngine* CCpStorageEngine::NewL(TLiwVariant& aDataMapCache)
       
    77     {
       
    78     CCpStorageEngine* self = CCpStorageEngine::NewLC(aDataMapCache);
       
    79     CleanupStack::Pop( self );
       
    80     return self;
       
    81     }
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // CCpStorageEngine::NewLC()
       
    85 //
       
    86 // ---------------------------------------------------------------------------
       
    87 //
       
    88 CCpStorageEngine* CCpStorageEngine::NewLC(TLiwVariant& aDataMapCache)
       
    89     {
       
    90     CCpStorageEngine* self = new (ELeave) CCpStorageEngine(aDataMapCache);
       
    91     CleanupStack::PushL( self );
       
    92     self->ConstructL( );
       
    93     return self;
       
    94     }
       
    95 
       
    96 // ---------------------------------------------------------------------------
       
    97 // CCpStorageEngine::~CCpStorageEngine() 
       
    98 //
       
    99 // ---------------------------------------------------------------------------
       
   100 //
       
   101 CCpStorageEngine::~CCpStorageEngine()
       
   102     {
       
   103     CP_DEBUG( _L8("CCpStorageEngine::~CCpStorageEngine()") );
       
   104     iSqlDb.Close( );
       
   105     delete iRepository;
       
   106 #ifdef CONTENT_PUBLISHER_DEBUG
       
   107     delete iDebug;
       
   108 #endif
       
   109     }
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // CCpStorageEngine::CreateTablesL 
       
   113 //
       
   114 // ---------------------------------------------------------------------------
       
   115 //
       
   116 void CCpStorageEngine::CreateTablesL()
       
   117     {
       
   118     CP_DEBUG( _L8("CCpStorageEngine::CreateTablesL()") );
       
   119 
       
   120     //Main Table
       
   121     User::LeaveIfError( iSqlDb.Exec( KSQLCreateMainTable ) );
       
   122     User::LeaveIfError( iSqlDb.Exec( KSQLIndexOnMain_Publisher ) );
       
   123     User::LeaveIfError( iSqlDb.Exec( KSQLIndexOnMain_Content_type ) );
       
   124     //Publisher Table
       
   125     User::LeaveIfError( iSqlDb.Exec( KSQLCreatePublisherTable ) );
       
   126     User::LeaveIfError( iSqlDb.Exec( KSQLIndexOnPub_Content_type ) );
       
   127     User::LeaveIfError( iSqlDb.Exec( KSQLIndexOnPub_Content_type_Content_id ) );
       
   128     }
       
   129 
       
   130 // ---------------------------------------------------------------------------
       
   131 // CCpStorageEngine::GetListL
       
   132 //
       
   133 // ---------------------------------------------------------------------------
       
   134 //    
       
   135 void CCpStorageEngine::GetListL( const CLiwMap* aMap,
       
   136     CLiwGenericParamList& aList )
       
   137     {
       
   138     CP_DEBUG( _L8("CCpStorageEngine::GetListL()") );
       
   139     const CCPLiwMap* map = static_cast<const CCPLiwMap*>(aMap);
       
   140     
       
   141     RArray<TInt32> itemsToDelete;
       
   142     CleanupClosePushL( itemsToDelete );
       
   143     
       
   144     TUint numberOfItems = map->GetListL( iSqlDb, &aList, itemsToDelete );
       
   145     //delete outdated items
       
   146     CContentMap* removeItemMap = CContentMap::NewLC( );
       
   147     for ( TInt i = 0; i<itemsToDelete.Count( ); i++ )
       
   148         {
       
   149         removeItemMap->InsertL( KId, TLiwVariant( itemsToDelete[i] ) );
       
   150         removeItemMap->InsertL( KType, TLiwVariant( KCpData ) );
       
   151         RemoveL( removeItemMap );
       
   152         removeItemMap->Remove( KId );
       
   153         }
       
   154     CleanupStack::PopAndDestroy( removeItemMap );
       
   155     CleanupStack::PopAndDestroy( &itemsToDelete );
       
   156 
       
   157     if ( numberOfItems == 0 )
       
   158         {
       
   159         User::Leave( KErrNotFound );
       
   160         }
       
   161     }
       
   162 
       
   163 // ---------------------------------------------------------------------------
       
   164 // CCpStorageEngine::AddL
       
   165 //
       
   166 // ---------------------------------------------------------------------------
       
   167 //  
       
   168 TInt32 CCpStorageEngine::AddL( const CLiwMap* aMap )
       
   169     {
       
   170     CP_DEBUG( _L8("CCpStorageEngine::AddL()") );
       
   171     TInt32 id(0);
       
   172     const CCPLiwMap* map = static_cast<const CCPLiwMap*>(aMap);
       
   173     if ( iCallback )
       
   174         {
       
   175         CLiwDefaultList* listOfMaps = CLiwDefaultList::NewLC( );
       
   176         id = map->AddL( iSqlDb, listOfMaps );
       
   177         iDataMapCache.Reset();
       
   178         map->FindL(KDataMap, iDataMapCache);
       
   179         iCallback->HandleChangeL( listOfMaps );
       
   180         //data cache must be cleaned here, otherwise it can mess up other requests.
       
   181         iDataMapCache.Reset();
       
   182         CleanupStack::PopAndDestroy( listOfMaps ) ;
       
   183         }
       
   184     else
       
   185         {
       
   186         id = map->AddL( iSqlDb );
       
   187         }
       
   188     return id;
       
   189     }
       
   190 
       
   191 // ---------------------------------------------------------------------------
       
   192 // CCpStorageEngine::RemoveL
       
   193 //
       
   194 // ---------------------------------------------------------------------------
       
   195 //  
       
   196 void CCpStorageEngine::RemoveL( const CLiwMap* aMap )
       
   197     {
       
   198     CP_DEBUG( _L8("CCpStorageEngine::RemoveL()") );
       
   199     const CCPLiwMap* map = static_cast<const CCPLiwMap*>(aMap);
       
   200     if ( iCallback )
       
   201         {
       
   202         CLiwDefaultList* listOfMaps = CLiwDefaultList::NewLC( );
       
   203         map->RemoveL( iSqlDb, listOfMaps );
       
   204         iCallback->HandleChangeL( listOfMaps );
       
   205         CleanupStack::PopAndDestroy( listOfMaps ) ;
       
   206         }
       
   207     else
       
   208         {
       
   209         map->RemoveL( iSqlDb );
       
   210         }
       
   211     }
       
   212 
       
   213 // ---------------------------------------------------------------------------
       
   214 // CCpStorageEngine::SetCallback()
       
   215 //
       
   216 // ---------------------------------------------------------------------------
       
   217 //  
       
   218 void CCpStorageEngine::SetCallback( MCPChangeNotifier* aCallback )
       
   219     {
       
   220     iCallback = aCallback;
       
   221     }
       
   222 
       
   223 // ---------------------------------------------------------------------------
       
   224 // CCpStorageEngine::DeleteCorruptedDbL()
       
   225 //
       
   226 // ---------------------------------------------------------------------------
       
   227 //  
       
   228 void CCpStorageEngine::DeleteCorruptedDbL()
       
   229 	{
       
   230 	User::LeaveIfError( iSqlDb.Delete( KCPdatabase ) );
       
   231 	TInt value( 0 );
       
   232 	if( !iRepository->Get( KCPStorageUid, value ) )
       
   233 		{
       
   234 		value ^= KSQLDBStateNormal;
       
   235 		value |= KSQLDBStateRestored;
       
   236 		}
       
   237 	else
       
   238 		{
       
   239 		value = KSQLDBStateRestored;
       
   240 		}
       
   241 	iRepository->Set( KCPStorageUid, value );
       
   242 	}
       
   243 
       
   244 // ---------------------------------------------------------------------------
       
   245 // CCpStorageEngine::CreateNewDbL()
       
   246 //
       
   247 // ---------------------------------------------------------------------------
       
   248 //  
       
   249 void CCpStorageEngine::CreateNewDbL()
       
   250     {
       
   251     TSecureId id( KServerUid ); // Uid of CP Server - only this process has access rights
       
   252     TSecurityPolicy defaultPolicy( id );
       
   253     RSqlSecurityPolicy sqlSecurityPolicy;
       
   254     CleanupClosePushL( sqlSecurityPolicy );
       
   255     User::LeaveIfError( sqlSecurityPolicy.Create( defaultPolicy ) );
       
   256 #ifdef CONTENT_PUBLISHER_STORAGE_MT
       
   257     iSqlDb.CreateL( KCPpathdatabase, &KMdsSqlDbaConfig );
       
   258 #else
       
   259     iSqlDb.CreateL( KCPdatabase, sqlSecurityPolicy, &KMdsSqlDbaConfig );
       
   260 #endif        
       
   261     CleanupStack::PopAndDestroy( &sqlSecurityPolicy );
       
   262     CreateTablesL( );
       
   263     
       
   264     TInt value( 0 );
       
   265     if( iRepository->Get( KCPStorageUid, value ) == KErrNotFound )
       
   266 		{
       
   267 		value = KSQLDBStateNormal;
       
   268 		iRepository->Set( KCPStorageUid, value );
       
   269 		}
       
   270     }
       
   271