freMediaDrainController.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   F.R.E.E. - flexible registration evaluation engine
00004   Version:   v.1.0.0
00005   Date:      $Date: 2006/09/01 12:00:00 $
00006   Module:    $RCSfile: freMediaDrainController.cxx,v $
00007   Language:  C++
00008 
00009 
00010 
00011   Copyright (c) 2007 Ralf o Floca (Department of Medical Informatics,
00012   Institute for Medical Biometry and Informatics, University of Heidelberg,
00013   Germany). All rights reserved.
00014   See FREECopyright.txt or http://www.mi.med.uni-hd.de/free/copyright.htm
00015   for details.
00016 
00017      This software is distributed WITHOUT ANY WARRANTY; without even 
00018      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00019      PURPOSE.  See the above copyright notices for more information.
00020 
00021 =========================================================================*/
00022 
00023 #include "freMediaDrainController.h"
00024 #include "freStoreableMediaControllerBase.h"
00025 #include "freExceptions.h"
00026 
00027 namespace FREE
00028 {
00029 
00033 DefineParameterMacro(MediaDrainController,SaveAllMedia,"SaveAllMedia","Request this parameter to save all linked media that have changed. In the returning parameter every layer is associated to one stored media. Values are media ID and IDPath of the source.");
00034 
00035 
00036 void
00037 MediaDrainController::
00038 SetMediaCasted(const MediaID& mediaID, GenericMediaType* pMedia, ComponentType* pComponent,
00039                SessionComponentCache* pComponentCache,
00040                SessionInfo* pSessionInfo) const
00041 {
00042   if (!pComponentCache) throwCtrlExceptionMacro("","Passed component cache is NULL.");
00043   if (!pSessionInfo) throwCtrlExceptionMacro("","Passed session info is NULL.");
00044 
00045   SessionComponentCache::Pointer smpMediaCache = pComponentCache->SubCaches().GetElement(mediaID);
00046   if (smpMediaCache.IsNotNull())
00047   { //there is a cache with the same idea, so use the cached controller to save the passed media
00048     if (!smpMediaCache->IsActive()) throwCtrlExceptionMacro("","Cannot save media via drain. Responsible subcomponent is inactive. (ComponentID: "<<mediaID<<")");
00049 
00050     StatisticEntry* pMediaEntry = AddStatisticEntry(smpMediaCache,pSessionInfo,"MediaSave","Saving of a Media");
00051 
00052     try
00053     {
00054       DirectSessionComponentAccessor::SetComponent(pMedia, smpMediaCache);
00055       const GenericStoreableMediaController* pStorableInterface = dynamic_cast<const GenericStoreableMediaController*>(smpMediaCache->Controller());
00056       
00057       if (!pStorableInterface) throwCtrlExceptionMacro("","Cannot save media via drain. Media seems to be no descendant of StoreableMediaControllerInterface. ControllerID: "<<smpMediaCache->GetControllerID());
00058       
00059       pStorableInterface->SaveMedia(smpMediaCache);
00060 
00061       if (pMediaEntry)
00062       {
00063         smpMediaCache->Controller()->SetStatisticEntry(*pMediaEntry, smpMediaCache, pSessionInfo);
00064         pMediaEntry->CloseEntry();
00065       }
00066 
00067       //now set the component of the "drained" media controller to a dummy object
00068       //to free memory because the media should only be temporary
00069       GenericMediaType::Pointer smpDummyMedia = GenericMediaType::New();
00070       DirectSessionComponentAccessor::SetComponent(smpDummyMedia, smpMediaCache);
00071     }
00072     catchAllNPassMacro("Error while setting and saving media. Media ID: "<<mediaID);
00073 
00074     this->ActualizeMediaValidityTag(mediaID,pComponentCache);
00075 
00076     this->NotifyProgress(2,"Media stored. MediaID: "+mediaID,pComponentCache);
00077   }
00078   else
00079   {
00080     Superclass::SetMediaCasted(mediaID, pMedia, pComponent, pComponentCache, pSessionInfo);
00081   }
00082 }; 
00083 
00084 MediaDrainController::
00085 MediaDrainController()
00086 {
00087   this->UpdateControllerID(ControllerID::MediaDrainController);
00088   this->m_Description = "Media drain is a special version of the media bridge that is able to store media via their subcomponents.";
00089 };
00090 
00091 
00092 Parameter::Pointer
00093 MediaDrainController::
00094 GetParameter(const SessionComponentCache* pComponentCache,
00095              const std::string& sParameterName) const
00096 {
00097         if (sParameterName == cParam_SaveAllMedia)
00098   {
00099     Parameter::Pointer smpParameter = Parameter::New(cParam_SaveAllMedia,3);
00100 
00101 //TODO sollte geprüft werden ob nicht parameter mit allem was dranhängt auch SessionInfo übergeben bekommt, damit dieses GetParameter überhaupt möglich ist
00102     //entscheidung für die hinzuname der Session info, weil nicht klar ist, ob man vielleicht doch einmal allgemeine Session informationen für die GetMedia
00103         //etc braucht. Deshalb die schnittstelle möglichst frei halten.
00104     //this->SaveAllMedia(pComponentCache,pSessionInfo);
00105 
00106     return smpParameter;
00107   }
00108 
00109   return Superclass::GetParameter(pComponentCache, sParameterName);
00110 };
00111 
00112 bool
00113 MediaDrainController::
00114 SaveMedia(const MediaID& mediaID, SessionComponentCache* pComponentCache,
00115           SessionInfo* pSessionInfo) const
00116 {
00117   bool result = false;
00118 
00119   if (!pComponentCache) throwCtrlExceptionMacro("","Error; cannot save media. Passed component cache is NULL.");
00120   if (!pSessionInfo) throwCtrlExceptionMacro("","Error; cannot save media. Passed session info is NULL.");
00121 
00122   ComponentMediaLink::Pointer pLink = pComponentCache->Setup()->MediaLinks().GetElement(mediaID);
00123 
00124   if (pLink.IsNull())
00125   {
00126     this->NotifyProgress(0,"No media linked; skip saving. MediaID: "+mediaID,pComponentCache);
00127   }
00128   else
00129   {
00130     ValidityTag::Pointer smpVal = this->GetMediaValidityTag(mediaID,pComponentCache);
00131     ValidityTag::Pointer smpValReq = this->GetMediaValidityTagRequirement(mediaID,pComponentCache);
00132 
00133     if (smpVal->IsOutdated(smpValReq))
00134     {
00135       this->NotifyProgress(1,"Generating media. MediaID: "+mediaID,pComponentCache);
00136 
00137       IDPath mediaPath = pLink->GetSourceIDPath().TransformIDPath(pComponentCache->GetIDPath());
00138       GenericMediaType::Pointer smpMedia = SessionAccessor::GetMedia(mediaPath,pSessionInfo);
00139 
00140       this->SetMedia(mediaID,smpMedia,pComponentCache,pSessionInfo);
00141 
00142       result = true;
00143     }
00144     else
00145     {
00146       this->NotifyProgress(0,"Media is up to date. MediaID: "+mediaID,pComponentCache);
00147     }
00148   }
00149 
00150   return result;
00151 };
00152 
00153 
00154 STLStringVector
00155 MediaDrainController::
00156 SaveAllMedia(SessionComponentCache* pComponentCache, SessionInfo* pSessionInfo) const
00157 {
00158   if (!pComponentCache) throwCtrlExceptionMacro("","Error; cannot save media. Passed component cache is NULL.");
00159   if (!pSessionInfo) throwCtrlExceptionMacro("","Error; cannot save media. Passed session info is NULL.");
00160 
00161   STLStringVector savedMedia;
00162 
00163   for (unsigned int i=0; i < pComponentCache->Setup()->MediaLinks().Size(); i++)
00164   { //check all media and save if needed
00165     ComponentMediaLink::Pointer pLink = pComponentCache->Setup()->MediaLinks().GetElement(i);
00166 
00167     if (this->SaveMedia(pLink->GetMediaID(),pComponentCache,pSessionInfo))
00168     {
00169       savedMedia.push_back(pLink->GetMediaID());
00170     }
00171   }
00172 
00173   return savedMedia;
00174 };
00175 
00176 void
00177 MediaDrainController::
00178 GenerateProfile(CtrlProfile::ControllerProfile& profile,
00179                              const SessionComponentCache* pComponentCache,
00180                              bool bRegardOldSetup) const
00181 {
00182   //parameters
00183   profile.Parameters().AddParameter(cParam_SaveAllMedia,Parameter::PVTIDPath,cParamDsc_SaveAllMedia,3,"",-1,false,0,DAGet);
00184 
00185   //subcomponents = in this case the controller to save the media
00186   CtrlProfile::SubComponent* pSub = profile.SubComponents().AddSubComponent("MediaWriter",0);
00187   pSub->SetIsTemplate(true);
00188 
00189   //Requirements
00190   CtrlProfile::ProfileOption* pOption;
00191   pOption = profile.Requirements().AddRequirement("MediaWriter")->AddProfileOption();
00192   pOption->Inheritance().AddAncestor(ControllerID::StoreableMediaControllerBase);
00193   pOption->CheckForInheritance();
00194 
00195   if (bRegardOldSetup)
00196   {
00197     //search for every non templated subcomponent. They indicate a media link the drain
00198     //should offer if a controller is specified.
00199     for ( unsigned int i = 0; i < pComponentCache->Setup()->Components().Size(); i++)
00200     {
00201       ComponentSetup* pComponent = pComponentCache->Setup()->Components().GetElement(i);
00202       if ((!pComponent->IsTemplateBased())&&(pComponent->ControllerIsAssigned()))
00203       { //Add a media link profile because we have an assigned controller
00204               profile.MediaMap().AddMedia(pComponent->GetComponentID(),pComponent->GetControllerID(),DASet);
00205       }
00206     }
00207   }
00208 };
00209 
00210 ValidityTag::Pointer
00211 MediaDrainController::
00212 GetMediaValidityTag(const MediaID& mediaID, SessionComponentCache* pComponentCache) const
00213 {
00214   return Superclass::Superclass::GetMediaValidityTag(mediaID, pComponentCache);
00215 }; 
00216 
00217 } //end of namespace free

Generated at Sat Oct 13 16:39:32 2007 for f.r.e.e. - Flexible Registration and Evaluation Engine by doxygen 1.5.3 written by Dimitri van Heesch, © 1997-2000