27#include <libdap/D4RValue.h>
28#include <libdap/DMR.h>
29#include <libdap/DDS.h>
30#include <libdap/BaseType.h>
31#include <libdap/Str.h>
32#include <libdap/Structure.h>
33#include <libdap/D4Group.h>
37#include <BESInternalError.h>
39#include "DapFunctionUtils.h"
41#define DEBUG_KEY "functions"
47void promote_atributes_to_global(libdap::Structure *sourceObj, libdap::DDS *fdds){
49 libdap::AttrTable sourceAttrTable = sourceObj->get_attr_table();
51 libdap::AttrTable::Attr_iter endIt = sourceAttrTable.attr_end();
52 libdap::AttrTable::Attr_iter it;
53 for (it = sourceAttrTable.attr_begin(); it != endIt; ++it) {
54 std::string childName = sourceAttrTable.get_name(it);
55 bool childIsContainer = sourceAttrTable.is_container(it);
56 BESDEBUG(DEBUG_KEY,
"DFU::promote_atributes_to_global() - Processing attribute " << childName << endl );
57 if (childIsContainer) {
58 libdap::AttrTable* pClonedAttrTable =
new libdap::AttrTable(*sourceAttrTable.get_attr_table(it));
59 fdds->get_attr_table().append_container(pClonedAttrTable, childName);
62 string type = sourceAttrTable.get_type(it);
65 fdds->get_attr_table().append_attr(childName, type, pAttrTokens);
98void promote_function_output_structures(libdap::DDS *fdds)
100 BESDEBUG(DEBUG_KEY,
"DFU::promote_function_output_structures() - BEGIN" << endl);
120 std::vector<libdap::BaseType *> upVars;
121 std::vector<libdap::BaseType *> droppedContainers;
122 for (libdap::DDS::Vars_citer di = fdds->var_begin(), de = fdds->var_end(); di != de; ++di) {
124 libdap::Structure *collection =
dynamic_cast<libdap::Structure *
>(*di);
126 BESDEBUG(DEBUG_KEY,
"DFU::promote_function_output_structures() - Promoting members of collection '" << collection->name() <<
"'" << endl);
130 droppedContainers.push_back(collection);
132 promote_atributes_to_global(collection,fdds);
134 libdap::Structure::Vars_iter vi;
135 for (vi =collection->var_begin(); vi != collection->var_end(); ++vi) {
136 libdap::BaseType *origVar = *vi;
142 libdap::BaseType *newVar = origVar->ptr_duplicate();
146 newVar->set_parent(0);
148 upVars.push_back(newVar);
153 for(std::vector<libdap::BaseType *>::iterator it=droppedContainers.begin(); it != droppedContainers.end(); ++it) {
154 libdap::BaseType *bt = *it;
155 BESDEBUG(DEBUG_KEY,
"DFU::promote_function_output_structures() - Deleting Promoted Collection '" << bt->name() <<
"' ptr: " << bt << endl);
158 fdds->del_var(bt->name());
162 for( std::vector<libdap::BaseType *>::iterator it = upVars.begin(); it != upVars.end(); it ++) {
163 libdap::BaseType *bt = *it;
164 BESDEBUG(DEBUG_KEY,
"DFU::promote_function_output_structures() - Adding Promoted Variable '" << bt->name() <<
"' to DDS. ptr: " << bt << endl);
169 BESDEBUG(DEBUG_KEY,
"DFU::promote_function_output_structures() - END" << endl);
176 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup_worker() - BEGIN" << endl);
178 std::string wrap_name=
"thing_to_unwrap";
179 libdap::Structure *dapResult =
new libdap::Structure(wrap_name);
180 int argc = argv.size();
182 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup_worker() - Attempting to return arguments bundled into "<< wrap_name << endl);
184 for(
int i=0; i<argc ; i++){
185 libdap::BaseType *bt = argv[i];
186 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup_worker() - Reading "<< bt->name() << endl);
188 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup_worker() - Adding a copy of "<< bt->name() <<
" to " << dapResult->name() << endl);
195 dapResult->add_var_nocopy(bt->ptr_duplicate());
198 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup_worker() - Copying Global Attributes" << endl << globals << endl);
200 libdap::AttrTable *newDatasetAttrTable =
new libdap::AttrTable(globals);
201 dapResult->set_attr_table(*newDatasetAttrTable);
202 delete newDatasetAttrTable;
203 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup_worker() - Result Structure attrs: " << endl << dapResult->get_attr_table() << endl);
207 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup_worker() - Creating response object called "<< dapResult->name() << endl);
209 libdap::Str *message =
new libdap::Str(
"promoted_message");
210 message->set_value(
"This libdap:Str object should appear at the top level of the response and not as a member of a libdap::Constructor type.");
211 dapResult->add_var_nocopy(message);
214 message->set_read_p(
true);
215 message->set_send_p(
true);
220 dapResult->set_read_p(
true);
221 dapResult->set_send_p(
true);
225 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup_worker() - END" << endl);
256void wrapitup(
int argc, libdap::BaseType *argv[], libdap::DDS &dds, libdap::BaseType **btpp) {
258 std::string wrap_name=dds.get_dataset_name()+
"_unwrap";
260 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - BEGIN" << endl);
262 libdap::Structure *dapResult =
new libdap::Structure(wrap_name);
266 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - Attempting to return arguments bundled into "<< wrap_name << endl);
268 for(
int i=0; i<argc ; i++){
269 libdap::BaseType *bt = argv[i];
270 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - Reading "<< bt->name() << endl);
272 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - Adding a copy of "<< bt->name() <<
" to " << dapResult->name() << endl);
279 dapResult->add_var_nocopy(bt->ptr_duplicate());
282 libdap::AttrTable &globals = dds.get_attr_table();
283 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - Copying Global Attributes" << endl << globals << endl);
285 libdap::AttrTable *newDatasetAttrTable =
new libdap::AttrTable(globals);
286 dapResult->set_attr_table(*newDatasetAttrTable);
287 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - Result Structure attrs: " << endl << dapResult->get_attr_table() << endl);
291 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - Creating response object called "<< dapResult->name() << endl);
293 libdap::Str *message =
new libdap::Str(
"promoted_message");
294 message->set_value(
"This libdap:Str object should appear at the top level of the response and not as a member of a libdap::Constructor type.");
295 dapResult->add_var_nocopy(message);
298 message->set_read_p(
true);
299 message->set_send_p(
true);
304 dapResult->set_read_p(
true);
305 dapResult->set_send_p(
true);
310 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - END" << endl);
315void function_dap2_wrapitup(
int argc, libdap::BaseType *argv[], libdap::DDS &dds, libdap::BaseType **btpp)
317 BESDEBUG(DEBUG_KEY,
"function_dap2_wrapitup() - BEGIN" << endl);
319 BESDEBUG(DEBUG_KEY,
"function_dap2_wrapitup() - Building argument vector for wrapitup_worker()" << endl);
321 for(
int i=0; i< argc; i++){
322 libdap::BaseType *bt = argv[i];
323 BESDEBUG(DEBUG_KEY,
"function_dap2_wrapitup() - Adding argument: "<< bt->name() << endl);
327 *btpp = wrapitup_worker(args,dds.get_attr_table());
329 BESDEBUG(DEBUG_KEY,
"function_dap2_wrapitup() - END (result: "<< (*btpp)->name() <<
")" << endl);
334libdap::BaseType *function_dap4_wrapitup(libdap::D4RValueList *dvl_args, libdap::DMR &dmr){
336 BESDEBUG(DEBUG_KEY,
"function_dap4_wrapitup() - Building argument vector for wrapitup_worker()" << endl);
338 for(
unsigned int i=0; i< dvl_args->size(); i++){
339 libdap::BaseType * bt = dvl_args->get_rvalue(i)->value(dmr);
340 BESDEBUG(DEBUG_KEY,
"function_dap4_wrapitup() - Adding argument: "<< bt->name() << endl);
345 libdap::BaseType *result = wrapitup_worker(args, dmr.root()->get_attr_table());
347 BESDEBUG(DEBUG_KEY,
"function_dap4_wrapitup() - END (result: "<< result->name() <<
")" << endl);
static bool endsWith(std::string const &fullString, std::string const &ending)