Index: branches/prototype-v0/zoo-project/zoo-kernel/response_print.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/response_print.c	(revision 851)
+++ branches/prototype-v0/zoo-project/zoo-kernel/response_print.c	(revision 854)
@@ -743,5 +743,13 @@
 }
 
-int addMetadata(map* meta,xmlDocPtr doc,xmlNodePtr nc,xmlNsPtr ns_ows,xmlNsPtr ns_xlink){
+/**
+ * Add a Metadata node to any existing node.
+ * @param meta the map defining the additional parameters
+ * @param doc the XML document used
+ * @param nb the node to add the additional parameters
+ * @param ns_ows the OWS namespace
+ * @param ns_xlink the xlink namespace
+ */
+void addMetadata(map* meta,xmlDocPtr doc,xmlNodePtr nc,xmlNsPtr ns_ows,xmlNsPtr ns_xlink){
     int hasTitle=-1;
     int hasValue=-1;
@@ -817,14 +825,30 @@
 }
 
-
-int addAdditionalParameters(map* meta,xmlDocPtr doc,xmlNodePtr nc,xmlNsPtr ns_ows,xmlNsPtr ns_xlink,int fromDb){
+/**
+ * Add AdditionalParameters nodes to any existing node.
+ * @param meta the map defining the additional parameters
+ * @param doc the XML document used
+ * @param nb the node to add the additional parameters
+ * @param ns_ows the OWS namespace
+ * @param ns_xlink the xlink namespace
+ * @param fromDb 1 if the metadata has been extracted from the metadb, 
+ * 0 otherwise
+ */
+void addAdditionalParameters(map* meta,xmlDocPtr doc,xmlNodePtr nc,xmlNsPtr ns_ows,xmlNsPtr ns_xlink,int fromDb){
     int hasTitle=-1;
     int hasValue=-1;
+    int toAddAtEnd=-1;
+    int cnt=0;
+    xmlNodePtr* ncr=NULL;
     xmlNodePtr nc1;
     map* oMeta=meta;
     int isAdditionalParameters=-1;
-    //if(count(oMeta)>=2){
     int level=0;
     map* test=getMap(meta,"title");
+    map* otitle=getMap(meta,"title");
+    map* length=getMap(meta,"length");
+    int len=0;
+    char *ctitle=NULL;
+    
     if(test!=NULL)
       level+=1;
@@ -837,10 +861,9 @@
     if(count(oMeta)>level+1)
       isAdditionalParameters=1;
-    //}
-    char *ctitle=NULL;
+
     while(meta!=NULL){
-      if(hasTitle<0)
-	if(hasValue<0)
-	  nc1 = xmlNewNode(ns_ows, BAD_CAST "AdditionalParameters");
+      if(hasTitle<0 && hasValue<0){
+	nc1 = xmlNewNode(ns_ows, BAD_CAST "AdditionalParameters");
+      }
       if(strncasecmp(meta->name,"title",5)==0 ||
 	 strcasecmp(meta->name,"href")==0 ||
@@ -851,8 +874,12 @@
 	  hasTitle=1;
 	  if(ctitle!=NULL && strcasecmp(meta->value,ctitle)!=0){
-	      xmlAddChild(nc,nc1);
-	      nc1 = xmlNewNode(ns_ows, BAD_CAST "AdditionalParameters");
-	      free(ctitle);
-	      ctitle=NULL;
+	    xmlNodePtr ncTmp = xmlDocCopyNodeList(doc,nc1);
+	    xmlAddChild(nc,ncTmp);
+	    xmlFreeNode(nc1);
+	    toAddAtEnd=1;
+	    cnt++;
+	    nc1 = xmlNewNode(ns_ows, BAD_CAST "AdditionalParameters");
+	    free(ctitle);
+	    ctitle=NULL;
 	  }
 	  if(ctitle==NULL){
@@ -865,4 +892,6 @@
 	    free(ctitle);
 	  ctitle=zStrdup(meta->value);
+	}else{
+	  xmlNewNsProp(nc1,ns_xlink,BAD_CAST meta->name,BAD_CAST meta->value);
 	}
       }else{
@@ -892,11 +921,17 @@
       meta=meta->next;
       if(hasTitle<0){
-	xmlAddChild(nc,nc1);
+	//xmlAddChild(nc,nc1);
 	hasValue=1;
-      }else
-	free(ctitle);
-    }
-    if(oMeta!=NULL && hasValue<0 && nc1!=NULL){
+      }/*else
+	if(ctitle!=NULL)
+	free(ctitle);*/
+    }
+    if(length!=NULL)
+      len=atoi(length->value);
+    if(otitle!=NULL)
+      len=1;
+    if(cnt<len){
       xmlAddChild(nc,nc1);
+      free(ctitle);
     }
 }
@@ -1406,6 +1441,7 @@
 		  }
 		}
-		else
+		else{
 		  xmlFreeNode(nc9);
+		}
 		if(strcasecmp(tmp1->name,"uom")==0)
 		  hasUOM1=true;
@@ -1428,6 +1464,7 @@
 	    if(hasUOM1==false && vid==0){
 	      xmlFreeNode(nc5);
-	      if(datatype==1)
+	      if(datatype==1){
 		xmlFreeNode(nc4);
+	      }
 	    }
 	    else
@@ -1440,6 +1477,7 @@
 	if(datatype!=1 && default1<0){
 	  xmlFreeNode(nc5);
-	  if(datatype!=2)
+	  if(datatype!=2){
 	    xmlFreeNode(nc4);
+	  }
 	}
 
@@ -1603,9 +1641,11 @@
 		if(vid==0 || oI!=4)
 		  xmlAddChild(nc5,nc6);
-		else
+		else{
 		  xmlFreeNode(nc6);
+		}
 	      }
-	      else
+	      else{
 		xmlFreeNode(nc6);
+	      }
 	    }
 	    tmp1=tmp1->next;
@@ -1634,5 +1674,4 @@
 	    }
 	    else{
-	      xmlFreeNode(nc4);
 	      xmlAddChild(nc3,nc5);
 	    }
@@ -1648,6 +1687,7 @@
 
       if(hasSupported==0){
-	if(datatype==0 && vid!=0)
+	if(datatype==0 && vid!=0){
 	  xmlFreeNode(nc4);
+	}
 	xmlFreeNode(nc5);
       }
@@ -2678,5 +2718,11 @@
       toto=getMap(tmpI->content,"asReference");
 #ifdef USE_MS
-      if(toto!=NULL && strcasecmp(toto->value,"true")==0 && (testMap==NULL || strncasecmp(testMap->value,"true",4)!=0) )
+      restartNoMS:
+      map* geodatatype=getMap(tmpI->content,"geodatatype");
+      
+      if(toto!=NULL && strcasecmp(toto->value,"true")==0 &&
+	 (testMap==NULL ||
+	  strncasecmp(testMap->value,"true",4)!=0 ||
+	  (geodatatype!=NULL && strcasecmp(geodatatype->value,"other")==0) ) ) 
 #else
       if(toto!=NULL && strcasecmp(toto->value,"true")==0)
@@ -2699,7 +2745,7 @@
 	  }
 
+	  char *file_path=NULL;
 	  if(gfile==NULL) {
 	    map *ext=getMap(tmpI->content,"extension");
-	    char *file_path;
 	    char file_ext[32];
 
@@ -2716,5 +2762,5 @@
 	      free(file_name);
 	    file_name=(char*)malloc((strlen(s->name)+strlen(usid->value)+strlen(file_ext)+strlen(tmpI->name)+45)*sizeof(char));
-	    sprintf(file_name,"%s_%s_%s_%d.%s",s->name,tmpI->name,usid->value,itn,file_ext);
+	    sprintf(file_name,"ZOO_DATA_%s_%s_%s_%d.%s",s->name,tmpI->name,usid->value,itn,file_ext);
 	    itn++;
 	    file_path=(char*)malloc((strlen(tmp1->value)+strlen(file_name)+2)*sizeof(char));
@@ -2730,5 +2776,4 @@
 	      return;
 	    }
-	    free(file_path);
 
 	    toto=getMap(tmpI->content,"value");
@@ -2744,10 +2789,9 @@
 	    }
 	    fclose(ofile);
-
 	  }
 
 	  map *tmp2=getMapFromMaps(m,"main","tmpUrl");
 	  map *tmp3=getMapFromMaps(m,"main","serverAddress");
-	  char *file_url;
+	  char *file_url=NULL;
 	  if(strncasecmp(tmp2->value,"http://",7)==0 ||
 	     strncasecmp(tmp2->value,"https://",8)==0){
@@ -2759,6 +2803,18 @@
 	  }
 	  addToMap(tmpI->content,"Reference",file_url);
-	  free(file_name);
-	  free(file_url);
+	  /*maps* curs=tmpI;
+	  curs=curs->next;
+	  map* test=getMap(tmpI->content,"replicateStorageNext");
+	  if(test!=NULL && strncasecmp(test->value,"true",4)==0)
+	    while(curs!=NULL){
+	      addToMap(curs->content,"storage",file_path);
+	      curs=curs->next;
+	      }
+	  if(file_path!=NULL)
+	  free(file_path);*/
+	  if(file_name!=NULL)
+	    free(file_name);
+	  if(file_url!=NULL)
+	    free(file_url);
 	  file_name=NULL;
 	}
@@ -2766,5 +2822,9 @@
       else{
 	if(testMap!=NULL){
+	  setMapInMaps(m,"lenv","state","out");
 	  setReferenceUrl(m,tmpI);
+	  geodatatype=getMap(tmpI->content,"geodatatype");
+	  if(geodatatype!=NULL && strcasecmp(geodatatype->value,"other")==0)
+	    goto restartNoMS;
 	}
       }
