NCBI C++ ToolKit
loading_app_job.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: loading_app_job.cpp 45881 2021-01-05 03:10:02Z rudnev $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Andrey Yazhuk, Liangshou Wu
27  *
28  */
29 
30 #include <ncbi_pch.hpp>
31 
34 
37 
39 
40 #include <corelib/ncbi_system.hpp>
41 
42 #include <wx/dirdlg.h>
43 
46 
49  , m_Data(data)
50 {
51 
52 }
53 
54 ///////////////////////////////////////////////////////////////////////////////
55 /// CDataLoadingAppJob
57 {
58 }
59 
60 
62 {
63 }
64 
66 
69 }
70 
72 {
73  static const char* kLogMsg = "CDataLoadingAppJob::x_Run() - exception ";
74 
75  x_ResetState();
76 
77  string err_msg;
78  bool err_user_level = false;
79  try {
80  x_Run();
81 
82  } catch( CException& e ){
83  LOG_POST(Error << kLogMsg << GetDescr() << ". " << e);
84  e.ReportAll();
85 
86  err_msg = e.GetMsg();
87  err_user_level = true;
88 
89  } catch( std::exception& ee ){
90  err_msg = kLogMsg + GetDescr() + ". " + ee.what();
91  LOG_POST(Error << err_msg );
92 
93  err_user_level = false;
94  }
95 
97 
98  if( !err_msg.empty() ){
99  m_Error.Reset( new CAppJobError( err_msg, err_user_level ) );
100  }
101 
102  if( m_Error ){
103  return eFailed;
104  }
105 
106  LOG_POST(Info
107  << "CDataLoadingAppJob::Run(): Finished job " << m_Descr
108  );
109  return eCompleted;
110 }
111 
112 
114 {
115  std::lock_guard<std::mutex> guard(m_DataMutex);
116  if (!m_Data.empty()) {
118  m_Data.clear();
119  return pr;
120  }
121  else {
122  return CAppJob::GetProgress();
123  }
124 }
125 
126 
128 {
130 
131  x_SetTextResult( m_Descr + ": no items created." );
132 }
133 
134 
136 {
137  // if we do not have project items - we need to create them
138  // otherwise - skip this step and add existing items to a project
139  if( m_Data.empty() ){
140  CLockerGuard guard = x_GetGuard();
141  if (IsCanceled()) return;
142  x_SetStatusText( "Creating data items..." );
143  x_CreateProjectItems(); /// populate m_Items
144  if (IsCanceled()) {
145  return;
146  }
147  }
148 
149  if (!m_Data.empty()) {
150  if (IsCanceled()) return;
151  x_SetTextResult("");
152  }
153 }
154 
156 {
157  std::lock_guard<std::mutex> guard(m_DataMutex);
158  m_Data.clear();
159 }
160 
162 {
163  std::lock_guard<std::mutex> guard(m_DataMutex);
165  m_Data[loader].push_back(CRef<CProjectItem>(&item));
166 }
167 
169 {
170  std::lock_guard<std::mutex> guard(m_DataMutex);
171  m_Data[CRef<CLoaderDescriptor>(&loader)].push_back(CRef<CProjectItem>(&item));
172 }
173 
174 void CDataLoadingAppJob::AddDataLoader(objects::CLoaderDescriptor& loader)
175 {
176  std::lock_guard<std::mutex> guard(m_DataMutex);
178 }
179 
181 {
182  CFastMutexGuard lock(m_Mutex);
183 
184  if( !m_TextResult.empty() ){
186  }
187 
188  return CRef<CObject>();
189 }
190 
191 ///////////////////////////////////////////////////////////////////////////////
192 /// CDataLoadingAppTask
193 
195  : CAppJobTask( job, true, descr.empty() ? job.GetDescr() : descr ), m_Service(service), m_Options(options)
196 {
197  x_SetOnAppJobProgress(std::bind(&CDataLoadingAppTask::x_OnAppJobProgress, this, std::placeholders::_1));
198 }
199 
200 
202 {
204 
205  if (state == eCompleted) {
206  IAppJob* job = GetJob().GetPointer();
207  CDataLoadingAppJob* data_job = dynamic_cast<CDataLoadingAppJob*>(job);
208  if (data_job->IsCanceled()) {
209  return state;
210  }
211 
212  _ASSERT(data_job);
213 
214  const CSelectProjectOptions::TData& data = data_job->GetData();
215 
216  if (!data.empty()) {
218  return eCanceled;
219 
220  vector<CRef<CProjectItem> > items;
222  ITERATE(CSelectProjectOptions::TItems, it2, it->second)
223  items.push_back(*it2);
224  }
225 
226  CPrefetchSeqDescr::PrefetchSeqDescr(m_Service->GetServiceLocator(), items);
227  }
228 
229  wxString resTitle, resHTML;
230  data_job->GetHTMLResults(resTitle, resHTML);
231 
232  if (!resHTML.empty()) {
233  CJobResultsDlg dlg;
234  dlg.SetHTML(resHTML);
235  dlg.Create(NULL, wxID_ANY, resTitle);
236  dlg.ShowModal();
237  }
238  }
239 
240  return state;
241 }
242 
243 
245 {
246  const CDataLoadingAppJobProgress* prg = dynamic_cast<const CDataLoadingAppJobProgress*>(&progress);
247  if (nullptr == prg)
248  return;
249 
251 }
252 
#define true
Definition: bool.h:35
CAppJobError Default implementation for IAppJobError - encapsulates a text error message.
CAppJobProgress Default implementation for IAppJobProgress - the class encapsulates a text message an...
CAppJobTask CAppJobTask is an adapter that allows for running IAppJobs as Tasks in App Task Service.
CAppJobTextResult Default implementation for result object - encapsulates a text error message.
CDataLoadingAppJobProgress(const TData &data, float done, const string &text)
const TData & GetData() const
CDataLoadingAppJob - a base class for Jobs loading data into projects.
virtual void x_ResetState()
prepares job for execution by clearing state variables
void AddProjectItem(objects::CProjectItem &item)
virtual CRef< CObject > GetResult()
Returns the Job Result.
void AddDataLoader(objects::CLoaderDescriptor &loader)
virtual EJobState Run()
Function that does all the useful work, called by the Engine.
virtual void x_Run()
virtual void x_CreateProjectItems()=0
override this function in derived classes and populate m_Items.
virtual CConstIRef< IAppJobProgress > GetProgress()
implement Run() in derived classes, make sure that exceptions are handled and m_StopRequested flag is...
CSelectProjectOptions::TData m_Data
virtual ~CDataLoadingAppJob()
virtual void GetHTMLResults(wxString &, wxString &)
void x_SetTextResult(const string &result)
const CSelectProjectOptions::TData & GetData() const
CDataLoadingAppJob()
constructor for derived classes
CIRef< CProjectService > m_Service
CSelectProjectOptions m_Options
CDataLoadingAppTask(CProjectService *service, CSelectProjectOptions &options, CDataLoadingAppJob &job, const string &descr=kEmptyStr)
CDataLoadingAppTask.
virtual ETaskState Run()
we override this function because our logic is a bit more complex than one implemented in CAppJobTask...
void x_OnAppJobProgress(const IAppJobProgress &progress)
void SetHTML(wxString value)
bool Create(wxWindow *parent, wxWindowID id=10049, const wxString &caption=_("Job Results"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(400, 300), long style=wxCAPTION|wxRESIZE_BORDER|wxSYSTEM_MENU|wxCLOSE_BOX|wxTAB_TRAVERSAL)
CLoaderDescriptor –.
static void PrefetchSeqDescr(IServiceLocator *serviceLocator, const vector< CRef< objects::CProjectItem > > &items)
CProjectService - a service providing API for operations with Workspaces and Projects.
CRef –.
Definition: ncbiobj.hpp:618
CProjectSelectOptions - describes how new Project Items shall be added to a workspace.
bool AddItemsToWorkspace(CProjectService *service, const TData &data)
IAppJobProgress.
Definition: app_job.hpp:50
IAppJob.
Definition: app_job.hpp:82
bool empty() const
Definition: map.hpp:149
void clear()
Definition: map.hpp:169
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define NULL
Definition: ncbistd.hpp:225
#define LOG_POST(message)
This macro is deprecated and it's strongly recomended to move in all projects (except tests) to macro...
Definition: ncbidiag.hpp:226
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1197
const string & GetMsg(void) const
Get message string.
Definition: ncbiexpt.cpp:461
string ReportAll(TDiagPostFlags flags=eDPF_Exception) const
Report all exceptions.
Definition: ncbiexpt.cpp:370
void Info(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1185
CIRef< IAppJob > GetJob()
ETaskState
List of task states defining the task management FSM.
Definition: app_task.hpp:87
void x_SetOnAppJobProgress(TOnAppJobProgressCallback callback)
virtual ETaskState Run()
execute the task, this function is called on the main UI thread if a task needs to execute in backgro...
@ eCanceled
canceled by Task Manager
Definition: app_task.hpp:94
@ eCompleted
successfully finished
Definition: app_task.hpp:92
CRef< CAppJobError > m_Error
string m_Descr
mutex to sync our internals
virtual void x_ResetState()
virtual bool IsCanceled() const override
virtual CConstIRef< IAppJobProgress > GetProgress() override
implement Run() in derived classes, make sure that exceptions are handled and m_StopRequested flag is...
CFastMutex m_Mutex
EJobState
Job states (describe FSM)
Definition: app_job.hpp:86
virtual void x_SetStatusText(const string &text)
string m_Status
Current status of the Job.
virtual string GetDescr() const override
Returns a human readable description of the Job (optional)
CLockerGuard x_GetGuard()
@ eCompleted
Definition: app_job.hpp:89
@ eFailed
Definition: app_job.hpp:90
TObjectType * GetPointer(void) THROWS_NONE
Get pointer,.
Definition: ncbiobj.hpp:998
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
USING_SCOPE(objects)
static void text(MDB_val *v)
Definition: mdb_dump.c:62
constexpr bool empty(list< Ts... >) noexcept
static static static wxID_ANY
#define _ASSERT
done
Definition: token1.c:1
else result
Definition: token2.c:20
Modified on Tue Dec 05 02:12:35 2023 by modify_doxy.py rev. 669887