action.h
1 
2 #ifndef FUNCTIONS_ACTION
3 #define FUNCTIONS_ACTION
4 
5 
6 #include <string>
7 #include <vector>
8 #include <memory>
9 #include <array>
10 #include <map>
11 #include <stdexcept>
12 #include <vector>
13 #include <thread>
14 
15 #include "Poco/JSON/Object.h"
16 #include <Poco/Net/HTTPServerRequest.h>
17 #include "Poco/Data/Session.h"
18 #include "Poco/Data/MySQL/Connector.h"
19 #include <Poco/Data/MySQL/MySQLException.h>
20 #include <Poco/Data/Statement.h>
21 #include <Poco/URI.h>
22 #include <Poco/StreamCopier.h>
23 #include <Poco/JSON/JSON.h>
24 #include <Poco/JSON/JSONException.h>
25 #include <Poco/JSON/Array.h>
26 #include <Poco/JSON/Object.h>
27 #include <Poco/JSON/Parser.h>
28 #include <Poco/Dynamic/Var.h>
29 #include <Poco/Dynamic/Struct.h>
30 #include <Poco/Data/RecordSet.h>
31 #include <Poco/Net/HTMLForm.h>
32 
33 #include "query/database_manager.h"
34 #include "tools/settings_manager.h"
35 #include "query/results.h"
36 #include "query/parameter.h"
37 #include "query/condition.h"
38 #include "tools/manage_json.h"
39 #include "tools/output_logger.h"
40 #include "tools/dvalue.h"
41 #include "files/file_manager.h"
42 
43 
44 namespace NAF
45 {
46  namespace Functions
47  {
48  class Action;
49  }
50 }
51 
52 using namespace Poco;
53 using namespace Poco::Net;
54 using namespace Poco::Data;
55 using namespace Poco::Data::Keywords;
56 
57 
59 {
60  public:
61  using Ptr = std::shared_ptr<Action>;
62 
63  Action(std::string identifier);
64  virtual ~Action();
65 
66  std::string get_identifier() const { return identifier_; };
67  std::string get_status() const { return status_; };
68  std::string get_message() const { return message_; };
69  std::string get_custom_error() const { return custom_error_; };
70  bool get_final() const { return final_; };
71  bool get_error() const { return error_; };
72  std::vector<Query::Parameter::Ptr>& get_parameters()
73  {
74  auto& var = parameters_;
75  return var;
76  }
77  Query::Condition<Action&>::Ptr get_condition()
78  {
79  auto& var = condition_;
80  return var;
81  }
82  Query::Results::Ptr get_results()
83  {
84  auto& var = results_;
85  return var;
86  }
87  JSON::Object::Ptr get_json_result()
88  {
89  auto& var = json_result_;
90  return var;
91  }
92  std::vector<Ptr>& get_actions_container()
93  {
94  auto& var = actions_container_;
95  return var;
96  }
97  std::string get_sql_code() const { return sql_code_; };
98  std::string get_final_query() const {return final_query_;}
99  int get_affected_rows_() const {return affected_rows_;}
100  std::shared_ptr<Data::Session>& get_session()
101  {
102  auto& var = session_;
103  return var;
104  }
105  std::shared_ptr<Data::Statement>& get_query()
106  {
107  auto& var = query_;
108  return var;
109  }
110  Query::DatabaseManager::Credentials& get_credentials()
111  {
112  auto& var = credentials_;
113  return var;
114  }
115 
116  void set_identifier(std::string identifier) { identifier_ = identifier; };
117  void set_status(std::string status) { status_ = status; };
118  void set_message(std::string message) { message_ = message; };
119  void set_custom_error(std::string custom_error) { custom_error_ = custom_error; };
120  void set_final(bool final) { final_ = final; };
121  void set_error(bool error) { error_ = error; };
122  void set_sql_code(std::string sql_code) { sql_code_ = sql_code; };
123  void set_final_query(std::string final_query) {final_query_ = final_query;}
124 
125  JSON::Array::Ptr GetParametersArray_(JSON::Array::Ptr json_array, int counter);
126  Query::Parameter::Ptr GetParameterObject_(JSON::Array::Ptr parameters_array, int counter);
127  void ReplaceParamater_(Query::Parameter::Ptr parameter);
128  Query::Parameter::Ptr AddParameter_(std::string name, Tools::DValue value, bool editable);
129  Query::Parameter::Ptr AddParameter_(std::string name, Query::Field::Position field_position, std::string related_action, bool editable);
130  void IdentifyParameters_(std::shared_ptr<Net::HTMLForm> form);
131  void IdentifyParameters_(Files::FileManager& files_parameters);
132  void IdentifyParameters_(JSON::Array::Ptr json_array);
133  void IdentifyParameters_(URI::QueryParameters& query_parameters);
134  void SetupCondition_(std::string identifier, Query::ConditionType type, Query::Condition<Action&>::Functor functor);
135  bool ComposeQuery_();
136  void ExecuteQuery_();
137  void ExecuteAsyncQuery_();
138  void MakeResults_();
139  JSON::Object::Ptr CreateJSONResult_();
140  virtual bool Work_();
141 
142  protected:
143  void NotifyError_(std::string message);
144  void SetupPositionParameter_(Query::Parameter::Ptr parameter);
145  bool VerifyParameterCondition_(Query::Parameter::Ptr parameter);
146  bool VerifyCondition_();
147 
148  private:
149  bool InitializeQuery_();
150 
151  bool async_;
152  bool async_finished_;
153  std::string identifier_;
154  std::string status_;
155  std::string message_;
156  std::string custom_error_;
157  bool final_;
158  bool error_;
159  std::vector<Query::Parameter::Ptr> parameters_;
160  Query::Condition<Action&>::Ptr condition_;
161  std::shared_ptr<Query::Results> results_;
162  JSON::Object::Ptr json_result_;
163  std::vector<Ptr> actions_container_;
164  std::string sql_code_;
165  std::string final_query_;
166  int affected_rows_;
167  std::shared_ptr<Data::Session> session_;
168  std::shared_ptr<Data::Statement> query_;
170  std::mutex mutex_;
171 
172 };
173 
174 #endif // FUNCTIONS_ACTION
Definition: file_manager.h:76
Definition: action.h:59
Definition: dvalue.h:48
Definition: database_manager.h:51
Definition: field.h:43