contentpublishingsrv/contentpublishingserver/cpsqlitestorage/src/cpstorageengine.cpp
changeset 93 82b66994846c
parent 92 782e3408c2ab
child 94 dbb8300717f7
equal deleted inserted replaced
92:782e3408c2ab 93:82b66994846c
     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()
       
    33     {
       
    34     }
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 // CCpStorageEngine::ConstructL()
       
    38 //
       
    39 // ---------------------------------------------------------------------------
       
    40 //
       
    41 void CCpStorageEngine::ConstructL()
       
    42     {
       
    43 #ifdef CONTENT_PUBLISHER_DEBUG
       
    44     if(!CCPDebug::Enable())
       
    45         {
       
    46         iDebug = CCPDebug::NewL( KCPDebugFileName2 );
       
    47         }
       
    48 #endif
       
    49     CP_DEBUG( _L8("CCpStorageEngine::ConstructL()") );
       
    50     
       
    51     iRepository = CRepository::NewL( KServerUid );
       
    52     TBool createNew( EFalse );
       
    53     TInt err = iSqlDb.Open( KCPdatabase, &KMdsSqlDbaConfig );
       
    54     if( err == KSqlErrCorrupt )
       
    55     	{
       
    56     	DeleteCorruptedDbL();
       
    57     	createNew = ETrue;
       
    58     	}
       
    59     else if( err/* == KErrNotFound */)
       
    60     	{
       
    61     	createNew = ETrue;
       
    62     	}
       
    63     
       
    64     if ( createNew )
       
    65         {
       
    66         CreateNewDbL();
       
    67         }
       
    68     }
       
    69 
       
    70 // ---------------------------------------------------------------------------
       
    71 // CCpStorageEngine::NewL()
       
    72 //
       
    73 // ---------------------------------------------------------------------------
       
    74 //
       
    75 CCpStorageEngine* CCpStorageEngine::NewL()
       
    76     {
       
    77     CCpStorageEngine* self = CCpStorageEngine::NewLC( );
       
    78     CleanupStack::Pop( self );
       
    79     return self;
       
    80     }
       
    81 
       
    82 // ---------------------------------------------------------------------------
       
    83 // CCpStorageEngine::NewLC()
       
    84 //
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 CCpStorageEngine* CCpStorageEngine::NewLC()
       
    88     {
       
    89     CCpStorageEngine* self = new (ELeave) CCpStorageEngine();
       
    90     CleanupStack::PushL( self );
       
    91     self->ConstructL( );
       
    92     return self;
       
    93     }
       
    94 
       
    95 // ---------------------------------------------------------------------------
       
    96 // CCpStorageEngine::~CCpStorageEngine() 
       
    97 //
       
    98 // ---------------------------------------------------------------------------
       
    99 //
       
   100 CCpStorageEngine::~CCpStorageEngine()
       
   101     {
       
   102     CP_DEBUG( _L8("CCpStorageEngine::~CCpStorageEngine()") );
       
   103     iSqlDb.Close( );
       
   104     delete iRepository;
       
   105 #ifdef CONTENT_PUBLISHER_DEBUG
       
   106     delete iDebug;
       
   107 #endif
       
   108     }
       
   109 
       
   110 // ---------------------------------------------------------------------------
       
   111 // CCpStorageEngine::CreateTablesL 
       
   112 //
       
   113 // ---------------------------------------------------------------------------
       
   114 //
       
   115 void CCpStorageEngine::CreateTablesL()
       
   116     {
       
   117     CP_DEBUG( _L8("CCpStorageEngine::CreateTablesL()") );
       
   118 
       
   119     //Main Table
       
   120     User::LeaveIfError( iSqlDb.Exec( KSQLCreateMainTable ) );
       
   121     User::LeaveIfError( iSqlDb.Exec( KSQLIndexOnMain_Publisher ) );
       
   122     User::LeaveIfError( iSqlDb.Exec( KSQLIndexOnMain_Content_type ) );
       
   123     //Publisher Table
       
   124     User::LeaveIfError( iSqlDb.Exec( KSQLCreatePublisherTable ) );
       
   125     User::LeaveIfError( iSqlDb.Exec( KSQLIndexOnPub_Content_type ) );
       
   126     User::LeaveIfError( iSqlDb.Exec( KSQLIndexOnPub_Content_type_Content_id ) );
       
   127     }
       
   128 
       
   129 // ---------------------------------------------------------------------------
       
   130 // CCpStorageEngine::GetListL
       
   131 //
       
   132 // ---------------------------------------------------------------------------
       
   133 //    
       
   134 void CCpStorageEngine::GetListL( const CLiwMap* aMap,
       
   135     CLiwGenericParamList& aList )
       
   136     {
       
   137     CP_DEBUG( _L8("CCpStorageEngine::GetListL()") );
       
   138     const CCPLiwMap* map = static_cast<const CCPLiwMap*>(aMap);
       
   139     RArray<TInt32> itemsToDelete;
       
   140     TUint numberOfItems = map->GetListL( iSqlDb, &aList, itemsToDelete );
       
   141     //delete outdated items
       
   142     CContentMap* removeItemMap = CContentMap::NewLC( );
       
   143     for ( TInt i = 0; i<itemsToDelete.Count( ); i++ )
       
   144         {
       
   145         removeItemMap->InsertL( KId, TLiwVariant( itemsToDelete[i] ) );
       
   146         removeItemMap->InsertL( KType, TLiwVariant( KCpData ) );
       
   147         RemoveL( removeItemMap );
       
   148         removeItemMap->Remove( KId );
       
   149         }
       
   150     CleanupStack::PopAndDestroy( removeItemMap );
       
   151     itemsToDelete.Reset( );
       
   152 
       
   153     if ( numberOfItems == 0 )
       
   154         {
       
   155         User::Leave( KErrNotFound );
       
   156         }
       
   157     }
       
   158 
       
   159 // ---------------------------------------------------------------------------
       
   160 // CCpStorageEngine::AddL
       
   161 //
       
   162 // ---------------------------------------------------------------------------
       
   163 //  
       
   164 TInt32 CCpStorageEngine::AddL( const CLiwMap* aMap )
       
   165     {
       
   166     CP_DEBUG( _L8("CCpStorageEngine::AddL()") );
       
   167     TInt32 id(0);
       
   168     const CCPLiwMap* map = static_cast<const CCPLiwMap*>(aMap);
       
   169     if ( iCallback )
       
   170         {
       
   171         CLiwDefaultList* listOfMaps = CLiwDefaultList::NewLC( );
       
   172         id = map->AddL( iSqlDb, listOfMaps );
       
   173         iCallback->HandleChangeL( listOfMaps );
       
   174         CleanupStack::PopAndDestroy( listOfMaps ) ;
       
   175         }
       
   176     else
       
   177         {
       
   178         id = map->AddL( iSqlDb );
       
   179         }
       
   180     return id;
       
   181     }
       
   182 
       
   183 // ---------------------------------------------------------------------------
       
   184 // CCpStorageEngine::RemoveL
       
   185 //
       
   186 // ---------------------------------------------------------------------------
       
   187 //  
       
   188 void CCpStorageEngine::RemoveL( const CLiwMap* aMap )
       
   189     {
       
   190     CP_DEBUG( _L8("CCpStorageEngine::RemoveL()") );
       
   191     const CCPLiwMap* map = static_cast<const CCPLiwMap*>(aMap);
       
   192     if ( iCallback )
       
   193         {
       
   194         CLiwDefaultList* listOfMaps = CLiwDefaultList::NewLC( );
       
   195         map->RemoveL( iSqlDb, listOfMaps );
       
   196         iCallback->HandleChangeL( listOfMaps );
       
   197         CleanupStack::PopAndDestroy( listOfMaps ) ;
       
   198         }
       
   199     else
       
   200         {
       
   201         map->RemoveL( iSqlDb );
       
   202         }
       
   203     }
       
   204 
       
   205 // ---------------------------------------------------------------------------
       
   206 // CCpStorageEngine::SetCallback()
       
   207 //
       
   208 // ---------------------------------------------------------------------------
       
   209 //  
       
   210 void CCpStorageEngine::SetCallback( MCPChangeNotifier* aCallback )
       
   211     {
       
   212     iCallback = aCallback;
       
   213     }
       
   214 
       
   215 // ---------------------------------------------------------------------------
       
   216 // CCpStorageEngine::DeleteCorruptedDbL()
       
   217 //
       
   218 // ---------------------------------------------------------------------------
       
   219 //  
       
   220 void CCpStorageEngine::DeleteCorruptedDbL()
       
   221 	{
       
   222 	User::LeaveIfError( iSqlDb.Delete( KCPdatabase ) );
       
   223 	TInt value( 0 );
       
   224 	if( !iRepository->Get( KCPStorageUid, value ) )
       
   225 		{
       
   226 		value ^= KSQLDBStateNormal;
       
   227 		value |= KSQLDBStateRestored;
       
   228 		}
       
   229 	else
       
   230 		{
       
   231 		value = KSQLDBStateRestored;
       
   232 		}
       
   233 	iRepository->Set( KCPStorageUid, value );
       
   234 	}
       
   235 
       
   236 // ---------------------------------------------------------------------------
       
   237 // CCpStorageEngine::CreateNewDbL()
       
   238 //
       
   239 // ---------------------------------------------------------------------------
       
   240 //  
       
   241 void CCpStorageEngine::CreateNewDbL()
       
   242     {
       
   243     TSecureId id( KServerUid ); // Uid of CP Server - only this process has access rights
       
   244     TSecurityPolicy defaultPolicy;
       
   245     TSecurityPolicy securityPolicy(id);
       
   246     RSqlSecurityPolicy sqlSecurityPolicy;
       
   247     CleanupClosePushL( sqlSecurityPolicy );
       
   248     User::LeaveIfError( sqlSecurityPolicy.Create( defaultPolicy ) );
       
   249     User::LeaveIfError( sqlSecurityPolicy.SetDbPolicy(
       
   250         RSqlSecurityPolicy::ESchemaPolicy, securityPolicy ) );
       
   251 #ifdef CONTENT_PUBLISHER_STORAGE_MT
       
   252     iSqlDb.CreateL( KCPpathdatabase, &KMdsSqlDbaConfig );
       
   253 #else
       
   254     iSqlDb.CreateL( KCPdatabase, sqlSecurityPolicy, &KMdsSqlDbaConfig );
       
   255 #endif        
       
   256     CleanupStack::PopAndDestroy( &sqlSecurityPolicy );
       
   257     CreateTablesL( );
       
   258     
       
   259     TInt value( 0 );
       
   260     if( iRepository->Get( KCPStorageUid, value ) == KErrNotFound )
       
   261 		{
       
   262 		value = KSQLDBStateNormal;
       
   263 		iRepository->Set( KCPStorageUid, value );
       
   264 		}
       
   265     }
       
   266