1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13 
14 // Modified by rmi, Thu Dec  4 14:24:24 1997
15 
16 #include <CDF_Application.hxx>
17 #include <CDF_MetaDataDriver.hxx>
18 #include <CDF_Store.hxx>
19 #include <CDF_StoreList.hxx>
20 #include <CDF_StoreSetNameStatus.hxx>
21 #include <CDM_Document.hxx>
22 #include <CDM_MetaData.hxx>
23 #include <PCDM.hxx>
24 #include <PCDM_StorageDriver.hxx>
25 #include <PCDM_StoreStatus.hxx>
26 #include <Standard_ProgramError.hxx>
27 
28 #define theMetaDataDriver Handle(CDF_Application)::DownCast((myCurrentDocument->Application()))->MetaDataDriver()
29 
30 static const Handle(TCollection_HExtendedString) blank = new TCollection_HExtendedString("");
31 
CDF_Store()32 CDF_Store::CDF_Store()
33 : myHasSubComponents(Standard_False),
34   myIsMainDocument(Standard_False),
35   myStatus(PCDM_SS_No_Obj)
36 {
37 }
CDF_Store(const Handle (CDM_Document)& aDocument)38 CDF_Store::CDF_Store(const Handle(CDM_Document)& aDocument):myHasSubComponents(Standard_False) {
39 
40   myMainDocument = aDocument;
41   Init();
42 }
43 
Init()44 void CDF_Store::Init() {
45 
46   myCurrentDocument = myMainDocument;
47   myList = new CDF_StoreList(myCurrentDocument);
48 
49   // getting the subcomponents.
50   //
51   myIsMainDocument = Standard_False;
52   myList->Init();
53   for ( myList->Init(); myList->More(); myList->Next()) {
54     myCurrentDocument = myList->Value();
55     if(myCurrentDocument != myMainDocument) {
56       myHasSubComponents  = Standard_True;
57       FindDefault();
58 
59     }
60   }
61   myIsMainDocument = Standard_True;
62   myCurrentDocument = myMainDocument;
63 }
64 
Handle(TCollection_HExtendedString)65 Handle(TCollection_HExtendedString) CDF_Store::Folder() const {
66   if(myCurrentDocument->HasRequestedFolder())
67     return new TCollection_HExtendedString(myCurrentDocument->RequestedFolder());
68   return blank;
69 }
70 
Handle(TCollection_HExtendedString)71 Handle(TCollection_HExtendedString) CDF_Store::Name() const {
72   return new TCollection_HExtendedString(myCurrentDocument->RequestedName());
73 }
74 
75 
SetFolder(const Standard_ExtString aFolder)76 Standard_Boolean  CDF_Store::SetFolder(const Standard_ExtString aFolder) {
77   TCollection_ExtendedString f(aFolder);
78   return SetFolder(f);
79 }
SetFolder(const TCollection_ExtendedString & aFolder)80 Standard_Boolean  CDF_Store::SetFolder(const TCollection_ExtendedString& aFolder) {
81 
82   TCollection_ExtendedString theFolder(aFolder);
83   Standard_Integer l = theFolder.Length();
84 
85   // if the last character is the folder separator (which is always the first character)
86   // it is removed.
87 	// This is correct for Unix systems but not for Windows! VMS and MAC? Thomas Haller, 23.11.01
88   if(l > 1) {
89 #ifndef _WIN32
90     if(theFolder.Value(l) == theFolder.Value(1)) theFolder.Trunc(l-1);
91 #else
92 	if (theFolder.Value(l) == '/' || theFolder.Value(l) == '\\')
93 		theFolder.Trunc(l-1);
94 #endif
95   }
96 
97   if(theMetaDataDriver->FindFolder(theFolder))  {
98     myCurrentDocument->SetRequestedFolder(theFolder);
99     return Standard_True;
100   }
101   return Standard_False;
102 }
103 
104 
RecheckName()105 CDF_StoreSetNameStatus CDF_Store::RecheckName () {
106    return SetName(myCurrentDocument->RequestedName());
107 }
108 
SetName(const TCollection_ExtendedString & aName)109 CDF_StoreSetNameStatus CDF_Store::SetName(const TCollection_ExtendedString&  aName)
110 {
111   TCollection_ExtendedString theName=theMetaDataDriver->SetName(myCurrentDocument,aName);
112 
113   if(myCurrentDocument->IsStored ()) {
114     Handle(CDM_MetaData)  E = myCurrentDocument->MetaData();
115     if(   E->Folder() == myCurrentDocument->RequestedFolder()
116        && E->Name()   == theName) return CDF_SSNS_OK;
117   }
118 
119   if(myCurrentDocument->HasRequestedFolder()) {
120     if (theMetaDataDriver->Find(myCurrentDocument->RequestedFolder(),theName)) {
121       if(theMetaDataDriver->MetaData(myCurrentDocument->RequestedFolder(),theName)->IsRetrieved())
122 	return CDF_SSNS_OpenDocument;
123       else {
124 	myCurrentDocument->SetRequestedName(theName);
125 	return CDF_SSNS_ReplacingAnExistentDocument;
126       }
127     }
128   }
129   myCurrentDocument->SetRequestedName(theName);
130   return  CDF_SSNS_OK;
131 }
SetName(const Standard_ExtString aName)132 CDF_StoreSetNameStatus CDF_Store::SetName(const Standard_ExtString aName)
133 {
134   TCollection_ExtendedString theName(aName);
135   return SetName(theName);
136 }
137 
Realize(const Message_ProgressRange & theRange)138 void CDF_Store::Realize (const Message_ProgressRange& theRange)
139 {
140   Standard_ProgramError_Raise_if(!myList->IsConsistent(),"information are missing");
141   Handle(CDM_MetaData) m;
142   myText = "";
143   myStatus = myList->Store(m, myText, theRange);
144   if(myStatus==PCDM_SS_OK) myPath = m->Path();
145 }
Path() const146 Standard_ExtString CDF_Store::Path() const {
147   return myPath.ToExtString();
148 }
Handle(TCollection_HExtendedString)149 Handle(TCollection_HExtendedString) CDF_Store::MetaDataPath() const {
150   if(myCurrentDocument->IsStored())
151     return new TCollection_HExtendedString(myCurrentDocument->MetaData()->Path());
152   return blank;
153 }
Handle(TCollection_HExtendedString)154 Handle(TCollection_HExtendedString) CDF_Store::Description() const {
155   if(myMainDocument->FindDescription())
156     return new TCollection_HExtendedString(myMainDocument->Description());
157   return blank;
158 }
159 
IsStored() const160 Standard_Boolean CDF_Store::IsStored() const {
161   return myCurrentDocument->IsStored();
162 }
IsModified() const163 Standard_Boolean CDF_Store::IsModified() const {
164   return myCurrentDocument->IsModified();
165 }
CurrentIsConsistent() const166 Standard_Boolean CDF_Store::CurrentIsConsistent() const {
167   if(!myCurrentDocument->IsStored())
168     return myCurrentDocument->HasRequestedFolder();
169   return Standard_True;
170 }
171 
IsConsistent() const172 Standard_Boolean CDF_Store::IsConsistent() const {
173   return myList->IsConsistent();
174 }
HasAPreviousVersion() const175 Standard_Boolean CDF_Store::HasAPreviousVersion() const {
176   return myCurrentDocument->HasRequestedPreviousVersion();
177 }
178 
Handle(TCollection_HExtendedString)179 Handle(TCollection_HExtendedString) CDF_Store::PreviousVersion() const {
180   if(myCurrentDocument->HasRequestedPreviousVersion())
181     return new TCollection_HExtendedString(myCurrentDocument->RequestedPreviousVersion());
182   return blank;
183 }
184 
SetPreviousVersion(const Standard_ExtString aPreviousVersion)185 Standard_Boolean CDF_Store::SetPreviousVersion (const Standard_ExtString aPreviousVersion) {
186   if(theMetaDataDriver->HasVersionCapability()) {
187     if(myCurrentDocument->HasRequestedFolder()) {
188       if(theMetaDataDriver->Find(myCurrentDocument->RequestedFolder(),myCurrentDocument->RequestedName(),aPreviousVersion)){
189 
190 	myCurrentDocument->SetRequestedPreviousVersion(aPreviousVersion);
191 	return Standard_True;
192       }
193       else
194 	return Standard_False;
195     }
196     else
197       return Standard_False;
198   }
199   return Standard_True;
200 }
201 
SetCurrent(const Standard_ExtString)202 void CDF_Store::SetCurrent(const Standard_ExtString /*aPresentation*/) {
203   myIsMainDocument = myCurrentDocument == myMainDocument;
204 }
SetMain()205 void CDF_Store::SetMain() {
206   myCurrentDocument = myMainDocument;
207   myIsMainDocument = Standard_True;
208 
209 }
IsMainDocument() const210 Standard_Boolean CDF_Store::IsMainDocument() const {
211   return myIsMainDocument;
212 }
213 
StoreStatus() const214 PCDM_StoreStatus CDF_Store::StoreStatus() const {
215   return myStatus;
216 }
AssociatedStatusText() const217 Standard_ExtString CDF_Store::AssociatedStatusText() const {
218   return myText.ToExtString();
219 }
220 
221 
FindDefault()222 void CDF_Store::FindDefault() {
223   if (!myCurrentDocument->IsStored ()) {
224     myCurrentDocument->SetRequestedFolder(Handle(CDF_Application)::DownCast((myCurrentDocument->Application()))->DefaultFolder());
225     myCurrentDocument->SetRequestedName(theMetaDataDriver->SetName(myCurrentDocument,myCurrentDocument->RequestedName()));
226   }
227 }
SetComment(const Standard_ExtString aComment)228 void CDF_Store::SetComment(const Standard_ExtString aComment) {
229   myCurrentDocument->SetRequestedComment(aComment);
230 }
231 
Handle(TCollection_HExtendedString)232 Handle(TCollection_HExtendedString) CDF_Store::Comment() const {
233   return new TCollection_HExtendedString(myCurrentDocument->RequestedComment());
234 }
235