applicationmanagement/omascomoadapter/src/omascomoadapterdb.cpp
changeset 0 3ce708148e4d
child 62 03849bd79877
equal deleted inserted replaced
-1:000000000000 0:3ce708148e4d
       
     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:  Implementation of applicationmanagement components
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <sysutil.h>
       
    21 #include <bautils.h>
       
    22 
       
    23 #include "omascomoadapterdb.h"
       
    24 #include "debug.h"
       
    25 
       
    26 // ============================ MEMBER FUNCTIONS =============================
       
    27 
       
    28 // ---------------------------------------------------------------------------
       
    29 //  CSCOMOAdapterDb::NewL()
       
    30 //  Creates a new instance of CSCOMOAdapterDb object.
       
    31 // ---------------------------------------------------------------------------
       
    32 // 
       
    33 CSCOMOAdapterDb* CSCOMOAdapterDb::NewL()
       
    34     {
       
    35     RDEBUG("CSCOMOAdapterDb::NewL(): begin");
       
    36 
       
    37     CSCOMOAdapterDb* self = CSCOMOAdapterDb::NewLC();
       
    38     CleanupStack::Pop(self);
       
    39 
       
    40     RDEBUG("CSCOMOAdapterDb::NewL(): end");
       
    41 
       
    42     return self;
       
    43     }
       
    44 
       
    45 // ---------------------------------------------------------------------------
       
    46 //  CSCOMOAdapterDb::NewLC()
       
    47 //  Creates a new instance of CSCOMOAdapterDb object. 
       
    48 //  Pushes and leaves new instance onto CleanupStack.
       
    49 // ---------------------------------------------------------------------------
       
    50 // 
       
    51 CSCOMOAdapterDb* CSCOMOAdapterDb::NewLC()
       
    52     {
       
    53     RDEBUG("CSCOMOAdapterDb::NewLC(): begin");
       
    54 
       
    55     CSCOMOAdapterDb* self = new( ELeave ) CSCOMOAdapterDb();
       
    56     CleanupStack::PushL(self);
       
    57     self->ConstructL();
       
    58 
       
    59     RDEBUG("CSCOMOAdapterDb::NewLC(): end");
       
    60 
       
    61     return self;
       
    62     }
       
    63 
       
    64 // ---------------------------------------------------------------------------
       
    65 //  CSCOMOAdapterDb::ConstructL()
       
    66 //  Second phase constructor.
       
    67 // ---------------------------------------------------------------------------
       
    68 // 
       
    69 void CSCOMOAdapterDb::ConstructL()
       
    70     {
       
    71     RDEBUG("CSCOMOAdapterDb::ConstructL(): begin");
       
    72 
       
    73     TParse name;
       
    74     TInt err;
       
    75 
       
    76     User::LeaveIfError(iFsSession.Connect());
       
    77     User::LeaveIfError(iRdbSession.Connect() );
       
    78 
       
    79     iFsSession.SetSessionToPrivate(EDriveC);
       
    80 
       
    81 #ifdef SYMBIAN_SECURE_DBMS
       
    82     name.Set(KAMAdapterDbName, NULL, NULL);
       
    83     err = iDatabase.Open(iRdbSession, name.FullName(), KDBMSSecureID);
       
    84 #else
       
    85     name.Set( KAMAdapterDbName, KNonSecureDbFullName, NULL );
       
    86     err = iDatabase.Open(iRdbSession, DBFileName);
       
    87 #endif
       
    88 
       
    89     if (err == KErrNotFound)
       
    90         {
       
    91         CreateandOpenL(name);
       
    92         }
       
    93     else
       
    94         {
       
    95 
       
    96         if ( (err == KErrEof ) || (err == KErrCorrupt ) || (err
       
    97                 == KErrArgument ))
       
    98             {
       
    99 
       
   100             iRdbSession.DeleteDatabase(name.FullName(), KAMPolicyUID);
       
   101             CreateandOpenL(name);
       
   102             }
       
   103 
       
   104         }
       
   105 
       
   106     RDEBUG("CSCOMOAdapterDb::ConstructL(): end");
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // CWPInternetAPDB::CreateandOpenL
       
   111 // -----------------------------------------------------------------------------
       
   112 //    
       
   113 void CSCOMOAdapterDb::CreateandOpenL(TParse& name)
       
   114     {
       
   115 
       
   116     TInt err;
       
   117 
       
   118 #ifdef SYMBIAN_SECURE_DBMS
       
   119     iDatabase.Create(iRdbSession, name.FullName(), KDBMSSecureID);
       
   120 #else
       
   121     if( SysUtil::FFSSpaceBelowCriticalLevelL( &iFsSession, KEmptyDbSizeEstimate ) )
       
   122         {
       
   123         User::Leave( KErrDiskFull );
       
   124         }
       
   125     iDatabase.Create(iFsSession, name.FullName());
       
   126 #endif	
       
   127 
       
   128     CreateTableL(iDatabase);
       
   129     iDatabase.Close();
       
   130 #ifdef SYMBIAN_SECURE_DBMS
       
   131 
       
   132     err = iDatabase.Open(iRdbSession, name.FullName(), KDBMSSecureID);
       
   133 
       
   134 #else
       
   135     err = iDatabase.Open(iFsSession, DBFileName);
       
   136 #endif
       
   137 
       
   138     //Debug
       
   139     if (err != KErrNone)
       
   140         {
       
   141 
       
   142         User::LeaveIfError(err);
       
   143         }
       
   144 
       
   145     }
       
   146 
       
   147 // -----------------------------------------------------------------------------
       
   148 // CWPInternetAPDB::CreateTableL
       
   149 // -----------------------------------------------------------------------------
       
   150 //  
       
   151 void CSCOMOAdapterDb::CreateTableL(RDbDatabase& aDatabase)
       
   152     {
       
   153 
       
   154     // Create a table definition
       
   155     CDbColSet* columns=CDbColSet::NewLC();
       
   156 
       
   157     // Add Columns
       
   158     TDbCol id(NCol1, EDbColInt32);
       
   159 
       
   160     // automatic indexing for items,it is our key field.
       
   161     id.iAttributes=id.EAutoIncrement;
       
   162     columns->AddL(id);
       
   163     columns->AddL(TDbCol(NCol2, EDbColInt32));
       
   164     columns->AddL(TDbCol(NCol3, EDbColInt32));
       
   165     columns->AddL(TDbCol(NCol4, EDbColText8, 255));
       
   166     columns->AddL(TDbCol(NCol5, EDbColText8, 255));
       
   167     columns->AddL(TDbCol(NCol6, EDbColText8, 255));
       
   168     columns->AddL(TDbCol(NCol7, EDbColInt32));
       
   169 
       
   170     //possibility of Panic 
       
   171 
       
   172     // Create a table
       
   173     TRAPD(err, aDatabase.CreateTable(KTableAMMgmtObject, *columns) );
       
   174 
       
   175     if(err!=KErrNone)
       
   176     User::Leave(err);
       
   177 
       
   178     // cleanup the column set
       
   179     CleanupStack::PopAndDestroy(columns);
       
   180 
       
   181     }
       
   182 
       
   183 // ---------------------------------------------------------------------------
       
   184 //  CSCOMOAdapterDb::CSCOMOAdapterDb()
       
   185 //  Constructor.
       
   186 // ---------------------------------------------------------------------------
       
   187 // 
       
   188 CSCOMOAdapterDb::CSCOMOAdapterDb()
       
   189     {
       
   190     RDEBUG("CSCOMOAdapterDb::CSCOMOAdapterDb(): begin");
       
   191     RDEBUG("CSCOMOAdapterDb::CSCOMOAdapterDb(): end");
       
   192     }
       
   193 
       
   194 // ---------------------------------------------------------------------------
       
   195 //  CSCOMOAdapterDb::~CSCOMOAdapterDb()
       
   196 //  Destructor.
       
   197 // ---------------------------------------------------------------------------
       
   198 // 
       
   199 CSCOMOAdapterDb::~CSCOMOAdapterDb()
       
   200     {
       
   201     RDEBUG("CSCOMOAdapterDb::~CSCOMOAdapterDb(): begin");
       
   202 
       
   203     iView.Close();
       
   204 
       
   205     delete iColSet;
       
   206     iAMObjectTable.Close();
       
   207 
       
   208     iDatabase.Close();
       
   209     iFsSession.Close();
       
   210     iRdbSession.Close();
       
   211 
       
   212     RDEBUG("CSCOMOAdapterDb::~CSCOMOAdapterDb(): ends");
       
   213     }
       
   214 
       
   215 CAMDbItem::~CAMDbItem()
       
   216     {
       
   217     delete iServerID;
       
   218     delete iCorrelator;
       
   219     delete iSourceURI;
       
   220     }
       
   221 
       
   222 void CSCOMOAdapterDb::GetEntryForServerIDL(RPointerArray<CAMDbItem>& aItemArray,
       
   223         TDesC8& aValue)
       
   224     {
       
   225 
       
   226     aItemArray.Reset();// first reset the array
       
   227 
       
   228     _LIT(KEqualToString,"='%S'");
       
   229     _LIT(KQuery, "SELECT * FROM %S WHERE %S");
       
   230 
       
   231     TBuf<256> serverid;
       
   232     serverid.Copy(aValue);
       
   233 
       
   234     TBuf<100> bufsql;
       
   235     bufsql.Append(KQuery);
       
   236     bufsql.Append(KEqualToString);
       
   237     TBuf<100> bufsql1;
       
   238     bufsql1.Format(bufsql, &KTableAMMgmtObject, &NCol4, &serverid);
       
   239 
       
   240     ReadItemsL(aItemArray, bufsql1);
       
   241 
       
   242     }
       
   243 
       
   244 
       
   245 
       
   246 // -----------------------------------------------------------------------------
       
   247 // CWPInternetAPDB::ReadItemsL
       
   248 // -----------------------------------------------------------------------------
       
   249 // 
       
   250 void CSCOMOAdapterDb::ReadItemsL(RPointerArray<CAMDbItem>& aItemArray,
       
   251         TDesC& aSqlQuery)
       
   252     {
       
   253 
       
   254     RDbView view;
       
   255     view.Prepare(iDatabase, TDbQuery(aSqlQuery));
       
   256     CleanupClosePushL(view);
       
   257     view.EvaluateAll();
       
   258     view.FirstL();
       
   259 
       
   260     while (view.AtRow())
       
   261         {
       
   262         view.GetL();
       
   263 
       
   264         CAMDbItem *dbitem = new(ELeave) CAMDbItem;
       
   265         dbitem->id = view.ColInt(1);
       
   266         dbitem->iLUID = view.ColInt(2);
       
   267         dbitem->iProfileId = view.ColInt(3);
       
   268 
       
   269         dbitem->iServerID = (view.ColDes8(4)).AllocL();
       
   270         dbitem->iCorrelator = (view.ColDes8(5)).AllocL();
       
   271         dbitem->iSourceURI = (view.ColDes8(6)).AllocL();
       
   272 
       
   273         dbitem->iRetryCount = view.ColInt(7);
       
   274 
       
   275         aItemArray.Append(dbitem);
       
   276         view.NextL();
       
   277         }
       
   278 
       
   279     CleanupStack::PopAndDestroy(); // view
       
   280 
       
   281     }
       
   282 
       
   283 // -----------------------------------------------------------------------------
       
   284 // CWPInternetAPDB::SaveToDatabaseL
       
   285 // -----------------------------------------------------------------------------
       
   286 //   
       
   287 void CSCOMOAdapterDb::SaveToDatabaseL(TInt& aIndex, TUint32 aLuid,
       
   288         TInt aProfileid, TInt aRetryCount, const TDesC8& aSourceURI,
       
   289         const TDesC8& aServerid, const TDesC8& aCorrelator)
       
   290     {
       
   291 
       
   292     BeginDatabaseTransaction();
       
   293 
       
   294     TFileName sqlQuery;
       
   295     sqlQuery.Copy(_L("SELECT * FROM "));
       
   296     sqlQuery.Append(KTableAMMgmtObject);
       
   297 
       
   298     RDbView view;
       
   299     view.Prepare(iDatabase, TDbQuery(sqlQuery));
       
   300     CleanupClosePushL(view);
       
   301 
       
   302     view.InsertL();
       
   303 
       
   304     view.SetColL(2, aLuid);
       
   305     view.SetColL(3, aProfileid);
       
   306     view.SetColL(4, aServerid);
       
   307     view.SetColL(5, aCorrelator);
       
   308     view.SetColL(6, aSourceURI);
       
   309     view.SetColL(7, aRetryCount);
       
   310 
       
   311     view.PutL();
       
   312 
       
   313     aIndex = view.ColInt(1);// autoincrement gives us unique index.
       
   314 
       
   315     CleanupStack::PopAndDestroy(1); // view
       
   316     
       
   317     CommitDatabaseTransaction();
       
   318 
       
   319     }
       
   320 
       
   321 // -----------------------------------------------------------------------------
       
   322 // CWPInternetAPDB::DeleteFromDatabaseL
       
   323 // -----------------------------------------------------------------------------
       
   324 //
       
   325 TBool CSCOMOAdapterDb::DeleteFromDatabaseL(TUint32 aLuid)
       
   326     {
       
   327 
       
   328     TBool rowsdeleted = EFalse;
       
   329     TFileName sqlQuery;
       
   330 	
       
   331 	PrepareLuidQuery(aLuid, sqlQuery);
       
   332 	
       
   333 
       
   334     BeginDatabaseTransaction();
       
   335 
       
   336     RDbView view;
       
   337     // query buffer with index finds only the selected item row.
       
   338     view.Prepare(iDatabase, TDbQuery(sqlQuery));
       
   339     CleanupClosePushL(view);
       
   340 
       
   341     view.EvaluateAll();
       
   342     view.FirstL();
       
   343 
       
   344     if (!view.IsEmptyL())
       
   345         {
       
   346         // we have autoincrement in index so it should be unique
       
   347         // but just to make sure, we use 'while', instead of 'if'
       
   348         while (view.AtRow())
       
   349             {
       
   350             view.GetL();
       
   351             view.DeleteL();
       
   352             view.NextL();
       
   353             }
       
   354 
       
   355         
       
   356         CommitDatabaseTransaction();
       
   357         // compacts the databse, by physically removig deleted data.
       
   358         iDatabase.Compact();
       
   359         rowsdeleted = ETrue;
       
   360         }
       
   361     CleanupStack::PopAndDestroy(1); // view
       
   362 
       
   363 
       
   364     return rowsdeleted;
       
   365     }
       
   366 
       
   367 void CSCOMOAdapterDb::UpdateRetryCountForLuidL(TInt aLuid, TInt aRetryCount)
       
   368 {
       
   369 	TFileName sqlQuery;
       
   370 	
       
   371 	PrepareLuidQuery(aLuid, sqlQuery);
       
   372 	
       
   373 	BeginDatabaseTransaction();
       
   374 	
       
   375 	RDbView view;
       
   376 	view.Prepare(iDatabase, TDbQuery(sqlQuery));
       
   377 	CleanupClosePushL(view);
       
   378 	
       
   379 	view.EvaluateAll();
       
   380 	view.FirstL();
       
   381 	
       
   382 	if(view.AtRow())            
       
   383 	{			
       
   384 		view.UpdateL();
       
   385 		view.SetColL(7, aRetryCount);		
       
   386 		view.PutL();
       
   387 	}
       
   388 			
       
   389 	CleanupStack::PopAndDestroy(1); // view
       
   390 	
       
   391     CommitDatabaseTransaction();
       
   392 }
       
   393 
       
   394 TInt CSCOMOAdapterDb::GetRetryCountForLuidL(TInt aLuid)
       
   395 {
       
   396     TInt retrycount = KErrNone;
       
   397 	TFileName sqlQuery;
       
   398     
       
   399     PrepareLuidQuery(aLuid, sqlQuery);
       
   400     
       
   401     BeginDatabaseTransaction();
       
   402     
       
   403     RDbView view;
       
   404     view.Prepare(iDatabase, TDbQuery(sqlQuery));
       
   405     CleanupClosePushL(view);
       
   406     view.EvaluateAll();
       
   407     view.FirstL();
       
   408 
       
   409     if (!view.IsEmptyL())
       
   410     {
       
   411     
       
   412     if(view.AtRow())            
       
   413 	{
       
   414     view.GetL();
       
   415 	}
       
   416 	
       
   417     retrycount = view.ColInt(7);
       
   418 	
       
   419     }
       
   420 
       
   421 	CleanupStack::PopAndDestroy(1); // view
       
   422 	
       
   423 	CommitDatabaseTransaction();
       
   424 
       
   425 	return retrycount;    
       
   426     
       
   427 }
       
   428 
       
   429 void CSCOMOAdapterDb::PrepareLuidQuery(TInt aLuid, TDes& aSqlQuery)
       
   430 {
       
   431 	aSqlQuery.Copy(_L("SELECT * FROM "));
       
   432     aSqlQuery.Append(KTableAMMgmtObject);
       
   433     aSqlQuery.Append(_L(" WHERE "));
       
   434     aSqlQuery.Append(NCol2);
       
   435     aSqlQuery.Append(_L(" = "));
       
   436     aSqlQuery.AppendNum(aLuid);
       
   437 }
       
   438 
       
   439 void CSCOMOAdapterDb::BeginDatabaseTransaction()
       
   440 {
       
   441 	iDatabase.Begin();
       
   442 }
       
   443 
       
   444 void CSCOMOAdapterDb::CommitDatabaseTransaction()
       
   445 {
       
   446 	iDatabase.Commit();
       
   447 }