bes  Updated for version 3.20.8
Granule.cc
1 // -*- mode: c++; c-basic-offset:4 -*-
2 
3 // This file is part of cmr_module, A C++ MODULE that can be loaded in to
4 // the OPeNDAP Back-End Server (BES) and is able to handle remote requests.
5 
6 // Copyright (c) 2015 OPeNDAP, Inc.
7 // Author: Nathan Potter <ndp@opendap.org>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24 
25 /*
26  * Granule.h
27  *
28  * Created on: July, 13 2018
29  * Author: ndp
30  */
31 #include "config.h"
32 
33 #include <stdlib.h> /* atol */
34 
35 #include "rjson_utils.h"
36 #include "BESDebug.h"
37 
38 #include "CmrNames.h"
39 #include "CmrError.h"
40 #include "Granule.h"
41 
42 
43 using namespace std;
44 
45 #define prolog std::string("Granule::").append(__func__).append("() - ")
46 
47 
48 namespace cmr {
49 string granule_LINKS_REL_DATA_ACCES = "http://esipfed.org/ns/fedsearch/1.1/data#";
50 string granule_LINKS_REL_METADATA_ACCESS = "http://esipfed.org/ns/fedsearch/1.1/data#";
51 string granule_LINKS = "links";
52 string granule_LINKS_REL= "rel";
53 string granule_LINKS_HREFLANG = "hreflang";
54 string granule_LINKS_HREF = "href";
55 string granule_SIZE = "granule_size";
56 string granule_LMT = "updated";
57 
58 string granule_ID = "id";
59 
60 string granule_NAME = "title";
61 
62 Granule::Granule(const rapidjson::Value& granule_obj){
63  setId(granule_obj);
64  setName(granule_obj);
65  setSize(granule_obj);
66  setDataAccessUrl(granule_obj);
67  setMetadataAccessUrl(granule_obj);
68  setLastModifiedStr(granule_obj);
69 }
70 
71 void Granule::setName(const rapidjson::Value& go){
72  rjson_utils rju;
73  this->d_name = rju.getStringValue(go, granule_NAME);
74 }
75 
76 void Granule::setId(const rapidjson::Value& go){
77  rjson_utils rju;
78  this->d_id = rju.getStringValue(go, granule_ID);
79 }
80 
81 void Granule::setSize(const rapidjson::Value& go){
82  rjson_utils rju;
83  this->d_size_str = rju.getStringValue(go, granule_SIZE);
84 }
85 
89 void Granule::setLastModifiedStr(const rapidjson::Value& go){
90  rjson_utils rju;
91  this->d_last_modified_time = rju.getStringValue(go, granule_LMT);
92 }
93 
97 const rapidjson::Value& Granule::get_links_array(const rapidjson::Value& go){
98 
99  rapidjson::Value::ConstMemberIterator itr = go.FindMember(granule_LINKS.c_str());
100  bool result = itr != go.MemberEnd();
101  string msg = prolog + (result?"Located":"FAILED to locate") + " the value '"+granule_LINKS+"' in object.";
102  BESDEBUG(MODULE, msg << endl);
103  if(!result){
104  throw CmrError("ERROR: Failed to located '"+granule_LINKS+"' section for CMRGranule!",__FILE__,__LINE__);
105  }
106  const rapidjson::Value& links = itr->value;
107  if(!links.IsArray())
108  throw CmrError("ERROR: The '"+granule_LINKS+"' object is NOT an array!",__FILE__,__LINE__);
109 
110  return links;
111 }
112 
116 void Granule::setDataAccessUrl(const rapidjson::Value& go){
117  rjson_utils rju;
118 
119  const rapidjson::Value& links = get_links_array(go);
120  for (rapidjson::SizeType i = 0; i < links.Size(); i++) { // Uses SizeType instead of size_t
121  const rapidjson::Value& link = links[i];
122  string rel = rju.getStringValue(link,granule_LINKS_REL);
123  if(rel == granule_LINKS_REL_DATA_ACCES){
124  this->d_data_access_url = rju.getStringValue(link,granule_LINKS_HREF);
125  return;
126  }
127  }
128  throw CmrError("ERROR: Failed to locate granule data access link ("+granule_LINKS_REL_DATA_ACCES+"). :(",__FILE__,__LINE__);
129 }
130 
134 void Granule::setMetadataAccessUrl(const rapidjson::Value& go){
135  rjson_utils rju;
136 
137  const rapidjson::Value& links = get_links_array(go);
138  for (rapidjson::SizeType i = 0; i < links.Size(); i++) { // Uses SizeType instead of size_t
139  const rapidjson::Value& link = links[i];
140  string rel = rju.getStringValue(link,granule_LINKS_REL);
141  if(rel == granule_LINKS_REL_METADATA_ACCESS){
142  this->d_metadata_access_url = rju.getStringValue(link,granule_LINKS_HREF);
143  return;
144  }
145  }
146  throw CmrError("ERROR: Failed to locate granule metadata access link ("+granule_LINKS_REL_METADATA_ACCESS+"). :(",__FILE__,__LINE__);
147 }
148 
149 
150 bes::CatalogItem *Granule::getCatalogItem(BESCatalogUtils *d_catalog_utils){
151  bes::CatalogItem *item = new bes::CatalogItem();
152  item->set_type(bes::CatalogItem::leaf);
153  item->set_name(getName());
154  item->set_lmt(getLastModifiedStr());
155  item->set_size(getSize());
156  item->set_is_data(d_catalog_utils->is_data(item->get_name()));
157  return item;
158 }
159 
160 
161 
162 } //namespace cmr
bool is_data(const std::string &item) const
is there a handler that can process this
(Constant) member iterator for a JSON object value
Definition: document.h:177
void set_name(std::string n)
Set the name of the item.
Definition: CatalogItem.h:135
std::string get_name() const
The name of this item in the node.
Definition: CatalogItem.h:133
void set_size(size_t s)
Set the size of the item.
Definition: CatalogItem.h:140
void set_is_data(bool id)
Is this item data that the BES should interpret?
Definition: CatalogItem.h:150
void set_lmt(std::string lmt)
Set the LMT for this item.
Definition: CatalogItem.h:145
void set_type(item_type t)
Set the type for this item.
Definition: CatalogItem.h:155
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:2189
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:384