brandingserver/tools/bsimport/src/cbsimportstorage.cpp
changeset 0 e6b17d312c8b
child 21 cfd5c2994f10
equal deleted inserted replaced
-1:000000000000 0:e6b17d312c8b
       
     1 /*
       
     2 * Copyright (c) 2006-2006 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:  Stores element data and writes it to stream
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <s32strm.h>
       
    20 #include <utf.h>
       
    21 #include <cbsfactory.h>
       
    22 #include <mbsupdater.h>
       
    23 #include <mbselement.h>
       
    24 
       
    25 #include "cbsimportstorage.h"
       
    26 #include "cbsimportconstants.h"
       
    27 #include "importlogwriter.h"
       
    28 
       
    29 // ======== MEMBER FUNCTIONS ========
       
    30 
       
    31 // ---------------------------------------------------------------------------
       
    32 // CBSImportStorage::NewL
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 CBSImportStorage* CBSImportStorage::NewL()
       
    36     {
       
    37     CBSImportStorage* self = NewLC();
       
    38     CleanupStack::Pop();
       
    39     return self;
       
    40     }
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 // CBSImportStorage::NewLC
       
    44 // ---------------------------------------------------------------------------
       
    45 //
       
    46 CBSImportStorage* CBSImportStorage::NewLC()
       
    47     {
       
    48     CBSImportStorage* self = new (ELeave) CBSImportStorage();
       
    49     CleanupStack::PushL( self );
       
    50     self->ConstructL();
       
    51     return self;
       
    52     }
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // CBSImportStorage::ConstructL
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 void CBSImportStorage::ConstructL()
       
    59     {
       
    60     }
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // CBSImportStorage::CBSImportStorage
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 CBSImportStorage::CBSImportStorage()
       
    67     {
       
    68     }
       
    69     
       
    70 // ---------------------------------------------------------------------------
       
    71 // CBSImportStorage::~CBSImportStorage
       
    72 // ---------------------------------------------------------------------------
       
    73 //
       
    74 CBSImportStorage::~CBSImportStorage()
       
    75     {
       
    76     iElements.ResetAndDestroy();
       
    77     delete iAppId;
       
    78     delete iStorageId;
       
    79     delete iFilename;
       
    80     }
       
    81 
       
    82 // ---------------------------------------------------------------------------
       
    83 // CBSImportStorage::AppendElement
       
    84 // ---------------------------------------------------------------------------
       
    85 //
       
    86 TInt CBSImportStorage::AppendElement( MBSElement* aElement )
       
    87     {
       
    88     if( aElement )
       
    89         {
       
    90         return iElements.Append( aElement );
       
    91         }
       
    92     // Ignore empty
       
    93     return KErrNone;
       
    94     }
       
    95 
       
    96 // ---------------------------------------------------------------------------
       
    97 // CBSImportStorage::ElementCount
       
    98 // ---------------------------------------------------------------------------
       
    99 //
       
   100 TInt CBSImportStorage::ElementCount()
       
   101     {
       
   102     return iElements.Count();
       
   103     }
       
   104     
       
   105 // ---------------------------------------------------------------------------
       
   106 // CBSImportStorage::Element
       
   107 // ---------------------------------------------------------------------------
       
   108 //
       
   109 MBSElement* CBSImportStorage::Element( TInt aIndex )
       
   110     {
       
   111     return iElements[ aIndex ];
       
   112     }
       
   113 
       
   114 // ---------------------------------------------------------------------------
       
   115 // CBSImportStorage::FlushtoServerL
       
   116 // ---------------------------------------------------------------------------
       
   117 //
       
   118 void CBSImportStorage::FlushToServerL()
       
   119     {
       
   120     if( !VerifyHeader() || !VerifyDataL( iElements.Array() ) )
       
   121         {
       
   122         // Header not complete. Will not flush
       
   123         User::Leave( KErrCorrupt );
       
   124         }
       
   125 
       
   126     // Create access to server
       
   127     CBSFactory* factory = CBSFactory::NewL( *iStorageId, *iAppId );
       
   128     CleanupStack::PushL( factory );
       
   129     MBSUpdater* updater = factory->CreateUpdaterLC();
       
   130     //updater->StartTransactionL( *iStorageId, iLanguage, iVersion );
       
   131     updater->StartTransactionL( *iStorageId, iLanguage );
       
   132     
       
   133     // Flush data
       
   134     TInt count = iElements.Count();
       
   135     for( TInt i = 0; i < count; i++ )
       
   136         {
       
   137         TRAPD( err, updater->InsertElementL( iElements[i] ) );
       
   138         if( err == KErrAlreadyExists )
       
   139             {
       
   140             err = KErrNone;
       
   141             TRAP( err, updater->ReplaceElementL( iElements[i] ) );
       
   142             }
       
   143         User::LeaveIfError( err );
       
   144         }
       
   145     
       
   146     // Cleanup
       
   147     updater->StopTransactionL();
       
   148     CleanupStack::PopAndDestroy( 2 ); // factory, updater
       
   149     }
       
   150     
       
   151 // ---------------------------------------------------------------------------
       
   152 // CBSImportStorage::SetVersion
       
   153 // ---------------------------------------------------------------------------
       
   154 //
       
   155 void CBSImportStorage::SetVersion( TInt aVersion )
       
   156     {
       
   157     iVersion = aVersion;
       
   158     }
       
   159 
       
   160 // ---------------------------------------------------------------------------
       
   161 // CBSImportStorage::SetApplicationIdL
       
   162 // ---------------------------------------------------------------------------
       
   163 //
       
   164 void CBSImportStorage::SetApplicationIdL( const TDesC& aAppId )
       
   165     {
       
   166     HBufC8* id = HBufC8::NewLC( aAppId.Length() );
       
   167     TPtr8 idPtr( id->Des() );
       
   168     CnvUtfConverter::ConvertFromUnicodeToUtf8( idPtr, aAppId );
       
   169     CleanupStack::Pop( id );
       
   170     delete iAppId;
       
   171     iAppId = id;   
       
   172     }
       
   173 
       
   174 // ---------------------------------------------------------------------------
       
   175 // CBSImportStorage::SetStorageIdL
       
   176 // ---------------------------------------------------------------------------
       
   177 //
       
   178 void CBSImportStorage::SetStorageIdL( const TDesC& aStorageId )
       
   179     {
       
   180     HBufC8* id = HBufC8::NewLC( aStorageId.Length() );
       
   181     TPtr8 idPtr( id->Des() );
       
   182     CnvUtfConverter::ConvertFromUnicodeToUtf8( idPtr, aStorageId );
       
   183     CleanupStack::Pop( id );
       
   184     delete iStorageId;
       
   185     iStorageId = id;   
       
   186     }
       
   187 
       
   188 // ---------------------------------------------------------------------------
       
   189 // CBSImportStorage::SetLanguageL
       
   190 // ---------------------------------------------------------------------------
       
   191 //
       
   192 void CBSImportStorage::SetLanguageL( TLanguage aLanguage )
       
   193     {
       
   194     iLanguage = aLanguage;
       
   195     }
       
   196 
       
   197 // ---------------------------------------------------------------------------
       
   198 // CBSImportStorage::VerifyHeader
       
   199 // ---------------------------------------------------------------------------
       
   200 //
       
   201 TBool CBSImportStorage::VerifyHeader()
       
   202     {
       
   203     // not checking version as it is been consider not to use. - Pankaj - 31 may 07
       
   204     //return ( iAppId && iStorageId && iVersion > 0 && iLanguage >= ELangTest );
       
   205     return ( iAppId && iStorageId && iLanguage >= ELangTest  );
       
   206     
       
   207     }
       
   208 
       
   209 
       
   210 // ---------------------------------------------------------------------------
       
   211 // CBSImportStorage::VerifyDataL
       
   212 // ---------------------------------------------------------------------------
       
   213 //
       
   214 TBool CBSImportStorage::VerifyDataL( TArray<MBSElement*> aArray, 
       
   215                                      TBool aIgnoreEmptyID /* = EFalse */ )
       
   216     {
       
   217     // Check that elements have unique ID's in their own namespace
       
   218     TInt count = aArray.Count();
       
   219     for( TInt i = 0; i < count; i++ )
       
   220         {
       
   221         // Verify inside list elements
       
   222         if( aArray[i]->ElementType() == EBSList )
       
   223             {
       
   224             if( !VerifyDataL( aArray[i]->GetStructureL(), ETrue ) )
       
   225                 {
       
   226                 return false;
       
   227                 }
       
   228             }
       
   229 
       
   230         if( aIgnoreEmptyID && 
       
   231             aArray[i]->ElementId().CompareC( KNullDesC8() ) == 0 )
       
   232             {
       
   233             // ignore empty id
       
   234             continue;
       
   235             }
       
   236         
       
   237         // Check that sibling elements have unique ID
       
   238         for( TInt j = i + 1; j < count; j++ ) 
       
   239             {
       
   240             if( aArray[i]->ElementId().CompareC( aArray[j]->ElementId() ) == 0 )
       
   241                 {
       
   242                 // convert elementID to 16-bit for event logger
       
   243                 TPtrC8 id( aArray[i]->ElementId() );
       
   244                 HBufC* conv = HBufC::NewLC( id.Length() );
       
   245                 TPtr uniptr( conv->Des() );
       
   246                 CnvUtfConverter::ConvertToUnicodeFromUtf8( uniptr, id );                
       
   247                 IMPORT_DP( D_IMPORT_LIT("ERROR: Found duplicate element ID (%S)!"), &uniptr );
       
   248                 CleanupStack::PopAndDestroy();
       
   249                 return false;
       
   250                 }
       
   251             }
       
   252         }
       
   253     
       
   254     // All elements ok
       
   255     return true;
       
   256     }
       
   257 
       
   258 // ---------------------------------------------------------------------------
       
   259 // CBSImportStorage::Compare
       
   260 // ---------------------------------------------------------------------------
       
   261 //
       
   262 TInt CBSImportStorage::Compare( const CBSImportStorage* aStorage ) const
       
   263     {
       
   264     if( iVersion != aStorage->iVersion || iLanguage != aStorage->iLanguage )
       
   265         {
       
   266         // no match
       
   267         return EFalse;
       
   268         }
       
   269     if( iAppId->Compare( aStorage->iAppId->Des() ) != 0 )
       
   270         {
       
   271         // no match
       
   272         return EFalse;
       
   273         }
       
   274     if( iStorageId->Compare( aStorage->iStorageId->Des() ) != 0 )
       
   275         {
       
   276         // no match
       
   277         return EFalse;
       
   278         }
       
   279     // they match
       
   280     return ETrue;
       
   281     }
       
   282     
       
   283 // ---------------------------------------------------------------------------
       
   284 // CBSImportStorage::TakeElementsL
       
   285 // ---------------------------------------------------------------------------
       
   286 //
       
   287 void CBSImportStorage::TakeElements( CBSImportStorage* aStorage )
       
   288     {
       
   289     TInt count = aStorage->iElements.Count();
       
   290     for( TInt i = 0; i < count; i++ )
       
   291         {
       
   292         iElements.Append( aStorage->iElements[i] );
       
   293         }
       
   294     aStorage->iElements.Reset();
       
   295     }
       
   296     
       
   297 // End of file