92321d05ee8a85c9ca7ecab3b7e1f3ffd57090b8
[occt.git] / src / CDF / CDF_Application.cxx
1 // Created on: 1997-08-08
2 // Created by: Jean-Louis Frenkel
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 // Modified by rmi, Wed Jan 14 08:17:35 1998
18
19
20 #include <CDF_Application.ixx>
21 #include <Standard_ErrorHandler.hxx>
22 #include <TCollection_ExtendedString.hxx>
23 #include <Standard_ProgramError.hxx>
24 #include <Standard_GUID.hxx>
25 #include <CDM_MetaData.hxx>
26 #include <CDM_CanCloseStatus.hxx>
27 #include <CDF_Session.hxx>
28 #include <CDF_Directory.hxx>
29 #include <CDF_MetaDataDriver.hxx>
30 #include <PCDM_ReaderStatus.hxx>
31 #include <PCDM_ReadWriter.hxx>
32 #include <PCDM_RetrievalDriver.hxx>
33 #include <PCDM_StorageDriver.hxx>
34
35
36 #include <Plugin.hxx>
37 #include <UTL.hxx>
38
39 #include <CDF_Timer.hxx>
40
41 #define theMetaDataDriver CDF_Session::CurrentSession()->MetaDataDriver()
42
43
44 //=======================================================================
45 //function : 
46 //purpose  : 
47 //=======================================================================
48 CDF_Application::CDF_Application():myRetrievableStatus(PCDM_RS_OK) {}
49
50 //=======================================================================
51 //function : Load
52 //purpose  : 
53 //=======================================================================
54 Handle(CDF_Application) CDF_Application::Load(const Standard_GUID& aGUID) {
55   return Handle(CDF_Application)::DownCast(Plugin::Load(aGUID));
56 }
57
58 //=======================================================================
59 //function : Open
60 //purpose  : 
61 //=======================================================================
62 void CDF_Application::Open(const Handle(CDM_Document)& aDocument) {
63   CDF_Session::CurrentSession()->Directory()->Add(aDocument);
64   aDocument->Open(this);
65   Activate(aDocument,CDF_TOA_New);
66 }
67
68 //=======================================================================
69 //function : CanClose
70 //purpose  : 
71 //=======================================================================
72 CDM_CanCloseStatus CDF_Application::CanClose(const Handle(CDM_Document)& aDocument) {
73   return aDocument->CanClose();
74 }
75
76 //=======================================================================
77 //function : Close
78 //purpose  : 
79 //=======================================================================
80 void CDF_Application::Close(const Handle(CDM_Document)& aDocument) {
81   CDF_Session::CurrentSession()->Directory()->Remove(aDocument);
82   aDocument->Close();
83 }
84
85 //=======================================================================
86 //function : Retrieve
87 //purpose  : 
88 //=======================================================================
89 Handle(CDM_Document) CDF_Application::Retrieve(const TCollection_ExtendedString& aFolder, 
90                                      const TCollection_ExtendedString& aName, 
91                                      const Standard_Boolean UseStorageConfiguration) {
92   TCollection_ExtendedString nullVersion;
93   return Retrieve(aFolder,aName,nullVersion,UseStorageConfiguration);
94 }
95
96 //=======================================================================
97 //function : Retrieve
98 //purpose  : 
99 //=======================================================================
100 Handle(CDM_Document)  CDF_Application::Retrieve(const TCollection_ExtendedString& aFolder, 
101                                      const TCollection_ExtendedString& aName,
102                                      const TCollection_ExtendedString& aVersion,
103                                      const Standard_Boolean UseStorageConfiguration) {
104 #ifdef DEB
105   CDF_Timer theTimer;
106 #endif
107
108   Handle(CDM_MetaData) theMetaData; 
109   
110   if(aVersion.Length() == 0) 
111     theMetaData=theMetaDataDriver->MetaData(aFolder,aName);
112   else 
113     theMetaData=theMetaDataDriver->MetaData(aFolder,aName,aVersion);
114
115 #ifdef DEB  
116   theTimer.ShowAndRestart("Getting MetaData: ");
117 #endif
118
119   CDF_TypeOfActivation theTypeOfActivation=TypeOfActivation(theMetaData);
120   Handle(CDM_Document) theDocument=Retrieve(theMetaData,UseStorageConfiguration,Standard_False);
121
122 #ifdef DEB
123   theTimer.ShowAndRestart("Creating Transient: ");
124 #endif
125
126   CDF_Session::CurrentSession()->Directory()->Add(theDocument);
127   Activate(theDocument,theTypeOfActivation);
128
129 #ifdef DEB
130   theTimer.ShowAndStop("Activate: ");
131 #endif
132
133   theDocument->Open(this);
134   return theDocument;
135
136
137 }
138
139 //=======================================================================
140 //function : CanRetrieve
141 //purpose  : 
142 //=======================================================================
143 PCDM_ReaderStatus CDF_Application::CanRetrieve(const TCollection_ExtendedString& aFolder, const TCollection_ExtendedString&  aName) {
144  TCollection_ExtendedString aVersion;
145  return CanRetrieve(aFolder,aName,aVersion);
146 }
147
148 //=======================================================================
149 //function : CanRetrieve
150 //purpose  : 
151 //=======================================================================
152 PCDM_ReaderStatus CDF_Application::CanRetrieve(const TCollection_ExtendedString&  aFolder, const TCollection_ExtendedString&  aName, const TCollection_ExtendedString&  aVersion) {
153   
154 #ifdef DEB
155   CDF_Timer theTimer;
156 #endif
157
158   if (!theMetaDataDriver->Find(aFolder,aName,aVersion))
159     return PCDM_RS_UnknownDocument;
160   else if (!theMetaDataDriver->HasReadPermission(aFolder,aName,aVersion))
161     return PCDM_RS_PermissionDenied;
162   else {
163 #ifdef DEB
164     theTimer.ShowAndRestart("theMetaDataDriver->Find: ");
165 #endif
166
167     Handle(CDM_MetaData) theMetaData = theMetaDataDriver->MetaData(aFolder,aName,aVersion);
168
169 #ifdef DEB
170     theTimer.ShowAndStop("Getting MetaData: ");
171 #endif
172
173     if(theMetaData->IsRetrieved()) {
174       return theMetaData->Document()->IsModified()
175         ? PCDM_RS_AlreadyRetrievedAndModified : PCDM_RS_AlreadyRetrieved;
176     }
177     else {
178       TCollection_ExtendedString theFileName=theMetaData->FileName();
179       TCollection_ExtendedString theFormat=PCDM_ReadWriter::FileFormat(theFileName);
180       if(theFormat.Length()==0) {
181         TCollection_ExtendedString ResourceName=UTL::Extension(theFileName);
182         ResourceName+=".FileFormat";
183         if(UTL::Find(Resources(),ResourceName))  {
184           theFormat=UTL::Value(Resources(),ResourceName);
185         }
186         else
187           return PCDM_RS_UnrecognizedFileFormat;
188       }
189       if(!FindReaderFromFormat(theFormat)) return PCDM_RS_NoDriver;
190     }
191   }
192   return PCDM_RS_OK;
193   
194 }
195
196
197
198 //=======================================================================
199 //function : Activate
200 //purpose  : 
201 //=======================================================================
202 //void CDF_Application::Activate(const Handle(CDM_Document)& aDocument,const CDF_TypeOfActivation aTypeOfActivation) {
203 void CDF_Application::Activate(const Handle(CDM_Document)& ,const CDF_TypeOfActivation ) {
204 }
205
206 //=======================================================================
207 //function : DefaultFolder
208 //purpose  : 
209 //=======================================================================
210 Standard_ExtString CDF_Application::DefaultFolder(){
211   if(myDefaultFolder.Length() == 0) {
212     myDefaultFolder=CDF_Session::CurrentSession()->MetaDataDriver()->DefaultFolder();
213   }
214   return myDefaultFolder.ToExtString();
215 }
216
217 //=======================================================================
218 //function : SetDefaultFolder
219 //purpose  : 
220 //=======================================================================
221 Standard_Boolean CDF_Application::SetDefaultFolder(const Standard_ExtString aFolder) {
222   Standard_Boolean found = CDF_Session::CurrentSession()->MetaDataDriver()->FindFolder(aFolder);
223   if(found) myDefaultFolder=aFolder;
224   return found;
225 }
226
227 //=======================================================================
228 //function : DefaultExtension
229 //purpose  : 
230 //=======================================================================
231 Standard_ExtString CDF_Application::DefaultExtension() {
232   static TCollection_ExtendedString theDefaultExtension;
233   theDefaultExtension="*";
234   TColStd_SequenceOfExtendedString theFormats;
235   Formats(theFormats);
236   
237   for (Standard_Integer i=1; i<= theFormats.Length(); i++) {
238     TCollection_ExtendedString theResource(theFormats(i));
239     theResource+=".FileExtension";
240     if(UTL::Find(Resources(),theResource)) {
241       theDefaultExtension=UTL::Value(Resources(),theResource);
242       return theDefaultExtension.ToExtString();
243     }
244   }
245   return theDefaultExtension.ToExtString();
246 }
247
248 //=======================================================================
249 //function : Retrieve
250 //purpose  : 
251 //=======================================================================
252 Handle(CDM_Document) CDF_Application::Retrieve(const Handle(CDM_MetaData)& aMetaData,const Standard_Boolean UseStorageConfiguration) {
253   return Retrieve(aMetaData,UseStorageConfiguration,Standard_True);
254
255
256 //=======================================================================
257 //function : Retrieve
258 //purpose  : 
259 //=======================================================================
260 Handle(CDM_Document) CDF_Application::Retrieve(const Handle(CDM_MetaData)& aMetaData,const Standard_Boolean UseStorageConfiguration, const Standard_Boolean IsComponent) {
261   
262   Handle(CDM_Document) theDocumentToReturn;
263   myRetrievableStatus = PCDM_RS_DriverFailure;
264   if(IsComponent) {
265     Standard_SStream aMsg;
266     switch (CanRetrieve(aMetaData)) {
267     case PCDM_RS_UnknownDocument: 
268       aMsg << "could not find the referenced document: " << aMetaData->Path() << "; not found."  <<(char)0 << endl;
269       myRetrievableStatus = PCDM_RS_UnknownDocument;
270       Standard_Failure::Raise(aMsg);
271       break;
272     case PCDM_RS_PermissionDenied:      
273       aMsg << "Could not find the referenced document: " << aMetaData->Path() << "; permission denied. " <<(char)0 << endl;
274       myRetrievableStatus = PCDM_RS_PermissionDenied;
275       Standard_Failure::Raise(aMsg);
276       break;
277     default:
278       break;
279     }
280     
281   }
282   Standard_Boolean AlreadyRetrieved=aMetaData->IsRetrieved();
283   if(AlreadyRetrieved) myRetrievableStatus = PCDM_RS_AlreadyRetrieved;
284   Standard_Boolean Modified=AlreadyRetrieved && aMetaData->Document()->IsModified();
285   if(Modified) myRetrievableStatus = PCDM_RS_AlreadyRetrievedAndModified;
286   if(!AlreadyRetrieved || Modified) {
287
288     Handle(PCDM_Reader) theReader=Reader(aMetaData->FileName());
289     
290     
291     Handle(CDM_Document) theDocument;
292
293     if(Modified)  {
294       theDocument=aMetaData->Document();
295       theDocument->RemoveAllReferences();
296     }
297     else
298       theDocument=theReader->CreateDocument();
299     
300     SetReferenceCounter(theDocument,PCDM_RetrievalDriver::ReferenceCounter(aMetaData->FileName(), MessageDriver()));
301     
302     SetDocumentVersion(theDocument,aMetaData);
303     theMetaDataDriver->ReferenceIterator()->LoadReferences(theDocument,aMetaData,this,UseStorageConfiguration);
304
305     try {    
306       OCC_CATCH_SIGNALS
307       theReader->Read(aMetaData->FileName(),theDocument,this);
308     } 
309     catch (Standard_Failure) {
310       myRetrievableStatus = theReader->GetStatus();
311       if(myRetrievableStatus  > PCDM_RS_AlreadyRetrieved){
312         Standard_SStream aMsg;
313         aMsg << Standard_Failure::Caught() << endl;
314         Standard_Failure::Raise(aMsg);
315       } 
316     }
317     myRetrievableStatus = theReader->GetStatus();    
318     theDocument->SetMetaData(aMetaData);
319
320     theDocumentToReturn=theDocument;
321   }
322   else
323     theDocumentToReturn=aMetaData->Document();
324   
325   return theDocumentToReturn;
326 }
327
328 //=======================================================================
329 //function : DocumentVersion
330 //purpose  : 
331 //=======================================================================
332 Standard_Integer CDF_Application::DocumentVersion(const Handle(CDM_MetaData)& theMetaData) {
333 //  const Handle(CDM_MessageDriver)& aMsgDriver = MessageDriver();
334   return PCDM_RetrievalDriver::DocumentVersion(theMetaData->FileName(), MessageDriver());
335 }
336
337 //=======================================================================
338 //function : TypeOfActivation
339 //purpose  : 
340 //=======================================================================
341 CDF_TypeOfActivation CDF_Application::TypeOfActivation(const Handle(CDM_MetaData)& aMetaData) {
342
343   if(aMetaData->IsRetrieved()) {
344     Handle(CDM_Document) theDocument=aMetaData->Document();
345     if(theDocument->IsOpened()) {
346       if(theDocument->IsModified())
347         return CDF_TOA_Modified;
348       else
349         return CDF_TOA_Unchanged;
350     }
351     
352     else
353       return CDF_TOA_New;
354   }
355   return CDF_TOA_New;
356 }
357
358
359 //=======================================================================
360 //function : FindReader
361 //purpose  : 
362 //=======================================================================
363 Standard_Boolean CDF_Application::FindReader(const TCollection_ExtendedString& aFileName) {
364   Standard_GUID voidGUID;
365   TCollection_ExtendedString voidResourceName;
366   return FindReader(aFileName,voidGUID,voidResourceName);
367 }
368
369 //=======================================================================
370 //function : Reader
371 //purpose  : code dp
372 //=======================================================================
373 Handle(PCDM_Reader) CDF_Application::Reader (const TCollection_ExtendedString& aFileName) {
374   TCollection_ExtendedString theFormat;
375   if (!Format(aFileName,theFormat)) {
376     Standard_SStream aMsg; 
377     aMsg << "Could not found the format" <<(char)0;
378     Standard_NoSuchObject::Raise(aMsg);
379
380   }
381   return ReaderFromFormat (theFormat);
382 }
383
384 //=======================================================================
385 //function : FindReaderFromFormat
386 //purpose  : 
387 //=======================================================================
388 Standard_Boolean CDF_Application::FindReaderFromFormat(const TCollection_ExtendedString& aFormat) {
389   Standard_GUID voidGUID;
390   TCollection_ExtendedString voidResourceName;
391   return FindReaderFromFormat(aFormat,voidGUID,voidResourceName);
392 }
393
394
395 //=======================================================================
396 //function : ReaderFromFormat
397 //purpose  : 
398 //=======================================================================
399 Handle(PCDM_Reader) CDF_Application::ReaderFromFormat(const TCollection_ExtendedString& aFormat) {
400   TCollection_ExtendedString UnfoundResourceName;
401   Standard_GUID thePluginId;
402   if(!FindReaderFromFormat(aFormat,thePluginId,UnfoundResourceName)) {
403     Standard_SStream aMsg; 
404     aMsg << "Could not found the item:" << UnfoundResourceName <<(char)0;
405     myRetrievableStatus = PCDM_RS_WrongResource;
406     Standard_NoSuchObject::Raise(aMsg);
407   } 
408   Handle(PCDM_Reader) R;
409   try {
410     OCC_CATCH_SIGNALS
411     R = Handle(PCDM_Reader)::DownCast(Plugin::Load(thePluginId));  
412   } 
413   catch (Standard_Failure) {
414     myRetrievableStatus = PCDM_RS_WrongResource;
415     Standard_SStream aMsg;
416     aMsg << Standard_Failure::Caught() << endl;
417     Standard_Failure::Raise(aMsg);
418   }     
419   Handle(PCDM_RetrievalDriver) RD = Handle(PCDM_RetrievalDriver)::DownCast(R);
420   if (!RD.IsNull()) {
421     RD->SetFormat(aFormat);
422     return RD;
423   } else 
424     myRetrievableStatus = PCDM_RS_WrongResource;
425   return R;
426 }
427
428 //=======================================================================
429 //function : FindReader
430 //purpose  : 
431 //=======================================================================
432 Standard_Boolean CDF_Application::FindReader(const TCollection_ExtendedString& aFileName, Standard_GUID& thePluginId, TCollection_ExtendedString& ResourceName) {
433   
434   TCollection_ExtendedString theFormat=PCDM_ReadWriter::FileFormat(aFileName);
435
436 // It is good if the format is in the file. Otherwise base on the extension.
437   
438   if(theFormat.Length()==0) {
439     ResourceName=UTL::Extension(aFileName);
440     ResourceName+=".FileFormat";
441     
442     if(UTL::Find(Resources(),ResourceName))  {
443       theFormat=UTL::Value(Resources(),ResourceName);
444     }
445     else 
446       return Standard_False;
447   }
448   return FindReaderFromFormat(theFormat,thePluginId,ResourceName);
449 }
450
451 //=======================================================================
452 //function : Format
453 //purpose  : dp 
454 //=======================================================================
455 Standard_Boolean CDF_Application::Format(const TCollection_ExtendedString& aFileName, 
456                                          TCollection_ExtendedString& theFormat)
457 {
458   
459   theFormat = PCDM_ReadWriter::FileFormat(aFileName);
460 // It is good if the format is in the file. Otherwise base on the extension.
461   if(theFormat.Length()==0) {
462     TCollection_ExtendedString ResourceName;
463     ResourceName=UTL::Extension(aFileName);
464     ResourceName+=".FileFormat";
465     
466     if(UTL::Find(Resources(),ResourceName))  {
467       theFormat = UTL::Value(Resources(),ResourceName);
468     }
469     else return Standard_False;
470   }
471   return Standard_True;
472 }
473
474 //=======================================================================
475 //function : FindReaderFromFormat
476 //purpose  : 
477 //=======================================================================
478 Standard_Boolean CDF_Application::FindReaderFromFormat(const TCollection_ExtendedString& aFormat, Standard_GUID& thePluginId, TCollection_ExtendedString& ResourceName) {
479   
480   ResourceName=aFormat;
481   ResourceName+=".RetrievalPlugin";
482   
483   if(UTL::Find(Resources(),ResourceName))  {
484     thePluginId=UTL::GUID(UTL::Value(Resources(),ResourceName));
485     return Standard_True;
486   }
487   return Standard_False;
488 }
489
490 //=======================================================================
491 //function : CanRetrieve
492 //purpose  : 
493 //=======================================================================
494 PCDM_ReaderStatus CDF_Application::CanRetrieve(const Handle(CDM_MetaData)& aMetaData) {
495   if(aMetaData->HasVersion())
496     return CanRetrieve(aMetaData->Folder(),aMetaData->Name(),aMetaData->Version());
497   else
498     return CanRetrieve(aMetaData->Folder(),aMetaData->Name());
499 }