32 #include <Structure.h>
37 #include <BESInternalError.h>
39 #include "DapFunctionUtils.h"
41 #define DEBUG_KEY "functions"
47 void 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);
63 vector<string>* pAttrTokens = sourceAttrTable.get_attr_vector(it);
65 fdds->get_attr_table().append_attr(childName, type, pAttrTokens);
98 void 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);
174 libdap::BaseType *wrapitup_worker(vector<libdap::BaseType*> argv, libdap::AttrTable globals) {
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 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup_worker() - Result Structure attrs: " << endl << dapResult->get_attr_table() << endl);
206 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup_worker() - Creating response object called "<< dapResult->name() << endl);
208 libdap::Str *message =
new libdap::Str(
"promoted_message");
209 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.");
210 dapResult->add_var_nocopy(message);
213 message->set_read_p(
true);
214 message->set_send_p(
true);
219 dapResult->set_read_p(
true);
220 dapResult->set_send_p(
true);
224 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup_worker() - END" << endl);
255 void wrapitup(
int argc, libdap::BaseType *argv[], libdap::DDS &dds, libdap::BaseType **btpp) {
257 std::string wrap_name=dds.get_dataset_name()+
"_unwrap";
259 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - BEGIN" << endl);
261 libdap::Structure *dapResult =
new libdap::Structure(wrap_name);
265 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - Attempting to return arguments bundled into "<< wrap_name << endl);
267 for(
int i=0; i<argc ; i++){
268 libdap::BaseType *bt = argv[i];
269 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - Reading "<< bt->name() << endl);
271 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - Adding a copy of "<< bt->name() <<
" to " << dapResult->name() << endl);
278 dapResult->add_var_nocopy(bt->ptr_duplicate());
281 libdap::AttrTable &globals = dds.get_attr_table();
282 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - Copying Global Attributes" << endl << globals << endl);
284 libdap::AttrTable *newDatasetAttrTable =
new libdap::AttrTable(globals);
285 dapResult->set_attr_table(*newDatasetAttrTable);
286 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - Result Structure attrs: " << endl << dapResult->get_attr_table() << endl);
290 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - Creating response object called "<< dapResult->name() << endl);
292 libdap::Str *message =
new libdap::Str(
"promoted_message");
293 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.");
294 dapResult->add_var_nocopy(message);
297 message->set_read_p(
true);
298 message->set_send_p(
true);
303 dapResult->set_read_p(
true);
304 dapResult->set_send_p(
true);
309 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - END" << endl);
314 void function_dap2_wrapitup(
int argc, libdap::BaseType *argv[], libdap::DDS &dds, libdap::BaseType **btpp)
316 BESDEBUG(DEBUG_KEY,
"function_dap2_wrapitup() - BEGIN" << endl);
318 BESDEBUG(DEBUG_KEY,
"function_dap2_wrapitup() - Building argument vector for wrapitup_worker()" << endl);
319 vector<libdap::BaseType*> args;
320 for(
int i=0; i< argc; i++){
321 libdap::BaseType *bt = argv[i];
322 BESDEBUG(DEBUG_KEY,
"function_dap2_wrapitup() - Adding argument: "<< bt->name() << endl);
326 *btpp = wrapitup_worker(args,dds.get_attr_table());
328 BESDEBUG(DEBUG_KEY,
"function_dap2_wrapitup() - END (result: "<< (*btpp)->name() <<
")" << endl);
333 libdap::BaseType *function_dap4_wrapitup(libdap::D4RValueList *dvl_args, libdap::DMR &dmr){
335 BESDEBUG(DEBUG_KEY,
"function_dap4_wrapitup() - Building argument vector for wrapitup_worker()" << endl);
336 vector<libdap::BaseType*> args;
337 for(
unsigned int i=0; i< dvl_args->size(); i++){
338 libdap::BaseType * bt = dvl_args->get_rvalue(i)->value(dmr);
339 BESDEBUG(DEBUG_KEY,
"function_dap4_wrapitup() - Adding argument: "<< bt->name() << endl);
344 libdap::BaseType *result = wrapitup_worker(args, dmr.root()->get_attr_table());
346 BESDEBUG(DEBUG_KEY,
"function_dap4_wrapitup() - END (result: "<< result->name() <<
")" << endl);