UltraScan III
us_sync_db.cpp
Go to the documentation of this file.
1 
3 #include "us_sync_db.h"
4 #include "us_settings.h"
5 #include "us_gui_settings.h"
6 #include "us_matrix.h"
7 #include "us_investigator.h"
8 #include "us_passwd.h"
9 #include "us_db2.h"
10 #include "us_util.h"
11 #include "us_constants.h"
12 
13 // Main constructor
14 
16 {
17  setAttribute ( Qt::WA_DeleteOnClose );
18  setWindowTitle( tr( "Synchronize Reports with Database" ) );
19  setPalette ( US_GuiSettings::frameColor() );
20  setMinimumSize( 300, 160 );
21  resize ( 400, 200 );
22 
23  // Main layout
24  QVBoxLayout* main = new QVBoxLayout( this );
25  main->setContentsMargins( 2, 2, 2, 2 );
26  main->setSpacing ( 2 );
27 
28  // Top layout: buttons and fields above list widget
29  QGridLayout* top = new QGridLayout;
30  int row = 0;
31 
32  // Investigator
33  // Only enable the investigator button for privileged users
34  pb_invest = us_pushbutton( tr( "Select Investigator" ) );
35 
36  int invlev = US_Settings::us_inv_level();
37  pb_invest->setEnabled( invlev > 0 );
38  connect( pb_invest, SIGNAL( clicked() ), SLOT( get_person() ) );
39  top->addWidget( pb_invest, row, 0 );
40 
41  QString name = ( invlev > 0 )
42  ? QString::number( US_Settings::us_inv_ID() ) + ": "
43  : "";
44 
46  -1, true );
47  top->addWidget( le_invest, row++, 1 );
48 
49  // Instructions text
50  te_desc = us_textedit();
51  us_setReadOnly( te_desc, true );
52  QString desc = tr(
53  "<b>Note:</b> Proceeding may result in local reports<br/>"
54  "being replaced from the database.<ul>"
55  "<li><b>Cancel</b> to abort synchronizing from the DB.</li>"
56  "<li><b>Download</b> to proceed with DB synchronization.</li>"
57  "<li><b>New Only</b> to only download new DB records.</li></ul>" );
58  te_desc->setHtml( desc );
59  top->addWidget( te_desc, row, 0, 4, 2 );
60 
61  main->addLayout( top );
62 
63  // Button Row
64  QHBoxLayout* buttons = new QHBoxLayout;
65  QPushButton* pb_cancel = us_pushbutton( tr( "Cancel" ) );
66  QPushButton* pb_accept = us_pushbutton( tr( "Download" ) );
67  QPushButton* pb_newonly = us_pushbutton( tr( "New Only" ) );
68 
69  connect( pb_cancel, SIGNAL( clicked() ), SLOT( cancelled() ) );
70  connect( pb_accept, SIGNAL( clicked() ), SLOT( accepted() ) );
71  connect( pb_newonly, SIGNAL( clicked() ), SLOT( downnew() ) );
72 
73  buttons->addWidget( pb_cancel );
74  buttons->addWidget( pb_accept );
75  buttons->addWidget( pb_newonly );
76 
77  main->addLayout( buttons );
78 
79 }
80 
81 // Investigator button clicked: get investigator from dialog
83 {
84  int invID = US_Settings::us_inv_ID();
85  US_Investigator* dialog = new US_Investigator( true, invID );
86 
87  dialog->exec();
88 }
89 
90 // Cancel button
92 {
93 qDebug() << "CANCELED";
94  reject();
95  close();
96 }
97 
98 // Download button: download all DB records and return accepted
100 {
101 qDebug() << "DOWNLOAD";
102  newonly = false;
103  scan_db_reports(); // Download reports from the database
104 
105  accept(); // Signal that selection was accepted
106  close();
107 }
108 
109 // New Only button: download only new DB records and return accepted
111 {
112 qDebug() << "NEWONLY";
113  newonly = true;
114  scan_db_reports(); // Download new reports from the database
115 
116  accept(); // Signal that selection was accepted
117  close();
118 }
119 
120 // Scan database for reports
122 {
123 //qDebug() << "ScDB:TM:00: " << QTime::currentTime().toString("hh:mm:ss:zzzz");
124  US_Passwd pw;
125  US_DB2 db( pw.getPasswd() );
126 
127  if ( db.lastErrno() != US_DB2::OK )
128  {
129  QMessageBox::information( this,
130  tr( "DB Connection Problem" ),
131  tr( "There was an error connecting to the database:\n" )
132  + db.lastError() );
133  return;
134  }
135 
136  QString desc = tr(
137  "<b>Note:</b> Proceeding may result in local reports<br/>"
138  "being replaced from the database.<ul>"
139  "<li><b>Cancel</b> to abort synchronizing from the DB.</li>"
140  "<li><b>Download</b> to proceed with DB synchronization.</li>"
141  "<li><b>New Only</b> to only download new DB records.</li></ul>"
142  "<b>Downloading report records from the database...</b>" );
143  te_desc->setHtml( desc );
144  qApp->processEvents();
145  QApplication::setOverrideCursor( QCursor( Qt::WaitCursor ) );
146 
147  QStringList query;
148  QString invID = QString::number( US_Settings::us_inv_ID() );
149 
150  setWindowTitle( tr( "Download Report Data from the Database" ) );
151 
152  query.clear();
153  query << "count_reports" << invID;
154  int nreports = db.functionQuery( query );
155 qDebug() << "Reports count" << nreports;
156  desc = desc + "<br/>&nbsp;&nbsp;&nbsp;";
157  int nbdchr = desc.length();
158  desc = desc.left( nbdchr )
159  + tr( "Total DB run reports is %1" ).arg( nreports );
160  te_desc->setHtml( desc );
161  qApp->processEvents();
162 
163  // Determine runIDs of existing local results
164  QString resdir = US_Settings::resultDir() + "/";
165  QDir dirres( resdir );
166  QStringList resruns = dirres.entryList(
167  QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name );
168 
169  query.clear();
170  query << "get_report_desc" << invID;
171  db.query( query );
172  QStringList runids;
173  int kreports = 0;
174  int jreports = 0;
175  nrunrpl = nrunadd = ndocrpl = ndocadd = 0;
176  QString rptdir = US_Settings::reportDir() + "/";
177  QDir dirrpt( rptdir );
178 
179  while ( db.next() )
180  {
181  QString runid = db.value( 4 ).toString();
182  jreports++;
183  if ( resruns.contains( runid ) )
184  {
185  runids << runid;
186 qDebug() << " report id" << db.value(0).toString() << "runID" << runid;
187  kreports++;
188  }
189  }
190 qDebug() << "Report descs count" << kreports;
191 
192  desc = desc.left( nbdchr )
193  + tr( "DB reports for local runIDs is %1" ).arg( kreports );
194  te_desc->setHtml( desc );
195  qApp->processEvents();
196 
197  for ( int ii = 0; ii < kreports; ii++ )
198  {
199  QString runid = runids[ ii ];
200  QString rundir = rptdir + runid;
201  QString runresd = resdir + runid;
202 
203  US_Report freport;
204  freport.readDB( runid, &db );
205 
206  int ntriples = freport.triples.count();
207 qDebug() << " Report" << ii << "triples count" << ntriples << "runID" << runid;
208 
209  if ( ntriples > 0 )
210  {
211  if ( QDir( rundir ).exists() )
212  {
213  if ( newonly ) continue;
214  nrunrpl++;
215  }
216 
217  else
218  {
219  nrunadd++;
220  dirrpt.mkdir( runid );
221  }
222  }
223 
224  int kdocadd = 0;
225 
226  for ( int jj = 0; jj < ntriples; jj++ )
227  {
228  US_Report::ReportTriple* tripl = &freport.triples[ jj ];
229  int ndocs = tripl->docs.count();
230 qDebug() << " Triple" << jj << "docs count" << ndocs;
231 
232  desc = desc.left( nbdchr )
233  + tr( "Run %1, Triple %2: comparing %3 documents...")
234  .arg( ii + 1 ).arg( jj + 1 ).arg( ndocs );
235  te_desc->setHtml( desc );
236  qApp->processEvents();
237 
238  for ( int kk = 0; kk < ndocs; kk++ )
239  {
240  US_Report::ReportDocument* doc = &tripl->docs[ kk ];
241  QString fname = doc->filename;
242 qDebug() << " Doc" << kk << "filename" << fname << "ID" << doc->documentID;
243  QString fpath = rundir + "/" + fname;
244  if ( fname.endsWith( ".csv" ) )
245  fpath = runresd + "/" + fname;
246 
247  QFile dfile( fpath );
248 
249  if ( dfile.exists() )
250  {
251  if ( newonly ) continue;
252  ndocrpl++;
253  }
254 
255  else
256  {
257  ndocadd++;
258  }
259 
260  int st = doc->readDB( rundir, &db );
261 
262  if ( st != US_DB2::OK )
263  {
264  qDebug() << "ReportDoc read ERROR: status" << st << fpath;
265  }
266  }
267  }
268 
269  if ( newonly && kdocadd != ndocadd )
270  nrunrpl++;
271 
272  }
273  QApplication::restoreOverrideCursor();
274 
275  QMessageBox::information( this,
276  tr( "DB Reports Downloaded" ),
277  tr( "Run IDs: %1 updated, %2 added.\n"
278  "Documents: %3 replaced, %4 added." )
279  .arg( nrunrpl ).arg( nrunadd ).arg( ndocrpl ).arg( ndocadd ) );
280 qDebug() << "nrunrpl nrunadd ndocrpl ndocadd" << nrunrpl << nrunadd
281  << ndocrpl << ndocadd;
282 }
283