Salome HOME
Revert "Synchronize adm files"
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_AttributeStudyProperties.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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 //  File   : SALOMEDSImpl_AttributeStudyProperties.cxx
24 //  Author : Sergey RUIN
25 //  Module : SALOME
26 //
27 #include "SALOMEDSImpl_AttributeStudyProperties.hxx"
28 #include <string.h>
29 #include <stdlib.h>
30
31 const std::string& SALOMEDSImpl_AttributeStudyProperties::GetID()
32 {
33   static std::string SALOMEDSImpl_AttributeStudyPropertiesID ("128371A2-8F52-11d6-A8A3-0001021E8C7F");
34   return SALOMEDSImpl_AttributeStudyPropertiesID;
35 }
36
37 SALOMEDSImpl_AttributeStudyProperties* SALOMEDSImpl_AttributeStudyProperties::Set(const DF_Label& label)
38 {
39   SALOMEDSImpl_AttributeStudyProperties* A = NULL;
40   if (!(A=(SALOMEDSImpl_AttributeStudyProperties*)label.FindAttribute(SALOMEDSImpl_AttributeStudyProperties::GetID()))) {
41     A = new SALOMEDSImpl_AttributeStudyProperties();
42     label.AddAttribute(A);
43   }
44   return A;
45 }
46
47 SALOMEDSImpl_AttributeStudyProperties::SALOMEDSImpl_AttributeStudyProperties()
48 :SALOMEDSImpl_GenericAttribute("AttributeStudyProperties")
49 {
50   myLocked = false;
51   myLockChanged = false;
52   myModified = false;
53   Init();
54 }
55
56 void SALOMEDSImpl_AttributeStudyProperties::Init()
57 {
58   myUserName.clear();
59   myMinute.clear();
60   myHour.clear();
61   myDay.clear();
62   myMonth.clear();
63   myYear.clear();
64   myMode = 0; // none
65   myComponentVersions.clear();
66 }
67
68 void SALOMEDSImpl_AttributeStudyProperties::SetModification(const std::string& theUserName,
69                                                             const int          theMinute,
70                                                             const int          theHour,
71                                                             const int          theDay,
72                                                             const int          theMonth,
73                                                             const int          theYear)
74 {
75   if (theMinute<0 || theMinute>60 || theHour<0 || theHour>24 ||
76       theDay<0 || theDay>31 || theMonth<0 || theMonth>12)
77     return;
78
79   CheckLocked();
80   Backup();
81
82   myUserName.push_back(theUserName);
83   myMinute.push_back(theMinute);
84   myHour.push_back(theHour);
85   myDay.push_back(theDay);
86   myMonth.push_back(theMonth);
87   myYear.push_back(theYear);
88 }
89
90 void SALOMEDSImpl_AttributeStudyProperties::GetModifications
91                   (std::vector<std::string>& theUserNames,
92                    std::vector<int>&    theMinutes,
93                    std::vector<int>&    theHours,
94                    std::vector<int>&    theDays,
95                    std::vector<int>&    theMonths,
96                    std::vector<int>&    theYears) const
97 {
98   theUserNames = myUserName;
99   theMinutes = myMinute;
100   theHours = myHour;
101   theDays = myDay;
102   theMonths = myMonth;
103   theYears = myYear;
104 }
105
106 std::string SALOMEDSImpl_AttributeStudyProperties::GetCreatorName() const
107 {
108   return myUserName.empty() ? std::string("") : myUserName[0];
109 }
110
111 bool SALOMEDSImpl_AttributeStudyProperties::GetCreationDate
112                               (int&           theMinute,
113                                int&           theHour,
114                                int&           theDay,
115                                int&           theMonth,
116                                int&           theYear) const
117 {
118   if (myMinute.size() != 0) {
119     theMinute = myMinute[0];
120     theHour = myHour[0];
121     theDay = myDay[0];
122     theMonth = myMonth[0];
123     theYear = myYear[0];
124     return true;
125   }
126   return false;
127 }
128
129 void SALOMEDSImpl_AttributeStudyProperties::ChangeCreatorName(const std::string& theName)
130 {
131   if (myUserName.size() > 0) {
132     CheckLocked();
133     Backup();
134     myUserName[0] = theName;
135   }
136 }
137
138 void SALOMEDSImpl_AttributeStudyProperties::SetCreationMode(const int theMode)
139 {
140   if (theMode == myMode) return;
141   CheckLocked();
142   Backup();
143   myMode = theMode;
144 }
145
146 int SALOMEDSImpl_AttributeStudyProperties::GetCreationMode() const
147 {
148   return myMode;
149 }
150
151 void SALOMEDSImpl_AttributeStudyProperties::SetModified(const int theModified)
152 {
153   myModified = theModified;
154 }
155
156 bool SALOMEDSImpl_AttributeStudyProperties::IsModified() const
157 {
158   return (myModified != 0);
159 }
160
161 int SALOMEDSImpl_AttributeStudyProperties::GetModified() const
162 {
163   return myModified;
164 }
165
166 void SALOMEDSImpl_AttributeStudyProperties::SetLocked(const bool theLocked)
167 {
168 //  Backup();
169   if (myLocked != theLocked) {
170     myLockChanged = true;
171     myLocked = theLocked;
172   }
173 }
174
175 bool SALOMEDSImpl_AttributeStudyProperties::IsLocked() const
176 {
177   return myLocked;
178 }
179
180 bool SALOMEDSImpl_AttributeStudyProperties::IsLockChanged(const bool theErase)
181 {
182   if (!myLockChanged) return false;
183   if (theErase) myLockChanged = false;
184   return true;
185 }
186
187 const std::string& SALOMEDSImpl_AttributeStudyProperties::ID() const
188 {
189   return GetID();
190 }
191
192 void SALOMEDSImpl_AttributeStudyProperties::Restore(DF_Attribute* with)
193 {
194   SALOMEDSImpl_AttributeStudyProperties* aProp =
195     dynamic_cast<SALOMEDSImpl_AttributeStudyProperties*>(with);
196
197   Init();
198   std::vector<std::string> aNames;
199   std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
200   aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
201   for (int i = 0, len = aNames.size(); i < len; i++) {
202     myUserName.push_back(aNames[i]);
203     myMinute.push_back(aMinutes[i]);
204     myHour.push_back(aHours[i]);
205     myDay.push_back(aDays[i]);
206     myMonth.push_back(aMonths[i]);
207     myYear.push_back(aYears[i]);
208   }
209   myMode = aProp->GetCreationMode();
210   myComponentVersions = aProp->GetComponentsVersions();
211 //  myModified = aProp->GetModified();
212 //  myLocked = aProp->IsLocked();
213 }
214
215 DF_Attribute* SALOMEDSImpl_AttributeStudyProperties::NewEmpty() const
216 {
217   return new SALOMEDSImpl_AttributeStudyProperties();
218 }
219
220 void SALOMEDSImpl_AttributeStudyProperties::Paste(DF_Attribute* into)
221 {
222   SALOMEDSImpl_AttributeStudyProperties* aProp =
223     dynamic_cast<SALOMEDSImpl_AttributeStudyProperties*>(into);
224   aProp->Init();
225
226   int i;
227   for(i = 0; i < myUserName.size(); i++) {
228     aProp->SetModification(myUserName[i],
229                            myMinute[i], myHour[i],
230                            myDay[i], myMonth[i], myYear[i]);
231   }
232
233   aProp->SetCreationMode(myMode);
234   aProp->SetComponentsVersions( myComponentVersions );
235 //  aProp->SetModified(myModified);
236 //  aProp->SetLocked(myLocked);
237 }
238
239
240 std::string SALOMEDSImpl_AttributeStudyProperties::Save()
241 {
242   std::vector<std::string> aNames;
243   std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
244   GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
245
246   int aLength, anIndex, unitsSize = 0, commentSize = 0;;
247   for (aLength = 0, anIndex = aNames.size()-1; anIndex >= 0; anIndex--)
248     aLength += aNames[anIndex].size() + 1;
249
250   std::string units = GetUnits();
251   std::string comment = GetComment();
252
253   int aLength1 = 0;
254   std::map<std::string, std::string> versions;
255   versionMap::const_iterator it;
256   for (aLength1 = 0, it = myComponentVersions.begin(); it != myComponentVersions.end(); ++it ) {
257     std::string vlist = "";
258     versionList vl = it->second;
259     versionList::const_iterator vlit;
260     for ( vlit = vl.begin(); vlit != vl.end(); ++vlit ) {
261       if ( vlist != "" ) vlist += ";";
262       vlist += *vlit;
263     }
264     versions[ it->first ] = vlist;
265     aLength1 += it->first.size() + vlist.size() + 2;
266   }
267   
268   unitsSize = units.size();
269   commentSize = comment.size();
270
271   char* aProperty = new char[3 + aLength + 12 * aNames.size() + 1 + unitsSize + 1 + commentSize + 1 + aLength1 ];
272
273   char crMode = (char)GetCreationMode();
274
275   sprintf(aProperty,"%c%c", crMode, IsLocked()?'l':'u');
276
277   aLength = aNames.size();
278   int a = 2;
279   for (anIndex = 0; anIndex  < aLength; anIndex++) {
280     sprintf(&(aProperty[a]),"%2d%2d%2d%2d%4d%s",
281             (int)(aMinutes[anIndex]),
282             (int)(aHours[anIndex]),
283             (int)(aDays[anIndex]),
284             (int)(aMonths[anIndex]),
285             (int)(aYears[anIndex]),
286             (char*)(aNames[anIndex].c_str()));
287     a = strlen(aProperty);
288     aProperty[a++] = 1;
289   }
290
291   //Write delimeter of the section to define end of the modifications section
292   aProperty[a++] = 30;
293
294   //Write units if need
295   if(units.size() > 0) {
296     sprintf(&(aProperty[a]),"%s",units.c_str());
297     a = strlen(aProperty);
298   }
299
300   aProperty[a++] = 1; //delimeter of the units and comments
301
302   //Write comments if need
303   if(comment.size() > 0) {
304     sprintf(&(aProperty[a]),"%s",comment.c_str());
305     a = strlen(aProperty);
306   }
307   
308   aProperty[a++] = 30; //delimeter of the component versions
309
310   std::map<std::string, std::string>::const_iterator versionsIt;
311   for ( versionsIt = versions.begin(); versionsIt != versions.end(); ++versionsIt ) {
312     sprintf(&(aProperty[a]),"%s=%s",
313             (char*)(versionsIt->first.c_str()),
314             (char*)(versionsIt->second.c_str()));
315     a = strlen(aProperty);
316     aProperty[a++] = 1;
317   }
318
319   aProperty[a] = 0;
320   std::string prop(aProperty);
321   delete aProperty;
322
323   return prop;
324 }
325
326 void SALOMEDSImpl_AttributeStudyProperties::SetUnits(const std::string& theUnits)
327 {
328   if(myUnits == theUnits)
329     return;
330
331   CheckLocked();
332   Backup();
333
334   myUnits = theUnits;
335 }
336
337 std::string SALOMEDSImpl_AttributeStudyProperties::GetUnits() const
338 {
339   return myUnits;
340 }
341
342 void SALOMEDSImpl_AttributeStudyProperties::SetComment(const std::string& theComment)
343 {
344   if(myComment == theComment)
345     return;
346
347   CheckLocked();
348   Backup();
349
350   myComment = theComment;
351 }
352
353 std::string SALOMEDSImpl_AttributeStudyProperties::GetComment() const 
354 {
355   return myComment;
356 }
357
358 void SALOMEDSImpl_AttributeStudyProperties::SetComponentVersion(const std::string& theComponent, const std::string& theVersion)
359 {
360   if (!theComponent.empty()) {
361     CheckLocked();
362     Backup();
363     if (myComponentVersions.find(theComponent) == myComponentVersions.end()) myComponentVersions[theComponent] = versionList();
364     if (myComponentVersions[theComponent].empty() || myComponentVersions[theComponent].back() != theVersion)
365       myComponentVersions[theComponent].push_back(theVersion);
366   }
367 }
368
369 std::vector<std::string> SALOMEDSImpl_AttributeStudyProperties::GetStoredComponents() const
370 {
371   std::vector<std::string> components;
372   versionMap::const_iterator it;  
373   for (it = myComponentVersions.begin(); it != myComponentVersions.end(); ++it)
374     components.push_back(it->first);
375   return components;
376 }
377
378 std::string SALOMEDSImpl_AttributeStudyProperties::GetComponentVersion(const std::string& theComponent) const
379 {
380   versionList versions = GetComponentVersions(theComponent);
381   return versions.size() > 0 ? versions[0] : std::string("");
382 }
383
384 std::vector<std::string> SALOMEDSImpl_AttributeStudyProperties::GetComponentVersions(const std::string& theComponent) const
385 {
386   versionList versions;
387   versionMap::const_iterator it = myComponentVersions.find(theComponent);
388   if ( it != myComponentVersions.end() ) versions = it->second;
389   return versions;
390 }
391
392 std::map< std::string, std::vector<std::string> > SALOMEDSImpl_AttributeStudyProperties::GetComponentsVersions() const
393 {
394   return myComponentVersions;
395 }
396
397 void SALOMEDSImpl_AttributeStudyProperties::SetComponentsVersions( const std::map< std::string, std::vector<std::string> >& theVersions )
398 {
399   myComponentVersions = theVersions;
400 }
401
402 void SALOMEDSImpl_AttributeStudyProperties::Load(const std::string& value)
403 {
404   char* aCopy = (char*)value.c_str();
405
406   int crMode = (int)aCopy[0];
407   SetCreationMode(crMode);
408
409   int anIndex;
410   // number 13 below is minimal size of modification data record, which has form:
411   // mmhhddMMyyyyname1, where
412   // - mm:   minute = 2 bytes
413   // - hh:   hour   = 2 bytes
414   // - dd:   day    = 2 bytes
415   // - MM:   month  = 2 bytes
416   // - yyyy: year   = 4 bytes
417   // - name: user's name = arbitrary value, minimal length is 0 bytes
418   // - 1   : records delimiter = 1 byte  
419   for (anIndex = 2; anIndex + 13 < value.size() ;) {
420     char str[10];
421     int aMinute, aHour, aDay, aMonth, aYear;
422     str[0] = aCopy[anIndex++];
423     str[1] = aCopy[anIndex++];
424     str[2] = 0;
425     aMinute = atoi(str);
426     str[0] = aCopy[anIndex++];
427     str[1] = aCopy[anIndex++];
428     aHour =  atoi(str);
429     str[0] = aCopy[anIndex++];
430     str[1] = aCopy[anIndex++];
431     aDay =  atoi(str);
432     str[0] = aCopy[anIndex++];
433     str[1] = aCopy[anIndex++];
434     aMonth =  atoi(str);
435     str[0] = aCopy[anIndex++];
436     str[1] = aCopy[anIndex++];
437     str[2] = aCopy[anIndex++];
438     str[3] = aCopy[anIndex++];
439     str[4] = 0;
440     aYear = atoi(str);
441
442     int aNameSize;
443     for(aNameSize = 0; aCopy[anIndex+aNameSize]!=1; aNameSize++);
444     char *aName = new char[aNameSize+1];
445     strncpy(aName, &(aCopy[anIndex]), aNameSize);
446     aName[aNameSize] = 0;
447     SetModification(aName,aMinute,aHour,aDay,aMonth,aYear);
448     delete [] (aName);
449     anIndex += aNameSize + 1;
450     
451     //Check end of the modifications section
452     if(anIndex < value.size() && aCopy[anIndex] == 30)
453       break;
454   }
455   
456   //Case when study contains units and comment properties
457   if( anIndex < value.size() ) {
458     anIndex++; //skip the delimeter of the sections: char(30)
459     int unitsSize;
460     for(unitsSize = 0; aCopy[anIndex+unitsSize] != 1; unitsSize++);
461
462     if(unitsSize > 0) {
463       char *anUnits = new char[unitsSize+1];
464       strncpy(anUnits, &(aCopy[anIndex]), unitsSize);
465       anUnits[unitsSize] = 0;
466       SetUnits(anUnits);
467       delete [] (anUnits);
468     }
469     anIndex += unitsSize + 1;
470
471     int commentSize;
472     for(commentSize = 0; aCopy[anIndex+commentSize] != 0 && aCopy[anIndex+commentSize] != 30; commentSize++);
473
474     if(commentSize > 0) {
475       char *aComment = new char[commentSize+1];
476       strncpy(aComment, &(aCopy[anIndex]), commentSize);
477       aComment[commentSize] = 0;
478       SetComment(aComment);
479       delete [] (aComment);
480     }
481     anIndex += commentSize + 1;
482   }
483
484   //Case when study contains components versions
485   if( anIndex < value.size() ) {
486     while ( anIndex < value.size() && aCopy[anIndex] != 0 ) {
487       int modSize;
488       for(modSize = 0; aCopy[anIndex+modSize] != '='; modSize++);
489       int verSize;
490       for(verSize = 0; aCopy[anIndex+modSize+1+verSize] != 1; verSize++);
491
492       if(modSize > 0) {
493         char *aModule = new char[modSize+1];
494         strncpy(aModule, &(aCopy[anIndex]), modSize);
495         aModule[modSize] = 0;
496         char *aVersions = new char[verSize+1];
497         if ( verSize > 0 )
498           strncpy(aVersions, &(aCopy[anIndex+modSize+1]), verSize);
499         aVersions[verSize] = 0;
500         
501         std::string mVersions = aVersions;
502         int start = 0, idx = mVersions.find( ';', start );
503         while ( idx != std::string::npos ) {
504           SetComponentVersion( aModule, mVersions.substr( start, idx-start ) );
505           start = idx + 1;
506           idx = mVersions.find( ';', start );
507         }
508         SetComponentVersion( aModule, mVersions.substr( start ) );
509
510         delete [] (aModule);
511         delete [] (aVersions);
512         anIndex += modSize + 1 + verSize + 1;
513       }
514     }
515   }
516   
517   if (aCopy[1] == 'l') {
518     SetLocked(true);
519   }
520   SetModified(0);  
521 }