]> SALOME platform Git repositories - modules/yacs.git/blob - src/SALOMEDS/SALOMEDS_Driver_i.cxx
Salome HOME
merge from BR_KERNEL_REFACTORING
[modules/yacs.git] / src / SALOMEDS / SALOMEDS_Driver_i.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "SALOMEDS_Driver_i.hxx"
24 #include "SALOMEDS_TMPFile_i.hxx"
25 #include "utilities.h"
26 #include "SALOMEDS_SObject_i.hxx"
27 #include "SALOMEDS_SComponent_i.hxx"
28 #include "SALOMEDS_Study_i.hxx"
29 #include "SALOMEDS.hxx"
30 #include <stdlib.h>
31
32 SALOMEDS_Driver_i::SALOMEDS_Driver_i(Engines::EngineComponent_ptr theEngine, CORBA::ORB_ptr theORB)
33 {
34   // engine should not be null - component is supposed to be inherited from Engines::EngineComponent
35   _engine = Engines::EngineComponent::_duplicate(theEngine);
36   // driver can be null - if component interface does not inherit SALOMEDS::Driver
37   _driver = SALOMEDS::Driver::_narrow(theEngine);
38   _orb = CORBA::ORB::_duplicate(theORB);
39 }
40
41 SALOMEDS_Driver_i::SALOMEDS_Driver_i(SALOMEDS::Driver_ptr theDriver, CORBA::ORB_ptr theORB)
42 {
43   // driver can be null - if component interface does not inherit SALOMEDS::Driver
44   _driver = SALOMEDS::Driver::_duplicate(theDriver);
45   // engine can be null - since it is narrowed from SALOMEDS::Driver ptr which can be null
46   _engine = Engines::EngineComponent::_narrow(theDriver);
47   _orb = CORBA::ORB::_duplicate(theORB);
48 }
49
50 SALOMEDS_Driver_i::~SALOMEDS_Driver_i()
51 {
52 }
53
54 std::string SALOMEDS_Driver_i::GetIOR()
55 {
56   std::string ior = "";
57   if ( !CORBA::is_nil(_engine) ) {
58     CORBA::String_var cior = _orb->object_to_string(_engine);
59     ior = cior;
60   }
61   return ior;
62 }
63
64 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::Save(const SALOMEDSImpl_SComponent& theComponent,
65                                               const std::string& theURL,
66                                               long& theStreamLength,
67                                               bool isMultiFile)
68 {
69   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
70   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
71
72   SALOMEDS::unlock();
73   SALOMEDS::TMPFile_var aStream;
74
75   if ( !CORBA::is_nil(_driver) )
76     aStream = _driver->Save(sco.in(), url, isMultiFile);
77
78   SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
79   theStreamLength = aTMPFile->Size();
80   SALOMEDS::lock();
81
82   return aTMPFile;
83 }
84
85 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::SaveASCII(const SALOMEDSImpl_SComponent& theComponent,
86                                                    const std::string& theURL,
87                                                    long& theStreamLength,
88                                                    bool isMultiFile)
89 {
90   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
91   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
92
93   SALOMEDS::unlock();
94   SALOMEDS::TMPFile_var aStream;
95
96   if ( !CORBA::is_nil(_driver) )
97     aStream = _driver->SaveASCII(sco.in(), url, isMultiFile);
98
99   SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
100   theStreamLength = aTMPFile->Size();
101   SALOMEDS::lock();
102
103   return aTMPFile;
104 }
105
106 bool SALOMEDS_Driver_i::Load(const SALOMEDSImpl_SComponent& theComponent,
107                              const unsigned char* theStream,
108                              const long theStreamLength,
109                              const std::string& theURL,
110                              bool isMultiFile)
111 {
112   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
113   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
114   CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
115
116   SALOMEDS::TMPFile_var aStream;
117   if (theStreamLength > 0)
118     aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
119   else
120     aStream = new SALOMEDS::TMPFile(0);
121
122   SALOMEDS::unlock();
123   bool isOk = false;
124
125   if ( !CORBA::is_nil(_driver) )
126     isOk = _driver->Load(sco.in(), aStream.in(), url, isMultiFile);
127
128   SALOMEDS::lock();
129   return isOk;
130 }
131
132 bool SALOMEDS_Driver_i::LoadASCII(const SALOMEDSImpl_SComponent& theComponent,
133                                   const unsigned char* theStream,
134                                   const long theStreamLength,
135                                   const std::string& theURL,
136                                   bool isMultiFile)
137 {
138   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
139   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
140   CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
141
142   SALOMEDS::TMPFile_var aStream;
143   if(theStreamLength > 0)
144     aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
145   else
146     aStream = new SALOMEDS::TMPFile(0);
147
148   SALOMEDS::unlock();
149   bool isOk = false;
150
151   if ( !CORBA::is_nil(_driver) )
152     isOk = _driver->LoadASCII(sco.in(), aStream.in(), url, isMultiFile);
153
154   SALOMEDS::lock();
155   return isOk;
156 }
157
158 void SALOMEDS_Driver_i::Close(const SALOMEDSImpl_SComponent& theComponent)
159 {
160   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
161
162   SALOMEDS::unlock();
163   if ( !CORBA::is_nil(_driver) )
164     _driver->Close(sco.in());
165   SALOMEDS::lock();
166 }
167
168 std::string SALOMEDS_Driver_i::ComponentDataType()
169 {
170   std::string dtype = "";
171   if ( !CORBA::is_nil(_driver) ) {
172     CORBA::String_var type = _driver->ComponentDataType();
173     dtype = type;
174   }
175   return dtype;
176 }
177
178 std::string SALOMEDS_Driver_i::Version()
179 {
180   return !CORBA::is_nil( _engine ) ? _engine->getVersion() : std::string("");
181 }
182
183 std::string SALOMEDS_Driver_i::IORToLocalPersistentID(const SALOMEDSImpl_SObject& theSObject,
184                                                       const std::string& IORString,
185                                                       bool isMultiFile,
186                                                       bool isASCII)
187 {
188   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theSObject, _orb);
189   CORBA::String_var ior = CORBA::string_dup(IORString.c_str());
190
191   SALOMEDS::unlock();
192
193   std::string pstr = "";
194
195   if ( !CORBA::is_nil(_driver) ) {
196     CORBA::String_var pers_string =_driver->IORToLocalPersistentID(so.in(), ior.in(), isMultiFile, isASCII);
197     pstr = pers_string;
198   }
199
200   SALOMEDS::lock();
201
202   return pstr;
203 }
204
205
206 std::string SALOMEDS_Driver_i::LocalPersistentIDToIOR(const SALOMEDSImpl_SObject& theObject,
207                                                       const std::string& aLocalPersistentID,
208                                                       bool isMultiFile,
209                                                       bool isASCII)
210 {
211   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
212   CORBA::String_var pers_string = CORBA::string_dup(aLocalPersistentID.c_str());
213   SALOMEDS::unlock();
214
215   std::string ior = "";
216
217   if ( !CORBA::is_nil(_driver) ) {
218     CORBA::String_var IOR = _driver->LocalPersistentIDToIOR(so.in(), pers_string.in(), isMultiFile, isASCII);
219     ior = IOR;
220   }
221
222   SALOMEDS::lock();
223   return ior;
224 }
225
226 bool SALOMEDS_Driver_i::CanCopy(const SALOMEDSImpl_SObject& theObject)
227 {
228   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
229
230   SALOMEDS::unlock();
231   bool isOk = false;
232
233   if ( !CORBA::is_nil(_driver) )
234     isOk = _driver->CanCopy(so.in());
235
236   so->UnRegister();
237   SALOMEDS::lock();
238
239   return isOk;
240 }
241
242
243 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::CopyFrom(const SALOMEDSImpl_SObject& theObject,
244                                                   int& theObjectID,
245                                                   long& theStreamLength)
246 {
247   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
248
249   SALOMEDS::unlock();
250   CORBA::Long anObjectID;
251   SALOMEDS::TMPFile_var aStream;
252
253   if ( !CORBA::is_nil(_driver) )
254     aStream = _driver->CopyFrom(so.in(), anObjectID);
255
256   SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
257   theStreamLength = aTMPFile->Size();
258   theObjectID = anObjectID;
259   SALOMEDS::lock();
260
261   return aTMPFile;
262 }
263
264 bool SALOMEDS_Driver_i::CanPaste(const std::string& theComponentName, int theObjectID)
265 {
266   SALOMEDS::unlock();
267   bool canPaste = false;
268
269   if ( !CORBA::is_nil(_driver) )
270     canPaste = _driver->CanPaste(theComponentName.c_str(), theObjectID);
271
272   SALOMEDS::lock();
273   return canPaste;
274 }
275
276 std::string SALOMEDS_Driver_i::PasteInto(const unsigned char* theStream,
277                                          const long theStreamLength,
278                                          int theObjectID,
279                                          const SALOMEDSImpl_SObject& theObject)
280 {
281   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
282   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)theStream;
283
284   SALOMEDS::TMPFile_var aStream;
285   if(theStreamLength > 0)
286     aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);
287   else
288     aStream = new SALOMEDS::TMPFile(0);
289
290   SALOMEDS::unlock();
291
292   std::string entry = "";
293
294   if ( !CORBA::is_nil(_driver) ) {
295     SALOMEDS::SObject_var ret_so = _driver->PasteInto(aStream.in(), theObjectID, so.in());
296     entry = ret_so->GetID();
297   }
298
299   SALOMEDS::lock();
300
301   return entry;
302 }
303
304 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::DumpPython(SALOMEDSImpl_Study* theStudy,
305                                                     bool isPublished,
306                                                     bool isMultiFile,
307                                                     bool& isValidScript,
308                                                     long& theStreamLength)
309 {
310   SALOMEDS_Study_i *  st_servant = SALOMEDS_Study_i::GetStudyServant(theStudy, _orb);//new SALOMEDS_Study_i (theStudy, _orb);
311   SALOMEDS::Study_var st  = SALOMEDS::Study::_narrow(st_servant->_this());
312
313   SALOMEDS::unlock();
314
315   Engines::TMPFile_var aStream;
316   CORBA::Boolean aValidScript = true; // VSR: maybe should be false by default ???
317
318   if ( !CORBA::is_nil( _engine ) )
319     aStream = _engine->DumpPython(st.in(), isPublished, isMultiFile, aValidScript);
320
321   SALOMEDSImpl_TMPFile* aTMPFile = new Engines_TMPFile_i(aStream._retn());
322   theStreamLength = aTMPFile->Size();
323   isValidScript = aValidScript;
324
325   SALOMEDS::lock();
326
327   return aTMPFile;
328 }
329
330 //###############################################################################################################
331 //                                          SALOMEDS_DriverFactory
332 //###############################################################################################################
333
334 SALOMEDS_DriverFactory_i::SALOMEDS_DriverFactory_i(CORBA::ORB_ptr theORB)
335 {
336   _orb = CORBA::ORB::_duplicate(theORB);
337   _name_service = new SALOME_NamingService(_orb);
338 }
339
340
341 SALOMEDS_DriverFactory_i::~SALOMEDS_DriverFactory_i()
342 {
343   delete _name_service;
344 }
345
346 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByType(const std::string& theComponentType)
347 {
348   CORBA::Object_var obj;
349
350   std::string aFactoryType;
351   if (theComponentType == "SUPERV") aFactoryType = "SuperVisionContainer";
352   else aFactoryType = "FactoryServer";
353
354   SALOMEDS::unlock();
355   obj = SALOME_LifeCycleCORBA(_name_service).FindOrLoad_Component(aFactoryType.c_str(), theComponentType.c_str());
356   SALOMEDS::lock();
357
358   if (CORBA::is_nil(obj)) {
359     obj = SALOME_LifeCycleCORBA(_name_service).FindOrLoad_Component("FactoryServer", theComponentType.c_str());
360   }
361
362   if (!CORBA::is_nil(obj)) {
363     Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
364     return new SALOMEDS_Driver_i(anEngine, _orb);
365   }
366
367   return NULL;
368 }
369
370 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByIOR(const std::string& theIOR)
371 {
372   CORBA::Object_var obj;
373   obj = _orb->string_to_object(theIOR.c_str());
374
375   if (!CORBA::is_nil(obj)) {
376     Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
377     return new SALOMEDS_Driver_i(anEngine, _orb);
378   }
379
380   return NULL;
381 }