internetradio2.0/sessionlogsrc/irnmslogdb.cpp
changeset 14 896e9dbc5f19
parent 12 608f67c22514
child 15 065198191975
equal deleted inserted replaced
12:608f67c22514 14:896e9dbc5f19
     1 /*
       
     2 * Copyright (c) 2006-2007 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:  ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <bautils.h>
       
    20 
       
    21 #include "irdebug.h" 
       
    22 #include "irnmslogdb.h"
       
    23 #include "irnmslogger.h"
       
    24 
       
    25 _LIT(KNmsLogTable,"NmsLog"); 
       
    26 _LIT(KID,"id");
       
    27 _LIT(KNmsLogCol,  "NmsLogData");
       
    28 _LIT(KNmsLogIndex,"NmsLogIndex");
       
    29 
       
    30 const TInt KMaxNoNmsLog = 15;
       
    31 
       
    32 
       
    33 // ======== LOCAL FUNCTIONS ========
       
    34 
       
    35 
       
    36 // ======== MEMBER FUNCTIONS ========
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // Function : NewL()
       
    40 // two phased construction
       
    41 // ---------------------------------------------------------------------------
       
    42 //
       
    43   CIRNmsLogDb* CIRNmsLogDb::NewL()
       
    44 	{
       
    45 	IRLOG_DEBUG( "CIRNmsLogDb::NewL" );
       
    46 	CIRNmsLogDb* self;
       
    47 	self=CIRNmsLogDb::NewLC();
       
    48 	CleanupStack::Pop(self);
       
    49 	IRLOG_DEBUG( "CIRNmsLogDb::NewL - Exiting." );
       
    50 	return self;
       
    51 	}
       
    52  
       
    53  
       
    54 // ---------------------------------------------------------------------------
       
    55 // Function : NewLC()
       
    56 // Two phased construction
       
    57 // ---------------------------------------------------------------------------
       
    58 //
       
    59   CIRNmsLogDb* CIRNmsLogDb::NewLC()
       
    60 	{
       
    61 	IRLOG_DEBUG( "CIRNmsLogDb::NewLC" );
       
    62 	CIRNmsLogDb *self;
       
    63 	self=new(ELeave)CIRNmsLogDb;
       
    64 	CleanupStack::PushL(self);
       
    65 	self->ConstructL();
       
    66 	IRLOG_DEBUG( "CIRNmsLogDb::NewLC - Exiting." );
       
    67 	return self;
       
    68 	}
       
    69  
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // CIRNmsLogDb::~CIRNmsLogDb()
       
    73 // default destructor
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 CIRNmsLogDb::~CIRNmsLogDb()
       
    77 	{
       
    78 	IRLOG_DEBUG( "CIRNmsLogDb::~CIRNmsLogDb" );
       
    79 	CloseDb();
       
    80 	iFsSession.Close();
       
    81 	IRLOG_DEBUG( "CIRNmsLogDb::~CIRNmsLogDb - Exiting." );
       
    82 	}
       
    83 
       
    84 // ---------------------------------------------------------------------------
       
    85 // CIRNmsLogDb::CreateDbConditional()
       
    86 // Creates both the dbms files conditionally(only if not yet created)
       
    87 // calls CreateDbL()
       
    88 // ---------------------------------------------------------------------------
       
    89 //
       
    90  TInt CIRNmsLogDb::CreateDbConditional(TFileName &aNmsLog)
       
    91 	{
       
    92 	IRLOG_DEBUG( "CIRNmsLogDb::CreateDbConditional" );
       
    93 	iDbFile.Copy(aNmsLog);
       
    94 	if( !BaflUtils::FileExists(iFsSession, iDbFile) )
       
    95 		{
       
    96 		TRAPD(error,CreateDbL(iDbFile));
       
    97 		if( error )
       
    98 			{
       
    99 			IRLOG_DEBUG( "CIRNmsLogDb::CreateDbConditional - Exiting (1)." );
       
   100 			return error;
       
   101 			}	
       
   102 		}
       
   103 	IRLOG_DEBUG( "CIRNmsLogDb::CreateDbConditional - Exiting (2)." );
       
   104 	return KErrNone;
       
   105 	}
       
   106 
       
   107 // ---------------------------------------------------------------------------
       
   108 // CIRNmsLogDb:AddNmsLogStartL()
       
   109 // adds the NmsLog log entry into data base
       
   110 // ---------------------------------------------------------------------------
       
   111 //
       
   112 void CIRNmsLogDb::AddNmsLogStartL(CIRNmsLogger& aNmsLog)
       
   113 	{
       
   114 	IRLOG_DEBUG( "CIRNmsLogDb::AddNmsLogStartL" );
       
   115 	OpenDbL();
       
   116 	RDbTable nmsLogtable;
       
   117 	TInt error=nmsLogtable.Open(iNmsLogDb,KNmsLogTable,nmsLogtable.
       
   118 		EUpdatable);
       
   119 	CleanupClosePushL(nmsLogtable);
       
   120 	if( error )
       
   121 		{
       
   122 		CloseDb();
       
   123 		User::LeaveIfError(error);	
       
   124 		}
       
   125 	
       
   126 	//! arrange the presets in incresing order of index
       
   127 	nmsLogtable.SetIndex(KNmsLogIndex);
       
   128 	nmsLogtable.Reset();
       
   129 
       
   130 	//if NmsLog log is greater or equal to than 5
       
   131 	if( nmsLogtable.CountL() >= KMaxNoNmsLog )
       
   132 		{
       
   133 		//first row is selected
       
   134 		nmsLogtable.FirstL();
       
   135 		//the current row is selected
       
   136 		nmsLogtable.GetL();
       
   137 		//delete that entry
       
   138 		nmsLogtable.DeleteL();
       
   139 		}	
       
   140     CleanupStack::PopAndDestroy(&nmsLogtable);
       
   141 	//Algorithm : else condition need not handle seperatly
       
   142 	//Algorithm : add NmsLogid and informations like
       
   143 	//starttime,connectedfrom,NmsLogid,connectiontype,channelid
       
   144 	//currentnetwork,homenetwork,NmsLogtable 
       
   145 	//Algorithm: if no. of NmsLog is greater than 5
       
   146 
       
   147 	_LIT(query,"SELECT * FROM %S");	
       
   148     HBufC* sqlStr=HBufC::NewLC(query().Length() + KNmsLogTable().Length());
       
   149 	sqlStr->Des().Format(query,&KNmsLogTable);
       
   150 	
       
   151 	// Create a view on the database
       
   152     RDbView view;     
       
   153     error = view.Prepare(iNmsLogDb,*sqlStr);
       
   154     if( error )
       
   155 		{
       
   156 		CloseDb();
       
   157 		User::LeaveIfError(error);	
       
   158 		}
       
   159     CleanupStack::PopAndDestroy(sqlStr);     
       
   160     CleanupClosePushL(view);
       
   161     error = view.EvaluateAll();
       
   162     if( error )
       
   163 		{
       
   164 		CloseDb();
       
   165 		User::LeaveIfError(error);	
       
   166 		}
       
   167     CDbColSet* columns = view.ColSetL();
       
   168     CleanupStack::PushL(columns);
       
   169     
       
   170     RDbColWriteStream writeStream;
       
   171 	TRAP(error,//trap start
       
   172 	   // Insert a row. Column order matches sql select statement
       
   173 	    view.InsertL();
       
   174 		//get index
       
   175 		view.SetColL(columns->ColNo(KID), aNmsLog.NmsLogId());	
       
   176 		//!open stream
       
   177 		writeStream.OpenLC(view,columns->ColNo(KNmsLogCol));
       
   178 		aNmsLog.ExternalizeL(writeStream);
       
   179 		writeStream.CommitL();
       
   180 		CleanupStack::PopAndDestroy(&writeStream);
       
   181  		);
       
   182  		
       
   183  	CleanupStack::PopAndDestroy(columns);
       
   184  	if( error!=KErrNone )
       
   185 	 	{
       
   186 	 	CloseDb();
       
   187 	 	User::LeaveIfError(error);
       
   188 	 	}
       
   189 	view.PutL();
       
   190  	CleanupStack::PopAndDestroy(&view);
       
   191  	CloseDb();
       
   192  	IRLOG_DEBUG( "CIRNmsLogDb::AddNmsLogStartL - Exiting." );
       
   193 	}
       
   194 
       
   195 // ---------------------------------------------------------------------------
       
   196 // CIRNmsLogDb::GetAllNmsLogL()
       
   197 // gets all the preset into an array 
       
   198 // ---------------------------------------------------------------------------
       
   199 //
       
   200 void CIRNmsLogDb::GetAllNmsLogL(
       
   201 	CArrayPtrFlat<CIRNmsLogger>& aNmsLogDataList)
       
   202 	{
       
   203 	IRLOG_DEBUG( "CIRNmsLogDb::GetAllNmsLogL" );
       
   204 	OpenDbL();
       
   205 	//not sure about this resetanddestroy
       
   206 	 //! Open for preset master
       
   207 	aNmsLogDataList.ResetAndDestroy();	    
       
   208 	//! temp item for holding the retrived data
       
   209 	CIRNmsLogger *item;
       
   210 	RDbColReadStream instream;    
       
   211 	RDbTable table;
       
   212 	TInt error = table.Open(iNmsLogDb, KNmsLogTable, table.EReadOnly);
       
   213 	CleanupClosePushL(table);
       
   214 	if( error!=KErrNone )
       
   215 	 	{
       
   216 	 	//if open fails function leaves
       
   217 	 	CloseDb();
       
   218 	 	User::LeaveIfError(error);
       
   219 	 	}
       
   220 
       
   221 	CDbColSet* colSet = table.ColSetL();
       
   222 	CleanupStack::PushL(colSet);
       
   223 
       
   224 	//! arrange the presets in incresing order of index
       
   225 	table.SetIndex(KNmsLogIndex);
       
   226 	table.Reset();
       
   227 	//! recursively retrive the preset data from the master table
       
   228 	for (table.FirstL(); table.AtRow(); table.NextL())
       
   229 	    {
       
   230 	    item=CIRNmsLogger::NewL();
       
   231 	    CleanupStack::PushL(item);
       
   232 	    table.GetL();
       
   233 	    instream.OpenLC( table, colSet->ColNo( KNmsLogCol ) );
       
   234 		item->InternalizeL(instream);
       
   235 		//update NmsLogid
       
   236 	    aNmsLogDataList.AppendL(item);
       
   237 	    CleanupStack::PopAndDestroy(&instream);
       
   238 	    CleanupStack::Pop(item);
       
   239 	    }
       
   240 	    
       
   241 	CleanupStack::PopAndDestroy(colSet);
       
   242 	//!close the master table
       
   243 	CleanupStack::PopAndDestroy(&table);
       
   244 	CloseDb();
       
   245 	IRLOG_DEBUG( "CIRNmsLogDb::GetAllNmsLogL - Exiting." );
       
   246 	}
       
   247 
       
   248 // ---------------------------------------------------------------------------
       
   249 // Function : DeleteAllNmsLog
       
   250 // delete all the NmsLog from NmsLog log entry
       
   251 // ---------------------------------------------------------------------------
       
   252 //
       
   253 void CIRNmsLogDb::DeleteAllNmsLogL()
       
   254 	{ 
       
   255 	IRLOG_DEBUG( "CIRNmsLogDb::DeleteAllNmsLogL" ); 
       
   256 	//opening a data base 
       
   257 	OpenDbL();
       
   258 	RDbTable nmsLogtable;
       
   259 	//data base table opened
       
   260     TInt error=nmsLogtable.Open(iNmsLogDb,KNmsLogTable,nmsLogtable.
       
   261     	EUpdatable);
       
   262     CleanupClosePushL(nmsLogtable);
       
   263     if( error!=KErrNone )
       
   264 	 	{
       
   265 	 	//if error we leave
       
   266 	 	CloseDb();
       
   267 	 	User::LeaveIfError(error);
       
   268 	 	}
       
   269 	//data base begin 	
       
   270     error = iNmsLogDb.Begin();
       
   271     if( error!=KErrNone )
       
   272 	 	{
       
   273 	 	//if open fails function leaves
       
   274 	 	CloseDb();
       
   275 	 	User::LeaveIfError(error);
       
   276 	 	}
       
   277 
       
   278     //! arrange the presets in incresing order of index
       
   279     nmsLogtable.SetIndex(KNmsLogIndex);
       
   280     nmsLogtable.Reset();
       
   281     nmsLogtable.FirstL();
       
   282     while(nmsLogtable.AtRow())
       
   283 	    {
       
   284 	    //deleting all the rows in the table
       
   285 	    nmsLogtable.GetL();
       
   286 	    nmsLogtable.DeleteL();
       
   287 		nmsLogtable.FirstL();
       
   288 	    }
       
   289 	//saving the change
       
   290 	CleanupStack::PopAndDestroy(&nmsLogtable);
       
   291     iNmsLogDb.Commit();
       
   292     CloseDb();	
       
   293     IRLOG_DEBUG( "CIRNmsLogDb::DeleteAllNmsLogL - Exiting." );
       
   294 	}   
       
   295 
       
   296 // ---------------------------------------------------------------------------
       
   297 // CIRNmsLogDb::ConstructL()
       
   298 // Standard 2nd phase construction
       
   299 // ---------------------------------------------------------------------------
       
   300 //
       
   301 void CIRNmsLogDb::ConstructL()
       
   302 	{
       
   303 	IRLOG_DEBUG( "CIRNmsLogDb::ConstructL" );
       
   304 	User::LeaveIfError(iFsSession.Connect());
       
   305 	IRLOG_DEBUG( "CIRNmsLogDb::ConstructL- Exiting." );	
       
   306 	}
       
   307 
       
   308 // ---------------------------------------------------------------------------
       
   309 // CIRNmsLogDb::CloseDb()
       
   310 // Closes the database 
       
   311 // ---------------------------------------------------------------------------
       
   312 //
       
   313 void CIRNmsLogDb::CloseDb()
       
   314 	{
       
   315 	IRLOG_DEBUG( "CIRNmsLogDb::CloseDb" );
       
   316 	iNmsLogDb.Close();
       
   317 	IRLOG_DEBUG( "CIRNmsLogDb::CloseDb - Exiting." );
       
   318 	}
       
   319 
       
   320 
       
   321 // ---------------------------------------------------------------------------
       
   322 // Function : CreateNmsLogTableL
       
   323 // creates NmsLogtable with two column one is NmsLogid and rest of NmsLog
       
   324 // log data
       
   325 // ---------------------------------------------------------------------------
       
   326 //NmsLogTable
       
   327 //---------------------------
       
   328 //| KID   |	 KNmsLogCol |
       
   329 //---------------------------
       
   330 //|TInt32 | EDbColLongText8 |
       
   331 //---------------------------
       
   332 //
       
   333 void CIRNmsLogDb::CreateNmsLogTableL()
       
   334 	{
       
   335 	IRLOG_DEBUG( "CIRNmsLogDb::CreateNmsLogTableL" );
       
   336 	//start time of NmsLog
       
   337 	TDbCol nmsLogid(KID, EDbColInt32);
       
   338 	nmsLogid.iAttributes = TDbCol::ENotNull; 
       
   339 	
       
   340     //!this column is used to store preset data
       
   341     //!The column stores a potentially large amount of non-Unicode text data.
       
   342     TDbCol nmsLogcol(KNmsLogCol, EDbColLongText8);
       
   343    	nmsLogcol.iAttributes = TDbCol::ENotNull; 
       
   344    	
       
   345 	CDbColSet* nmsLogcolset = CDbColSet::NewLC();
       
   346 	nmsLogcolset->AddL(nmsLogid);
       
   347 	nmsLogcolset->AddL(nmsLogcol);
       
   348 
       
   349 	// Create the NmsLoglog table with two columns
       
   350 	
       
   351 	User::LeaveIfError(iNmsLogDb.CreateTable(KNmsLogTable,
       
   352 		 *nmsLogcolset));
       
   353 	
       
   354 	//Create the KeyIndex for the table
       
   355 	CreateNmsLogIndexL();
       
   356 	CleanupStack::PopAndDestroy(nmsLogcolset);
       
   357 	IRLOG_DEBUG( "CIRNmsLogDb::CreateNmsLogTableL - Exiting." );	
       
   358 	}
       
   359 
       
   360 // ---------------------------------------------------------------------------
       
   361 // Function : CreateNmsLogIndexL
       
   362 // sets NmsLogid as the primary key
       
   363 // ---------------------------------------------------------------------------
       
   364 //
       
   365 void CIRNmsLogDb::CreateNmsLogIndexL()
       
   366 	{
       
   367 	IRLOG_DEBUG( "CIRNmsLogDb::CreateNmsLogIndexL" );
       
   368 	TDbKeyCol nmsLogid(KID);
       
   369 	CDbKey* index = CDbKey::NewLC();
       
   370 	index->AddL(nmsLogid);
       
   371 	User::LeaveIfError(iNmsLogDb.CreateIndex( KNmsLogIndex, KNmsLogTable,
       
   372 		*index));
       
   373 	CleanupStack::PopAndDestroy(index);
       
   374 	IRLOG_DEBUG( "CIRNmsLogDb::CreateNmsLogIndexL - Exiting." );
       
   375 	}
       
   376 	
       
   377 // ---------------------------------------------------------------------------
       
   378 // CIRNmsLogDb::OpenDbL()
       
   379 // opening the data base 
       
   380 // ---------------------------------------------------------------------------
       
   381 //
       
   382  void CIRNmsLogDb::OpenDbL()
       
   383 	{
       
   384 	IRLOG_DEBUG( "CIRNmsLogDb::OpenDbL" );
       
   385 	CloseDb();
       
   386 	TInt error = KErrNone;
       
   387 	if( !BaflUtils::FileExists(iFsSession, iDbFile) )
       
   388 	    {
       
   389 	    //if file doesn't exist function leaves with error code
       
   390 	    //KErrNotFound
       
   391 	    error = KErrNotFound;
       
   392 	    User::LeaveIfError(error);
       
   393 	    }
       
   394 
       
   395 	error = iNmsLogDb.Open(iFsSession,iDbFile);	
       
   396 	if( error!=KErrNone )
       
   397 		{
       
   398 		//if database is failed to open then 
       
   399 		//function leaves
       
   400 		IRLOG_ERROR2( "CIRNmsLogDb::OpenDbL - Opening NmsLog database failed (%d)", error );
       
   401 		User::LeaveIfError(error);
       
   402 		}
       
   403 	if( iNmsLogDb.IsDamaged() || !iNmsLogDb.InTransaction() )
       
   404 		{
       
   405 		//if data base is damaged then 
       
   406 		//it tried to recover
       
   407 		//if recovery is not possible function leaves
       
   408 		error = iNmsLogDb.Recover();
       
   409 		if ( KErrNone == error)
       
   410 			{
       
   411 			//if recovered data base is compacted
       
   412 			error = iNmsLogDb.Compact();	
       
   413 			}
       
   414 	    User::LeaveIfError(error);	
       
   415 		}
       
   416 	IRLOG_DEBUG( "CIRNmsLogDb::OpenDbL - Exiting." );
       
   417 	}
       
   418 	
       
   419 
       
   420 // ---------------------------------------------------------------------------
       
   421 // CIRNmsLogDb::CreateDbL()
       
   422 // Creates both the dbms files 
       
   423 // calls CreateFavMasterTableL(),CreateFavUrlTableL
       
   424 // database filename
       
   425 // ---------------------------------------------------------------------------
       
   426 //
       
   427  void CIRNmsLogDb::CreateDbL(TFileName& aNmsLog)
       
   428 	{
       
   429 	IRLOG_DEBUG( "CIRNmsLogDb::CreateDbL" );
       
   430 	CloseDb();
       
   431 	TInt error = iNmsLogDb.Replace(iFsSession,aNmsLog);
       
   432 	if ( error != KErrNone )
       
   433 	    {
       
   434 	    IRLOG_ERROR2( "CIRNmsLogDb::CreateDbL - Creating NmsLog database failed (%d)", error );
       
   435 	    }
       
   436 	User::LeaveIfError(error);
       
   437 	CreateNmsLogTableL();
       
   438 	IRLOG_DEBUG( "CIRNmsLogDb::CreateDbL - Exiting." );
       
   439 	}
       
   440 
       
   441 
       
   442 
       
   443 
       
   444 
       
   445 
       
   446 
       
   447 
       
   448 
       
   449