bes  Updated for version 3.20.6
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 <stdlib.h> /* atol */
32 
33 #include "rjson_utils.h"
34 #include "BESDebug.h"
35 
36 #include "CmrNames.h"
37 #include "CmrError.h"
38 #include "Granule.h"
39 
40 
41 using namespace std;
42 
43 #define prolog std::string("Granule::").append(__func__).append("() - ")
44 
45 
46 namespace cmr {
47 string granule_LINKS_REL_DATA_ACCES = "http://esipfed.org/ns/fedsearch/1.1/data#";
48 string granule_LINKS_REL_METADATA_ACCESS = "http://esipfed.org/ns/fedsearch/1.1/data#";
49 string granule_LINKS = "links";
50 string granule_LINKS_REL= "rel";
51 string granule_LINKS_HREFLANG = "hreflang";
52 string granule_LINKS_HREF = "href";
53 string granule_SIZE = "granule_size";
54 string granule_LMT = "updated";
55 
56 string granule_ID = "id";
57 
58 string granule_NAME = "title";
59 
60 Granule::Granule(const rapidjson::Value& granule_obj){
61  setId(granule_obj);
62  setName(granule_obj);
63  setSize(granule_obj);
64  setDataAccessUrl(granule_obj);
65  setMetadataAccessUrl(granule_obj);
66  setLastModifiedStr(granule_obj);
67 }
68 
69 void Granule::setName(const rapidjson::Value& go){
70  rjson_utils rju;
71  this->d_name = rju.getStringValue(go, granule_NAME);
72 }
73 
74 void Granule::setId(const rapidjson::Value& go){
75  rjson_utils rju;
76  this->d_id = rju.getStringValue(go, granule_ID);
77 }
78 
79 void Granule::setSize(const rapidjson::Value& go){
80  rjson_utils rju;
81  this->d_size_str = rju.getStringValue(go, granule_SIZE);
82 }
83 
87 void Granule::setLastModifiedStr(const rapidjson::Value& go){
88  rjson_utils rju;
89  this->d_last_modified_time = rju.getStringValue(go, granule_LMT);
90 }
91 
95 const rapidjson::Value& Granule::get_links_array(const rapidjson::Value& go){
96 
97  rapidjson::Value::ConstMemberIterator itr = go.FindMember(granule_LINKS.c_str());
98  bool result = itr != go.MemberEnd();
99  string msg = prolog + (result?"Located":"FAILED to locate") + " the value '"+granule_LINKS+"' in object.";
100  BESDEBUG(MODULE, msg << endl);
101  if(!result){
102  throw CmrError("ERROR: Failed to located '"+granule_LINKS+"' section for CMRGranule!",__FILE__,__LINE__);
103  }
104  const rapidjson::Value& links = itr->value;
105  if(!links.IsArray())
106  throw CmrError("ERROR: The '"+granule_LINKS+"' object is NOT an array!",__FILE__,__LINE__);
107 
108  return links;
109 }
110 
114 void Granule::setDataAccessUrl(const rapidjson::Value& go){
115  rjson_utils rju;
116 
117  const rapidjson::Value& links = get_links_array(go);
118  for (rapidjson::SizeType i = 0; i < links.Size(); i++) { // Uses SizeType instead of size_t
119  const rapidjson::Value& link = links[i];
120  string rel = rju.getStringValue(link,granule_LINKS_REL);
121  if(rel == granule_LINKS_REL_DATA_ACCES){
122  this->d_data_access_url = rju.getStringValue(link,granule_LINKS_HREF);
123  return;
124  }
125  }
126  throw CmrError("ERROR: Failed to locate granule data access link ("+granule_LINKS_REL_DATA_ACCES+"). :(",__FILE__,__LINE__);
127 }
128 
132 void Granule::setMetadataAccessUrl(const rapidjson::Value& go){
133  rjson_utils rju;
134 
135  const rapidjson::Value& links = get_links_array(go);
136  for (rapidjson::SizeType i = 0; i < links.Size(); i++) { // Uses SizeType instead of size_t
137  const rapidjson::Value& link = links[i];
138  string rel = rju.getStringValue(link,granule_LINKS_REL);
139  if(rel == granule_LINKS_REL_METADATA_ACCESS){
140  this->d_metadata_access_url = rju.getStringValue(link,granule_LINKS_HREF);
141  return;
142  }
143  }
144  throw CmrError("ERROR: Failed to locate granule metadata access link ("+granule_LINKS_REL_METADATA_ACCESS+"). :(",__FILE__,__LINE__);
145 }
146 
147 
148 bes::CatalogItem *Granule::getCatalogItem(BESCatalogUtils *d_catalog_utils){
149  bes::CatalogItem *item = new bes::CatalogItem();
150  item->set_type(bes::CatalogItem::leaf);
151  item->set_name(getName());
152  item->set_lmt(getLastModifiedStr());
153  item->set_size(getSize());
154  item->set_is_data(d_catalog_utils->is_data(item->get_name()));
155  return item;
156 }
157 
158 
159 
160 } //namespace cmr
GenericMemberIterator
(Constant) member iterator for a JSON object value
Definition: cmr_module/rapidjson/document.h:101
BESCatalogUtils::is_data
bool is_data(const std::string &item) const
is there a handler that can process this
Definition: BESCatalogUtils.cc:447
bes::CatalogItem::set_size
void set_size(size_t s)
Set the size of the item.
Definition: CatalogItem.h:140
BESCatalogUtils
Definition: BESCatalogUtils.h:61
bes::CatalogItem::set_name
void set_name(std::string n)
Set the name of the item.
Definition: CatalogItem.h:135
bes::CatalogItem::set_is_data
void set_is_data(bool id)
Is this item data that the BES should interpret?
Definition: CatalogItem.h:150
SizeType
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: cmr_module/rapidjson/rapidjson.h:380
Value
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: cmr_module/rapidjson/document.h:2010
bes::CatalogItem
Definition: CatalogItem.h:72
bes::CatalogItem::get_name
std::string get_name() const
The name of this item in the node.
Definition: CatalogItem.h:133
bes::CatalogItem::set_type
void set_type(item_type t)
Set the type for this item.
Definition: CatalogItem.h:155
bes::CatalogItem::set_lmt
void set_lmt(std::string lmt)
Set the LMT for this item.
Definition: CatalogItem.h:145