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