UltraScan III
us_sync_exper.cpp
Go to the documentation of this file.
1 
3 #include "us_sync_exper.h"
4 #include "us_util.h"
5 #include "us_settings.h"
6 
7 // Check synchronizing of experiment on database and local with raw data
8 US_SyncExperiment::US_SyncExperiment( US_DB2* a_db, QWidget* parent /*= 0*/ )
9 {
10  db = a_db;
11  parentw = parent;
13 
14  QString invID = QString::number( US_Settings::us_inv_ID() );
15 
16  // get lists of experimental IDs and run IDs
17  QStringList query;
18  query << "get_experiment_desc" << invID;
19  db->query( query );
20 
21  while ( db->next() )
22  {
23  expIDs << db->value( 0 ).toString();
24  runIDs << db->value( 1 ).toString();
25  }
26 }
27 
28 // flag state of synchronization of raw data with experiment
30 {
31  int status = 0;
32  QString runID;
33  QString oper = "upload/download";
34  QString lexpGUID = "";
35  QString dexpGUID = "";
36  QString rexpGUID = cdesc.parentGUID;
37  QStringList query;
38  bool havelraw = false;
39  bool havelexp = false;
40  bool havedraw = false;
41  bool havedexp = false;
42  bool haverexp = !rexpGUID.isEmpty();
43  bool no_go = true;
44 
45  if ( cdesc.recType != 1 ) // error return if this is not a raw record
46  return 3001;
47 
48  // get the associated experiment for this raw data record
49 
50  if ( ( cdesc.recState & US_DataModel::REC_LO ) != 0 )
51  { // there is a local raw record: check its parent
52  havelraw = true;
53  lexpGUID = expGUIDauc( cdesc.filename );
54  havelexp = !lexpGUID.startsWith( "0000" );
55  runID = cdesc.filename.section( "/", -1, -1 ).section( ".", 0, 0 );
56 
57  if ( ( cdesc.recState & US_DataModel::REC_DB ) == 0 )
58  { // local only, so this must be an upload operation
59  oper = "upload";
60 
61  if ( havelexp && havelraw )
62  no_go = false; // we can proceed if we have both exp and raw
63  }
64  }
65 
66  if ( ( cdesc.recState & US_DataModel::REC_DB ) != 0 )
67  { // there is a database raw record: check its parent
68  query.clear();
69  query << "get_rawDataID_from_GUID" << cdesc.dataGUID;
70 
71  db->query( query );
72 
73  if ( db->lastErrno() == US_DB2::OK )
74  { // get IDs for raw(AUC), experiment, and run
75  db->next();
76  rawID = db->value( 0 ).toString();
77  expID = db->value( 1 ).toString();
78  int jj = expIDs.indexOf( expID );
79  havedexp = ( jj >= 0 );
80  havedraw = true;
81  runID = ( havedexp ) ? runIDs.at( jj ) : runID;
82  cdesc.recordID = rawID.toInt();
83 
84  if ( havedexp )
85  { // pick up GUID for possible compare to local
86  query.clear();
87  query << "get_experiment_info" << expID;
88  db->query( query );
89  db->next();
90  dexpGUID = db->value( 0 ).toString();
91 DbgLv(1) << "db get_exp_inf: expGUID" << dexpGUID;
92 DbgLv(1) << "db get_exp_inf: projID " << db->value(1).toString();
93 DbgLv(1) << "db get_exp_inf: runID " << db->value(2).toString();
94 DbgLv(1) << "db get_exp_inf: rotorID" << db->value(6).toString();
95 DbgLv(1) << "db get_exp_inf: type " << db->value(7).toString();
96 DbgLv(1) << "db get_exp_inf: comment" << db->value(10).toString();
97 DbgLv(1) << "db get_exp_inf: dateUpd" << db->value(12).toString();
98  if ( dexpGUID.isEmpty() || dexpGUID.startsWith( "0000" ) )
99  { // try to set DB experiment GUID
100  QString projID = db->value( 1 ).toString();
101  QString runID = db->value( 2 ).toString();
102  QString labID = db->value( 3 ).toString();
103  QString instrID = db->value( 4 ).toString();
104  QString operID = db->value( 5 ).toString();
105  QString rotorID = db->value( 6 ).toString();
106  QString type = db->value( 7 ).toString();
107  QString runTemp = db->value( 8 ).toString();
108  QString label = db->value( 9 ).toString();
109  QString comment = db->value( 10 ).toString();
110  QString centrf = db->value( 11 ).toString();
111  QString persID = db->value( 13 ).toString();
112  dexpGUID = lexpGUID.isEmpty() ?
114  lexpGUID;
115  query.clear();
116  query << "update_experiment" << expID << dexpGUID
117  << projID << runID << labID << instrID
118  << operID << rotorID << type << runTemp << label
119  << comment << centrf;
120  int stat = db->statusQuery( query );
121 
122  if ( stat == US_DB2::OK )
123  {
124  DbgLv(1) << "Successful EXP update, GUID" << dexpGUID;
125  rexpGUID = dexpGUID;
126  haverexp = true;
127  cdesc.parentGUID = rexpGUID;
128  cdesc.parentID = expID.toInt();
129  cdesc.recordID = rawID.toInt();
130  DbgLv(1) << " EXP update: ID exp run raw" << expID << runID << rawID;
131  }
132  else
133  {
134  qDebug() << "*ERROR* EXP update, GUID" << dexpGUID;
135  dexpGUID = "";
136  status = 6000 + stat;
137  }
138  }
139  }
140  }
141 
142  if ( ( cdesc.recState & US_DataModel::REC_LO ) == 0 )
143  { // database only, so this must be an download operation
144  oper = "download";
145 
146  if ( havedexp && havedraw )
147  no_go = false;
148  }
149  }
150 
151  if ( havedexp && havelexp )
152  { // if we have experiment on db and local, check for consistency
153  if ( dexpGUID == lexpGUID )
154  no_go = false; // can proceed if GUIDs match
155 
156  else
157  no_go = true; // otherwise, we cannot
158  }
159 
160  if ( no_go )
161  { // appears we can not do the upload/download: inform user
162  QString msg =
163  tr( "A parent Experiment record in the database\n"
164  "or on local disk is missing or inconsistent, so\n"
165  "you are unable to %1 this Raw record.\n\n"
166  "You may separately run us_convert and create\n"
167  "or repair the experiment record; then return here\n"
168  "later to attempt the operation on this record.\n\n"
169  "NOTES:\n" )
170  .arg( oper );
171 
172  msg += havedexp ? tr( " Experiment is in database;\n" )
173  : tr( " Experiment missing from database;\n" );
174  msg += havelexp ? tr( " Experiment file exists locally;\n" )
175  : tr( " Experiment missing from local disk;\n" );
176  msg += havedraw ? tr( " Raw data is in database;\n" )
177  : tr( " Raw data missing from database;\n" );
178  msg += havelraw ? tr( " Raw data file exists locally;\n" )
179  : tr( " Raw data missing from local disk;\n" );
180  msg += tr( " Database experiment GUID:" );
181  msg += ( havedexp ? "\n \"" + dexpGUID + "\"\n"
182  : tr( " (missing)\n" ) );
183  msg += tr( " Local experiment GUID:" );
184  msg += ( havelexp ? "\n \"" + lexpGUID + "\"\n"
185  : tr( " (missing)\n" ) );
186  msg += tr( " Raw record parent GUID:" );
187  msg += ( haverexp ? "\n \"" + rexpGUID + "\"\n"
188  : tr( " (missing)\n" ) );
189 
190  QMessageBox::information( parentw,
191  tr( "Experiment Record Problem" ),
192  msg );
193  status = 3003;
194  }
195 
196  else
197  { // if we can to do operation, insure record has correct parent GUID
198  cdesc.parentGUID = dexpGUID.isEmpty() ? lexpGUID : dexpGUID;
199  }
200 
201  return status;
202 }
203 
204 // get the experiment GUID from the local XML file associated with an AUC
205 QString US_SyncExperiment::expGUIDauc( QString aucfile )
206 {
207  // default dummy GUID
208  QString expGUID = "00000000-0000-0000-0000-000000000000";
209  // compose experiment file name from that of AUC
210  QString expfnam = aucfile.section( "/", -1, -1 )
211  .section( ".", 0, 1 ) + ".xml";
212  QString expfile = aucfile.section( "/", 0, -2 ) + "/" + expfnam;
213 
214  QFile file( expfile );
215 
216  if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) )
217  { // navigate to experiment guid attribute
218  QXmlStreamReader xml( &file );
219 
220  while( ! xml.atEnd() )
221  {
222  xml.readNext();
223 
224  if ( xml.isStartElement() && xml.name() == "experiment" )
225  { // found it! return its value
226  QXmlStreamAttributes a = xml.attributes();
227  expGUID = a.value( "guid" ).toString();
228  break;
229  }
230  }
231 
232  file.close();
233  }
234 
235  return expGUID;
236 }
237