Salome HOME
1) Add CMake files to the dist rules (make dist)
[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(SALOMEDS::Driver_ptr theDriver, CORBA::ORB_ptr theORB) 
33 {
34   _driver = SALOMEDS::Driver::_duplicate(theDriver);
35   _orb = CORBA::ORB::_duplicate(theORB);        
36 }
37
38 SALOMEDS_Driver_i::~SALOMEDS_Driver_i()
39 {
40 }
41
42 std::string SALOMEDS_Driver_i::GetIOR() 
43 {
44   std::string ior = "";
45   if ( !CORBA::is_nil(_driver) ) {
46     CORBA::String_var cior = _orb->object_to_string(_driver);
47     ior = cior;
48   }
49   return ior;
50 }
51
52 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::Save(const SALOMEDSImpl_SComponent& theComponent,
53                                               const std::string& theURL,
54                                               long& theStreamLength,
55                                               bool isMultiFile)
56 {  
57   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
58   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
59
60   SALOMEDS::unlock();
61   SALOMEDS::TMPFile_var aStream;
62
63   if ( !CORBA::is_nil(_driver) )
64     aStream = _driver->Save(sco.in(), url, isMultiFile);
65
66   SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
67   theStreamLength = aTMPFile->Size();
68   SALOMEDS::lock();
69
70   return aTMPFile;
71 }
72
73 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::SaveASCII(const SALOMEDSImpl_SComponent& theComponent,
74                                                    const std::string& theURL,
75                                                    long& theStreamLength,
76                                                    bool isMultiFile)
77 {
78   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
79   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
80
81   SALOMEDS::unlock();
82   SALOMEDS::TMPFile_var aStream;
83
84   if ( !CORBA::is_nil(_driver) )
85     aStream = _driver->SaveASCII(sco.in(), url, isMultiFile);
86
87   SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
88   theStreamLength = aTMPFile->Size();
89   SALOMEDS::lock();
90
91   return aTMPFile;
92 }
93   
94 bool SALOMEDS_Driver_i::Load(const SALOMEDSImpl_SComponent& theComponent,
95                              const unsigned char* theStream,
96                              const long theStreamLength,
97                              const std::string& theURL,
98                              bool isMultiFile)
99 {
100   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
101   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
102   CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
103
104   SALOMEDS::TMPFile_var aStream;
105   if (theStreamLength > 0) 
106     aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);  
107   else 
108     aStream = new SALOMEDS::TMPFile(0);
109
110   SALOMEDS::unlock();
111   bool isOk = false;
112
113   if ( !CORBA::is_nil(_driver) )
114     isOk = _driver->Load(sco.in(), aStream.in(), url, isMultiFile);
115
116   SALOMEDS::lock();
117   return isOk;
118 }
119
120 bool SALOMEDS_Driver_i::LoadASCII(const SALOMEDSImpl_SComponent& theComponent,
121                                   const unsigned char* theStream,
122                                   const long theStreamLength,
123                                   const std::string& theURL,
124                                   bool isMultiFile)
125 {
126   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
127   CORBA::String_var url = CORBA::string_dup(theURL.c_str());
128   CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
129
130   SALOMEDS::TMPFile_var aStream;
131   if(theStreamLength > 0) 
132     aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);  
133   else 
134     aStream = new SALOMEDS::TMPFile(0);
135
136   SALOMEDS::unlock();
137   bool isOk = false;
138
139   if ( !CORBA::is_nil(_driver) )
140     isOk = _driver->LoadASCII(sco.in(), aStream.in(), url, isMultiFile);
141
142   SALOMEDS::lock();
143   return isOk;
144 }
145
146 void SALOMEDS_Driver_i::Close(const SALOMEDSImpl_SComponent& theComponent)
147 {
148   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
149
150   SALOMEDS::unlock();
151   if ( !CORBA::is_nil(_driver) )
152     _driver->Close(sco.in());
153   SALOMEDS::lock();
154 }
155
156 std::string SALOMEDS_Driver_i::ComponentDataType() 
157 {
158   std::string dtype = "";
159   if ( !CORBA::is_nil(_driver) ) {
160     CORBA::String_var type = _driver->ComponentDataType();
161     dtype = type;
162   }
163   return dtype;
164 }
165
166 std::string SALOMEDS_Driver_i::IORToLocalPersistentID(const SALOMEDSImpl_SObject& theSObject,
167                                                       const std::string& IORString,
168                                                       bool isMultiFile,
169                                                       bool isASCII)
170 {
171   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theSObject, _orb);
172   CORBA::String_var ior = CORBA::string_dup(IORString.c_str());
173
174   SALOMEDS::unlock();
175
176   std::string pstr = "";
177
178   if ( !CORBA::is_nil(_driver) ) {
179     CORBA::String_var pers_string =_driver->IORToLocalPersistentID(so.in(), ior.in(), isMultiFile, isASCII);
180     pstr = pers_string;
181   }
182
183   SALOMEDS::lock();
184
185   return pstr;
186 }
187
188
189 std::string SALOMEDS_Driver_i::LocalPersistentIDToIOR(const SALOMEDSImpl_SObject& theObject,
190                                                       const std::string& aLocalPersistentID,
191                                                       bool isMultiFile,
192                                                       bool isASCII)
193 {
194   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
195   CORBA::String_var pers_string = CORBA::string_dup(aLocalPersistentID.c_str());
196   SALOMEDS::unlock();
197
198   std::string ior = "";
199
200   if ( !CORBA::is_nil(_driver) ) {
201     CORBA::String_var IOR = _driver->LocalPersistentIDToIOR(so.in(), pers_string.in(), isMultiFile, isASCII);
202     ior = IOR;
203   }
204
205   SALOMEDS::lock();
206   return ior;
207 }
208
209 bool SALOMEDS_Driver_i::CanCopy(const SALOMEDSImpl_SObject& theObject)
210 {
211   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
212
213   SALOMEDS::unlock();
214   bool isOk = false;
215
216   if ( !CORBA::is_nil(_driver) )
217     isOk = _driver->CanCopy(so.in());
218
219   so->UnRegister();
220   SALOMEDS::lock();
221
222   return isOk;
223 }
224
225
226 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::CopyFrom(const SALOMEDSImpl_SObject& theObject, 
227                                                   int& theObjectID,
228                                                   long& theStreamLength)
229 {
230   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
231
232   SALOMEDS::unlock();
233   CORBA::Long anObjectID;
234   SALOMEDS::TMPFile_var aStream;
235
236   if ( !CORBA::is_nil(_driver) )
237     aStream = _driver->CopyFrom(so.in(), anObjectID);
238
239   SALOMEDSImpl_TMPFile* aTMPFile = new SALOMEDS_TMPFile_i(aStream._retn());
240   theStreamLength = aTMPFile->Size();
241   theObjectID = anObjectID;
242   SALOMEDS::lock();
243
244   return aTMPFile;
245 }
246
247 bool SALOMEDS_Driver_i::CanPaste(const std::string& theComponentName, int theObjectID)
248 {
249   SALOMEDS::unlock();
250   bool canPaste = false;
251
252   if ( !CORBA::is_nil(_driver) )
253     canPaste = _driver->CanPaste(theComponentName.c_str(), theObjectID);
254
255   SALOMEDS::lock();
256   return canPaste;
257 }
258
259 std::string SALOMEDS_Driver_i::PasteInto(const unsigned char* theStream,
260                                          const long theStreamLength,
261                                          int theObjectID,
262                                          const SALOMEDSImpl_SObject& theObject)
263 {
264   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
265   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)theStream;
266
267   SALOMEDS::TMPFile_var aStream;
268   if(theStreamLength > 0) 
269     aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);  
270   else 
271     aStream = new SALOMEDS::TMPFile(0);
272
273   SALOMEDS::unlock();
274
275   std::string entry = "";
276
277   if ( !CORBA::is_nil(_driver) ) {
278     SALOMEDS::SObject_var ret_so = _driver->PasteInto(aStream.in(), theObjectID, so.in());
279     entry = ret_so->GetID();
280   }
281
282   SALOMEDS::lock();
283
284   return entry;
285 }
286
287 SALOMEDSImpl_TMPFile* SALOMEDS_Driver_i::DumpPython(SALOMEDSImpl_Study* theStudy, 
288                                                     bool isPublished, 
289                                                     bool isMultiFile,
290                                                     bool& isValidScript,
291                                                     long& theStreamLength)
292 {
293   SALOMEDS_Study_i *  st_servant = SALOMEDS_Study_i::GetStudyServant(theStudy, _orb);//new SALOMEDS_Study_i (theStudy, _orb);
294   SALOMEDS::Study_var st  = SALOMEDS::Study::_narrow(st_servant->_this());
295
296   SALOMEDS::unlock();
297
298   Engines::TMPFile_var aStream;
299   CORBA::Boolean aValidScript = true; // VSR: maybe should be false by default ???
300
301   Engines::EngineComponent_ptr aComponent = Engines::EngineComponent::_narrow(_driver);
302   if ( !CORBA::is_nil( aComponent ) )
303     aStream = aComponent->DumpPython(st.in(), isPublished, isMultiFile, aValidScript);
304
305   SALOMEDSImpl_TMPFile* aTMPFile = new Engines_TMPFile_i(aStream._retn());
306   theStreamLength = aTMPFile->Size();
307   isValidScript = aValidScript;
308
309   SALOMEDS::lock();
310
311   return aTMPFile;
312 }
313
314 //###############################################################################################################
315 //                                          SALOMEDS_DriverFactory
316 //###############################################################################################################
317
318 SALOMEDS_DriverFactory_i::SALOMEDS_DriverFactory_i(CORBA::ORB_ptr theORB) 
319 {
320   _orb = CORBA::ORB::_duplicate(theORB);
321   _name_service = new SALOME_NamingService(_orb);
322 }
323
324
325 SALOMEDS_DriverFactory_i::~SALOMEDS_DriverFactory_i() 
326 {
327   delete _name_service;
328 }
329
330 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByType(const std::string& theComponentType)
331 {
332   CORBA::Object_var obj;
333
334   std::string aFactoryType;
335   if (theComponentType == "SUPERV") aFactoryType = "SuperVisionContainer";
336   else aFactoryType = "FactoryServer";
337
338   SALOMEDS::unlock();
339   obj = SALOME_LifeCycleCORBA(_name_service).FindOrLoad_Component(aFactoryType.c_str(), theComponentType.c_str());
340   SALOMEDS::lock();
341
342   if (CORBA::is_nil(obj)) {
343     obj = SALOME_LifeCycleCORBA(_name_service).FindOrLoad_Component("FactoryServerPy", theComponentType.c_str());
344   }
345
346   if (!CORBA::is_nil(obj)) {
347     SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(obj);
348     return new SALOMEDS_Driver_i(aDriver, _orb);
349   }
350
351   return NULL;
352 }
353
354 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByIOR(const std::string& theIOR)
355 {
356   CORBA::Object_var obj;
357   obj = _orb->string_to_object(theIOR.c_str());
358  
359   if (!CORBA::is_nil(obj)) {
360     SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(obj);
361     return new SALOMEDS_Driver_i(aDriver, _orb);
362   }
363
364   return NULL;
365 }