aboutsummaryrefslogtreecommitdiffstats
path: root/Websocket/query.cpp
blob: c0bcaf66d59e419b4a3cc39b9350714a756b0665 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "query.h"

const std::string QueryKeys::ACTION       = "action";
const std::string QueryKeys::OBJECT_NAME  = "object_name";
const std::string QueryKeys::FILTER       = "filter";
const std::string QueryKeys::PARAMS       = "params";
const std::string QueryKeys::FIELD_NAMES  = "field_names";
const std::string QueryKeys::LAST         = "last";

Query::Query()
    : query(Json2::object())
{
}

Query::Query(const std::string &action, const std::string &objectName, const std::list<std::vector<std::string>> &filter,
             const std::map<std::string, std::string> &params, const std::list<std::string> &fields, bool last)
{
  this->action = action;
  this->objectName = objectName;
  this->filter = filter;
  this->params = params;
  this->fields = fields;
  this->last = last;

  if (fields.size() == 0) {
    this->fields.push_back("*");
  }

  query[QueryKeys::ACTION] = action;
  query[QueryKeys::OBJECT_NAME] = objectName;
  query[QueryKeys::FILTER] = Json2(filter);
  query[QueryKeys::PARAMS] = Json2(params);
  query[QueryKeys::FIELD_NAMES] = Json2(fields);
  query[QueryKeys::LAST] = last;
}

Query
Query::fromJsonString(const std::string &jsonString)
{
  Query query;
  Json2 jsonQuery = Json2::parse(jsonString);

  std::cout << jsonQuery << std::endl;

  std::cout << jsonQuery[QueryKeys::LAST] << std::endl;

  query.setAction(jsonQuery[QueryKeys::ACTION]);
  query.setLast(jsonQuery[QueryKeys::LAST]);
  query.setObjectName(jsonQuery[QueryKeys::OBJECT_NAME]);

  Json2 list = jsonQuery[QueryKeys::FIELD_NAMES];

  for (Json2::iterator it =list.begin(); it != list.end(); ++it) {
    query.fields.push_back(*it);
  }

  if (query.fields.size() == 0) {
    query.fields.push_back("*");
  }

  Json2 list2 = jsonQuery[QueryKeys::FILTER];

  for (Json2::iterator it = list2.begin(); it != list2.end(); ++it) {
    query.filter.push_back(*it);
  }

  Json2 map = jsonQuery[QueryKeys::PARAMS];

  for (Json2::iterator it = map.begin(); it != map.end(); ++it) {
    std::cout << it.key() << " " << it.value().dump() << std::endl;
    query.params[it.key()] = it.value().dump();
  }

  query.query[QueryKeys::ACTION] = query.action;
  query.query[QueryKeys::OBJECT_NAME] = query.objectName;
  query.query[QueryKeys::FILTER] = Json2(query.filter);
  query.query[QueryKeys::PARAMS] = Json2(query.params);
  query.query[QueryKeys::FIELD_NAMES] = Json2(query.fields);
  query.query[QueryKeys::LAST] = query.last;

  return query;
}

void
Query::removeQuotes(std::string& string)
{
  std::cout << string.front() << std::endl;
  string.erase(remove(string.begin(), string.end(), '\"'), string.end());
}

std::string
Query::toJsonString(const Query &query)
{
  return query.query.dump();
}

std::string
Query::toJsonString(const std::string &action, const  std::string &objectName, const  std::list<std::vector<std::string>> &filter,
                    const std::map<std::string, std::string> &params, const  std::list<std::string> &fields, bool last)
{
  Json2 jsonQuery;

  jsonQuery[QueryKeys::ACTION] = action;
  jsonQuery[QueryKeys::OBJECT_NAME] = objectName;
  jsonQuery[QueryKeys::FILTER] = Json2(filter);
  jsonQuery[QueryKeys::PARAMS] = Json2(params);
  jsonQuery[QueryKeys::FIELD_NAMES] = Json2(fields);
  jsonQuery[QueryKeys::LAST] = last;

  return jsonQuery.dump();
}

std::string
Query::toJsonString()
{
  return query.dump();
}

const std::string&
Query::getAction() const
{
  return action;
}

void
Query::setAction(const std::string &action)
{
  Query::action = action;
  removeQuotes(this->action);
  query[QueryKeys::ACTION] = this->action;
}

const std::string&
Query::getObjectName() const
{
  return objectName;
}

void
Query::setObjectName(const std::string &objectName)
{
  Query::objectName = objectName;
  removeQuotes(this->objectName);
  query[QueryKeys::OBJECT_NAME] = this->objectName;
}

const std::list<std::vector<std::string>>&
Query::getFilter() const
{
  return filter;
}

void
Query::setFilter(const std::list<std::vector<std::string>> &filter)
{
  Query::filter = filter;

  for (auto f : this->filter) {
    for (auto field : f) {
      removeQuotes(field);
    }
  }

  query[QueryKeys::FILTER] = this->filter;
}

const std::map<std::string, std::string>&
Query::getParams() const
{
  return params;
}

void
Query::setParams(const std::map<std::string, std::string> &params)
{
  Query::params = params;
  query[QueryKeys::PARAMS] = this->params;
}

const std::list<std::string>&
Query::getFields() const
{
  return fields;
}

void
Query::setFields(const std::list<std::string> &fields)
{
  Query::fields = fields;

  for (auto field : this->fields) {
    removeQuotes(field);
  }

  query[QueryKeys::FIELD_NAMES] = this->fields;
}

bool
Query::isLast() const
{
  return last;
}

void
Query::setLast(int last)
{
  Query::last = last;
  query[QueryKeys::LAST] = this->last;
}

bool
Query::isEmpty()
{
  return query.empty();
}