/**
Change the sorting order/text definition. It should be always called when 
there is not an iteration started in persistence layer.

@param aTextDef the new text definition to be used in the view session.
*/
void CCntPplViewSession::ChangeSortOrderL(const CContactTextDef& aTextDef)
	{
	//Cleanup the cached Prepare statement as the sort order will be changed
	Cancel();
    CleanupCachedPrepareStatement();
	
	//Copy the text definition
	CContactTextDef* textDef = CContactTextDef::NewLC();
	const TInt KTextDefCount = aTextDef.Count();
	for (TInt index = 0; index < KTextDefCount; ++index)
		{
		textDef->AppendL(TContactTextDefItem(aTextDef.At(index).iFieldType));
		}
	
	// Create select statement on contact table
	TCntSqlStatementType statementType(ESelect, KSqlContactTableName());
	CCntSqlStatement* sqlSmt = TSqlProvider::GetSqlStatementL(statementType);
	CleanupStack::PushL(sqlSmt);
	
	// Always select id, type flags.	
	sqlSmt->SetParamL(KContactId(), KSpace());
	sqlSmt->SetParamL(KContactTypeFlags(), KSpace());
	
	//Go through text definition to construct select statement.
	TBool isFastAccessOnly = ETrue;
	for(TInt ii = 0; ii < KTextDefCount; ++ii)
		{
		const TDesC& KColunmName = TCntPersistenceUtility::GetFastAccessColumnNameById(aTextDef.At(ii).iFieldType.iUid);
		if(KColunmName.Length() > 0) 
			{
			sqlSmt->SetParamL(KColunmName, KSpace());
			}
		else
			{
			isFastAccessOnly = EFalse;		
			}
		}
	
	if(!isFastAccessOnly)
		{
		//Fields in text blob are needed, add text fields header and
        //text blob columns in the select statement.		
		sqlSmt->SetParamL(KContactTextFieldHeader(), KSpace());
		sqlSmt->SetParamL(KContactTextFields(), KSpace());
		}

	CleanupStack::Pop(2, textDef); // sqlSmt, textDef.
	
	delete iCntSqlStatement;
	iCntSqlStatement = sqlSmt;
	
	delete iTextDef;
	iTextDef = textDef;
	
	iIsFastAccessFieldsOnly = isFastAccessOnly;
	}
HBufC8* CPolicyPatcher::CreatePatchDataL(const CPolicyPatchInfo* aPatchInfo)
    {
    TInt patchDataLength = 0;

    TBuf8<20> keyBuf;

    const TDesC8& certSubjectName = aPatchInfo->CertSubjectName();

    if (0 == aPatchInfo->UserCertKeyLen())
        {
        // CA cert patch
        patchDataLength = KName().Length() + KNewLine().Length() +
                          KDataField().Length() + KSpace().Length() +
                          certSubjectName.Length();
        }
    else
        {
        // user cert patch
        keyBuf.Num(aPatchInfo->UserCertKeyLen());
        patchDataLength = KDNField().Length() + certSubjectName.Length() +
                          KNewLine().Length() + KKeyLenField().Length() +
                          keyBuf.Length();
        }

    HBufC8* patchData = HBufC8::NewL(patchDataLength);
    CleanupStack::PushL(patchData);

    TPtr8 ptrPatchData(patchData->Des());

    if (0 == aPatchInfo->UserCertKeyLen())
        {
        // CA cert patch
        ptrPatchData.Append(KName);
        ptrPatchData.Append(KNewLine);
        ptrPatchData.Append(KDataField);
        ptrPatchData.Append(KSpace);
        ptrPatchData.Append(certSubjectName);
        }
    else
        {
        // User cert patch
        ptrPatchData.Append(KKeyLenField);
        ptrPatchData.Append(keyBuf);
        ptrPatchData.Append(KNewLine);
        ptrPatchData.Append(KDNField);
        ptrPatchData.Append(certSubjectName);
        iUserCertPatched = ETrue;
        }

    CleanupStack::Pop(); // patchData

    return patchData;
    }
// -----------------------------------------------------------------------------
// CAtomFeedParser::SetItemIdStrAttributeL
//
// Determine and set the unique IdStr attribute (unique to the feed that is).
// -----------------------------------------------------------------------------
//
void CAtomFeedParser::SetItemIdStrAttributeL(TXmlEngElement aItemNode, 
        MFeedParserObserver& aObserver)
    {    
    const TInt  KStrChunk = 30;
    
    TDesC*    content = NULL;
    TDesC*    summary = NULL;
    TDesC*    description = NULL;
    TDesC*    title = NULL;
    HBufC*    url = NULL;
    HBufC*    idStr = NULL;
    TXmlEngElement  node;

    // If the id node is present use it.
    node = iXmlUtils.GetFirstNamedChild(aItemNode, KId);
    if (node.NotNull())
        {
        ElementHandlerCDataL(*this, iXmlUtils, node, EItemAttributeIdStr, aObserver);        
        return;
        }

    // Otherwise create a idStr from the first 30 chars of the description and title
    // and the last 30 chars of the url.  This doesn't guarantee a unique id, but 
    // it very likely.
    node = iXmlUtils.GetFirstNamedChild(aItemNode, KSummary);
    summary = iXmlUtils.ExtractSimpleTextL(node, KStrChunk);
    CleanupStack::PushL(summary);
        
    node = iXmlUtils.GetFirstNamedChild(aItemNode, KContent);
    content = iXmlUtils.ExtractSimpleTextL(node, KStrChunk);
    CleanupStack::PushL(content);

    node = iXmlUtils.GetFirstNamedChild(aItemNode, KTitle);
    title = iXmlUtils.ExtractSimpleTextL(node, KStrChunk);
    CleanupStack::PushL(title);
    
    node = iXmlUtils.GetFirstNamedChild(aItemNode, KLink);
    url = iXmlUtils.ExtractSimpleTextL(node, KStrChunk, ETrue);
    CleanupStack::PushL(url);

    // Determine whether content or summary will be used for the description.
    description = summary;
    
    if (content != NULL)
        {
        if (summary == NULL)
            {
            description = content;
            }
        else if (content->Length() > summary->Length())
            {
            description = content;
            }
        }

    // Construct the idStr from the parts.
    TInt  len = 0;
    
    if (description != NULL)
        {
        len += description->Length();
        }
    if (title != NULL)
        {
        len += title->Length();
        }
    if (url != NULL)
        {
        len += url->Length();
        }
    
    idStr = HBufC::NewL(len);
    CleanupStack::PushL(idStr);

    TPtr  ptr(idStr->Des());
    
    if (description != NULL)
        {
        ptr.Append(*description);
        }
    if (title != NULL)
        {
        ptr.Append(*title);
        }
    if (url != NULL)
        {
        ptr.Append(*url);
        }
        
    // Replace any chars that may interfere with the database.
    _LIT(KSpace, " ");
    
    for (TInt i = 0; i < ptr.Length(); i++)
        {
        if (ptr[i] == '\'')
            {
            ptr.Replace(i, 1, KSpace());
            }
        }
    
    // Set the idStr attribute.
    aObserver.AddAttributeL(EItemAttributeIdStr, *idStr);
    
    CleanupStack::PopAndDestroy(idStr);    
    CleanupStack::PopAndDestroy(url);    
    CleanupStack::PopAndDestroy(title);    
    CleanupStack::PopAndDestroy(content);    
    CleanupStack::PopAndDestroy(summary);    
    }