Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thermohub import #17

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions GUI/Dataman/v_dbm.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ class TDataBase
bool isDelete;
int frstOD;
unsigned char nOD;
int frstODjson;
int lastODjson;
TDBKeyList ind;
std::vector< std::shared_ptr<TDBFile>> aFile; // list of add files
TCIntArray fls;
Expand Down Expand Up @@ -254,6 +256,11 @@ class TDataBase
void toCFG( fstream& f );
void AddFile(const std::string& path);
void DelFile(const std::string& path);
void updateJsonOD(int frstOD, int lastOD)
{
frstODjson =frstOD;
lastODjson =lastOD;
}


//--- Selectors
Expand Down Expand Up @@ -393,6 +400,8 @@ class TDataBase

void toJsonObject( QJsonObject& obj ) const;
std::string fromJsonObject( const QJsonObject& obj );
void toJsonObjectNew(QJsonObject &obj, const std::string& project_name) const;
std::string fromJsonObjectNew( const QJsonObject& obj );
};

// Data Base container : rt
Expand Down
50 changes: 47 additions & 3 deletions GUI/Dataman/v_dbm2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ out_stream << isDelete << " "; // endl;
out_stream << frstOD << ' '; // endl;
// f.write( (char*)&nOD, sizeof(unsigned char) );
out_stream << uint(nOD) << " "; // endl;
out_stream << frstODjson << " "; // endl;
out_stream << lastODjson << " "; // endl;
// f.write( (char*)&specialFilesNum, sizeof(int) );
out_stream << specialFilesNum << " "; // endl;

Expand Down Expand Up @@ -129,6 +131,8 @@ in_stream >> frstOD;
// f.read( (char*)&nOD, sizeof(unsigned char) );
in_stream >> tmp;
nOD = tmp;
in_stream >> frstODjson;
in_stream >> lastODjson;
// f.read( (char*)&specialFilesNum, sizeof(int) );
in_stream >> specialFilesNum;

Expand Down Expand Up @@ -159,7 +163,7 @@ TDataBase::TDataBase( size_t nrt, const char* name,
bool Rclose, bool isDel, int nOf, unsigned char Nobj,
int filesNum, unsigned char nRkflds, const unsigned char* rkfrm ):
nRT(nrt), status(UNDF_), rclose(Rclose), isDelete(isDel),
frstOD(nOf), nOD(Nobj),
frstOD(nOf), nOD(Nobj), frstODjson(frstOD), lastODjson(frstOD+nOD-1),
ind( nRkflds, rkfrm ),
aFile(), fls(), specialFilesNum(filesNum)
{
Expand Down Expand Up @@ -541,9 +545,9 @@ void TDataBase::toJsonObject( QJsonObject &obj ) const

// write objects
QJsonArray dodArray;
for( int ii=0, no=frstOD; ii<nOD; ii++, no++)
for( int no=frstODjson; no<=lastODjson; no++)
{
if( aObj[no]->GetDescription(0,0) == "ejdbignore" )
if( frstODjson!=frstOD && aObj[no]->GetDescription(0,0) == "internaldb" )
continue;
QJsonObject dodObject;
dodObject[ "id" ] = no;
Expand Down Expand Up @@ -591,6 +595,46 @@ std::string TDataBase::fromJsonObject(const QJsonObject &obj)
}


void TDataBase::toJsonObjectNew(QJsonObject &obj, const std::string& project_name) const
{
QJsonArray keyArray;
for( int ii=0; ii < KeyNumFlds(); ii++) {
auto fld_key = char_array_to_string( FldKey(ii), FldLen(ii) );
strip(fld_key);
keyArray.append(fld_key.c_str());
}
obj["key"] = keyArray;
obj["project"] = project_name.c_str();

QJsonObject dodAll;
for( int no=frstODjson; no<=lastODjson; no++) {
if( frstODjson!=frstOD && aObj[no]->GetDescription(0,0) == "internaldb" )
continue;
dodAll[aObj[no]->GetKeywd()] = aObj[no]->toJsonValue();
}
obj["dod"] = dodAll;
}

std::string TDataBase::fromJsonObjectNew(const QJsonObject &obj)
{
QJsonArray keyArray = obj["key"].toArray();
string keyStr = "", kbuf;
for(int ii=0; ii< std::min<int>(KeyNumFlds(), keyArray.size()); ii++ ) {
kbuf = keyArray[ii].toString("*").toStdString();
strip( kbuf );
keyStr += kbuf.substr(0, FldLen( ii ));
keyStr += ":";
}
auto dodAll = obj["dod"].toObject();
for( int no=frstODjson; no<=lastODjson; no++) {
if( frstODjson!=frstOD && aObj[no]->GetDescription(0,0) == "internaldb" )
continue;
aObj[no]->fromJsonValue(dodAll[aObj[no]->GetKeywd()]);
}
return keyStr;
}


//Read record update time from PDB file to memory.
time_t TDataBase::GetTime( uint i )
{
Expand Down
108 changes: 57 additions & 51 deletions GUI/Dataman/v_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,7 @@ string TCModule::makeKeyFilter()
( RT_PARAM==nRT || RT_SYSEQ==nRT || RT_PROCES==nRT ||
RT_GTDEMO==nRT || RT_UNSPACE==nRT || RT_DUALTH==nRT || RT_GEM2MT==nRT ) )
{
strfilt = char_array_to_string( rt[RT_PARAM]->FldKey(0), rt[RT_PARAM]->FldLen(0) );
StripLine(strfilt);
strfilt = TProfil::pm->projectName();
strfilt += ":";
}
else
Expand All @@ -322,8 +321,7 @@ bool TCModule::testKeyFilter()
( RT_PARAM==nRT || RT_SYSEQ==nRT || RT_PROCES==nRT ||
RT_GTDEMO==nRT || RT_UNSPACE==nRT || RT_DUALTH==nRT || RT_GEM2MT==nRT ) )
{
string strfilt = char_array_to_string( rt[RT_PARAM]->FldKey(0), rt[RT_PARAM]->FldLen(0) );
StripLine(strfilt);
string strfilt = TProfil::pm->projectName();
strfilt += ":";
if(Filter.rfind(strfilt, 0) != 0)
return true;
Expand Down Expand Up @@ -1637,33 +1635,39 @@ TCModule::CmRestore()

void TCModule::CmBackuptoJson()
{
try
{
try {
if( !MessageToSave() )
return;

RecListToJSON( Filter.c_str() );
std::string s=GetName();
std::string filename = s + ".backup.json";
s += " : Please, give a file name for unloading records";
if( vfChooseFileSave( window(), filename, s.c_str(), "*.json" ) == false )
return;

RecListToJSON(Filter.c_str(), filename);
pVisor->Update();
}
catch( TError& xcpt )
{
catch( TError& xcpt ) {
pVisor->Update();
vfMessage(window(), xcpt.title, xcpt.mess);
}
}

void TCModule::CmRestorefromJson()
{
try
{
try {
if( !MessageToSave() )
return;

RecListFromJSON();
// Choose file name
std::string s = std::string( GetName() )+" : Please, select file with unloaded records";
std::string filename;
if( vfChooseFileOpen( window(), filename, s.c_str(), "*.json" ) == false )
return;
RecListFromJSON(filename);
pVisor->Update();
}
catch( TError& xcpt )
{
catch( TError& xcpt ) {
pVisor->Update();
vfMessage(window(), xcpt.title, xcpt.mess);
}
Expand Down Expand Up @@ -2017,69 +2021,71 @@ void TCModule::RecImport()
dyn_set();
}

void TCModule::RecListToJSON(const char *pattern)
void TCModule::RecListToJSON(const char *pattern, const std::string& filename, bool all_records)
{
TCStringArray aKey = vfMultiKeys( window(),
"Please, mark records to be unloaded to JSON",
nRT, pattern );
if( aKey.size() <1 )
return;

string s = GetName();
string filename = GetName();
filename += "_backup.json";
s += " : Please, give a file name for unloading records";
if( vfChooseFileSave( window(), filename, s.c_str(), "*.json" ) == false )
TCStringArray aKey;
if( all_records ) {
TCIntArray anR;
db->GetKeyList(pattern, aKey, anR);
}
else {
aKey = vfMultiKeys(window(), "Please, mark records to be unloaded to JSON", nRT, pattern);
}
if( aKey.size()<1 ) {
return;
}
// get project name from file
auto pos_ext = filename.find_last_of(".");
auto pos_name = filename.find_last_of(".", pos_ext-1);
if(pos_name == std::string::npos) {
pos_name = 0;
}
else {
pos_name += 1;
}
std::string project_name = filename.substr(pos_name, pos_ext-pos_name);

QJsonArray allArray;
for( size_t i=0; i<aKey.size(); i++ )
{
int Rnum = db->Find( aKey[i].c_str() );
db->Get( Rnum );
db->SetKey( aKey[i].c_str() );
for( size_t i=0; i<aKey.size(); i++ ) {
int Rnum = db->Find(aKey[i].c_str());
db->Get(Rnum);
db->SetKey(aKey[i].c_str());
QJsonObject recObject;
db->toJsonObject( recObject );
db->toJsonObjectNew(recObject, project_name);
allArray.append(recObject);
}
QJsonDocument saveDoc(allArray);
fstream f_out( filename, ios::out );
if( f_out.good() )
fstream f_out(filename, ios::out);
if(f_out.good()) {
f_out << saveDoc.toJson().data() << std::endl;
}
dyn_set();
}

void TCModule::RecListFromJSON()
void TCModule::RecListFromJSON(const std::string&filename)
{
int fnum= -1 ;// FileSelection dialog: implement "Ok to All"

// Choose file name
string s =string( GetName() )+" : Please, select file with unloaded records";
string filename;
if( vfChooseFileOpen( window(), filename, s.c_str(), "*.json" ) == false )
return;

QFile CurrentFile(filename.c_str());
if(!CurrentFile.open(QIODevice::ReadOnly)) return;
QByteArray json_data = CurrentFile.readAll();

QJsonDocument readDoc = QJsonDocument::fromJson(json_data);
QJsonArray allArray = readDoc.array();
int quest_reply = VF_UNDEF;
for( const auto& val : allArray)
{
std::string keyp = db->fromJsonObject( val.toObject() );
for( const auto& val : allArray) {
dyn_kill();
set_def(); // set default data or zero if necessary
std::string keyp = db->fromJsonObjectNew( val.toObject() );
auto Rnum = db->Find( keyp.c_str() );
if( Rnum >= 0 )
{
if( Rnum >= 0 ) {
ReplaceRecordwithQuestion(Rnum, keyp.c_str(), quest_reply);
if( quest_reply == VF_CANCEL )
break;
}
else
{
AddRecord( keyp.c_str(), fnum );
if( fnum == -2 )
else {
AddRecord(keyp.c_str(), fnum);
if(fnum == -2)
break;
}
}
Expand Down
23 changes: 11 additions & 12 deletions GUI/Dataman/v_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,24 +282,23 @@ class TCModule:
virtual void RecordPlot( const char *key );

//-- Database manipulation
void DelList( const char *pattern );
void Transfer( const char *pattern );
void CopyRecordsList( const char *pattern, bool if_rename );
void KeysToTXT( const char *pattern );
void RecToTXT( const char *pattern );
void RecOfTXT( );
void RecExport( const char *pattern );
void RecImport( );
void RecListToJSON( const char *pattern );
void RecListFromJSON();
void DelList(const char *pattern);
void Transfer(const char *pattern);
void CopyRecordsList(const char *pattern, bool if_rename);
void KeysToTXT(const char *pattern);
void RecToTXT(const char *pattern);
void RecOfTXT();
void RecExport(const char *pattern);
void RecImport();
void RecListToJSON(const char *pattern, const std::string&filename, bool all_records=false);
void RecListFromJSON(const std::string&filename);

TCIntArray SelectFileList(int mode);

virtual void MakeQuery();

//-- Module manipulation
virtual string GetKeyofRecord( const char *oldKey, const char *strTitle,
int keyType );
virtual string GetKeyofRecord(const char *oldKey, const char *strTitle, int keyType);
virtual string makeKeyFilter();
virtual bool testKeyFilter();
const char *getFilter()
Expand Down
Loading