Back to index

salome-kernel  6.5.0
SALOMEDS_Driver_i.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "SALOMEDS_Driver_i.hxx"
00024 #include "SALOMEDS_TMPFile_i.hxx"
00025 #include "utilities.h"
00026 #include "SALOMEDS_SObject_i.hxx"
00027 #include "SALOMEDS_SComponent_i.hxx"
00028 #include "SALOMEDS_Study_i.hxx"
00029 #include "SALOMEDS.hxx"
00030 #include <stdlib.h>
00031 
00032 SALOMEDS_Driver_i::SALOMEDS_Driver_i(SALOMEDS::Driver_ptr theDriver, CORBA::ORB_ptr theORB) 
00033 {
00034   _driver = SALOMEDS::Driver::_duplicate(theDriver);
00035   _orb = CORBA::ORB::_duplicate(theORB);        
00036 }
00037 
00038 SALOMEDS_Driver_i::~SALOMEDS_Driver_i()
00039 {
00040 }
00041 
00042 std::string SALOMEDS_Driver_i::GetIOR() 
00043 {
00044   std::string ior = "";
00045   if ( !CORBA::is_nil(_driver) ) {
00046     CORBA::String_var cior = _orb->object_to_string(_driver);
00047     ior = cior;
00048   }
00049   return ior;
00050 }
00051 
00052 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::Save(const SALOMEDSImpl_SComponent& theComponent,
00053                                          const std::string& theURL,
00054                                          long& theStreamLength,
00055                                          bool isMultiFile)
00056 {  
00057   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
00058   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
00059 
00060   SALOMEDS::unlock();
00061   SALOMEDS::TMPFile_var aStream;
00062 
00063   if ( !CORBA::is_nil(_driver) )
00064     aStream = _driver->Save(sco.in(), url, isMultiFile);
00065 
00066   SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
00067   theStreamLength = aTMPFile->Size();
00068   SALOMEDS::lock();
00069 
00070   return aTMPFile;
00071 }
00072 
00073 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::SaveASCII(const SALOMEDSImpl_SComponent& theComponent,
00074                                                    const std::string& theURL,
00075                                                    long& theStreamLength,
00076                                                    bool isMultiFile)
00077 {
00078   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
00079   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
00080 
00081   SALOMEDS::unlock();
00082   SALOMEDS::TMPFile_var aStream;
00083 
00084   if ( !CORBA::is_nil(_driver) )
00085     aStream = _driver->SaveASCII(sco.in(), url, isMultiFile);
00086 
00087   SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
00088   theStreamLength = aTMPFile->Size();
00089   SALOMEDS::lock();
00090 
00091   return aTMPFile;
00092 }
00093   
00094 bool SALOMEDS_Driver_i::Load(const SALOMEDSImpl_SComponent& theComponent,
00095                              const unsigned char* theStream,
00096                              const long theStreamLength,
00097                              const std::string& theURL,
00098                              bool isMultiFile)
00099 {
00100   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
00101   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
00102   CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
00103 
00104   SALOMEDS::TMPFile_var aStream;
00105   if (theStreamLength > 0) 
00106     aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);  
00107   else 
00108     aStream = new SALOMEDS::TMPFile(0);
00109 
00110   SALOMEDS::unlock();
00111   bool isOk = false;
00112 
00113   if ( !CORBA::is_nil(_driver) )
00114     isOk = _driver->Load(sco.in(), aStream.in(), url, isMultiFile);
00115 
00116   SALOMEDS::lock();
00117   return isOk;
00118 }
00119 
00120 bool SALOMEDS_Driver_i::LoadASCII(const SALOMEDSImpl_SComponent& theComponent,
00121                                   const unsigned char* theStream,
00122                                   const long theStreamLength,
00123                                   const std::string& theURL,
00124                                   bool isMultiFile)
00125 {
00126   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
00127   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
00128   CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
00129 
00130   SALOMEDS::TMPFile_var aStream;
00131   if(theStreamLength > 0) 
00132     aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);  
00133   else 
00134     aStream = new SALOMEDS::TMPFile(0);
00135 
00136   SALOMEDS::unlock();
00137   bool isOk = false;
00138 
00139   if ( !CORBA::is_nil(_driver) )
00140     isOk = _driver->LoadASCII(sco.in(), aStream.in(), url, isMultiFile);
00141 
00142   SALOMEDS::lock();
00143   return isOk;
00144 }
00145 
00146 void SALOMEDS_Driver_i::Close(const SALOMEDSImpl_SComponent& theComponent)
00147 {
00148   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
00149 
00150   SALOMEDS::unlock();
00151   if ( !CORBA::is_nil(_driver) )
00152     _driver->Close(sco.in());
00153   SALOMEDS::lock();
00154 }
00155 
00156 std::string SALOMEDS_Driver_i::ComponentDataType() 
00157 {
00158   std::string dtype = "";
00159   if ( !CORBA::is_nil(_driver) ) {
00160     CORBA::String_var type = _driver->ComponentDataType();
00161     dtype = type;
00162   }
00163   return dtype;
00164 }
00165 
00166 std::string SALOMEDS_Driver_i::IORToLocalPersistentID(const SALOMEDSImpl_SObject& theSObject,
00167                                                 const std::string& IORString,
00168                                                 bool isMultiFile,
00169                                                 bool isASCII)
00170 {
00171   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theSObject, _orb);
00172   CORBA::String_var ior = CORBA::string_dup(IORString.c_str());
00173 
00174   SALOMEDS::unlock();
00175 
00176   std::string pstr = "";
00177 
00178   if ( !CORBA::is_nil(_driver) ) {
00179     CORBA::String_var pers_string =_driver->IORToLocalPersistentID(so.in(), ior.in(), isMultiFile, isASCII);
00180     pstr = pers_string;
00181   }
00182 
00183   SALOMEDS::lock();
00184 
00185   return pstr;
00186 }
00187 
00188 
00189 std::string SALOMEDS_Driver_i::LocalPersistentIDToIOR(const SALOMEDSImpl_SObject& theObject,
00190                                                 const std::string& aLocalPersistentID,
00191                                                 bool isMultiFile,
00192                                                 bool isASCII)
00193 {
00194   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
00195   CORBA::String_var pers_string = CORBA::string_dup(aLocalPersistentID.c_str());
00196   SALOMEDS::unlock();
00197 
00198   std::string ior = "";
00199 
00200   if ( !CORBA::is_nil(_driver) ) {
00201     CORBA::String_var IOR = _driver->LocalPersistentIDToIOR(so.in(), pers_string.in(), isMultiFile, isASCII);
00202     ior = IOR;
00203   }
00204 
00205   SALOMEDS::lock();
00206   return ior;
00207 }
00208 
00209 bool SALOMEDS_Driver_i::CanCopy(const SALOMEDSImpl_SObject& theObject)
00210 {
00211   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
00212 
00213   SALOMEDS::unlock();
00214   bool isOk = false;
00215 
00216   if ( !CORBA::is_nil(_driver) )
00217     isOk = _driver->CanCopy(so.in());
00218 
00219   so->UnRegister();
00220   SALOMEDS::lock();
00221 
00222   return isOk;
00223 }
00224 
00225 
00226 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::CopyFrom(const SALOMEDSImpl_SObject& theObject, 
00227                                                   int& theObjectID,
00228                                                   long& theStreamLength)
00229 {
00230   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
00231 
00232   SALOMEDS::unlock();
00233   CORBA::Long anObjectID;
00234   SALOMEDS::TMPFile_var aStream;
00235 
00236   if ( !CORBA::is_nil(_driver) )
00237     aStream = _driver->CopyFrom(so.in(), anObjectID);
00238 
00239   SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
00240   theStreamLength = aTMPFile->Size();
00241   theObjectID = anObjectID;
00242   SALOMEDS::lock();
00243 
00244   return aTMPFile;
00245 }
00246 
00247 bool SALOMEDS_Driver_i::CanPaste(const std::string& theComponentName, int theObjectID)
00248 {
00249   SALOMEDS::unlock();
00250   bool canPaste = false;
00251 
00252   if ( !CORBA::is_nil(_driver) )
00253     canPaste = _driver->CanPaste(theComponentName.c_str(), theObjectID);
00254 
00255   SALOMEDS::lock();
00256   return canPaste;
00257 }
00258 
00259 std::string SALOMEDS_Driver_i::PasteInto(const unsigned char* theStream,
00260                                     const long theStreamLength,
00261                                     int theObjectID,
00262                                     const SALOMEDSImpl_SObject& theObject)
00263 {
00264   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
00265   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)theStream;
00266 
00267   SALOMEDS::TMPFile_var aStream;
00268   if(theStreamLength > 0) 
00269     aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);  
00270   else 
00271     aStream = new SALOMEDS::TMPFile(0);
00272 
00273   SALOMEDS::unlock();
00274 
00275   std::string entry = "";
00276 
00277   if ( !CORBA::is_nil(_driver) ) {
00278     SALOMEDS::SObject_var ret_so = _driver->PasteInto(aStream.in(), theObjectID, so.in());
00279     entry = ret_so->GetID();
00280   }
00281 
00282   SALOMEDS::lock();
00283 
00284   return entry;
00285 }
00286 
00287 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::DumpPython(SALOMEDSImpl_Study* theStudy, 
00288                                                     bool isPublished, 
00289                                                     bool isMultiFile,
00290                                                     bool& isValidScript,
00291                                                     long& theStreamLength)
00292 {
00293   SALOMEDS_Study_i *  st_servant = SALOMEDS_Study_i::GetStudyServant(theStudy, _orb);//new SALOMEDS_Study_i (theStudy, _orb);
00294   SALOMEDS::Study_var st  = SALOMEDS::Study::_narrow(st_servant->_this());
00295 
00296   SALOMEDS::unlock();
00297 
00298   Engines::TMPFile_var aStream;
00299   CORBA::Boolean aValidScript = true; // VSR: maybe should be false by default ???
00300 
00301   Engines::EngineComponent_ptr aComponent = Engines::EngineComponent::_narrow(_driver);
00302   if ( !CORBA::is_nil( aComponent ) )
00303     aStream = aComponent->DumpPython(st.in(), isPublished, isMultiFile, aValidScript);
00304 
00305   SALOMEDSImpl_TMPFile* aTMPFile = new Engines_TMPFile_i(aStream._retn());
00306   theStreamLength = aTMPFile->Size();
00307   isValidScript = aValidScript;
00308 
00309   SALOMEDS::lock();
00310 
00311   return aTMPFile;
00312 }
00313 
00314 //###############################################################################################################
00315 //                                          SALOMEDS_DriverFactory
00316 //###############################################################################################################
00317 
00318 SALOMEDS_DriverFactory_i::SALOMEDS_DriverFactory_i(CORBA::ORB_ptr theORB) 
00319 {
00320   _orb = CORBA::ORB::_duplicate(theORB);
00321   _name_service = new SALOME_NamingService(_orb);
00322 }
00323 
00324 
00325 SALOMEDS_DriverFactory_i::~SALOMEDS_DriverFactory_i() 
00326 {
00327   delete _name_service;
00328 }
00329 
00330 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByType(const std::string& theComponentType)
00331 {
00332   CORBA::Object_var obj;
00333 
00334   std::string aFactoryType;
00335   if (theComponentType == "SUPERV") aFactoryType = "SuperVisionContainer";
00336   else aFactoryType = "FactoryServer";
00337 
00338   SALOMEDS::unlock();
00339   obj = SALOME_LifeCycleCORBA(_name_service).FindOrLoad_Component(aFactoryType.c_str(), theComponentType.c_str());
00340   SALOMEDS::lock();
00341 
00342   if (CORBA::is_nil(obj)) {
00343     obj = SALOME_LifeCycleCORBA(_name_service).FindOrLoad_Component("FactoryServerPy", theComponentType.c_str());
00344   }
00345 
00346   if (!CORBA::is_nil(obj)) {
00347     SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(obj);
00348     return new SALOMEDS_Driver_i(aDriver, _orb);
00349   }
00350 
00351   return NULL;
00352 }
00353 
00354 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByIOR(const std::string& theIOR)
00355 {
00356   CORBA::Object_var obj;
00357   obj = _orb->string_to_object(theIOR.c_str());
00358  
00359   if (!CORBA::is_nil(obj)) {
00360     SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(obj);
00361     return new SALOMEDS_Driver_i(aDriver, _orb);
00362   }
00363 
00364   return NULL;
00365 }