javaextensions/iapinfo/src.s60/javacommdb.cpp
branchRCL_3
changeset 14 04becd199f91
child 25 ae942d28ec0e
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     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:  JavaCommDB class implementation
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include "javacommdb.h"
       
    20 #include "jstringutils.h"
       
    21 #include "s60commonutils.h"
       
    22 #include <e32base.h>
       
    23 #include <e32debug.h>
       
    24 #include "logger.h"
       
    25 #include "javajniutils.h"
       
    26 
       
    27 using namespace CommsDat;
       
    28 using namespace java::util;
       
    29 
       
    30 // CONSTANTS
       
    31 _LIT(KIAPTable, "IAP");
       
    32 _LIT(KConnectionPreferencesTable, "ConnectionPreferences");
       
    33 _LIT(KGlobalSettingsTable, "GlobalSettings");
       
    34 //_LIT( KNetworkTable, "Network" );
       
    35 _LIT(KFieldNameRecordId, "RecordId");
       
    36 _LIT(KFieldNameName, "Name");
       
    37 
       
    38 JNIEXPORT void JNICALL Java_com_nokia_mid_iapinfo_CommsTable__1destroy(JNIEnv* ,
       
    39         jclass,
       
    40         jint aHandle)
       
    41 {
       
    42     LOG1(ESOCKET, EInfo, "CJAVACOMMDB + destroy() handle: %D", aHandle);
       
    43     JavaCommDB* commdb = reinterpret_cast<JavaCommDB *>(aHandle);
       
    44     delete commdb;
       
    45     LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB - destroy()");
       
    46 }
       
    47 
       
    48 JNIEXPORT jint JNICALL Java_com_nokia_mid_iapinfo_CommsTable__1construct(JNIEnv* ,
       
    49         jclass)
       
    50 {
       
    51     LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB + construct()");
       
    52     JavaCommDB *cdb = new JavaCommDB();
       
    53     TInt handle = reinterpret_cast<jint>(cdb);
       
    54     LOG1(EJavaIapInfo, EInfo, "CJAVACOMMDB - construct() handle: %D", handle);
       
    55     return handle;
       
    56 }
       
    57 
       
    58 JNIEXPORT jint JNICALL Java_com_nokia_mid_iapinfo_CommsTable__1open(JNIEnv* aJni,
       
    59         jclass, jint aHandle,
       
    60         jstring aTableName)
       
    61 {
       
    62     LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB + open()");
       
    63     JavaCommDB* commdb = reinterpret_cast<JavaCommDB *>(aHandle);
       
    64     JStringUtils table(*aJni, aTableName);
       
    65 
       
    66     //LOG1(EJavaIapInfo,  EInfo,  "CJAVACOMMDB opening table: %S", table );
       
    67     TRAPD(error, commdb->OpenL(table));
       
    68     LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB - open()");
       
    69     return error;
       
    70 }
       
    71 
       
    72 JNIEXPORT void JNICALL Java_com_nokia_mid_iapinfo_CommsTable__1close(JNIEnv* ,
       
    73         jclass, jint aHandle)
       
    74 {
       
    75     LOG1(EJavaIapInfo, EInfo, "CJAVACOMMDB + close() handle: %D", aHandle);
       
    76     JavaCommDB* commdb = reinterpret_cast<JavaCommDB *>(aHandle);
       
    77     TRAP_IGNORE(commdb->Close());
       
    78     LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB - close()");
       
    79 }
       
    80 
       
    81 JNIEXPORT jint JNICALL Java_com_nokia_mid_iapinfo_CommsTable__1getRecordCount(JNIEnv* ,
       
    82         jclass,
       
    83         jint aHandle)
       
    84 {
       
    85     LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB + getRecordCount()");
       
    86     JavaCommDB* commdb = reinterpret_cast<JavaCommDB *>(aHandle);
       
    87     TInt count = 0;
       
    88     count = commdb->GetRecordCount();
       
    89     LOG1(EJavaIapInfo, EInfo, "CJAVACOMMDB returning value: %D", count);
       
    90     LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB - getRecordCount()");
       
    91     return count;
       
    92 }
       
    93 
       
    94 JNIEXPORT jint JNICALL Java_com_nokia_mid_iapinfo_CommsTable__1readIntFieldValue(JNIEnv* aJni,
       
    95         jclass,
       
    96         jint aHandle,
       
    97         jstring aFieldName,
       
    98         jintArray aError)
       
    99 {
       
   100     LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB + readIntFieldValue()");
       
   101     TInt value = 0;
       
   102 
       
   103     JavaCommDB* commdb = reinterpret_cast<JavaCommDB *>(aHandle);
       
   104     JStringUtils field(*aJni, aFieldName);
       
   105 
       
   106     //LOG1(EJavaIapInfo,  EInfo,  "CJAVACOMMDB  reading field value: %S", field );
       
   107     TRAPD(err, value = commdb->GetIntFieldValueL(field));
       
   108 
       
   109     // Put the native error code into the Java error array
       
   110     jint javaError[1] =
       
   111         {   err};
       
   112     aJni->SetIntArrayRegion(aError, 0, 1, javaError);
       
   113 
       
   114     LOG1(EJavaIapInfo, EInfo, "CJAVACOMMDB  returning value: %D", value);
       
   115     LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB - readIntFieldValue()");
       
   116     return value;
       
   117 }
       
   118 
       
   119 JNIEXPORT jstring JNICALL Java_com_nokia_mid_iapinfo_CommsTable__1readStringFieldValue(JNIEnv* aJni,
       
   120         jclass,
       
   121         jint aHandle,
       
   122         jstring aFieldName,
       
   123         jintArray aError)
       
   124 {
       
   125     LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB + readStringFieldValue()");
       
   126 
       
   127     HBufC* value = NULL;
       
   128     jstring str = NULL;
       
   129 
       
   130     JavaCommDB* commdb = reinterpret_cast<JavaCommDB *>(aHandle);
       
   131     JStringUtils field(*aJni, aFieldName);
       
   132 
       
   133     //LOG1(EJavaIapInfo,  EInfo,  "CJAVACOMMDB  reading field value: %S", field );
       
   134     TRAPD(err, value = commdb->GetStringFieldValueL(field));
       
   135     if (NULL != value && KErrNone == err)
       
   136     {
       
   137         str = S60CommonUtils::NativeToJavaString(*aJni, value->Des());
       
   138         //LOG1(EJavaIapInfo, EInfo, "CJAVACOMMDB  returning value: %S", value->Des() );
       
   139         delete value;
       
   140     }
       
   141 
       
   142     // Put the native error code into the Java error array
       
   143     jint javaError[1] =
       
   144         {   err};
       
   145     aJni->SetIntArrayRegion(aError, 0, 1, javaError);
       
   146 
       
   147     LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB - readStringFieldValue()");
       
   148     return str;
       
   149 }
       
   150 
       
   151 JNIEXPORT jint JNICALL Java_com_nokia_mid_iapinfo_CommsTable__1next(JNIEnv* ,
       
   152         jclass, jint aHandle)
       
   153 {
       
   154     LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB + next()");
       
   155     JavaCommDB* commdb = reinterpret_cast<JavaCommDB *>(aHandle);
       
   156     TInt rec = commdb->Next();
       
   157     LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB - next()");
       
   158     return rec;
       
   159 }
       
   160 
       
   161 JNIEXPORT jint JNICALL Java_com_nokia_mid_iapinfo_CommsTable__1previous(JNIEnv* ,
       
   162         jclass,
       
   163         jint /*aHandle*/)
       
   164 {
       
   165     return 0;
       
   166 }
       
   167 
       
   168 JNIEXPORT jint JNICALL Java_com_nokia_mid_iapinfo_CommsTable__1findById(JNIEnv* ,
       
   169         jclass,
       
   170         jint aHandle,
       
   171         jint aId)
       
   172 {
       
   173     LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB + findById()");
       
   174     TInt recId = 0;
       
   175 
       
   176     JavaCommDB* commdb = reinterpret_cast<JavaCommDB *>(aHandle);
       
   177     recId = commdb->FindById(aId);
       
   178     LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB - findById()");
       
   179     return recId;
       
   180 }
       
   181 
       
   182 JNIEXPORT jint JNICALL Java_com_nokia_mid_iapinfo_CommsTable__1findByName(JNIEnv* aJni,
       
   183         jclass,
       
   184         jint aHandle,
       
   185         jstring aName)
       
   186 {
       
   187     LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB + findByName()");
       
   188     TInt recId = 0;
       
   189     JStringUtils name(*aJni, aName);
       
   190 
       
   191     JavaCommDB* commdb = reinterpret_cast<JavaCommDB *>(aHandle);
       
   192     TRAP(recId, recId = commdb->FindByNameL(name));
       
   193     LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB - findByName()");
       
   194     return recId;
       
   195 }
       
   196 
       
   197 /* ---------------------------------------------------------------------------
       
   198  *    JavaCommDB class implementation ;
       
   199  * ---------------------------------------------------------------------------
       
   200  */
       
   201 
       
   202 // ---------------------------------------------------------------------------
       
   203 //    Delete allocated member objects.
       
   204 // -----------------------------------------------------------------------------
       
   205 JavaCommDB::~JavaCommDB()
       
   206 {
       
   207     LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB  + ~JavaCommDB()");
       
   208     Close();
       
   209     LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB  - ~JavaCommDB()");
       
   210 }
       
   211 
       
   212 // ---------------------------------------------------------------------------
       
   213 //    Delete allocated member objects.
       
   214 //    @return error code
       
   215 // -----------------------------------------------------------------------------
       
   216 TInt JavaCommDB::Close()
       
   217 {
       
   218     LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB + Close()");
       
   219     iCurrentRecord = 0;
       
   220     delete iTable;
       
   221     iTable = NULL;
       
   222     delete iDb;
       
   223     iDb = NULL;
       
   224 
       
   225     LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB - Close()");
       
   226     return KErrNone;
       
   227 }
       
   228 
       
   229 // ---------------------------------------------------------------------------
       
   230 //    Opens the given table.
       
   231 //    @param aTablename name of the CommDB table to be opened
       
   232 //    @return error code
       
   233 // -----------------------------------------------------------------------------
       
   234 void JavaCommDB::OpenL(const TDesC& aTableName)
       
   235 {
       
   236     //LOG1(EJavaIapInfo, EInfo, "CJAVACOMMDB + OpenL(): open table: %S",   aTableName);
       
   237     iDb = CMDBSession::NewL(KCDVersion1_1);
       
   238     iCurrentRecord = 0;
       
   239 
       
   240     if (0 == aTableName.Compare(KIAPTable))
       
   241     {
       
   242         // Create IAP table recordset
       
   243         iTable = (CMDBRecordSet<CCDRecordBase> *)(new(ELeave) CMDBRecordSet<
       
   244                  CCDIAPRecord> (KCDTIdIAPRecord));
       
   245         LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB  OpenL(): IAP table opened.");
       
   246     }
       
   247     else if (0 == aTableName.Compare(KGlobalSettingsTable))
       
   248     {
       
   249         iTable = (CMDBRecordSet<CCDRecordBase> *) new(ELeave) CMDBRecordSet<
       
   250                  CCDGlobalSettingsRecord> (KCDTIdGlobalSettingsRecord);
       
   251         LOG(EJavaIapInfo, EInfo,
       
   252             "CJAVACOMMDB  OpenL(): GlobalSettings table opened.");
       
   253     }
       
   254     else if (0 == aTableName.Compare(KConnectionPreferencesTable))
       
   255     {
       
   256         iTable = (CMDBRecordSet<CCDRecordBase> *) new(ELeave) CMDBRecordSet<
       
   257                  CCDConnectionPrefsRecord> (KCDTIdConnectionPrefsRecord);
       
   258         LOG(EJavaIapInfo, EInfo,
       
   259             "CJAVACOMMDB  OpenL(): ConnectionPreferences table opened.");
       
   260     }
       
   261     // Load the table
       
   262     iTable->LoadL(*iDb);
       
   263     LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB - OpenL()");
       
   264 }
       
   265 
       
   266 // ---------------------------------------------------------------------------
       
   267 //    Get number of records in the current table.
       
   268 //    @return record count
       
   269 // -----------------------------------------------------------------------------
       
   270 TInt JavaCommDB::GetRecordCount()
       
   271 {
       
   272     return iTable->iRecords.Count();
       
   273 }
       
   274 
       
   275 // ---------------------------------------------------------------------------
       
   276 //    Retrieves the value of the given field from the actual record.
       
   277 //    @param aFieldName name of the field.
       
   278 //    @return integer value of the field
       
   279 // -----------------------------------------------------------------------------
       
   280 TInt JavaCommDB::GetIntFieldValueL(const TDesC& aFieldName)
       
   281 {
       
   282     TPtrC fieldName(aFieldName);
       
   283     TInt valueType;
       
   284     TUint32 value = 0;
       
   285 
       
   286     // If the field is the RecordId we have to use RecordId() function
       
   287     if (0 == aFieldName.Compare(KFieldNameRecordId))
       
   288     {
       
   289         value
       
   290         = (static_cast<CCDRecordBase*>(iTable->iRecords[iCurrentRecord]))->RecordId();
       
   291         LOG1(EJavaIapInfo, EInfo,
       
   292              "CJAVACOMMDB  GetIntFieldValueL(): getting RecordId: %D", value);
       
   293         return value;
       
   294     }
       
   295 
       
   296     // Get reference to the field by its name
       
   297     CMDBField<TUint32>
       
   298     * field =
       
   299         (CMDBField<TUint32> *)(static_cast<CCDRecordBase*>(iTable->iRecords[iCurrentRecord]))->GetFieldByNameL(
       
   300             fieldName, valueType);
       
   301 
       
   302     if (field->IsNull())
       
   303     {
       
   304         // The field is not initialized with a value
       
   305         LOG(EJavaIapInfo, EInfo,
       
   306             "CJAVACOMMDB  GetIntFieldValueL(): CMDBField is NULL!");
       
   307     }
       
   308     else
       
   309     {
       
   310         value = *field;
       
   311         LOG1(EJavaIapInfo, EInfo,
       
   312              "CJAVACOMMDB  GetIntFieldValueL(): getting int value: %D",
       
   313              value);
       
   314     }
       
   315 
       
   316     return value;
       
   317 }
       
   318 
       
   319 // ---------------------------------------------------------------------------
       
   320 //    Retrieves the value of the given field from the actual record.
       
   321 //    @param aFieldName name of the field.
       
   322 //    @return String value of the field
       
   323 // -----------------------------------------------------------------------------
       
   324 HBufC* JavaCommDB::GetStringFieldValueL(const TDesC& aFieldName)
       
   325 {
       
   326     TPtrC fieldName(aFieldName);
       
   327     HBufC* strValue = NULL;
       
   328     TInt valueType;
       
   329 
       
   330     // Get reference to the field by its name
       
   331     CMDBField<TDesC>
       
   332     * descField =
       
   333         (CMDBField<TDesC> *)(static_cast<CCDRecordBase*>(iTable->iRecords[iCurrentRecord]))->GetFieldByNameL(
       
   334             fieldName, valueType);
       
   335 
       
   336     if (descField->IsNull())
       
   337     {
       
   338         LOG(EJavaIapInfo, EInfo,
       
   339             "CJAVACOMMDB  GetStringFieldValueL(): CMDBField is NULL!");
       
   340     }
       
   341     else
       
   342     {
       
   343         TPtrC value = descField->GetL();
       
   344         strValue = value.AllocL();
       
   345         //LOG1(EJavaIapInfo, EInfo,"CJAVACOMMDB  GetStringFieldValue(): getting string value: %S", value);
       
   346     }
       
   347 
       
   348     return strValue;
       
   349 }
       
   350 
       
   351 // ---------------------------------------------------------------------------
       
   352 //    Changes the actual record pointer to the next record.
       
   353 //    @return number of the actual record
       
   354 // -----------------------------------------------------------------------------
       
   355 TInt JavaCommDB::Next()
       
   356 {
       
   357     if (iCurrentRecord < GetRecordCount() - 1)
       
   358     {
       
   359         ++iCurrentRecord;
       
   360         return iCurrentRecord;
       
   361     }
       
   362     else
       
   363     {
       
   364         return KErrEof;
       
   365     }
       
   366 }
       
   367 
       
   368 // ---------------------------------------------------------------------------
       
   369 //    Changes the actual record pointer to the previous record.
       
   370 //    @return number of the actual record
       
   371 // -----------------------------------------------------------------------------
       
   372 TInt JavaCommDB::Previous()
       
   373 {
       
   374     return NULL;
       
   375 }
       
   376 
       
   377 // ---------------------------------------------------------------------------
       
   378 //    Finds a record in the current table by its name. The search is case
       
   379 //    sensitive. If the record exists the current record will be changed to
       
   380 //    this record.
       
   381 //    @param aName name of the record
       
   382 //    @return with the RecordId of the found record, KErrNotFound if no record
       
   383 //    found with the given name.
       
   384 // -----------------------------------------------------------------------------
       
   385 TInt JavaCommDB::FindByNameL(const TDesC& aName)
       
   386 {
       
   387     TInt ret = KErrNotFound;
       
   388     TInt count = GetRecordCount();
       
   389     TInt i = 0;
       
   390     HBufC* strValue = NULL;
       
   391     TPtrC FName(KFieldNameName);
       
   392     TInt valueType;
       
   393 
       
   394     while ((i < count) && (ret == KErrNotFound))
       
   395     {
       
   396         // Get reference to the field by its name
       
   397         CMDBField<TDesC>
       
   398         * descField =
       
   399             (CMDBField<TDesC> *)(static_cast<CCDRecordBase*>(iTable->iRecords[i]))->GetFieldByNameL(
       
   400                 FName, valueType);
       
   401 
       
   402         if (!(descField->IsNull()))
       
   403         {
       
   404             TPtrC value = descField->GetL();
       
   405 
       
   406             strValue = value.AllocL();
       
   407             //LOG1(EJavaIapInfo, EInfo, "CJAVACOMMDB  FindByName(): getting string value: %S", value);
       
   408             if (0 == aName.Compare(strValue->Des()))
       
   409             {
       
   410                 LOG(EJavaIapInfo, EInfo,
       
   411                     "CJAVACOMMDB  FindByName(): record found.");
       
   412                 iCurrentRecord = i;
       
   413                 ret
       
   414                 = (static_cast<CCDRecordBase*>(iTable->iRecords[i])->RecordId());
       
   415             }
       
   416 
       
   417             delete strValue;
       
   418         }
       
   419 
       
   420         i++;
       
   421     }
       
   422 
       
   423     return ret;
       
   424 }
       
   425 
       
   426 // ---------------------------------------------------------------------------
       
   427 //    Finds a record in the current table by its RecordId. If the record exists
       
   428 //    the current record will be changed to this record.
       
   429 //    @param aId RecordId of the record
       
   430 //    @return with the RecordId of the found record, KErrNotFound if no record
       
   431 //    found with the given RecordId.
       
   432 // -----------------------------------------------------------------------------
       
   433 TInt JavaCommDB::FindById(TInt aId)
       
   434 {
       
   435     TInt ret = KErrNotFound;
       
   436     TInt count = GetRecordCount();
       
   437     TInt i = 0;
       
   438 
       
   439     while ((i < count) && (ret == KErrNotFound))
       
   440     {
       
   441         if (aId
       
   442                 == (static_cast<CCDRecordBase*>(iTable->iRecords[i])->RecordId()))
       
   443         {
       
   444             LOG(EJavaIapInfo, EInfo, "CJAVACOMMDB  FindById(): record found.");
       
   445             iCurrentRecord = i;
       
   446             ret
       
   447             = (static_cast<CCDRecordBase*>(iTable->iRecords[i])->RecordId());
       
   448         }
       
   449 
       
   450         i++;
       
   451     }
       
   452 
       
   453     return ret;
       
   454 }
       
   455