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