AMScanDatabaseImportWizardDoImportPage::AMScanDatabaseImportWizardDoImportPage(AMScanDatabaseImportController *controller, QWidget *parent) : QWizardPage(parent)
{
	controller_ = controller;

	progressBar_ = new QProgressBar();
	progressBar_->setRange(0, 100);
	progressBar_->setOrientation(Qt::Horizontal);
	progressLabel_ = new QLabel();

	setTitle("Importing...");

	QHBoxLayout* hl2 = new QHBoxLayout();
	hl2->addWidget(progressLabel_);
	hl2->addWidget(progressBar_);


	QVBoxLayout* vl = new QVBoxLayout();
	vl->addWidget(new QLabel("Pleases wait while this Acquaman Bundle is imported into your library."));
	vl->addStretch();
	vl->addLayout(hl2);

	setLayout(vl);

	connect(controller_, SIGNAL(stepProgress(int)), progressBar_, SLOT(setValue(int)));
	connect(controller_, SIGNAL(progressDescription(QString)), progressLabel_, SLOT(setText(QString)));
	connect(controller_, SIGNAL(stepProgress(int)), this, SIGNAL(completeChanged()));
	connect(controller_, SIGNAL(progressDescription(QString)), this, SIGNAL(completeChanged()));
}
Esempio n. 2
0
void AMScanDatabaseImportController::copySamples()
{
	if(state_ != Importing) return;
	emit progressDescription("Copying Samples...");
	emit stepProgress(-1);
	int totalSteps = s2dSampleIds_.count();
	int currentStep = 0;

	QMutableMapIterator<int, int> i(s2dSampleIds_);
	while (i.hasNext()) {
		if(state_ != Importing) return;
		i.next();
		emit stepProgress(int(100.0*(++currentStep)/totalSteps));
		// key is id; value is id in destination database, or -1 if not there yet. (if not there, need to insert)
		int sourceId = i.key(), destinationId = i.value();
		if(destinationId<1) {
			AMSample s;
			s.loadFromDb(sourceDb_, sourceId);
			if(s.storeToDb(destinationDb_))
				i.setValue(s.id());
			else
				AMErrorMon::report(AMErrorReport(this, AMErrorReport::Alert, -2, "Could not import the sample '" % sourceSamples_.value(sourceId) % "' into the database."));
		}
		qApp->sendPostedEvents();
		qApp->processEvents();
	}
}
Esempio n. 3
0
void AMScanDatabaseImportController::copyExperiments()
{
	if(state_ != Importing) return;
	emit progressDescription("Copying Experiments...");
	emit stepProgress(-1);
	int totalSteps = s2dExperimentIds_.count();
	int currentStep = 0;

	QMutableMapIterator<int, int> i(s2dExperimentIds_);
	while (i.hasNext()) {
		if(state_ != Importing) return;
		i.next();
		emit stepProgress(int(100.0*(++currentStep)/totalSteps));
		// key is id; value is id in destination database, or -1 if not there yet. (if not there, need to insert)
		int sourceId = i.key(), destinationId = i.value();
		if(destinationId<1) {
			AMExperiment e;
			e.loadFromDb(sourceDb_, sourceId);
			if(e.storeToDb(destinationDb_))
				i.setValue(e.id());
			else
				AMErrorMon::report(AMErrorReport(this, AMErrorReport::Alert, -2, "Could not import the experiment '" % sourceExperiments_.value(sourceId) % "' into the database."));
		}
		// TODO: need to update the scans that should be in this experiment. This will be tricky... don't know new scan IDs yet.
		qApp->sendPostedEvents();
		qApp->processEvents();
	}
}
Esempio n. 4
0
void AMScanDatabaseImportController::analyzeFacilitiesForDuplicates()
{
	if(state_ != Analyzing) {
		return;
	}

	sourceFacilities_.clear();
	s2dFacilityIds_.clear();

	emit progressDescription("Checking Facilities...");
	emit stepProgress(-1);

	// go through facilities in source, and make sure they all exist in destination. Map ids.
	QSqlQuery q = AMDbObjectSupport::s()->select<AMFacility>(sourceDb_, "id, name, description");
	q.exec();
	int stepIndex = 0;
	int totalSteps = q.size();
	while(q.next()) {
		emit stepProgress(int(100.0*(++stepIndex) / totalSteps));	// if size is not supported in db, then this will be negative... which will trigger a "undefined" progress bar. Perfect.

		int id = q.value(0).toInt();
		QString name = q.value(1).toString();
		QString description = q.value(2).toString();

		sourceFacilities_[id] = name % " " % description;

		// Find a matching one in destinationDb_.
		QSqlQuery q2 = AMDbObjectSupport::s()->select<AMFacility>(destinationDb_, "id", "name = ? AND description = ?");
		q2.bindValue(0, name);
		q2.bindValue(1, description);
		q2.exec();
		if(q2.first()) {
			// found one.
			s2dFacilityIds_[id] = q2.value(0).toInt();	// insert a mapping from source facility IDs to destination facility IDs
		}
		else {
			// none found in destination. Will need to create it.
			s2dFacilityIds_[id] = -1;
		}

		qApp->sendPostedEvents();
		qApp->processEvents();
	}

	emit stepProgress(100);
}
Esempio n. 5
0
void AMScanDatabaseImportController::analyzeSamplesForDuplicates()
{
	if(state_ != Analyzing)
		return;

	sourceSamples_.clear();
	s2dSampleIds_.clear();

	emit progressDescription("Reviewing Samples...");
	emit stepProgress(-1);

	// go through samples in source, and see if any exist in destination
	QSqlQuery q = AMDbObjectSupport::s()->select<AMSample>(sourceDb_, "id, name, dateTime");
	q.exec();
	int stepIndex = 0;
	int totalSteps = q.size();
	while(q.next()) {
		emit stepProgress(int(100.0*(++stepIndex) / totalSteps));	// if size is not supported in db, then this will be negative... which will trigger a "undefined" progress bar. Perfect.

		int id = q.value(0).toInt();
		QString name = q.value(1).toString();
		QDateTime dateTime = q.value(2).toDateTime();

		sourceSamples_[id] = name % ", created " % AMDateTimeUtils::prettyDateTime(dateTime);

		// Find a matching one in destinationDb_.
		QSqlQuery q2 = AMDbObjectSupport::s()->select<AMSample>(destinationDb_, "id", "name = ? AND dateTime = ?");
		q2.bindValue(0, name);
		q2.bindValue(1, dateTime);
		q2.exec();
		if(q2.first()) {
			// found one.
			s2dSampleIds_[id] = q2.value(0).toInt();	// insert a mapping from source sample IDs to destination sample IDs
		}
		else {
			// none found in destination. Will need to create it.
			s2dSampleIds_[id] = -1;
		}
		qApp->sendPostedEvents();
		qApp->processEvents();
	}

	emit stepProgress(100);
	state_ = ReadyToImport;
}
AMScanDatabaseImportWizardReviewSamplesPage::AMScanDatabaseImportWizardReviewSamplesPage(AMScanDatabaseImportController *controller, QWidget *parent) : QWizardPage(parent)
{
	controller_ = controller;

	progressBar_ = new QProgressBar();
	progressBar_->setRange(0, 100);
	progressBar_->setOrientation(Qt::Horizontal);
	progressLabel_ = new QLabel();

	setTitle("Review Samples");

	itemsFoundList_ = new QListWidget();
	itemsToMergeList_ = new QListWidget();
	QGroupBox* gb1 = new QGroupBox("Samples found");
	QGroupBox* gb2 = new QGroupBox("Already in your library\n(Check to keep separate)");
	gb1->setLayout(new QVBoxLayout());
	gb1->layout()->addWidget(itemsFoundList_);
	gb2->setLayout(new QVBoxLayout());
	gb2->layout()->addWidget(itemsToMergeList_);

	QHBoxLayout* hl = new QHBoxLayout();
	hl->addWidget(gb1);
	hl->addWidget(gb2);

	QHBoxLayout* hl2 = new QHBoxLayout();
	hl2->addWidget(progressLabel_);
	hl2->addWidget(progressBar_);


	QVBoxLayout* vl = new QVBoxLayout();
	vl->addWidget(new QLabel("These are the samples we found in the bundle.\n\nAny samples that are already in your library are shown on the right and will be merged automatically. \nIf you want to keep these separate instead, check the 'Keep Separate' box and a new sample will be added.\n"));
	vl->addLayout(hl);
	vl->addStretch();
	vl->addLayout(hl2);

	setLayout(vl);
	setMinimumHeight(400);

	connect(controller_, SIGNAL(stepProgress(int)), progressBar_, SLOT(setValue(int)));
	connect(controller_, SIGNAL(progressDescription(QString)), progressLabel_, SLOT(setText(QString)));

	setCommitPage(true);
}
Esempio n. 7
0
void AMScanDatabaseImportController::startDatabaseOperations()
{
	if(state_ != ReadyToImport)
		return;

	destinationDb_->startTransaction();
	state_ = Importing;
	copyFacilities();
	copyRuns();
	copyExperiments();
	copySamples();
	copyScans();
	if(state_ == Importing) {	// if we were cancelled, don't commit the transaction.
		state_ = Finished;
		destinationDb_->commitTransaction();
		emit progressDescription("Import complete.");
		emit stepProgress(100);
	}
}
Esempio n. 8
0
void AMScanDatabaseImportController::copyScans()
{
	if(state_ != Importing) return;

	emit progressDescription("Copying Scans...");
	emit stepProgress(-1);

	QString scanTableName = AMDbObjectSupport::s()->tableNameForClass<AMScan>();


	QList<int> scanIds = sourceDb_->objectsWhere(scanTableName, QString());
	AMScan::setAutoLoadData(false);	// disable loading data for performance. It will also fail to load because it's in the wrong spot.
	for(int i=0, cc=scanIds.count(); i<cc; i++) {
		if(state_ != Importing) return;

		emit stepProgress(int(100.0*(i+1)/cc));

		AMDbObject* object = AMDbObjectSupport::s()->createAndLoadObjectAt(sourceDb_, scanTableName, scanIds.at(i));
		if(!object) {
			AMErrorMon::report(AMErrorReport(this,
											 AMErrorReport::Alert,
											 -3,
											 QString("Error copying the scan with ID '%1' out of the import database. Your database might be corrupted. Please report this problem to the Acquaman developers.").arg(scanIds.at(i))));
			continue;
		}
		AMScan* scan = qobject_cast<AMScan*>(object);
		if(!scan) {
			AMErrorMon::report(AMErrorReport(this,
											 AMErrorReport::Alert,
											 -4,
											 QString("Error copying the scan with ID '%1' out of the import database: it wasn't a scan object. Your database might be corrupted. Please report this problem to the Acquaman developers.").arg(scanIds.at(i))));
			delete object;
			continue;
		}
		scan->retain(this);

		// update scan fields:
		scan->setRunId(s2dRunIds_.value(scan->runId(), -1));
		scan->setSampleId(s2dSampleIds_.value(scan->sampleId(), -1));

		// copy the raw data: filePath and additionalFilePaths
		QDir destinationDir(destinationPath_);
		// Assuming paths always in relative file format.
		QString filePath = scan->filePath();

		// Does the file exist already in the destination?
		if(destinationDir.exists(filePath)) {
			// I know we're not supposed to be a GUI module... but is it okay to prompt here for what to do?
			if(userSaysShouldSkipDuplicateScan(scan)) {
				scan->release(this);
				continue;
			}
			filePath = makeUniqueFileName(destinationPath_, filePath);
		}
		copyFileAndMakeFoldersAsRequired(sourcePath_ % "/" % scan->filePath(),
										 destinationPath_ % "/" % filePath);
		scan->setFilePath(filePath);

		// Copy additional file paths:
		QStringList extraPaths = scan->additionalFilePaths();
		for(int j=0, cc=extraPaths.count(); j<cc; j++) {
			QString destinationFilePath = extraPaths.at(j);
			if(destinationDir.exists(destinationFilePath)) {
				destinationFilePath = makeUniqueFileName(destinationPath_, destinationFilePath);
			}
			copyFileAndMakeFoldersAsRequired(sourcePath_ % "/" % extraPaths.at(j),
											 destinationPath_ % "/" % destinationFilePath);
			extraPaths[j] = destinationFilePath;
		}
		scan->setAdditionalFilePaths(extraPaths);


		// OK. raw data copied and file paths adjusted. Just store this scan in the new db:
		if(!scan->storeToDb(destinationDb_, false)) {	// HOLD UP! Don't generate those thumbnails... We'll do a direct copy.
			AMErrorMon::report(AMErrorReport(this,
											 AMErrorReport::Alert,
											 -4,
											 QString("Error copying the scan with ID '%1' into the new database. Your database might be corrupted. Please report this problem to the Acquaman developers.").arg(scanIds.at(i))));
			scan->release(this);
			continue;
		}



		// Copy the thumbnails from the old DB to the new DB:
		// find the old thumbnails:
		QStringList thumbnailCols;
		thumbnailCols << "objectId" << "objectTableName" << "number" << "type" << "title" << "subtitle" << "thumbnail";
		int thumbnailFirstId = -1;
		int thumbnailCount = 0;
		QSqlQuery q = sourceDb_->select(AMDbObjectSupport::thumbnailTableName(),
										thumbnailCols.join(","),
										"objectId = ? AND objectTableName = ?");
		q.bindValue(0, scanIds.at(i));
		q.bindValue(1, scanTableName);
		if(!q.exec()) {
			AMErrorMon::report(AMErrorReport(this,
											 AMErrorReport::Alert,
											 -4,
											 QString("Error copying the the thumbnails for scan with ID '%1' out of the old database. Please report this problem to the Acquaman developers.").arg(scanIds.at(i))));
		}
		else {
			if(q.first()) {
				QVariantList thumbnailVals;
				thumbnailVals << q.value(0) << q.value(1) << q.value(2) << q.value(3) << q.value(4) << q.value(5) << q.value(6);
				thumbnailVals[0] = scan->id();	// update the scan id from old DB to new DB's value
				thumbnailFirstId = destinationDb_->insertOrUpdate(0, AMDbObjectSupport::thumbnailTableName(), thumbnailCols, thumbnailVals);
				if(thumbnailFirstId > 0)
					thumbnailCount++;
				else
					AMErrorMon::report(AMErrorReport(this,
													 AMErrorReport::Alert,
													 -4,
													 QString("Error copying the the thumbnails for scan with ID '%1' into the new database. Please report this problem to the Acquaman developers.").arg(scanIds.at(i))));
			}
			while(q.next()) {
				QVariantList thumbnailVals;
				thumbnailVals << q.value(0) << q.value(1) << q.value(2) << q.value(3) << q.value(4) << q.value(5) << q.value(6);
				thumbnailVals[0] = scan->id(); // update the scan id from old DB to new DB's value
				if(destinationDb_->insertOrUpdate(0, AMDbObjectSupport::thumbnailTableName(), thumbnailCols, thumbnailVals) > 0)
					thumbnailCount++;
			}
		}

		// Finally, update the thumbnail references in the scan object:
		// now that we know where the thumbnails are, update this in the object table
		if(!destinationDb_->update(scan->id(),
								   scanTableName,
								   QStringList() << "thumbnailCount" << "thumbnailFirstId",
								   QVariantList() << thumbnailCount << thumbnailFirstId)) {
			AMErrorMon::report(AMErrorReport(this, AMErrorReport::Alert, AMSCANDATABASEIMPORTCONTROLLER_ERROR_STORING_UPDATED_THUMBNAIL_COUNT_AND_FIRST_ID, QString("While importing, error trying to store the updated thumbnail count and firstThumbnailId for database object %1. Please report this problem to the Acquaman developers.").arg(scanIds.at(i))));
		}


		// What experiments was this scan in? Need to map and add in new database.
		QList<int> experiments = AMExperiment::experimentsContainingScan(scanIds.at(i), sourceDb_);
		foreach(int oldExperimentId, experiments) {
			int newExperimentId = s2dExperimentIds_.value(oldExperimentId);
			if(newExperimentId > 0)
				AMExperiment::addScanToExperiment(scan->id(), newExperimentId, destinationDb_);
		}

		// All done!
		scan->release();
		qApp->sendPostedEvents();
		qApp->processEvents();
	}