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);
168 BESDEBUG(DEBUG_KEY,
"DFU::promote_function_output_structures() - END" << endl);
173 libdap::BaseType *wrapitup_worker(vector<libdap::BaseType*> argv, libdap::AttrTable globals) {
175 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup_worker() - BEGIN" << endl);
177 std::string wrap_name=
"thing_to_unwrap";
178 libdap::Structure *dapResult =
new libdap::Structure(wrap_name);
179 int argc = argv.size();
181 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup_worker() - Attempting to return arguments bundled into "<< wrap_name << endl);
183 for(
int i=0; i<argc ; i++){
184 libdap::BaseType *bt = argv[i];
185 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup_worker() - Reading "<< bt->name() << endl);
187 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup_worker() - Adding a copy of "<< bt->name() <<
" to " << dapResult->name() << endl);
194 dapResult->add_var_nocopy(bt->ptr_duplicate());
197 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup_worker() - Copying Global Attributes" << endl << globals << endl);
199 libdap::AttrTable *newDatasetAttrTable =
new libdap::AttrTable(globals);
200 dapResult->set_attr_table(*newDatasetAttrTable);
201 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup_worker() - Result Structure attrs: " << endl << dapResult->get_attr_table() << endl);
205 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup_worker() - Creating response object called "<< dapResult->name() << endl);
207 libdap::Str *message =
new libdap::Str(
"promoted_message");
208 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.");
209 dapResult->add_var_nocopy(message);
212 message->set_read_p(
true);
213 message->set_send_p(
true);
218 dapResult->set_read_p(
true);
219 dapResult->set_send_p(
true);
223 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup_worker() - END" << endl);
254 void wrapitup(
int argc, libdap::BaseType *argv[], libdap::DDS &dds, libdap::BaseType **btpp) {
256 std::string wrap_name=dds.get_dataset_name()+
"_unwrap";
258 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - BEGIN" << endl);
260 libdap::Structure *dapResult =
new libdap::Structure(wrap_name);
264 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - Attempting to return arguments bundled into "<< wrap_name << endl);
266 for(
int i=0; i<argc ; i++){
267 libdap::BaseType *bt = argv[i];
268 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - Reading "<< bt->name() << endl);
270 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - Adding a copy of "<< bt->name() <<
" to " << dapResult->name() << endl);
277 dapResult->add_var_nocopy(bt->ptr_duplicate());
280 libdap::AttrTable &globals = dds.get_attr_table();
281 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - Copying Global Attributes" << endl << globals << endl);
283 libdap::AttrTable *newDatasetAttrTable =
new libdap::AttrTable(globals);
284 dapResult->set_attr_table(*newDatasetAttrTable);
285 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - Result Structure attrs: " << endl << dapResult->get_attr_table() << endl);
289 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - Creating response object called "<< dapResult->name() << endl);
291 libdap::Str *message =
new libdap::Str(
"promoted_message");
292 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.");
293 dapResult->add_var_nocopy(message);
296 message->set_read_p(
true);
297 message->set_send_p(
true);
302 dapResult->set_read_p(
true);
303 dapResult->set_send_p(
true);
308 BESDEBUG(DEBUG_KEY,
"DFU::wrapitup() - END" << endl);
313 void function_dap2_wrapitup(
int argc, libdap::BaseType *argv[], libdap::DDS &dds, libdap::BaseType **btpp)
315 BESDEBUG(DEBUG_KEY,
"function_dap2_wrapitup() - BEGIN" << endl);
317 BESDEBUG(DEBUG_KEY,
"function_dap2_wrapitup() - Building argument vector for wrapitup_worker()" << endl);
318 vector<libdap::BaseType*> args;
319 for(
int i=0; i< argc; i++){
320 libdap::BaseType *bt = argv[i];
321 BESDEBUG(DEBUG_KEY,
"function_dap2_wrapitup() - Adding argument: "<< bt->name() << endl);
325 *btpp = wrapitup_worker(args,dds.get_attr_table());
327 BESDEBUG(DEBUG_KEY,
"function_dap2_wrapitup() - END (result: "<< (*btpp)->name() <<
")" << endl);
332 libdap::BaseType *function_dap4_wrapitup(libdap::D4RValueList *dvl_args, libdap::DMR &dmr){
334 BESDEBUG(DEBUG_KEY,
"function_dap4_wrapitup() - Building argument vector for wrapitup_worker()" << endl);
335 vector<libdap::BaseType*> args;
336 for(
unsigned int i=0; i< dvl_args->size(); i++){
337 libdap::BaseType * bt = dvl_args->get_rvalue(i)->value(dmr);
338 BESDEBUG(DEBUG_KEY,
"function_dap4_wrapitup() - Adding argument: "<< bt->name() << endl);
343 libdap::BaseType *result = wrapitup_worker(args, dmr.root()->get_attr_table());
345 BESDEBUG(DEBUG_KEY,
"function_dap4_wrapitup() - END (result: "<< result->name() <<
")" << endl);
static bool endsWith(std::string const &fullString, std::string const &ending)