contentpublishingsrv/contentpublishingserver/cpserver/src/cpactiondatacache.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Tue, 14 Sep 2010 21:55:16 +0300
branchRCL_3
changeset 118 8baec10861af
parent 114 a5a39a295112
permissions -rw-r--r--
Revision: 201033 Kit: 201035

/*
* Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:  ?Description

 *
*/


#include <liwcommon.h>
#include <liwvariant.h>
#include <s32mem.h>
#include <escapeutils.h>

#include "cpactiondatacache.h"
#include "cpglobals.h"

using namespace LIW;

_LIT8(KCachedMap, "cached_map");

static const int KMaxCacheItems = 24;


// ======== MEMBER FUNCTIONS ========


// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
CCPActionDataCache* CCPActionDataCache::NewL()
    {
    CCPActionDataCache* self = CCPActionDataCache::NewLC();
    CleanupStack::Pop(self);
    return self;
    }

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
CCPActionDataCache* CCPActionDataCache::NewLC()
    {
    CCPActionDataCache* self = new (ELeave) CCPActionDataCache;
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
    }

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void CCPActionDataCache::ConstructL( )
    {
    iInternalList = CLiwDefaultList::NewL();
    }

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
CCPActionDataCache::CCPActionDataCache()
    {
    }

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
CCPActionDataCache::~CCPActionDataCache()
    {
    if (iInternalList)
        {
        iInternalList->Close();
        }
    }

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void CCPActionDataCache::HandleCacheRemoveL(const CLiwMap* aMap)
    {
    TInt id = FindL(aMap);
    if (id != KErrNotFound)
        {
        iInternalList->Remove(id);
        }
    }

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void CCPActionDataCache::AppendL( const CLiwGenericParamList* aParamList)
    {
    const TLiwGenericParam* param;
    TInt pos( 0);
    param = aParamList->FindFirst( pos, KListMap );

    if (param && param->Value().TypeId() == EVariantTypeMap)
        {
        const CLiwMap* inputMap = param->Value().AsMap();
        CLiwDefaultMap* map = CLiwDefaultMap::NewLC();
        CopyVariantL(KType, inputMap, map);
        CopyVariantL(KId, inputMap, map);
        CopyVariantL(KPublisherId, inputMap, map);
        CopyVariantL(KContentType, inputMap, map);
        CopyVariantL(KContentId, inputMap, map);
        map->InsertL(KCachedMap, TLiwVariant(inputMap));
        iInternalList->AppendL(TLiwVariant(map));
        CleanupStack::PopAndDestroy(map);

        if (iInternalList->Count() > KMaxCacheItems)
            {
            iInternalList->Remove(0);
            }
        }
    }

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void CCPActionDataCache::AppendEmptyL( const CLiwMap* aIdsMap )
    {
    CLiwDefaultMap* map = CLiwDefaultMap::NewLC();
    CopyVariantL(KType, aIdsMap, map);
    CopyVariantL(KId, aIdsMap, map);
    CopyVariantL(KPublisherId, aIdsMap, map);
    CopyVariantL(KContentType, aIdsMap, map);
    CopyVariantL(KContentId, aIdsMap, map);
    iInternalList->AppendL(TLiwVariant(map));
    CleanupStack::PopAndDestroy(map);
    }

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
TBool CCPActionDataCache::IsCacheableL(const CLiwMap* aMap)
    {
    TBool result(EFalse);
    TLiwVariant value;
    value.PushL();

    if (aMap->FindL(KId, value) && value.AsTInt32() > 0)
        {
        result = ETrue;
        }
    else if (IsSpecifiedL(aMap, KPublisherId) && IsSpecifiedL(aMap,
            KContentType) && IsSpecifiedL(aMap, KContentId))
        {
        result = ETrue;
        }

    CleanupStack::PopAndDestroy(&value);
    return result;
    }

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
TBool CCPActionDataCache::ExistL(const CLiwMap* aMap)
    {
    TBool result(EFalse);
    if (FindL(aMap) != KErrNotFound)
        {
        result = ETrue;
        }
    return result;
    }

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void CCPActionDataCache::GetL(const CLiwMap* aMap,
        CLiwGenericParamList* aParamList)
    {
    TInt id = FindL(aMap);
    if (id != KErrNotFound)
        {
        TLiwVariant value;
        value.PushL();
        iInternalList->AtL(id, value);
        const CLiwMap* map = value.AsMap();
        if (map->FindL(KCachedMap, value))
            {
            CLiwDefaultMap* outMap = CLiwDefaultMap::NewLC();
            value.Get(*outMap);
            TLiwGenericParam genericParam(KListMap, TLiwVariant(outMap));
            aParamList->AppendL(genericParam);
            CleanupStack::PopAndDestroy(outMap);
            }
        CleanupStack::PopAndDestroy(&value);
        }
    }

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
TBool CCPActionDataCache::MatchL(const CLiwMap* aCachedMap,
        const CLiwMap* aInputMap)
    {
    TBool idMatch(EFalse);
    TLiwVariant l, r;
    l.PushL();
    r.PushL();
    if (TypesMatchL(aCachedMap,aInputMap))
        {
        if (aCachedMap->FindL(KId, l) && aInputMap->FindL(KId, r) && l.AsTInt32()
                == r.AsTInt32())
            {
            idMatch = ETrue;
            }
        if (!idMatch)
            {
            if (MatchL(aCachedMap, aInputMap, KPublisherId) && MatchL(aCachedMap,
                    aInputMap, KContentType) && MatchL(aCachedMap, aInputMap,
                    KContentId))
                {
                idMatch = ETrue;
                }
            }
        }
    CleanupStack::PopAndDestroy(&r);
    CleanupStack::PopAndDestroy(&l);
    return idMatch;
    }

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
TBool CCPActionDataCache::TypesMatchL(const CLiwMap* aCachedMap,
        const CLiwMap* aInputMap)
    {
    TBool typesMatch(EFalse);
    TLiwVariant cacheVariant;
    TLiwVariant inputVariant;
    cacheVariant.PushL();
    inputVariant.PushL();
    if (aCachedMap->FindL(KType, cacheVariant) && aInputMap->FindL(KType,
            inputVariant))
        {
        RBuf typeCache;
        RBuf typeInput;
        CleanupClosePushL(typeCache);
        CleanupClosePushL(typeInput);
        ExtractRBufL(cacheVariant, typeCache);
        ExtractRBufL(inputVariant, typeInput);
        if (typeCache.Compare(typeInput)==0)
            {
            typesMatch = ETrue;
            }
        CleanupStack::PopAndDestroy( &typeInput );
        CleanupStack::PopAndDestroy( &typeCache );
        }
    CleanupStack::PopAndDestroy(&inputVariant);
    CleanupStack::PopAndDestroy(&cacheVariant);
    return typesMatch;
    }

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
void CCPActionDataCache::ExtractRBufL(const TLiwVariant& aVariant, RBuf& aBuf)
    {
    TPtrC type( KNullDesC );
    if( !aVariant.Get( type ) )
        {
        TPtrC8 type8( KNullDesC8 );
        if( !aVariant.Get( type8 ) )
            {
            User::Leave( KErrBadName );
            }
        aBuf.Assign( EscapeUtils::ConvertToUnicodeFromUtf8L( type8 ) );
        }
    else
        {
        aBuf.CreateL( type );
        }
    }

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
TBool CCPActionDataCache::MatchL(const CLiwMap* aLeft, const CLiwMap* aRight,
        const TDesC8& aKey)
    {
    TBool match(EFalse);
    TLiwVariant l, r;
    l.PushL();
    r.PushL();
    if (aLeft->FindL(aKey, l) && aRight->FindL(aKey, r)
            && !l.AsDes().Compare(r.AsDes()))
        {
        match = ETrue;
        }
    CleanupStack::PopAndDestroy(&r);
    CleanupStack::PopAndDestroy(&l);
    return match;
    }

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
TBool CCPActionDataCache::IsSpecifiedL(const CLiwMap* aMap,
        const TDesC8& aKey)
    {
    TBool result(EFalse);
    TLiwVariant value;
    value.PushL();
    if (aMap->FindL(aKey, value) && value.AsDes().Compare(KNullDesC) != 0
            && value.AsDes().Compare(KAll) != 0)
        {
        result = ETrue;
        }
    CleanupStack::PopAndDestroy(&value);
    return result;
    }

// ---------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------
//
TInt CCPActionDataCache::FindL(const CLiwMap* aKey)
    {
    TInt result(KErrNotFound);
    TLiwVariant value;
    value.PushL();
    for (TInt i = 0; i < iInternalList->Count(); i++)
        {
        iInternalList->AtL(i, value);
        if (MatchL(value.AsMap(), aKey))
            {
            result = i;
            break;
            }
        }
    CleanupStack::PopAndDestroy(&value);
    return result;
    }

// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
//
void CCPActionDataCache::CopyVariantL(const TDesC8& aKey,
        const CLiwMap* aInMap, CLiwDefaultMap* aOutMap)
    {
    //TODO: method exist also in data manager - should be refactored
    TLiwVariant variant;
    variant.PushL();
    if (aInMap->FindL(aKey, variant))
        {
        aOutMap->InsertL(aKey, variant);
        }
    CleanupStack::PopAndDestroy(&variant);
    }