metadataengine/server/src/mdsmaintenanceengine.cpp
changeset 0 c53acadfccc6
child 3 b73a2e62868f
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2009 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:  Class for database import/export and maintenance
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include "mdsmaintenanceengine.h"
       
    20 #include "mdslogger.h"
       
    21 #include "mdsmanipulationengine.h"
       
    22 #include "mdcserializationbuffer.h"
       
    23 #include "mdsimportexport.h"
       
    24 #include "mdssqliteconnection.h"
       
    25 #include "mdsschema.h"
       
    26 #include "mdssqldbmaintenance.h"
       
    27 #include "mdsdbconnectionpool.h"
       
    28 #include "mdsindexer.h"
       
    29 #include "mdspreferences.h"
       
    30 
       
    31 __USES_LOGGER
       
    32 
       
    33 // ------------------------------------------------
       
    34 // NewL
       
    35 // ------------------------------------------------
       
    36 //
       
    37 CMdSMaintenanceEngine* CMdSMaintenanceEngine::NewL()
       
    38     {
       
    39     CMdSMaintenanceEngine* self = CMdSMaintenanceEngine::NewLC();
       
    40     CleanupStack::Pop( self );
       
    41     return self;
       
    42     }
       
    43 
       
    44 // ------------------------------------------------
       
    45 // NewLC
       
    46 // ------------------------------------------------
       
    47 //
       
    48 CMdSMaintenanceEngine* CMdSMaintenanceEngine::NewLC()
       
    49     {
       
    50     CMdSMaintenanceEngine* self = new ( ELeave ) CMdSMaintenanceEngine();
       
    51     CleanupStack::PushL( self );
       
    52     self->ConstructL( );
       
    53     return self;
       
    54     }
       
    55 
       
    56 // ------------------------------------------------
       
    57 // Constructor
       
    58 // ------------------------------------------------
       
    59 //
       
    60 CMdSMaintenanceEngine::CMdSMaintenanceEngine()
       
    61     {
       
    62     }
       
    63 
       
    64 // ------------------------------------------------
       
    65 // NewL
       
    66 // ------------------------------------------------
       
    67 //
       
    68 void CMdSMaintenanceEngine::ConstructL()
       
    69     {
       
    70     iMaintenance = CMdSSqlDbMaintenance::NewL();
       
    71     }
       
    72 
       
    73 // ------------------------------------------------
       
    74 // Destructor
       
    75 // ------------------------------------------------
       
    76 //
       
    77 CMdSMaintenanceEngine::~CMdSMaintenanceEngine()
       
    78     {
       
    79     delete iMaintenance;
       
    80     }
       
    81 
       
    82 // ------------------------------------------------
       
    83 // InitConnectionL
       
    84 // ------------------------------------------------
       
    85 //
       
    86 void CMdSMaintenanceEngine::InitConnectionL()
       
    87     {
       
    88     // open the database
       
    89     MMdSDbConnectionPool::ConnectAllL();
       
    90     }
       
    91 
       
    92 // ------------------------------------------------
       
    93 // CloseDatabaseL
       
    94 // ------------------------------------------------
       
    95 //
       
    96 void CMdSMaintenanceEngine::CloseDatabase()
       
    97 	{
       
    98 	MMdSDbConnectionPool::DisconnectAll();
       
    99 	}
       
   100 
       
   101 // ------------------------------------------------
       
   102 // DeleteDatabaseL
       
   103 // ------------------------------------------------
       
   104 //
       
   105 void CMdSMaintenanceEngine::DeleteDatabase()
       
   106 	{
       
   107 	CMdSMaintenanceEngine::CloseDatabase();    
       
   108 	CMdSSqLiteConnection::DeleteDb( );
       
   109 	}
       
   110 
       
   111 // ------------------------------------------------
       
   112 // InstallL
       
   113 // ------------------------------------------------
       
   114 //
       
   115 void CMdSMaintenanceEngine::InstallL( CMdSManipulationEngine& aManipulate, CMdsSchema& aSchema )
       
   116     {
       
   117     if ( !(iMaintenance->ValidateL( ) ) )
       
   118         {
       
   119         // first-time init: re-form the database completely
       
   120     	iMaintenance->CreateDatabaseL( );
       
   121 		const TUint KMdSServerUid = 0x0320e65f; // temporal uid
       
   122 
       
   123 		// try to read schema file from C drive
       
   124 		TRAPD( err, ImportSchemaL( aSchema, KSchemaImportFile, KMdSServerUid) );
       
   125 
       
   126 		if( err != KErrNone )
       
   127 			{
       
   128 			__LOG1( ELogAlways, "Schema reading error: %d", err );
       
   129 			// if schema file is not found, try to read from rom (Z) drive
       
   130 			if ( err == KErrNotFound || err == KErrPathNotFound )
       
   131 				{
       
   132 				TRAP( err, ImportSchemaL( aSchema, KSchemaRomImportFile, KMdSServerUid) );
       
   133 				}
       
   134 			if( err != KErrNone )
       
   135 				{
       
   136 				__LOG1( ELogAlways, "Schema reading error: %d", err );
       
   137 				DeleteDatabase();
       
   138 				User::Leave( err );
       
   139 				}
       
   140 			}
       
   141 
       
   142 		if ( FailedImports() != 0 )
       
   143   			{
       
   144 	       	User::Leave( KErrCorrupt );
       
   145        		}
       
   146 		
       
   147 		// try to read default import file from C drive
       
   148        	TRAP( err, ImportMetadataL( aManipulate, aSchema, KMdsDefaultImportFile ) );
       
   149        	if ( err == KErrNotFound || err == KErrPathNotFound )
       
   150        		{
       
   151        		// if default import file is not found, try to read from rom (Z) drive
       
   152        		// and ignore errors
       
   153        		TRAP_IGNORE( ImportMetadataL( aManipulate, aSchema, KMdsDefaultRomImportFile ) );
       
   154        		}
       
   155        	
       
   156        	StoreCDriveMediaIdL();
       
   157         }
       
   158     else
       
   159         {
       
   160         TRAPD( err, LoadSchemaL( aSchema ) );
       
   161 
       
   162         if( err != KErrNone )
       
   163         	{ 
       
   164 			DeleteDatabase();
       
   165 			User::Leave( err );
       
   166         	}
       
   167         }
       
   168     }
       
   169 
       
   170 // ------------------------------------------------
       
   171 // LoadSchemaL
       
   172 // ------------------------------------------------
       
   173 //
       
   174 void CMdSMaintenanceEngine::LoadSchemaL( CMdsSchema& aSchema )
       
   175     {
       
   176     CMdsImportExport* impSchema = CMdsImportExport::NewLC();
       
   177     impSchema->ImportSchemaFromDBL( aSchema );
       
   178     CleanupStack::PopAndDestroy( impSchema );
       
   179     aSchema.SerializeToSharedMemoryL();
       
   180     }
       
   181     
       
   182 // ------------------------------------------------
       
   183 // ImportSchemaL
       
   184 // ------------------------------------------------
       
   185 //
       
   186 void CMdSMaintenanceEngine::ImportSchemaL( CMdsSchema& aSchema, const TDesC16& aFileName, TUint32 aVendorId )
       
   187     {
       
   188     CMdsImportExport* impSchema = CMdsImportExport::NewLC();
       
   189     impSchema->ImportSchemaFromFileL( aFileName, aSchema, aVendorId );
       
   190     CleanupStack::PopAndDestroy( impSchema );
       
   191 
       
   192     aSchema.StoreToDBL();
       
   193 
       
   194 	aSchema.SerializeToSharedMemoryL();
       
   195     }
       
   196 
       
   197 // ------------------------------------------------
       
   198 // ImportMetadataL
       
   199 // ------------------------------------------------
       
   200 //
       
   201 TInt CMdSMaintenanceEngine::ImportMetadataL( CMdSManipulationEngine& aManipulate, CMdsSchema& aSchema, const TDesC16& aFileName )
       
   202     {
       
   203     CMdsImportExport* importMetadata = CMdsImportExport::NewLC();
       
   204     TInt failed = KErrNone;
       
   205     TRAPD( err, failed = importMetadata->ImportMetadataL( aManipulate.Manipulate(), aSchema, aFileName ) );
       
   206     if (err != KErrNone)
       
   207     	{
       
   208     	failed = err;
       
   209     	}
       
   210     CleanupStack::PopAndDestroy( importMetadata );
       
   211     
       
   212    	iFailedImports = failed;
       
   213     return failed;
       
   214     }
       
   215     
       
   216 // ------------------------------------------------
       
   217 // ExportMetadataL
       
   218 // ------------------------------------------------
       
   219 //
       
   220 void CMdSMaintenanceEngine::ExportMetadataL( CMdsSchema& aSchema, const TDesC16& aFileName, 
       
   221                                              CMdCSerializationBuffer& aItems )
       
   222 	{
       
   223 	CMdsImportExport* exporter = CMdsImportExport::NewLC();
       
   224 	exporter->ExportMetadataL( aSchema, aFileName, aItems );
       
   225 	CleanupStack::PopAndDestroy( exporter );
       
   226     }
       
   227 
       
   228 // ------------------------------------------------
       
   229 // StoreCDriveMediaIdL
       
   230 // ------------------------------------------------
       
   231 //
       
   232 void CMdSMaintenanceEngine::StoreCDriveMediaIdL()
       
   233 	{
       
   234 	RFs fs;
       
   235     User::LeaveIfError( fs.Connect() );
       
   236     CleanupClosePushL( fs );
       
   237     TVolumeInfo volumeInfo;
       
   238     fs.Volume( volumeInfo, EDriveC );
       
   239     MMdsPreferences::InsertL( KCMediaIdKey, MMdsPreferences::EPreferenceValueSet,
       
   240     		(TUint32) volumeInfo.iUniqueID );
       
   241     
       
   242     CleanupStack::PopAndDestroy( &fs );
       
   243 	}
       
   244