metadataengine/client/src/mdeasynchronousfindao.cpp
changeset 0 c53acadfccc6
child 1 acef663c1218
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2005-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:  Asynchronous query active object
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "mdeasynchronousfindao.h"
       
    21 #include "mdequeryimpl.h"
       
    22 
       
    23 #include "mdeenginesession.h"
       
    24 #include "mdelogiccondition.h"
       
    25 #include "mdesessionimpl.h"
       
    26 #include "mdequerycriteriaserialization.h"
       
    27 #include "mdeinstanceitem.h"
       
    28 
       
    29 // ========================= MEMBER FUNCTIONS ==================================
       
    30 
       
    31 CMdEAsynchronousFindAO* CMdEAsynchronousFindAO::NewL( CMdEQuery& aQuery,
       
    32     CMdESessionImpl& aSessionImpl, RMdEEngineSession& aSession )
       
    33     {
       
    34     CMdEAsynchronousFindAO* self = CMdEAsynchronousFindAO::NewLC( aQuery, aSessionImpl, aSession );
       
    35     CleanupStack::Pop( self );
       
    36     return self;
       
    37     }
       
    38 
       
    39 CMdEAsynchronousFindAO* CMdEAsynchronousFindAO::NewLC( CMdEQuery& aQuery,
       
    40 	    CMdESessionImpl& aSessionImpl, RMdEEngineSession& aSession )
       
    41     {
       
    42     CMdEAsynchronousFindAO* self =
       
    43         new ( ELeave ) CMdEAsynchronousFindAO( aQuery, aSessionImpl, aSession );
       
    44     CleanupStack::PushL( self );
       
    45     self->ConstructL();
       
    46     return self;
       
    47     }
       
    48 
       
    49 void CMdEAsynchronousFindAO::ConstructL()
       
    50     {
       
    51     }
       
    52 
       
    53 CMdEAsynchronousFindAO::CMdEAsynchronousFindAO( CMdEQuery& aQuery,
       
    54     CMdESessionImpl& aSessionImpl, RMdEEngineSession& aSession )
       
    55     : CActive( CActive::EPriorityStandard )
       
    56     , iQuery( aQuery )
       
    57     , iSessionImpl ( aSessionImpl )
       
    58     , iSession( aSession )
       
    59     , iCountResult( 0 )
       
    60     , iConditionBuffer( NULL )
       
    61     , iDistinctResults( NULL )
       
    62     {
       
    63     CActiveScheduler::Add( this );
       
    64     }
       
    65 
       
    66 
       
    67 void CMdEAsynchronousFindAO::DoFindL( TUint aMaxCount, TUint aNotifyCount )
       
    68     {    
       
    69 	CMdEObjectDef* objectDef = NULL;
       
    70 
       
    71 	RPointerArray<CMdEObjectDef>* objectDefs = NULL;
       
    72 	
       
    73     RPointerArray<CMdEPropertyDef>* propertyFilter = NULL;
       
    74 
       
    75 	// get query's internal optimization flags
       
    76 	TUint32 queryOptimizationFlags = 0;
       
    77 	iQuery.Conditions().InternalQueryOptimizationFlags(queryOptimizationFlags);
       
    78 
       
    79 	TQueryResultMode resultMode = iQuery.ResultMode();
       
    80 
       
    81     // set property filter and distinct
       
    82     if( EQueryTypeObject == iQuery.Type() )
       
    83     	{
       
    84     	CMdEObjectQuery& objectQuery = (CMdEObjectQuery&)iQuery;
       
    85 
       
    86 		objectDef = &objectQuery.ObjectDef();
       
    87 		
       
    88 		objectDefs = objectQuery.ObjectDefs();
       
    89 
       
    90 		// if result mode is objects with freetext
       
    91 		// change it to normal items for server 
       
    92 		// (freetext including to results is in query optimization flags)
       
    93 		if( EQueryResultModeObjectWithFreetexts == resultMode )
       
    94 			{
       
    95 			resultMode = EQueryResultModeItem;
       
    96 			queryOptimizationFlags |= EContainsFreetextCondition;
       
    97 			}
       
    98 
       
    99     	if( EQueryResultModeItem == resultMode || 
       
   100     		EQueryResultModeDistinctValues == resultMode )
       
   101         	{
       
   102         	if( objectQuery.PropertyFilter().Count() > 0 )
       
   103         		{        		
       
   104         		propertyFilter = &objectQuery.PropertyFilter();
       
   105         		}
       
   106         	}
       
   107         }
       
   108 
       
   109     iConditionBuffer = 
       
   110     	CMdEQueryCriteriaSerialization::NewLC( resultMode, iQuery.Type(),     										   
       
   111     										   iQuery.NamespaceDef(), 
       
   112     										   objectDef, 
       
   113     										   objectDefs, 
       
   114     										   aMaxCount, 
       
   115     										   0, // 0 offset because it's not used currently
       
   116     										   queryOptimizationFlags, 
       
   117     										   iQuery.Conditions(), iQuery.OrderRules(),
       
   118     										   propertyFilter );
       
   119 	CleanupStack::Pop();
       
   120     iSession.DoFindAsync( 
       
   121     	iQuery.GetQueryId(), 
       
   122         *iConditionBuffer,
       
   123         &iResultSize, 
       
   124         iStatus, 
       
   125         aNotifyCount );
       
   126 
       
   127     SetActive();
       
   128     }
       
   129 
       
   130 
       
   131 CMdEAsynchronousFindAO::~CMdEAsynchronousFindAO()
       
   132     {
       
   133     Cancel(); // Causes call to DoCancel()
       
   134 
       
   135 	delete iDistinctResults;
       
   136 
       
   137 	delete iConditionBuffer;
       
   138 
       
   139     iResultList.ResetAndDestroy();
       
   140     iResultList.Close();
       
   141 
       
   142     iIdResultList.Close();
       
   143     }
       
   144 
       
   145 
       
   146 void CMdEAsynchronousFindAO::RunL()
       
   147     {
       
   148     // condition buffer can be safely removed now
       
   149     if( iConditionBuffer )
       
   150     	{
       
   151     	delete iConditionBuffer;
       
   152     	iConditionBuffer = NULL;
       
   153     	}
       
   154 
       
   155     // check if client side query is in error state (for example cancelled)
       
   156     if( iQuery.State() == CMdEQuery::EStateError )
       
   157     	{
       
   158     	TRAP_IGNORE( iSession.DoFindCancelL( &iQuery ) );
       
   159     	return;
       
   160     	}
       
   161     
       
   162     const TInt status = iStatus.Int();
       
   163     switch ( status )
       
   164         {
       
   165         case EAsyncFindSetReady:
       
   166         case EAsyncFindComplete:
       
   167             {            
       
   168             // fetch results from server
       
   169             CMdCSerializationBuffer* results = CMdCSerializationBuffer::NewLC(iResultSize());
       
   170 
       
   171             iSession.DoGetDataL( &iQuery, results, status );
       
   172 
       
   173 			const TQueryResultMode resultMode = iQuery.ResultMode();
       
   174 
       
   175             if ( resultMode == EQueryResultModeItem || resultMode == EQueryResultModeObjectWithFreetexts )
       
   176                 {
       
   177                 iSessionImpl.DeserializeQueryResultL( *results, iResultList );
       
   178                 }
       
   179             else if ( resultMode == EQueryResultModeId )
       
   180                 {
       
   181                 iSessionImpl.GetItemIdL( results, iIdResultList );
       
   182                 }
       
   183             else if ( resultMode == EQueryResultModeCount )
       
   184                 {
       
   185                 iSessionImpl.GetCountL( results, iCountResult );
       
   186                 }
       
   187             else if ( resultMode == EQueryResultModeDistinctValues )
       
   188             	{
       
   189             	if( iDistinctResults )
       
   190             		{            		
       
   191             		delete iDistinctResults;
       
   192             		iDistinctResults = NULL;
       
   193             		}
       
   194 
       
   195             	iDistinctResults = new (ELeave) CDesCArrayFlat( 16 );
       
   196             	iSessionImpl.GetDistinctValuesL( *results, *iDistinctResults );
       
   197             	}
       
   198 
       
   199             CleanupStack::PopAndDestroy( results );
       
   200 
       
   201             // notify the results
       
   202             DoNotifyL( ETrue, status );
       
   203             
       
   204             if ( status == EAsyncFindSetReady )
       
   205                 {
       
   206                 iResultSize = 0;
       
   207                 
       
   208                 // continue find, fetch next set
       
   209                 iSession.DoFindContinue( &iQuery, &iResultSize, iStatus );
       
   210                 SetActive();
       
   211                 }
       
   212             }
       
   213             break;
       
   214 
       
   215         default:
       
   216             {
       
   217             // notify of an error
       
   218             DoNotifyL( EFalse, status );
       
   219             }
       
   220             break;
       
   221         }
       
   222     }
       
   223 
       
   224 TInt CMdEAsynchronousFindAO::RunError(TInt aError)
       
   225 	{
       
   226 	// in error cases make sure that results are cleaned
       
   227 	iResultList.Reset();
       
   228 	
       
   229 	iIdResultList.Reset();
       
   230 	
       
   231 	delete iDistinctResults;
       
   232 	iDistinctResults = NULL;
       
   233 
       
   234 	if( aError == KErrServerTerminated )
       
   235 		{
       
   236 		iSessionImpl.NotifyError( aError );
       
   237 		}
       
   238 	else
       
   239 		{
       
   240 		// cancel query if it's still running
       
   241 		if( iQuery.State() != EAsyncFindComplete )
       
   242 			{
       
   243 			TRAP_IGNORE( iSession.DoFindCancelL( &iQuery ) );
       
   244 			}
       
   245 		}
       
   246 
       
   247     const TQueryType mode = iQuery.Type();
       
   248 
       
   249     if ( mode == EQueryTypeObject ) 
       
   250         {
       
   251         CMdEObjectQueryImpl& qi = (CMdEObjectQueryImpl&)iQuery;
       
   252         qi.DoNotifyCompleted( aError );
       
   253         }
       
   254     else if ( mode == EQueryTypeRelation )
       
   255         {
       
   256         CMdERelationQueryImpl& qi = (CMdERelationQueryImpl&)iQuery;
       
   257         qi.DoNotifyCompleted( aError );
       
   258         }
       
   259     else if ( mode == EQueryTypeEvent )
       
   260         {
       
   261         CMdEEventQueryImpl& qi = (CMdEEventQueryImpl&)iQuery;
       
   262         qi.DoNotifyCompleted( aError );
       
   263         }
       
   264 
       
   265 	return KErrNone;
       
   266 	}
       
   267 
       
   268 void CMdEAsynchronousFindAO::DoNotifyResultsL( TInt aStatus )
       
   269     {
       
   270     // Notify result instances
       
   271     if ( ( aStatus == EAsyncFindSetReady ||
       
   272            aStatus == EAsyncFindComplete ) &&
       
   273            iResultList.Count() > 0)
       
   274         {
       
   275         const TQueryType mode = iQuery.Type();
       
   276 
       
   277         if ( mode == EQueryTypeObject )
       
   278             {
       
   279             CMdEObjectQueryImpl& qi = (CMdEObjectQueryImpl&)iQuery;
       
   280             qi.DoNotifyResultsL( iResultList );
       
   281             }
       
   282         else if ( mode == EQueryTypeRelation )
       
   283             {
       
   284             CMdERelationQueryImpl& qi = (CMdERelationQueryImpl&)iQuery;
       
   285             qi.DoNotifyResultsL( iResultList );
       
   286             }
       
   287         else if ( mode == EQueryTypeEvent )
       
   288             {
       
   289             CMdEEventQueryImpl& qi = (CMdEEventQueryImpl&)iQuery;
       
   290             qi.DoNotifyResultsL( iResultList );
       
   291             }
       
   292 
       
   293         // get rid of local results list
       
   294         iResultList.Reset();
       
   295         }
       
   296     // Notify result ids
       
   297     else if ( ( aStatus == EAsyncFindSetReady ||
       
   298            aStatus == EAsyncFindComplete ) &&
       
   299            iIdResultList.Count() > 0)
       
   300         {
       
   301         const TQueryType mode = iQuery.Type();
       
   302         
       
   303         if ( mode == EQueryTypeObject )
       
   304             {
       
   305             CMdEObjectQueryImpl& qi = (CMdEObjectQueryImpl&)iQuery;
       
   306             qi.DoNotifyResultsL( iIdResultList );
       
   307             }
       
   308         else if ( mode == EQueryTypeRelation )
       
   309             {
       
   310             CMdERelationQueryImpl& qi = (CMdERelationQueryImpl&)iQuery;
       
   311             qi.DoNotifyResultsL( iIdResultList );
       
   312             }
       
   313         else if ( mode == EQueryTypeEvent )
       
   314             {
       
   315             CMdEEventQueryImpl& qi = (CMdEEventQueryImpl&)iQuery;
       
   316             qi.DoNotifyResultsL( iIdResultList );
       
   317             }
       
   318 
       
   319         // get rid of local results list
       
   320         iIdResultList.Reset();
       
   321         }
       
   322     else if ( iQuery.ResultMode() == EQueryResultModeCount )
       
   323         {
       
   324         const TQueryType mode = iQuery.Type();
       
   325         
       
   326         if ( mode == EQueryTypeObject )
       
   327             {
       
   328             CMdEObjectQueryImpl& qi = (CMdEObjectQueryImpl&)iQuery;
       
   329             qi.DoNotifyResults( iCountResult );
       
   330             }
       
   331         else if ( mode == EQueryTypeRelation )
       
   332             {
       
   333             CMdERelationQueryImpl& qi = (CMdERelationQueryImpl&)iQuery;
       
   334             qi.DoNotifyResults( iCountResult );
       
   335             }
       
   336         else if ( mode == EQueryTypeEvent )
       
   337             {
       
   338             CMdEEventQueryImpl& qi = (CMdEEventQueryImpl&)iQuery;
       
   339             qi.DoNotifyResults( iCountResult );
       
   340             }
       
   341         }
       
   342     else if ( ( aStatus == EAsyncFindSetReady ||
       
   343            aStatus == EAsyncFindComplete ) &&
       
   344            iDistinctResults && iQuery.ResultMode() == EQueryResultModeDistinctValues )
       
   345     	{
       
   346     	CMdEObjectQueryImpl& qi = (CMdEObjectQueryImpl&)iQuery;    	
       
   347     	qi.DoNotifyResultsL( *iDistinctResults );
       
   348 
       
   349     	delete iDistinctResults;
       
   350     	iDistinctResults = NULL;
       
   351     	}
       
   352     }
       
   353 
       
   354 void CMdEAsynchronousFindAO::DoNotifyL( TBool aNewResults, TInt aStatus )
       
   355 	{
       
   356 	if( aNewResults )
       
   357 		{
       
   358 		DoNotifyResultsL( aStatus );
       
   359 		}
       
   360 
       
   361     // Notify complete
       
   362     if ( aStatus != EAsyncFindSetReady )
       
   363         {
       
   364         const TInt code = ( aStatus == EAsyncFindComplete ? KErrNone : aStatus );
       
   365         
       
   366         const TQueryType mode = iQuery.Type();
       
   367 
       
   368         if ( mode == EQueryTypeObject ) 
       
   369             {
       
   370             CMdEObjectQueryImpl& qi = (CMdEObjectQueryImpl&)iQuery;
       
   371             qi.DoNotifyCompleted( code );
       
   372             }
       
   373         else if ( mode == EQueryTypeRelation )
       
   374             {
       
   375             CMdERelationQueryImpl& qi = (CMdERelationQueryImpl&)iQuery;
       
   376             qi.DoNotifyCompleted( code );
       
   377             }
       
   378         else if ( mode == EQueryTypeEvent )
       
   379             {
       
   380             CMdEEventQueryImpl& qi = (CMdEEventQueryImpl&)iQuery;
       
   381             qi.DoNotifyCompleted( code );
       
   382             }
       
   383         }
       
   384     
       
   385     if( aStatus == KErrServerTerminated )
       
   386     	{
       
   387     	iSessionImpl.NotifyError( aStatus );
       
   388     	}
       
   389     }
       
   390 
       
   391 
       
   392 void CMdEAsynchronousFindAO::DoCancel()
       
   393     {
       
   394     TRAP_IGNORE( iSession.DoFindCancelL( &iQuery ) );
       
   395     }
       
   396 
       
   397 // End of File