Salome HOME
Fixed incorrect check in method DefineComponentInstance
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_Driver_i.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21
22 #include "SALOMEDS_Driver_i.hxx"
23 #include <SALOMEDS_TMPFile_i.hxx>
24 #include "utilities.h"
25 #include "SALOMEDS_SObject_i.hxx"
26 #include "SALOMEDS_SComponent_i.hxx"
27 #include "SALOMEDS_Study_i.hxx"
28 #include "SALOMEDS.hxx"
29 #include <stdlib.h>
30
31 using namespace std;  
32
33 SALOMEDS_Driver_i::~SALOMEDS_Driver_i()
34 {
35 }
36
37 Handle(SALOMEDSImpl_TMPFile) SALOMEDS_Driver_i::Save(const Handle(SALOMEDSImpl_SComponent)& theComponent,
38                                                      const TCollection_AsciiString& theURL,
39                                                      long& theStreamLength,
40                                                      bool isMultiFile)
41 {  
42   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
43   CORBA::String_var url = CORBA::string_dup(theURL.ToCString());
44
45   SALOMEDS::unlock();
46   SALOMEDS::TMPFile_var aStream = _driver->Save(sco.in(), url, isMultiFile);
47   Handle(SALOMEDSImpl_TMPFile) aTMPFile(new SALOMEDS_TMPFile_i(aStream._retn()));
48   theStreamLength = aTMPFile->Size();
49   SALOMEDS::lock();
50
51   return aTMPFile;
52 }
53
54 Handle(SALOMEDSImpl_TMPFile) SALOMEDS_Driver_i::SaveASCII(const Handle(SALOMEDSImpl_SComponent)& theComponent,
55                                                           const TCollection_AsciiString& theURL,
56                                                           long& theStreamLength,
57                                                           bool isMultiFile)
58 {
59   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
60   CORBA::String_var url = CORBA::string_dup(theURL.ToCString());
61
62   SALOMEDS::unlock();
63   SALOMEDS::TMPFile_var aStream = _driver->SaveASCII(sco.in(), url, isMultiFile);
64   Handle(SALOMEDSImpl_TMPFile) aTMPFile(new SALOMEDS_TMPFile_i(aStream._retn()));
65   theStreamLength = aTMPFile->Size();
66   SALOMEDS::lock();
67
68   return aTMPFile;
69 }
70   
71 bool SALOMEDS_Driver_i::Load(const Handle(SALOMEDSImpl_SComponent)& theComponent,
72                              const unsigned char* theStream,
73                              const long theStreamLength,
74                              const TCollection_AsciiString& theURL,
75                              bool isMultiFile)
76 {
77   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
78   CORBA::String_var url = CORBA::string_dup(theURL.ToCString());
79   CORBA::Octet* anOctetBuf = (CORBA::Octet*)theStream;
80
81   SALOMEDS::TMPFile_var aStream;
82   if (theStreamLength > 0) 
83     aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);  
84   else 
85     aStream = new SALOMEDS::TMPFile(0);
86
87   SALOMEDS::unlock();
88   bool isOk = _driver->Load(sco.in(), aStream.in(), url, isMultiFile);
89   SALOMEDS::lock();
90
91   return isOk;
92 }
93
94 bool SALOMEDS_Driver_i::LoadASCII(const Handle(SALOMEDSImpl_SComponent)& theComponent,
95                                   const unsigned char* theStream,
96                                   const long theStreamLength,
97                                   const TCollection_AsciiString& 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.ToCString());
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 = _driver->LoadASCII(sco.in(), aStream.in(), url, isMultiFile);
112   SALOMEDS::lock();
113
114   return isOk;
115 }
116
117 void SALOMEDS_Driver_i::Close(const Handle(SALOMEDSImpl_SComponent)& theComponent)
118 {
119   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (theComponent, _orb);
120
121   SALOMEDS::unlock();
122   _driver->Close(sco.in());
123   SALOMEDS::lock();
124 }
125
126
127
128 TCollection_AsciiString SALOMEDS_Driver_i::IORToLocalPersistentID(const Handle(SALOMEDSImpl_SObject)& theSObject,
129                                                                   const TCollection_AsciiString& IORString,
130                                                                   bool isMultiFile,
131                                                                   bool isASCII)
132 {
133   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theSObject, _orb);
134   CORBA::String_var ior = CORBA::string_dup(IORString.ToCString());
135
136   SALOMEDS::unlock();
137   CORBA::String_var pers_string =_driver->IORToLocalPersistentID(so.in(), ior.in(), isMultiFile, isASCII);
138   SALOMEDS::lock();
139
140   return TCollection_AsciiString(pers_string);
141 }
142
143
144 TCollection_AsciiString SALOMEDS_Driver_i::LocalPersistentIDToIOR(const Handle(SALOMEDSImpl_SObject)& theObject,
145                                                                   const TCollection_AsciiString& aLocalPersistentID,
146                                                                   bool isMultiFile,
147                                                                   bool isASCII)
148 {
149   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
150   CORBA::String_var pers_string = CORBA::string_dup(aLocalPersistentID.ToCString());
151   SALOMEDS::unlock();
152   CORBA::String_var IOR = _driver->LocalPersistentIDToIOR(so.in(), pers_string.in(), isMultiFile, isASCII);
153   SALOMEDS::lock();
154   return TCollection_AsciiString(IOR);
155 }
156
157 bool SALOMEDS_Driver_i::CanCopy(const Handle(SALOMEDSImpl_SObject)& theObject)
158 {
159   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
160
161   SALOMEDS::unlock();
162   bool isOk = _driver->CanCopy(so.in());
163   SALOMEDS::lock();
164
165   return isOk;
166 }
167
168
169 Handle(SALOMEDSImpl_TMPFile) SALOMEDS_Driver_i::CopyFrom(const Handle(SALOMEDSImpl_SObject)& theObject, 
170                                                          int& theObjectID,
171                                                          long& theStreamLength)
172 {
173   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
174
175   SALOMEDS::unlock();
176   CORBA::Long anObjectID;
177   SALOMEDS::TMPFile_var aStream = _driver->CopyFrom(so.in(), anObjectID);
178   Handle(SALOMEDSImpl_TMPFile) aTMPFile(new SALOMEDS_TMPFile_i(aStream._retn()));
179   theStreamLength = aTMPFile->Size();
180   theObjectID = anObjectID;
181   SALOMEDS::lock();
182
183   return aTMPFile;
184 }
185
186 bool SALOMEDS_Driver_i::CanPaste(const TCollection_AsciiString& theComponentName, int theObjectID)
187 {
188   SALOMEDS::unlock();
189   bool canPaste = _driver->CanPaste(theComponentName.ToCString(), theObjectID);
190   SALOMEDS::lock();
191   return canPaste;
192 }
193
194 TCollection_AsciiString SALOMEDS_Driver_i::PasteInto(const unsigned char* theStream,
195                                                      const long theStreamLength,
196                                                      int theObjectID,
197                                                      const Handle(SALOMEDSImpl_SObject)& theObject)
198 {
199   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (theObject, _orb);
200   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)theStream;
201
202   SALOMEDS::TMPFile_var aStream;
203   if(theStreamLength > 0) 
204     aStream = new SALOMEDS::TMPFile(theStreamLength, theStreamLength, anOctetBuf, 0);  
205   else 
206     aStream = new SALOMEDS::TMPFile(0);
207
208   SALOMEDS::unlock();
209   SALOMEDS::SObject_var ret_so = _driver->PasteInto(aStream.in(), theObjectID, so.in());
210   SALOMEDS::lock();
211
212   return TCollection_AsciiString((char*)ret_so->GetID());
213 }
214
215 Handle(SALOMEDSImpl_TMPFile) SALOMEDS_Driver_i::DumpPython(const Handle(SALOMEDSImpl_Study)& theStudy, 
216                                                            bool isPublished, 
217                                                            bool& isValidScript,
218                                                            long& theStreamLength)
219 {
220   SALOMEDS_Study_i *  st_servant = new SALOMEDS_Study_i (theStudy, _orb);
221   SALOMEDS::Study_var st  = SALOMEDS::Study::_narrow(st_servant->_this());
222   Engines::Component_ptr aComponent = Engines::Component::_narrow(_driver);
223
224   SALOMEDS::unlock();
225   CORBA::Boolean aValidScript, aPublished = isPublished;
226   Engines::TMPFile_var aStream = aComponent->DumpPython(st.in(), aPublished, aValidScript);
227   Handle(SALOMEDSImpl_TMPFile) aTMPFile(new Engines_TMPFile_i(aStream._retn()));
228   theStreamLength = aTMPFile->Size();
229   isValidScript = aValidScript;
230   SALOMEDS::lock();
231
232   return aTMPFile;
233 }
234
235 //###############################################################################################################
236 //                                          SALOMEDS_DriverFactory
237 //###############################################################################################################
238
239 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByType
240                      (const TCollection_AsciiString& theComponentType)
241 {
242   CORBA::Object_var obj;
243
244   TCollection_AsciiString aFactoryType;
245   if (theComponentType == "SUPERV") aFactoryType = "SuperVisionContainer";
246   else aFactoryType = "FactoryServer";
247
248   SALOMEDS::unlock();
249   obj = SALOME_LifeCycleCORBA(_name_service).FindOrLoad_Component
250     (aFactoryType.ToCString(), theComponentType.ToCString());
251   SALOMEDS::lock();
252
253   if (CORBA::is_nil(obj)) {
254     obj = SALOME_LifeCycleCORBA(_name_service).FindOrLoad_Component
255       ("FactoryServerPy", theComponentType.ToCString());
256   }
257
258   if (!CORBA::is_nil(obj)) {
259     SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(obj);
260     return new SALOMEDS_Driver_i(aDriver, _orb);
261   }
262
263   return NULL;
264 }
265
266 SALOMEDSImpl_Driver* SALOMEDS_DriverFactory_i::GetDriverByIOR(const TCollection_AsciiString& theIOR)
267 {
268   CORBA::Object_var obj;
269   obj = _orb->string_to_object(theIOR.ToCString());
270  
271   if (!CORBA::is_nil(obj)) {
272     SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(obj);
273     return new SALOMEDS_Driver_i(aDriver, _orb);
274   }
275
276   return NULL;
277 }