@@ -3324,14 +3324,51 @@ void InterSpec::saveStateToDb( Wt::Dbo::ptr<UserState> entry )
33243324 // JIC, make sure indices have all been assigned to everything.
33253325 m_sql->session ()->flush ();
33263326
3327- auto create_copy_or_update_in_db = [this , deepCopy, &entry]( Dbo::ptr<UserFileInDb> &dbfile, shared_ptr<const SpecMeas> &file ){
3328- if ( !dbfile || !file )
3327+ auto create_copy_or_update_in_db = [this , deepCopy, &entry]( Dbo::ptr<UserFileInDb> &dbfile,
3328+ shared_ptr<const SpecMeas> &file, const SpecUtils::SpectrumType type ){
3329+ if ( !file )
33293330 return ;
3330-
3331- // We dont need to make a copy of the file if it was not part of a save state, but now its
3332- // becoming part of one
3333- if ( deepCopy && (dbfile->isPartOfSaveState || dbfile->snapshotParent ) )
3331+
3332+ if ( !dbfile )
33343333 {
3334+ // We seem to get here if we recently uploaded the foreground file, so it hasnt been saved
3335+ // to the database yet.
3336+ Wt::Dbo::ptr<UserFileInDb> answer;
3337+
3338+ SpectraFileModel *fileModel = m_fileManager->model ();
3339+ assert ( measurment (type) == file );
3340+
3341+ const WModelIndex index = fileModel->index ( file );
3342+ assert ( index.isValid () );
3343+
3344+ if ( !index.isValid () )
3345+ throw runtime_error ( " Error saving " + string (SpecUtils::descriptionText (type))
3346+ + " spectrum file to the database - unable to find in SpectraFileModel!?!" );
3347+
3348+ shared_ptr<SpectraFileHeader> header = fileModel->fileHeader ( index.row () );
3349+ assert ( header );
3350+ if ( !header )
3351+ throw runtime_error ( " Error saving " + string (SpecUtils::descriptionText (type))
3352+ + " spectrum file to the database - no associated header in SpectraFileModel!?!." );
3353+
3354+ try
3355+ {
3356+ header->saveToDatabase ( file );
3357+ }catch ( std::exception &e )
3358+ {
3359+ throw runtime_error ( " Error saving " + string (SpecUtils::descriptionText (type))
3360+ + " spectrum file to the database - call to write to db failed: " + string (e.what ()) );
3361+ }
3362+
3363+ dbfile = header->dbEntry ();
3364+ assert ( dbfile );
3365+ if ( !dbfile )
3366+ throw runtime_error ( " Error saving " + string (SpecUtils::descriptionText (type))
3367+ + " spectrum file to the database - unexpectedly missing reference in db!?!." );
3368+ }else if ( deepCopy && (dbfile->isPartOfSaveState || dbfile->snapshotParent ) )
3369+ {
3370+ // We dont need to make a copy of the file if it was not part of a save state, but now its
3371+ // becoming part of one
33353372 dbfile = UserFileInDb::makeDeepCopyOfFileInDatabase ( dbfile, *m_sql, true );
33363373 m_sql->session ()->flush ();
33373374 }
@@ -3363,7 +3400,7 @@ void InterSpec::saveStateToDb( Wt::Dbo::ptr<UserState> entry )
33633400 filedata.modify ()->setFileData ( file, UserFileInDbData::SerializedFileFormat::k2012N42 );
33643401 };// create_copy_or_update_in_db lambda
33653402
3366- create_copy_or_update_in_db ( dbforeground, foreground );
3403+ create_copy_or_update_in_db ( dbforeground, foreground, SpecUtils::SpectrumType::Foreground );
33673404 if ( !dbforeground && foreground )
33683405 throw runtime_error ( " Error saving foreground to the database" ); // not displayed to user, so not internationalized
33693406
@@ -3374,7 +3411,7 @@ void InterSpec::saveStateToDb( Wt::Dbo::ptr<UserState> entry )
33743411 dbsecond = dbforeground;
33753412 }else
33763413 {
3377- create_copy_or_update_in_db ( dbsecond, second );
3414+ create_copy_or_update_in_db ( dbsecond, second, SpecUtils::SpectrumType::SecondForeground );
33783415 if ( !dbsecond )
33793416 throw runtime_error ( " Error saving second foreground to the database" );
33803417 }
@@ -3390,7 +3427,7 @@ void InterSpec::saveStateToDb( Wt::Dbo::ptr<UserState> entry )
33903427 dbbackground = dbsecond;
33913428 }else
33923429 {
3393- create_copy_or_update_in_db ( dbbackground, background );
3430+ create_copy_or_update_in_db ( dbbackground, background, SpecUtils::SpectrumType::Background );
33943431 if ( !dbbackground )
33953432 throw runtime_error ( " Error saving background to the database" );
33963433 }
@@ -3706,7 +3743,9 @@ void InterSpec::saveStateToDb( Wt::Dbo::ptr<UserState> entry )
37063743 }catch ( std::exception &e )
37073744 {
37083745 cerr << " saveStateToDb(...) caught: " << e.what () << endl;
3709- throw runtime_error ( WString::tr (" err-save-sate-to-db" ).toUTF8 () );
3746+ string errmsg = e.what ();
3747+ errmsg = Wt::Utils::htmlEncode ( errmsg, WFlags<Wt::Utils::HtmlEncodingFlag>{} );
3748+ throw runtime_error ( WString::tr (" err-save-sate-to-db" ).arg (errmsg).toUTF8 () );
37103749 }// try / catch
37113750}// void saveStateToDb( Wt::Dbo::ptr<UserState> entry )
37123751
0 commit comments