// -----------------------------------------------------------------------------
// CMediatorServerCommandHandler::RegisterCommandListL
//  
// (other items were commented in a header).
// -----------------------------------------------------------------------------
//
void CMediatorServerCommandHandler::RegisterCommandListL( 
                TMediatorCategory aCategory, 
                const RCommandList& aCommands,
                TSecureId aSecureId,
                MMediatorCommandObserver* aObserver )
    {
    LOG(_L("[Mediator Server]\t CMediatorServerCommandHandler::RegisterCommandListL"));
    // Check that domain exists --> if not add new
    CDomain* domain = iObjectHandler.FindDomain( aCategory.iDomain );
    if ( !domain )
        {
        domain = iObjectHandler.AddDomainL( aCategory.iDomain );
        }
    
    // Check that category exists --> if not add new
    TInt ignore = 0; // not used here    
    CCategory* category = domain->FindCategory( aCategory.iCategory, ignore );
    if ( !category )
        {
        category = domain->AddCategoryL( aCategory.iCategory );
        }
    
    // Loop through the commands and add them to list
    // Take the possible error to variable
    TInt error = KErrNone;
    TBool stop = EFalse;
    TInt index = 0;
    for ( index = 0; index < aCommands.Count() && !stop; index++ )
        {
        CCommand* newCommand = CCommand::NewL( aCommands[index] );
        CleanupStack::PushL( newCommand );
        newCommand->SetSecureId( aSecureId );   // For unregistering
        newCommand->SetObserver( aObserver );   // For getting the commands
        newCommand->SetCommitState( CItem::EAdded ); // For transaction handling
        TInt addError = category->AddCommand( newCommand );
        if ( addError )
            {
            ERROR_TRACE(Print(_L("[Mediator] CMediatorServerCommandHandler::RegisterCommandListL: addError=%d\n"), addError ) );
            ERROR_TRACE(Print(_L("[Mediator] Failed to add command %d to category %d of domain %d\n"), newCommand->Id(), 
                                                                                                       aCategory.iCategory.iUid,
                                                                                                       aCategory.iDomain.iUid ) );
            // in case of error, delete event and take error
            CleanupStack::PopAndDestroy( newCommand );
            error = addError;
            stop = ETrue;
            }
        else
            {
            // Event has been added properly --> just pop
            CleanupStack::Pop( newCommand );
            }     
        newCommand = NULL;
        }  
    
    TRACE(Print(_L("[Mediator Server]\t Commands registered:\n")));
    TRACE(Print(_L("[Mediator Server]\t Success/count: %d/%d \tstatus: %d"), index, aCommands.Count(), error ));    
    
    // Check error if we need to do partial recovery
    if ( error != KErrNone ) 
        {
        // Remove the registered commands
        category->RollbackCommands();
        }
    else
        {
        // Complete command registration
        category->CommitCommands();
        
        // Use the object handler to notify command registration
        iObjectHandler.CommandsAdded( aCategory.iDomain, 
                                      aCategory.iCategory, 
                                      aCommands );
        }
    // In the end leave if error --> client gets error code
    User::LeaveIfError( error );
    }
// -----------------------------------------------------------------------------
// CMediatorServerCommandHandler::UnregisterCommandListL
//  
// (other items were commented in a header).
// -----------------------------------------------------------------------------
//
void CMediatorServerCommandHandler::UnregisterCommandListL( TMediatorCategory aCategory, 
                                                            const RCommandList& aCommands,
                                                            TSecureId aSecureId )
    {
    LOG(_L("[Mediator Server]\t CMediatorServerCommandHandler::UnregisterCommandListL"));
    CCategory* category = iObjectHandler.CategoryL( aCategory );
    TInt error = KErrNone;
    if ( category )
        {
        TBool stop = EFalse;
        TInt index = 0;
        
        // Loop through the list of commands and unregister those.
        for ( index = 0; index < aCommands.Count() && !stop; index++ )
            {
            TInt commandIndex = 0;
            TCommand removeCommand = aCommands[index];
            CCommand* commandPtr = category->FindCommand( removeCommand.iCommandId, 
                                                           commandIndex );
            if ( !commandPtr )
                {
                ERROR_LOG(_L("[Mediator] CMediatorServerCommandHandler::UnregisterCommandListL: Command not found\n") );
                ERROR_TRACE(Print(_L("[Mediator] Failed to remove command %d in category %d of domain %d\n"), removeCommand.iCommandId, 
                                                                                                       aCategory.iCategory.iUid,
                                                                                                       aCategory.iDomain.iUid ) );
                error = KErrMediatorCommandNotFound;
                stop = ETrue;
                }
            else
                {
                // Found the command --> is it own registration?
                if ( commandPtr->SecureId() != aSecureId )
                    {
                    ERROR_LOG(_L("[Mediator] CMediatorServerCommandHandler::UnregisterCommandListL: Secure ID mismatch\n") );
                    ERROR_TRACE(Print(_L("[Mediator] commandPtr()->SecureId()=0x%x, aSecureId=0x%x\n"), commandPtr->SecureId().iId, 
                                                                                                        aSecureId.iId ) );
                    error = KErrMediatorSecureIdMismatch;
                    stop = ETrue;
                    }
                else    // Should be ok to unregister
                    {
                    commandPtr->SetCommitState( CItem::ERemoved );
                    }
                }
            }
            
        TRACE(Print(_L("[Mediator Server]\t Commands unregistered:\n")));
        TRACE(Print(_L("[Mediator Server]\t Processed/Total: %d/%d \tstatus: %d"), index, aCommands.Count(), error ));    
        
        // Check error status --> if there's error, need to roll back
        if ( error != KErrNone )
            {
            category->RollbackCommands();    
            }
        else
            {
            category->CommitCommands();
            
            // loop through unregistered commands, and if they can be found from pending commands list,
            // inform the command issuer
            for ( TInt i = 0; i < aCommands.Count(); i++ )
                {
                // ignore return value
                IssueResponse( aCategory, aCommands[i], KNullDesC8, KErrMediatorCommandRemoved );
                }
            
            // Use the object handler to notify command registration
            iObjectHandler.CommandsRemoved( aCategory.iDomain, 
                                            aCategory.iCategory, 
                                            aCommands );
            }
        
        }
    // Leave in case of error situation
    User::LeaveIfError( error );
    }