0022573: Extend the range of status values returned by the method TDocStd_Application...
[occt.git] / src / CDF / CDF_Store.cxx
1 // Modified by rmi, Thu Dec  4 14:24:24 1997
2 #include <CDF_Store.ixx>
3 #include <CDF_Session.hxx>
4 #include <CDF_Application.hxx>
5 #include <PCDM_StoreStatus.hxx>
6 #include <CDF_StoreSetNameStatus.hxx>
7 #include <Standard_ProgramError.hxx>
8 #include <CDF_MetaDataDriver.hxx>
9 #include <PCDM_StorageDriver.hxx>
10 #include <PCDM.hxx>
11 #include <TCollection_ExtendedString.hxx>
12 #include <CDM_MetaData.hxx>
13
14 #define theMetaDataDriver CDF_Session::CurrentSession()->MetaDataDriver()
15
16
17 static TCollection_ExtendedString blank("");
18
19
20
21 CDF_Store::CDF_Store() {}
22 CDF_Store::CDF_Store(const Handle(CDM_Document)& aDocument):myHasSubComponents(Standard_False) {
23   
24   myMainDocument = aDocument;
25   Init();
26 }
27
28 void CDF_Store::Init() {
29   
30   myCurrentDocument = myMainDocument;
31   myList = new CDF_StoreList(myCurrentDocument);
32
33   myIsMainDocument = Standard_True;
34   FindDefault();
35
36 // getting the subcomponents.
37   //
38   myIsMainDocument = Standard_False;
39   myList->Init();
40   for ( myList->Init(); myList->More(); myList->Next()) {
41     myCurrentDocument = myList->Value();
42     if(myCurrentDocument != myMainDocument) {
43       myHasSubComponents  = Standard_True;
44       FindDefault();
45       
46     }
47   }
48   myIsMainDocument = Standard_True;
49   myCurrentDocument = myMainDocument;
50 }
51
52 CDF_TryStoreStatus CDF_Store::Check() {
53   if(!PCDM::FindStorageDriver(myMainDocument))  return CDF_TS_NoDriver;
54
55   
56   // Checking the subcomponent.
57   Handle(CDM_Document) theCurrentDocument;
58   myList->Init();
59   for ( myList->Init(); myList->More(); myList->Next()) {
60     theCurrentDocument = myList->Value();
61
62     if(theCurrentDocument != myMainDocument) {
63       if( theCurrentDocument->IsModified()) {
64    
65         myLastName = theCurrentDocument->Presentation();
66         
67         if(!PCDM::FindStorageDriver(theCurrentDocument))  return CDF_TS_NoDriver;
68         
69       }
70     }
71   }
72   return CDF_TS_OK;
73 }
74
75
76 Standard_ExtString CDF_Store::LastName() const {
77   return myLastName.ToExtString();
78 }
79
80 Standard_ExtString CDF_Store::Folder() const {
81   static TCollection_ExtendedString retv;
82   if(myCurrentDocument->HasRequestedFolder())
83     retv =  myCurrentDocument->RequestedFolder();
84   else
85     retv= blank;
86   return retv.ToExtString();
87 }
88
89 Standard_ExtString CDF_Store::Name() const {
90   static TCollection_ExtendedString retv;
91   retv = myCurrentDocument->RequestedName();
92   return retv.ToExtString();
93 }
94
95
96 Standard_Boolean  CDF_Store::SetFolder(const Standard_ExtString aFolder) {
97   TCollection_ExtendedString f(aFolder);
98   return SetFolder(f);
99 }
100 Standard_Boolean  CDF_Store::SetFolder(const TCollection_ExtendedString& aFolder) {
101
102   TCollection_ExtendedString theFolder(aFolder);
103   Standard_Integer l = theFolder.Length();
104
105   // if the last character is the folder separator (which is always the first character)
106   // it is removed.
107         // This is correct for Unix systems but not for Windows! VMS and MAC? Thomas Haller, 23.11.01
108   if(l > 1) {
109 #ifndef WNT
110     if(theFolder.Value(l) == theFolder.Value(1)) theFolder.Trunc(l-1);
111 #else
112         if (theFolder.Value(l) == '/' || theFolder.Value(l) == '\\')
113                 theFolder.Trunc(l-1);
114 #endif
115   }
116
117   if(theMetaDataDriver->FindFolder(theFolder))  {
118     myCurrentDocument->SetRequestedFolder(theFolder);
119     return Standard_True;
120   }
121   return Standard_False;
122 }
123
124
125 CDF_StoreSetNameStatus CDF_Store::RecheckName () {
126    return SetName(myCurrentDocument->RequestedName());
127 }
128
129 CDF_StoreSetNameStatus CDF_Store::SetName(const TCollection_ExtendedString&  aName)
130 {
131   TCollection_ExtendedString theName=theMetaDataDriver->SetName(myCurrentDocument,aName);
132
133   if(myCurrentDocument->IsStored ()) { 
134     Handle(CDM_MetaData)  E = myCurrentDocument->MetaData();
135     if(   E->Folder() == myCurrentDocument->RequestedFolder() 
136        && E->Name()   == theName) return CDF_SSNS_OK;
137   }
138   
139   if(myCurrentDocument->HasRequestedFolder()) {
140     if (theMetaDataDriver->Find(myCurrentDocument->RequestedFolder(),theName)) {
141       if(theMetaDataDriver->MetaData(myCurrentDocument->RequestedFolder(),theName)->IsRetrieved())
142         return CDF_SSNS_OpenDocument;
143       else {
144         myCurrentDocument->SetRequestedName(theName);
145         return CDF_SSNS_ReplacingAnExistentDocument;
146       }
147     }
148   }
149   myCurrentDocument->SetRequestedName(theName);
150   return  CDF_SSNS_OK;
151 }
152 CDF_StoreSetNameStatus CDF_Store::SetName(const Standard_ExtString aName)
153 {
154   TCollection_ExtendedString theName(aName);
155   return SetName(theName);
156 }
157
158 void CDF_Store::Realize() {
159   Standard_ProgramError_Raise_if(!myList->IsConsistent(),"information are missing");
160   Handle(CDM_MetaData) m;
161   myText = "";
162   myStatus = myList->Store(m,myText);
163   if(myStatus==PCDM_SS_OK) myPath = m->Path();
164 }
165 Standard_ExtString CDF_Store::Path() const {
166   return myPath.ToExtString();
167 }
168 Standard_ExtString CDF_Store::MetaDataPath() const {
169   static TCollection_ExtendedString retv;
170   retv="";
171   if(myCurrentDocument->IsStored()) retv=myCurrentDocument->MetaData()->Path();
172   return retv.ToExtString();
173 }
174 Standard_ExtString CDF_Store::Description() const {
175   static TCollection_ExtendedString retv;
176
177   if(myMainDocument->FindDescription())
178     retv = myMainDocument->Description();
179   else
180     retv= blank;
181
182   return retv.ToExtString();
183 }
184
185 Standard_Boolean CDF_Store::IsStored() const {
186   return myCurrentDocument->IsStored();
187 }
188 Standard_Boolean CDF_Store::IsModified() const {
189   return myCurrentDocument->IsModified();
190 }
191 Standard_Boolean CDF_Store::CurrentIsConsistent() const {
192   if(!myCurrentDocument->IsStored())
193     return myCurrentDocument->HasRequestedFolder();
194   return Standard_True;
195 }
196
197 Standard_Boolean CDF_Store::IsConsistent() const {
198   return myList->IsConsistent();
199 }
200 Standard_Boolean CDF_Store::HasAPreviousVersion() const {
201   return myCurrentDocument->HasRequestedPreviousVersion();
202 }
203
204 Standard_ExtString CDF_Store::PreviousVersion() const {
205   static TCollection_ExtendedString retv;
206   if(myCurrentDocument->HasRequestedPreviousVersion())
207     retv= myCurrentDocument->RequestedPreviousVersion();
208   else
209     retv=blank;
210   return retv.ToExtString();
211 }
212
213 Standard_Boolean CDF_Store::SetPreviousVersion (const Standard_ExtString aPreviousVersion) {
214   if(theMetaDataDriver->HasVersionCapability()) {
215     if(myCurrentDocument->HasRequestedFolder()) {
216       if(theMetaDataDriver->Find(myCurrentDocument->RequestedFolder(),myCurrentDocument->RequestedName(),aPreviousVersion)){
217         
218         myCurrentDocument->SetRequestedPreviousVersion(aPreviousVersion);
219         return Standard_True;
220       }
221       else
222         return Standard_False;
223     }
224     else 
225       return Standard_False;
226   }
227   return Standard_True;
228 }
229
230 void CDF_Store::InitComponent() {
231    myList->Init();
232 }
233
234 Standard_Boolean CDF_Store::MoreComponent() const {
235   return myList->More();
236 }
237
238 void CDF_Store::NextComponent()  {
239   myList->Next();
240 }
241 void CDF_Store::SetCurrent() {
242   myCurrentDocument = myList->Value();
243   myIsMainDocument = myCurrentDocument == myMainDocument;
244
245
246 }
247
248 Standard_ExtString CDF_Store::Component() const {
249   
250   static TCollection_ExtendedString retv;
251   retv=myList->Value()->Presentation();
252   return retv.ToExtString();
253 }
254 Standard_Boolean CDF_Store::HasSubComponents () const {
255    return myHasSubComponents;
256 }
257
258 void CDF_Store::SetCurrent(const Standard_ExtString aPresentation) {
259   myCurrentDocument = CDM_Document::FindFromPresentation(aPresentation);
260   myIsMainDocument = myCurrentDocument == myMainDocument;
261 }
262 void CDF_Store::SetMain() {
263   myCurrentDocument = myMainDocument;
264   myIsMainDocument = Standard_True;
265
266 }
267 Standard_Boolean CDF_Store::IsMainDocument() const {
268   return myIsMainDocument;
269 }
270
271 CDF_SubComponentStatus CDF_Store::SubComponentStatus(const Standard_ExtString aPresentation) const {
272    Handle(CDM_Document) d = CDM_Document::FindFromPresentation(aPresentation);
273
274   if(!d->IsStored()) 
275     return d->HasRequestedFolder()? CDF_SCS_Consistent : CDF_SCS_Unconsistent;
276
277   if(d->IsModified()) return CDF_SCS_Modified;
278   return CDF_SCS_Stored;
279 }
280
281
282
283 PCDM_StoreStatus CDF_Store::StoreStatus() const {
284   return myStatus;
285 }
286 Standard_ExtString CDF_Store::AssociatedStatusText() const {
287   return myText.ToExtString();
288 }
289
290
291 void CDF_Store::FindDefault() {
292   if (!myCurrentDocument->IsStored ()) {
293     myCurrentDocument->SetRequestedFolder(CDF_Session::CurrentSession()->CurrentApplication()->DefaultFolder());
294 //    myCurrentDocument->SetRequestedName(theMetaDataDriver->SetName(myCurrentDocument,myCurrentDocument->Presentation()));
295     myCurrentDocument->SetRequestedName(theMetaDataDriver->SetName(myCurrentDocument,myCurrentDocument->RequestedName()));
296   }
297 }
298 void CDF_Store::SetComment(const Standard_ExtString aComment) {
299   myCurrentDocument->SetRequestedComment(aComment);
300 }
301
302 Standard_ExtString CDF_Store::Comment() const {
303   static TCollection_ExtendedString retv;
304   retv=myCurrentDocument->RequestedComment();
305   return retv.ToExtString();
306 }