32 #include "rapidjson/document.h"
33 #include "rapidjson/writer.h"
34 #include "rapidjson/prettywriter.h"
35 #include "rapidjson/stringbuffer.h"
36 #include "rapidjson/filereadstream.h"
47 #include <BESSyntaxUserError.h>
50 #include <TheBESKeys.h>
54 #include "RemoteHttpResource.h"
56 #include "rjson_utils.h"
60 #define prolog string("CmrApi::").append(__func__).append("() - ")
71 itr = obj.FindMember(
"children");
72 bool result = itr != obj.MemberEnd();
73 string msg = prolog + (result?
"Located":
"FAILED to locate") +
" the value 'children' in the object.";
74 BESDEBUG(MODULE, msg << endl);
76 throw CmrError(msg,__FILE__,__LINE__);
80 result = children.IsArray();
81 msg = prolog +
"The value 'children' is" + (result?
"":
" NOT") +
" an array.";
82 BESDEBUG(MODULE, msg << endl);
84 throw CmrError(msg,__FILE__,__LINE__);
95 bool result = cmr_doc.IsObject();
96 string msg = prolog +
"Json document is" + (result?
"":
" NOT") +
" an object.";
97 BESDEBUG(MODULE, msg << endl);
99 throw CmrError(msg,__FILE__,__LINE__);
104 result = itr != cmr_doc.MemberEnd();
105 msg = prolog + (result?
"Located":
"FAILED to locate") +
" the value 'feed'.";
106 BESDEBUG(MODULE, msg << endl);
108 throw CmrError(msg,__FILE__,__LINE__);
112 result = feed.IsObject();
113 msg = prolog +
"The value 'feed' is" + (result?
"":
" NOT") +
" an object.";
114 BESDEBUG(MODULE, msg << endl);
116 throw CmrError(msg,__FILE__,__LINE__);
132 result = itr != feed.MemberEnd();
133 msg = prolog + (result?
"Located":
"FAILED to locate") +
" the value 'entry'.";
134 BESDEBUG(MODULE, msg << endl);
136 throw CmrError(msg,__FILE__,__LINE__);
140 result = entry.IsArray();
141 msg = prolog +
"The value 'entry' is" + (result?
"":
" NOT") +
" an Array.";
142 BESDEBUG(MODULE, msg << endl);
144 throw CmrError(msg,__FILE__,__LINE__);
162 result = itr != feed.MemberEnd();
163 msg = prolog + (result?
"Located":
"FAILED to locate") +
" the value 'facets'." ;
164 BESDEBUG(MODULE, msg << endl);
166 throw CmrError(msg,__FILE__,__LINE__);
170 result = facets_obj.IsObject();
171 msg = prolog +
"The value 'facets' is" + (result?
"":
" NOT") +
" an object.";
172 BESDEBUG(MODULE, msg << endl);
174 throw CmrError(msg,__FILE__,__LINE__);
181 string facet_title = ru.getStringValue(facet,
"title");
182 string temporal_title(
"Temporal");
183 if(facet_title == temporal_title){
184 msg = prolog +
"Found Temporal object.";
185 BESDEBUG(MODULE, msg << endl);
189 msg = prolog +
"The child of 'facets' with title '"+facet_title+
"' does not match 'Temporal'";
190 BESDEBUG(MODULE, msg << endl);
193 msg = prolog +
"Failed to locate the Temporal facet.";
194 BESDEBUG(MODULE, msg << endl);
195 throw CmrError(msg,__FILE__,__LINE__);
212 string temporal_child_title = rju.getStringValue(temporal_child,
"title");
213 string year_title(
"Year");
214 if(temporal_child_title == year_title){
215 msg = prolog +
"Found Year object.";
216 BESDEBUG(MODULE, msg << endl);
217 return temporal_child;
220 msg = prolog +
"The child of 'Temporal' with title '"+temporal_child_title+
"' does not match 'Year'";
221 BESDEBUG(MODULE, msg << endl);
224 msg = prolog +
"Failed to locate the Year group.";
225 BESDEBUG(MODULE, msg << endl);
226 throw CmrError(msg,__FILE__,__LINE__);
242 string year_title = rju.getStringValue(year_obj,
"title");
243 if(r_year == year_title){
244 msg = prolog +
"Found Year object.";
245 BESDEBUG(MODULE, msg << endl);
250 string title = rju.getStringValue(child,
"title");
251 string month_title(
"Month");
252 if(title == month_title){
253 msg = prolog +
"Found Month object.";
254 BESDEBUG(MODULE, msg << endl);
258 msg = prolog +
"The child of 'Year' with title '"+title+
"' does not match 'Month'";
259 BESDEBUG(MODULE, msg << endl);
264 msg = prolog +
"The child of 'Year' group with title '"+year_title+
"' does not match the requested year ("+r_year+
")";
265 BESDEBUG(MODULE, msg << endl);
268 msg = prolog +
"Failed to locate the Year group.";
269 BESDEBUG(MODULE, msg << endl);
270 throw CmrError(msg,__FILE__,__LINE__);
274 CmrApi::get_month(
const string r_month,
const string r_year,
const rapidjson::Document &cmr_doc){
282 string month_id = rju.getStringValue(month,
"title");
283 if(month_id == r_month){
285 msg << prolog <<
"Located requested month ("<<r_month <<
")";
286 BESDEBUG(MODULE, msg.str() << endl);
291 msg << prolog <<
"The month titled '"<<month_id <<
"' does not match the requested month ("<< r_month <<
")";
292 BESDEBUG(MODULE, msg.str() << endl);
296 msg << prolog <<
"Failed to locate request Year/Month.";
297 BESDEBUG(MODULE, msg.str() << endl);
298 throw CmrError(msg.str(),__FILE__,__LINE__);
302 CmrApi::get_day_group(
const string r_month,
const string r_year,
const rapidjson::Document &cmr_doc){
311 string title = rju.getStringValue(
object,
"title");
312 string day_group_title =
"Day";
313 if(title == day_group_title){
315 msg << prolog <<
"Located Day group for year: " << r_year <<
" month: "<< r_month;
316 BESDEBUG(MODULE, msg.str() << endl);
321 msg << prolog <<
"Failed to locate requested Day year: " << r_year <<
" month: "<< r_month;
322 BESDEBUG(MODULE, msg.str() << endl);
323 throw CmrError(msg.str(),__FILE__,__LINE__);
334 CmrApi::get_years(
string collection_name, vector<string> &years_result){
339 string url =
BESUtil::assemblePath(cmr_search_endpoint_url,
"granules.json") +
"?concept_id="+collection_name +
"&include_facets=v2";
348 years_result.push_back(year);
362 CmrApi::get_months(
string collection_name,
string r_year, vector<string> &months_result){
368 +
"?concept_id="+collection_name
369 +
"&include_facets=v2"
370 +
"&temporal_facet[0][year]="+r_year;
374 BESDEBUG(MODULE, prolog <<
"Got JSON Document: "<< endl << rju.
jsonDocToString(doc) << endl);
378 if(years.Size() != 1){
380 msg << prolog <<
"We expected to get back one year (" << r_year <<
") but we got back " << years.Size();
381 BESDEBUG(MODULE, msg.str() << endl);
382 throw CmrError(msg.str(),__FILE__,__LINE__);
387 if(year_title != r_year){
389 msg << prolog <<
"The returned year (" << year_title <<
") does not match the requested year ("<< r_year <<
")";
390 BESDEBUG(MODULE, msg.str() << endl);
391 throw CmrError(msg.str(),__FILE__,__LINE__);
395 if(year_children.Size() != 1){
397 msg << prolog <<
"We expected to get back one child for the year (" << r_year <<
") but we got back " << years.Size();
398 BESDEBUG(MODULE, msg.str() << endl);
399 throw CmrError(msg.str(),__FILE__,__LINE__);
404 if(title !=
string(
"Month")){
406 msg << prolog <<
"We expected to get back a Month object, but we did not.";
407 BESDEBUG(MODULE, msg.str() << endl);
408 throw CmrError(msg.str(),__FILE__,__LINE__);
415 months_result.push_back(month_id);
425 CmrApi::get_days(
string collection_name,
string r_year,
string r_month, vector<string> &days_result){
430 +
"?concept_id="+collection_name
431 +
"&include_facets=v2"
432 +
"&temporal_facet[0][year]="+r_year
433 +
"&temporal_facet[0][month]="+r_month;
437 BESDEBUG(MODULE, prolog <<
"Got JSON Document: "<< endl << rju.
jsonDocToString(cmr_doc) << endl);
439 const rapidjson::Value& day_group = get_day_group(r_month, r_year, cmr_doc);
444 days_result.push_back(day_id);
454 CmrApi::get_granule_ids(
string collection_name,
string r_year,
string r_month,
string r_day, vector<string> &granules_ids){
459 granule_search(collection_name, r_year, r_month, r_day, cmr_doc);
465 granules_ids.push_back(day_id);
475 CmrApi::granule_count(
string collection_name,
string r_year,
string r_month,
string r_day){
478 granule_search(collection_name, r_year, r_month, r_day, cmr_doc);
480 return entries.Size();
488 CmrApi::granule_search(
string collection_name,
string r_year,
string r_month,
string r_day,
rapidjson::Document &result_doc){
492 +
"?concept_id="+collection_name
493 +
"&include_facets=v2"
497 url +=
"&temporal_facet[0][year]="+r_year;
500 url +=
"&temporal_facet[0][month]="+r_month;
503 url +=
"&temporal_facet[0][day]="+r_day;
505 BESDEBUG(MODULE, prolog <<
"CMR Granule Search Request Url: : " << url << endl);
506 rju.getJsonDoc(url,result_doc);
507 BESDEBUG(MODULE, prolog <<
"Got JSON Document: "<< endl << rju.jsonDocToString(result_doc) << endl);
516 CmrApi::get_granules(
string collection_name,
string r_year,
string r_month,
string r_day, vector<Granule *> &granules){
520 granule_search(collection_name, r_year, r_month, r_day, cmr_doc);
527 granules.push_back(g);
535 CmrApi::get_collection_ids(std::vector<std::string> &collection_ids){
537 string key = CMR_COLLECTIONS;
541 +
"' field has not been configured.", __FILE__, __LINE__);
549 cmr::Granule* CmrApi::get_granule(
string collection_name,
string r_year,
string r_month,
string r_day,
string granule_id)
551 vector<Granule *> granules;
554 get_granules(collection_name, r_year, r_month, r_day, granules);
555 for(
size_t i=0; i<granules.size() ;i++){
556 string id = granules[i]->getName();
557 BESDEBUG(MODULE, prolog <<
"Comparing granule id: " << granule_id <<
" to collection member id: " <<
id << endl);
558 if(
id == granule_id){
559 result = granules[i];