harvester/server/src/mdeobjecthandler.cpp
changeset 0 c53acadfccc6
child 15 3cebc1a84278
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2008-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:  
       
    15 */
       
    16 
       
    17 #include "mdeobjecthandler.h"
       
    18 #include "mdesession.h"
       
    19 #include "harvesterdata.h"
       
    20 #include "harvesterlog.h"
       
    21 #include "harvesterplugin.h"
       
    22 #include "mdeconstants.h"
       
    23 #include "mdsutils.h"
       
    24 #include <harvesterclientdata.h>
       
    25 
       
    26 using namespace MdeConstants;
       
    27 
       
    28 CMdeObjectHandler::CMdeObjectHandler( CMdESession& aSession ) : iDefaultNamespace( NULL )
       
    29     {
       
    30 	iMdeSession = &aSession;
       
    31     }
       
    32 
       
    33 
       
    34 CMdeObjectHandler::~CMdeObjectHandler()
       
    35     {
       
    36 	iFs.Close();
       
    37     }
       
    38 
       
    39 CMdeObjectHandler* CMdeObjectHandler::NewLC( CMdESession& aSession )
       
    40     {
       
    41 	CMdeObjectHandler* self = new (ELeave)CMdeObjectHandler( aSession );
       
    42 	CleanupStack::PushL(self);
       
    43 	self->ConstructL();
       
    44 	return self;
       
    45     }
       
    46 
       
    47 CMdeObjectHandler* CMdeObjectHandler::NewL( CMdESession& aSession )
       
    48     {
       
    49 	CMdeObjectHandler* self=CMdeObjectHandler::NewLC( aSession );
       
    50 	CleanupStack::Pop(); // self;
       
    51 	return self;
       
    52     }
       
    53 
       
    54 void CMdeObjectHandler::ConstructL()
       
    55     {
       
    56 	iDefaultNamespace = &iMdeSession->GetDefaultNamespaceDefL();
       
    57 	User::LeaveIfError( iFs.Connect() );
       
    58     }
       
    59 
       
    60 void CMdeObjectHandler::SetMetadataObjectL( CHarvesterData& aHD )
       
    61 	{
       
    62 	WRITELOG( "CMdeObjectHandler::SetMetadataObjectL()" );
       
    63     
       
    64     TTime time( 0 );
       
    65     time.UniversalTime();
       
    66     CMdEEventDef* eventDef = NULL;
       
    67     CMdEObject& mdeObject = aHD.MdeObject();
       
    68     
       
    69     const THarvesterEventType type( aHD.EventType() );
       
    70     if ( type == EHarvesterAdd )
       
    71         {
       
    72         if( aHD.IsBinary() && !MdsUtils::FileExists( iFs, mdeObject.Uri() )) 
       
    73         	{
       
    74             const TDesC16& uri = mdeObject.Uri();
       
    75             WRITELOG1( "CMdeObjectHandler::SetMetadataObjectL - File doesn't exist any more! Uri: %S", &uri );
       
    76             aHD.SetErrorCode( KMdEErrHarvestingFailedPermanent );
       
    77             return;
       
    78             }
       
    79         
       
    80         const TInt id( iMdeSession->AddObjectL( mdeObject ) );
       
    81         if( id == KNoId )
       
    82             {
       
    83             // If add failure occures, retry
       
    84             iMdeSession->AddObjectL( mdeObject );
       
    85             }
       
    86         
       
    87         if ( mdeObject.Id() == 0 )
       
    88             {
       
    89             const TDesC16& uri = mdeObject.Uri();
       
    90             WRITELOG1( "CMdeObjectHandler::SetMetadataObjectL - Adding to db failed! Uri: %S", &uri );
       
    91             aHD.SetErrorCode( KMdEErrHarvestingFailedPermanent );
       
    92             return;
       
    93             }
       
    94         
       
    95         // Since event was EHarvesterAdd
       
    96         eventDef = &iDefaultNamespace->GetEventDefL( Events::KCreated );
       
    97         }
       
    98     else if ( type== EHarvesterEdit )
       
    99         {    
       
   100         iMdeSession->CommitObjectL( mdeObject );
       
   101         eventDef = &iDefaultNamespace->GetEventDefL( Events::KEdited );        
       
   102         }    
       
   103 
       
   104     RPointerArray<CMdEInstanceItem> relationEventArray;
       
   105     TCleanupItem cleanupItem( MdsUtils::CleanupPtrArray<CMdEInstanceItem>, &relationEventArray );
       
   106     CleanupStack::PushL( cleanupItem );
       
   107 
       
   108     // If additional parameters are given, add relations between metadata objects and albums.
       
   109     if ( aHD.ClientData() )
       
   110         { 
       
   111         WRITELOG( "CMdeObjectHandler::SetMetadataObjectL() - Creating album relations." );
       
   112         
       
   113         CMdERelationDef& albumRelationDef = iDefaultNamespace->
       
   114         		GetRelationDefL( Relations::KContains );        
       
   115         
       
   116         TItemId objectId = mdeObject.Id();
       
   117         if ( objectId != 0 )
       
   118             {
       
   119             CHarvestClientData* clientData = STATIC_CAST( CHarvestClientData*, aHD.ClientData() );
       
   120             const TInt count = clientData->iAlbumIds.Count();
       
   121             for ( TInt i = 0 ; i < count; i++ )
       
   122                 {
       
   123                 TItemId albumId = clientData->iAlbumIds[i];
       
   124 
       
   125                 WRITELOG1( "CMdeObjectHandler::SetMetadataObjectL() - AlbumID: %d", albumId );
       
   126                 CMdERelation* relation =  
       
   127                		iMdeSession->NewRelationLC(
       
   128                 		albumRelationDef, albumId, objectId, 0 );
       
   129                 relationEventArray.Append( STATIC_CAST( CMdEInstanceItem*, relation ) );
       
   130                 CleanupStack::Pop();
       
   131                 }
       
   132             }
       
   133         }
       
   134     
       
   135     if ( eventDef )
       
   136         {
       
   137         WRITELOG( "CMdeObjectHandler::SetMetadataObjectL() - Creating event array" );
       
   138         const TItemId objectId = mdeObject.Id();
       
   139         if ( objectId != 0 )
       
   140             {
       
   141             CMdEEvent* event = iMdeSession->NewEventLC( 
       
   142             		*eventDef, objectId, time );
       
   143             relationEventArray.Append( STATIC_CAST( CMdEInstanceItem*, event ) );
       
   144             CleanupStack::Pop();
       
   145             }
       
   146         }
       
   147 
       
   148     if ( relationEventArray.Count() > 0 )
       
   149         {
       
   150 #ifdef _DEBUG
       
   151         WRITELOG1( "CMdeObjectHandler::SetMetadataObjectL() - Event and relation array count: %d", relationEventArray.Count() );
       
   152 #endif
       
   153         iMdeSession->AddItemsL( relationEventArray );
       
   154         }        
       
   155 
       
   156     CleanupStack::PopAndDestroy( &relationEventArray );
       
   157 	}
       
   158 
       
   159 // ---------------------------------------------------------------------------
       
   160 // GetMetadataObjectL
       
   161 // ---------------------------------------------------------------------------
       
   162 //
       
   163 CMdEObject* CMdeObjectHandler::GetMetadataObjectL( CHarvesterData& aHD, const TDesC& aObjectDefStr )
       
   164     {
       
   165     WRITELOG( "CMdeObjectHandler::GetMetadataObjectL()" );
       
   166     
       
   167     CMdEObjectDef& mdeObjectDef = 
       
   168     	iDefaultNamespace->GetObjectDefL( aObjectDefStr );
       
   169     	
       
   170     if ( aHD.EventType() == EHarvesterEdit )
       
   171         {
       
   172         if( aHD.IsBinary() )
       
   173         	{
       
   174         	// Checking whether file is open
       
   175         	TBool isOpen( EFalse );
       
   176         	TInt error = iFs.IsFileOpen( aHD.Uri(), isOpen );
       
   177             if ( error != KErrNone )
       
   178                 {
       
   179                 WRITELOG1( "CMdeObjectHandler::GetMetadataObjectL() - file error %d", error );
       
   180                 User::Leave( error );
       
   181                 }
       
   182         	if ( isOpen )
       
   183         		{
       
   184         		// The file can be open, as long as it is not open for modification anywhere
       
   185                 RFile64 tempFile;
       
   186                 error = tempFile.Open( iFs, aHD.Uri(), EFileRead | EFileShareReadersOnly );
       
   187                 if( error != KErrNone )
       
   188                     {
       
   189                     WRITELOG( "CMdeObjectHandler::GetMetadataObjectL() - file handle is open! Returning." );
       
   190                     return NULL;
       
   191                     }
       
   192                 tempFile.Close();
       
   193         		}
       
   194         	}
       
   195 
       
   196         WRITELOG( "CMdeObjectHandler::GetMetadataObjectL() - open object" );
       
   197         return iMdeSession->OpenObjectL( aHD.Uri(), mdeObjectDef );
       
   198         }
       
   199 
       
   200     WRITELOG( "CMdeObjectHandler::GetMetadataObjectL() - new object" );
       
   201     CMdEObject* mdeObject = iMdeSession->NewObjectL( mdeObjectDef, aHD.Uri() );
       
   202 
       
   203     return mdeObject;
       
   204     }