Salome HOME
Removed CASCatch
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_AttributeStudyProperties.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //  File   : SALOMEDSImpl_AttributeStudyProperties.cxx
21 //  Author : Sergey RUIN
22 //  Module : SALOME
23
24 #include <SALOMEDSImpl_AttributeStudyProperties.hxx>
25
26 using namespace std;
27
28
29 const std::string& SALOMEDSImpl_AttributeStudyProperties::GetID()
30 {
31   static std::string SALOMEDSImpl_AttributeStudyPropertiesID ("128371A2-8F52-11d6-A8A3-0001021E8C7F");
32   return SALOMEDSImpl_AttributeStudyPropertiesID;
33 }
34
35 SALOMEDSImpl_AttributeStudyProperties* SALOMEDSImpl_AttributeStudyProperties::Set(const DF_Label& label)
36 {
37   SALOMEDSImpl_AttributeStudyProperties* A = NULL;
38   if (!(A=(SALOMEDSImpl_AttributeStudyProperties*)label.FindAttribute(SALOMEDSImpl_AttributeStudyProperties::GetID()))) {
39     A = new SALOMEDSImpl_AttributeStudyProperties();
40     label.AddAttribute(A);
41   }
42   return A;
43 }
44
45 SALOMEDSImpl_AttributeStudyProperties::SALOMEDSImpl_AttributeStudyProperties()
46 :SALOMEDSImpl_GenericAttribute("AttributeStudyProperties")
47 {
48   myLocked = false;
49   myLockChanged = false;
50   Init();
51 }
52
53 void SALOMEDSImpl_AttributeStudyProperties::Init()
54 {
55   myUserName.clear();
56   myMinute.clear();
57   myHour.clear();
58   myDay.clear();
59   myMonth.clear();
60   myYear.clear();
61   myMode = 0; // none
62 }
63
64 void SALOMEDSImpl_AttributeStudyProperties::SetModification(const std::string& theUserName,
65                                                             const int            theMinute,
66                                                             const int            theHour,
67                                                             const int            theDay,
68                                                             const int            theMonth,
69                                                             const int            theYear)
70 {
71   if (theMinute<0 || theMinute>60 || theHour<0 || theHour>24 ||
72       theDay<0 || theDay>31 || theMonth<0 || theMonth>12)
73     return;
74
75   CheckLocked();
76   Backup();
77
78   myUserName.push_back(theUserName);
79   myMinute.push_back(theMinute);
80   myHour.push_back(theHour);
81   myDay.push_back(theDay);
82   myMonth.push_back(theMonth);
83   myYear.push_back(theYear);
84 }
85
86 void SALOMEDSImpl_AttributeStudyProperties::GetModifications
87                   (vector<string>& theUserNames,
88                    vector<int>&    theMinutes,
89                    vector<int>&    theHours,
90                    vector<int>&    theDays,
91                    vector<int>&    theMonths,
92                    vector<int>&    theYears) const
93 {
94   theUserNames = myUserName;
95   theMinutes = myMinute;
96   theHours = myHour;
97   theDays = myDay;
98   theMonths = myMonth;
99   theYears = myYear;
100 }
101
102 std::string SALOMEDSImpl_AttributeStudyProperties::GetCreatorName() const
103 {
104   if (myUserName.size() == 0)
105     return std::string("");
106   return myUserName[0];
107 }
108
109 bool SALOMEDSImpl_AttributeStudyProperties::GetCreationDate
110                               (int&           theMinute,
111                                int&           theHour,
112                                int&           theDay,
113                                int&           theMonth,
114                                int&           theYear) const
115 {
116   if (myMinute.size() != 0) {
117     theMinute = myMinute[0];
118     theHour = myHour[0];
119     theDay = myDay[0];
120     theMonth = myMonth[0];
121     theYear = myYear[0];
122     return true;
123   }
124   return false;
125 }
126
127 void SALOMEDSImpl_AttributeStudyProperties::ChangeCreatorName(const std::string& theName)
128 {
129   if (myUserName.size() > 0) {
130     CheckLocked();
131     Backup();
132     myUserName[0] = theName;
133   }
134 }
135
136 void SALOMEDSImpl_AttributeStudyProperties::SetCreationMode(const int theMode)
137 {
138   CheckLocked();
139   Backup();
140   myMode = theMode;
141 }
142
143 int SALOMEDSImpl_AttributeStudyProperties::GetCreationMode() const
144 {
145   return myMode;
146 }
147
148 void SALOMEDSImpl_AttributeStudyProperties::SetModified(const int theModified)
149 {
150   myModified = theModified;
151 }
152
153 bool SALOMEDSImpl_AttributeStudyProperties::IsModified() const
154 {
155   return (myModified != 0);
156 }
157
158 int SALOMEDSImpl_AttributeStudyProperties::GetModified() const
159 {
160   return myModified;
161 }
162
163 void SALOMEDSImpl_AttributeStudyProperties::SetLocked(const bool theLocked)
164 {
165 //  Backup();
166   if (myLocked != theLocked) {
167     myLockChanged = true;
168     myLocked = theLocked;
169   }
170 }
171
172 bool SALOMEDSImpl_AttributeStudyProperties::IsLocked() const
173 {
174   return myLocked;
175 }
176
177 bool SALOMEDSImpl_AttributeStudyProperties::IsLockChanged(const bool theErase) {
178   if (!myLockChanged) return false;
179   if (theErase) myLockChanged = false;
180   return true;
181 }
182
183 const std::string& SALOMEDSImpl_AttributeStudyProperties::ID() const
184 {
185   return GetID();
186 }
187
188 void SALOMEDSImpl_AttributeStudyProperties::Restore(DF_Attribute* with)
189 {
190   SALOMEDSImpl_AttributeStudyProperties* aProp =
191     dynamic_cast<SALOMEDSImpl_AttributeStudyProperties*>(with);
192
193   Init();
194   vector<string> aNames;
195   vector<int> aMinutes, aHours, aDays, aMonths, aYears;
196   aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
197   for (int i = 0, len = aNames.size(); i < len; i++) {
198     myUserName.push_back(aNames[i]);
199     myMinute.push_back(aMinutes[i]);
200     myHour.push_back(aHours[i]);
201     myDay.push_back(aDays[i]);
202     myMonth.push_back(aMonths[i]);
203     myYear.push_back(aYears[i]);
204   }
205   myMode = aProp->GetCreationMode();
206 //  myModified = aProp->GetModified();
207 //  myLocked = aProp->IsLocked();
208 }
209
210 DF_Attribute* SALOMEDSImpl_AttributeStudyProperties::NewEmpty() const
211 {
212   return new SALOMEDSImpl_AttributeStudyProperties();
213 }
214
215 void SALOMEDSImpl_AttributeStudyProperties::Paste(DF_Attribute* into)
216 {
217   SALOMEDSImpl_AttributeStudyProperties* aProp =
218     dynamic_cast<SALOMEDSImpl_AttributeStudyProperties*>(into);
219   aProp->Init();
220
221   int i;
222   for(i = 0; i < myUserName.size(); i++) {
223     aProp->SetModification(myUserName[i],
224                            myMinute[i], myHour[i],
225                            myDay[i], myMonth[i], myYear[i]);
226   }
227
228   aProp->SetCreationMode(myMode);
229 //  aProp->SetModified(myModified);
230 //  aProp->SetLocked(myLocked);
231 }
232
233
234 string SALOMEDSImpl_AttributeStudyProperties::Save()
235 {
236   vector<string> aNames;
237   vector<int> aMinutes, aHours, aDays, aMonths, aYears;
238   GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
239
240   int aLength, anIndex;
241   for (aLength = 0, anIndex = aNames.size()-1; anIndex >= 0; anIndex--)
242     aLength += aNames[anIndex].size() + 1;
243
244   char* aProperty = new char[3 + aLength + 12 * aNames.size()];
245
246   char crMode = (char)GetCreationMode();
247
248   sprintf(aProperty,"%c%c", crMode, IsLocked()?'l':'u');
249
250   aLength = aNames.size();
251   int a = 2;
252   for (anIndex = 0; anIndex  < aLength; anIndex++) {
253     sprintf(&(aProperty[a]),"%2d%2d%2d%2d%4d%s",
254             (int)(aMinutes[anIndex]),
255             (int)(aHours[anIndex]),
256             (int)(aDays[anIndex]),
257             (int)(aMonths[anIndex]),
258             (int)(aYears[anIndex]),
259             (char*)(aNames[anIndex].c_str()));
260     a = strlen(aProperty);
261     aProperty[a++] = 1;
262   }
263   aProperty[a] = 0;
264   string prop(aProperty);
265   delete aProperty;
266
267   return prop;
268 }
269
270 void SALOMEDSImpl_AttributeStudyProperties::Load(const string& value)
271 {
272   char* aCopy = (char*)value.c_str();
273
274   int crMode = (int)aCopy[0];
275   SetCreationMode(crMode);
276
277   int anIndex;
278   for (anIndex = 2; anIndex + 2 < value.size() ;) {
279     char str[10];
280     int aMinute, aHour, aDay, aMonth, aYear;
281     str[0] = aCopy[anIndex++];
282     str[1] = aCopy[anIndex++];
283     str[2] = 0;
284     aMinute = atoi(str);
285     str[0] = aCopy[anIndex++];
286     str[1] = aCopy[anIndex++];
287     aHour =  atoi(str);
288     str[0] = aCopy[anIndex++];
289     str[1] = aCopy[anIndex++];
290     aDay =  atoi(str);
291     str[0] = aCopy[anIndex++];
292     str[1] = aCopy[anIndex++];
293     aMonth =  atoi(str);
294     str[0] = aCopy[anIndex++];
295     str[1] = aCopy[anIndex++];
296     str[2] = aCopy[anIndex++];
297     str[3] = aCopy[anIndex++];
298     str[4] = 0;
299     aYear = atoi(str);
300
301     int aNameSize;
302     for(aNameSize = 0; aCopy[anIndex+aNameSize]!=1; aNameSize++);
303     char *aName = new char[aNameSize+1];
304     strncpy(aName, &(aCopy[anIndex]), aNameSize);
305     aName[aNameSize] = 0;
306     SetModification(aName,aMinute,aHour,aDay,aMonth,aYear);
307     delete(aName);
308     anIndex += aNameSize + 1;
309   }
310   if (aCopy[1] == 'l') {
311     SetLocked(true);
312   }
313   SetModified(0);
314 }