[Mapbender-commits] r9806 - in trunk/mapbender/http: classes php

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Fri Oct 20 04:04:45 PDT 2017


Author: armin11
Date: 2017-10-20 04:04:45 -0700 (Fri, 20 Oct 2017)
New Revision: 9806

Modified:
   trunk/mapbender/http/classes/class_syncCkan.php
   trunk/mapbender/http/php/mod_syncCkan_server.php
Log:
New option to sync to different ckan instances via ckan api

Modified: trunk/mapbender/http/classes/class_syncCkan.php
===================================================================
--- trunk/mapbender/http/classes/class_syncCkan.php	2017-10-19 12:26:18 UTC (rev 9805)
+++ trunk/mapbender/http/classes/class_syncCkan.php	2017-10-20 11:04:45 UTC (rev 9806)
@@ -111,6 +111,89 @@
         return $array;
     }
 
+    public function getRemoteCkanRecordList($orga_filter,$central_filter,$filter,$ckan_api_url,$ckan_api_version) {
+        $ckanConnector = new connector($ckan_api_url.$ckan_api_version."/"."action/package_search?q=".$filter."&rows=1000"."&facet=true");
+        $ckanConnector->set("timeOut", "3");
+        if ($ckanConnector->timedOut == true) {
+            return false;
+        }
+        $listOfFilteredData = json_decode($ckanConnector->file);
+        $externalCkanMetadataArray = array();
+        $countExternalCkanMetadataArray = 0;
+        if ($listOfFilteredData->success == true) {
+            foreach($listOfFilteredData->result->results as $dataset) {
+                $externalCkanMetadataArray[$countExternalCkanMetadataArray]['uuid'] = $dataset->id;
+                //$externalCkanMetadataArray[$countExternalCkanMetadataArray]['name'] = $dataset->name;
+                $externalCkanMetadataArray[$countExternalCkanMetadataArray]['changedate'] = $dataset->metadata_modified;
+                $countExternalCkanMetadataArray++;
+	        //echo $dataset->id." - ".$dataset->metadata_modified."<br>";
+            }
+	    //$e = new mb_exception("Number of results: ".$countExternalCkanMetadataArray);	
+        }
+        return $externalCkanMetadataArray;
+    }
+
+    public function getCkanRepresentationFromCkan($ckan_api_url, $ckan_api_version, $ckan_package_id, $central_filter, $orga_ident) {
+	$ckanConnector = new connector($ckan_api_url.$ckan_api_version."/"."action/package_show?id=".$ckan_package_id);
+        $ckanConnector->set("timeOut", "3");
+        if ($ckanConnector->timedOut == true) {
+            return false;
+        }
+        $ckanPackageJson = $ckanConnector->file;
+//$e = new mb_exception("remote ckan package: ".$ckanPackageJson);
+        $ckanPackageRemote = json_decode($ckanPackageJson);
+	$ckanPackage->title = "Demo: ".$ckanPackageRemote->result->title;
+//use identical name and value!
+	$ckanPackage->id = $ckanPackageRemote->result->id;	
+$ckanPackage->name = $ckanPackageRemote->result->id;
+//."_external_id";
+$ckanPackage->owner_org = $orga_ident;	
+$ckanPackage->notes = "notes...";
+
+$ckanPackage->groups[0]->name = "transparenzgesetz";
+$ckanPackage->state = "active";
+$ckanPackage->type = "decision-rp-1";
+$ckanPackage->license_id = "odc-odbl";
+	//pull filter
+	if (isset($central_filter) && $central_filter !== "") {
+		$central_filterArray = explode(":",$central_filter);
+		$ckanPackage->{$central_filterArray[0]} = $central_filterArray[1];
+	}
+	//$ckanPackage->transparency_category_de_rp = "spatial_data";
+	$resourcesArray = array();
+
+	foreach($ckanPackageRemote->result->resources as $resource) {
+		$newResource = new stdClass();
+		if (isset($resource->name) && $resource->name !=='') {
+			$newResource->name = $resource->name;
+		} else {
+			$newResource->name = "Testtitel (name) für resource";
+		}
+		if (isset($resource->url) && $resource->url !=='') {
+			$newResource->url = $resource->url;
+		} else {
+			$newResource->url = "http://www.geoportal.rlp.de";
+		}
+		if (isset($resource->format) && $resource->format !=='') {
+			$newResource->format = $resource->format;
+		} else {
+			$newResource->format = "PDF";
+		}	
+		$resourcesArray[] = $newResource;
+	}
+
+	/*$resourcesArray[0]->url = "http://www.geoportal.rlp.de";
+	$resourcesArray[0]->format = "PDF";*/
+
+	$ckanPackage->resources = $resourcesArray;
+
+	//$e = new mb_exception("ckan json object test: ".json_encode($ckanPackage));
+        $returnArray = array();
+        $returnArray['json'] = json_encode($ckanPackage);	
+	return $returnArray;	
+    }
+
+
     public function getCswRecordList($cswId, $orgaName, $recordType) {
 	//function to call external csw with orga filter (filter orga which is responsible for publication!)
 	$recordsPerPage = 20;
@@ -164,11 +247,11 @@
 
         if (isset($this->mapbenderUserId) && (integer)$this->mapbenderUserId > 0) {
             if (isset($this->syncOrgaId) && (integer)$this->syncOrgaId > 0) {
-	        $sql = "SELECT DISTINCT mb_group_id, mb_group_title, mb_group_name, mb_group_title, mb_group_email, mb_group_ckan_uuid, mb_group_ckan_api_key, mb_group_csw_catalogues, mb_user_mb_group.mb_user_mb_group_type FROM mb_group JOIN mb_user_mb_group ON mb_group_id = fkey_mb_group_id AND fkey_mb_user_id = $1 AND mb_group_id = $2 AND mb_user_mb_group_type IN (2,3)";
+	        $sql = "SELECT DISTINCT mb_group_id, mb_group_title, mb_group_name, mb_group_title, mb_group_email, mb_group_ckan_uuid, mb_group_ckan_api_key, mb_group_csw_catalogues, mb_group_ckan_catalogues, mb_user_mb_group.mb_user_mb_group_type FROM mb_group JOIN mb_user_mb_group ON mb_group_id = fkey_mb_group_id AND fkey_mb_user_id = $1 AND mb_group_id = $2 AND mb_user_mb_group_type IN (2,3)";
 	        $v = array($this->mapbenderUserId, $this->syncOrgaId);
 	        $t = array('i','i');
             } else {
-	        $sql = "SELECT DISTINCT mb_group_id, mb_group_title, mb_group_name, mb_group_title, mb_group_email, mb_group_ckan_uuid, mb_group_ckan_api_key, mb_group_csw_catalogues, mb_user_mb_group.mb_user_mb_group_type FROM mb_group JOIN mb_user_mb_group ON mb_group_id = fkey_mb_group_id AND fkey_mb_user_id = $1 AND mb_user_mb_group_type IN (2,3)";
+	        $sql = "SELECT DISTINCT mb_group_id, mb_group_title, mb_group_name, mb_group_title, mb_group_email, mb_group_ckan_uuid, mb_group_ckan_api_key, mb_group_csw_catalogues, mb_group_ckan_catalogues, mb_user_mb_group.mb_user_mb_group_type FROM mb_group JOIN mb_user_mb_group ON mb_group_id = fkey_mb_group_id AND fkey_mb_user_id = $1 AND mb_user_mb_group_type IN (2,3)";
 	        $v = array($this->mapbenderUserId);
 	        $t = array('i');
             }
@@ -189,6 +272,7 @@
                 $departmentsArray[$countDepArray]["ckan_api_key"] = $row["mb_group_ckan_api_key"];
 		//if ($row["mb_group_csw_catalogues"] !== null && $row["mb_group_csw_catalogues"] !== '') {
 			$departmentsArray[$countDepArray]["csw_catalogues"] = $row["mb_group_csw_catalogues"];
+			$departmentsArray[$countDepArray]["ckan_catalogues"] = $row["mb_group_ckan_catalogues"];
 		//}
                 if ($row["mb_user_mb_group_type"] == 2) {
                     $departmentsArray[$countDepArray]["is_primary_group"] = true;
@@ -224,7 +308,201 @@
     	return $result;
     }
 
-    public function getSyncListCswJson($departmentsArray, $listAllMetadataInJson = false) {
+    public function getExternalSource() {
+
+    }
+
+    public function syncExternalSource() {
+    
+    }
+
+    public function getSyncListRemoteCkanJson($departmentsArray, $departmentId, $listAllMetadataInJson = false) {
+        $syncListRemoteCkan = new stdClass(); //should handle the returned json object
+	$syncListResultRemoteCkan = new stdClass();
+	$syncListRemoteCkan->help = "helptext";
+	$syncListRemoteCkan->success = false;
+        $syncListRemoteCkan->function = "getSyncListRemoteCkanJson";
+        $numberOfCatalogue = 0;
+	//TODO - choose right department!!!!
+	//get array index 
+	$index = 0;
+	$idFound == false;
+
+$e = new mb_exception("classes/class_syncCkan.php: parameter departmentId: ".$departmentId);
+
+	foreach ($departmentsArray as $department) {
+		$dId = (integer)$department['id'];
+		if ($dId == $departmentId) {	
+			$idFound = true;
+			break;
+		}
+		$index++;
+	}
+	if ($idFound == false) {
+	    $e = new mb_exception("classes/class_syncCkan.php: Requested department not found in departmentArray!");
+	    return false;
+	}
+	$organization = $departmentsArray[$index];
+	$catalogues = json_decode($departmentsArray[$index]["ckan_catalogues"])->ckan_catalogues;
+        foreach ($catalogues as $catalogue) { //only one in this case
+
+	    $syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->name = $catalogue->ckan_name;
+	    $syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->orga_filter = $catalogue->ckan_organisation_filter;
+	    $syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->ckan_server_ip = $catalogue->ckan_server_ip;
+
+	    $syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->filter = $catalogue->ckan_filter;
+	    $syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->central_filter = $catalogue->central_ckan_filter;
+	    $syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->ckan_api_url = $catalogue->ckan_api_url;
+	    $syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->ckan_api_version = $catalogue->ckan_api_version;
+
+	    //things from mapbender database
+ 	    $syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->name = $organization["name"];
+$syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->id = $organization["id"];
+            $syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->title = $organization["title"];
+            $syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->email = $organization["email"];
+	    $syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->ckan_orga_ident = $organization["ckan_uuid"];
+
+$e = new mb_exception("classes/class_syncCkan.php: uuid from departmentArray: ".$organization["ckan_uuid"]);
+
+            if (isset($organization["ckan_uuid"]) && isset($organization["ckan_api_key"])) {
+                //Test if organization with the given external uuid exists in the coupled ckan
+                //show organizations of the authorized user (from geoportal group table) via action api
+                $ckan = new ckanApi($organization["ckan_api_key"], CKAN_SERVER_IP);
+                $ckan->base_url = $this->ckanApiProtocol.'://'.$this->ckanApiUrl.'/api/3/';
+
+                $ckan->api_version = $this->ckanApiVersion;
+                $result = $ckan->action_organization_list_for_user();
+                foreach($result->result as $orga) {
+                    if ($orga->id == $organization["ckan_uuid"]) {
+			//foreach catalogue entry to sync
+			$syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->ckan_orga_ident = $organization["ckan_uuid"];        
+                        //echo "Corresponding ckan organization ".$orga->display_name." found for geoportal group ".$organization["name"]." with id ".$organization["id"]."!<br>";
+                        //get list of ids for existing spatial datasets - category spatial should be defined!!!!!!
+                        //http://localhost:5000/api/3/action/package_search?fq=transparency_category_de_rp:spatial_data
+                        //with org: http://localhost:5000/api/3/action/package_search?fq=transparency_category_de_rp:spatial_data%20AND%20owner_org:81476cf5-6c52-4e99-8b9f-6150d63fcb32
+			
+                        //TODO: define standard category filter in ckan.conf!
+                        //$queryObject->fq = STANDARD_CKAN_FILTER." AND owner_org:".$organization["ckan_uuid"];
+			$queryObject->fq = $catalogue->central_ckan_filter." AND owner_org:".$organization["ckan_uuid"];
+			//$queryObject->fq = "transparency_category_de_rp:spatial_data AND owner_org:".$organization["ckan_uuid"];
+                        $queryObject->facet = "true";
+			$queryObject->rows = "1000"; //TODO: maybe an problem somewhen
+                        $listOfFilteredData = $ckan->action_package_search(json_encode($queryObject));
+                        $countCkanMetadataArray = 0;
+                        $ckanMetadataArray = array();
+                        //echo "List of datasets in ckan instance:"."<br>";
+                        $ckanPackageNames = array();
+                        if ($listOfFilteredData->success == true) {
+			    //TODO - why only 10 records are given back when search?
+                            $syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->count_ckan_packages = $listOfFilteredData->result->count;
+                            //echo json_encode($listOfFilteredData)."<br>";
+                            //$e = new mb_exception("Number of results: ".$listOfFilteredData->result->count);
+                            foreach ($listOfFilteredData->result->results as $dataset) {
+                                $ckanMetadataArray[$countCkanMetadataArray]['id'] = $dataset->id;
+                                $ckanMetadataArray[$countCkanMetadataArray]['name'] = $dataset->name;
+                                $ckanMetadataArray[$countCkanMetadataArray]['changedate'] = $dataset->metadata_modified;
+                                if ($listAllMetadataInJson == true) {
+                                    $syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->ckan_packages[$countCkanMetadataArray]->id = $dataset->name;
+                                    $syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->ckan_packages[$countCkanMetadataArray]->date_time = $dataset->metadata_modified;
+                                }
+                                //echo $dataset->title." - ".$dataset->name." - ".$dataset->metadata_modified."<br>";
+                                $ckanPackageNames[] = $dataset->name;
+                                $countCkanMetadataArray++;
+				//$e = new mb_exception("ckan dataset number: ".$countCkanMetadataArray);
+                            }
+                        } else {
+                            $e = new mb_exception("classes/class_syncCkan.php: A problem while searching for datasets in ckan occured!");
+                        }				
+                        // only list http://localhost:5000/api/3/action/package_list?q=owner_org:81476cf5-6c52-4e99-8b9f-6150d63fcb32	
+                        //pull all relevant information from mapbender database - first pull the resources which are owned by the corresponding group!
+                        //only use metadata for which real licenses are defined !!!!!! - what should be done with the other metadata?- DO a left join!!!
+			//get list of distributed metadata sets with their relevant arributes from csw search!!!!!
+
+			//TODO: - exchange with ckan reader!!!!!
+			//$externalCkanMetadataArray = $this->getCswRecordList((integer)$catalogue->catalogue_id, $catalogue->organisation_filter, 'nonGeographicDataset');
+			$externalCkanMetadataArray = $this->getRemoteCkanRecordList($syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->orga_filter,$syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->central_filter,$syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->filter,$syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->ckan_api_url,$syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->ckan_api_version);
+			$countMetadataArray = count($externalCkanMetadataArray);
+                        if ($countMetadataArray == 0) {
+                        } else {
+                            $numberRemoteCkanMetadata = 0;
+                            $cswUuids = array();
+                            foreach($externalCkanMetadataArray as $externalCkanMetadata) {
+                                if ($listAllMetadataInJson == true) {
+                                    $syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->datasource_metadata[$numberRemoteCkanMetadata]->id = $externalCkanMetadata['uuid'];
+                                    $syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->datasource_metadata[$numberRemoteCkanMetadata]->date_time = $externalCkanMetadata['changedate'];
+                                }
+                                $cswUuids[] = $externalCkanMetadata['uuid'];
+                                $numberRemoteCkanMetadata++;
+                            }
+                            $syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->count_external_ckan_packages = $numberRemoteCkanMetadata;
+                            //build diffs for ckan and geoportal
+                            //Arrays: ckanPackageNames / cswUuids
+                            //Those which are only in ckan: delete them
+			    //TODO: Problem is that portalu/ingrid mix lowercase and uppercase uuids - to compare them always use lowercase
+			    $cswUuidsLower = array_map('strtolower', $cswUuids);
+//$e = new mb_exception(json_encode($cswUuidsLower));
+//$e = new mb_exception(json_encode($ckanPackageNames));
+                            $onlyInCkan = array_values(array_diff($ckanPackageNames, $cswUuidsLower)); //every time lowercase names!
+//$e = new mb_exception(json_encode($onlyInCkan));
+                            //Those which are only in csw: create them
+                            $onlyInRemoteCkan = array_values(array_diff($cswUuidsLower, $ckanPackageNames));
+//$e = new mb_exception(json_encode($onlyInRemoteCkan));
+                            //Those which are in both - update them if geoportal metadata is newer than the package in ckan	
+                            $inBoth = array_values(array_intersect($ckanPackageNames, $cswUuidsLower));
+//$e = new mb_exception(json_encode($inBoth));
+			    //rebuild key arrays to mixed case
+			    $onlyInRemoteCkan = $this->searchUpperLowerCase($onlyInRemoteCkan, $cswUuids);
+			    $inBoth = $this->searchUpperLowerCase($inBoth, $cswUuids);
+                            //if the timestamps should be compared before
+                            if ($this->compareTimestamps == true) { 
+                                foreach ($inBoth as $uuid) {
+                                    $e = new mb_notice("classes/class_syncCkan.php: search for uuid: ".$uuid."ckan time : ".$ckanMetadataArray[array_search($uuid, $this->array_column($ckanMetadataArray,'name'))]['changedate']. " - csw time: ".$externalCkanMetadataArray[array_search($uuid, $this->array_column($externalCkanMetadataArray,'uuid'))]['changedate']);
+                                    $dateTimeCkan = new DateTime($ckanMetadataArray[array_search($uuid, $this->array_column($ckanMetadataArray,'name'))]['changedate']);
+                                    $dateTimeRemoteCkan = new DateTime($externalCkanMetadataArray[array_search($uuid, $this->array_column($externalCkanMetadataArray,'uuid'))]['changedate']);
+                                    if ($dateTimeCkan > $dateTimeRemoteCkan) {
+                                        //delete from $inBoth!
+                                        $e = new mb_notice("classes/class_syncCkan.php: Ckans package newer than csw metadata!");
+                                        $inBoth = array_values(array_diff($inBoth, [$uuid]));
+                                    } else {
+                                        $e = new mb_notice("classes/class_syncCkan.php: Ckans package older than csw metadata!");
+                                    }
+                                }
+                            }
+                            //$e = new mb_exception("Number of packages which are only in ckan: ".count($onlyInCkan)." - number of packages which are only in geoportal: ".count($onlyInGeoportal)." - number of packages which are in both catalogues: ".count($inBoth));
+			    //$e = new mb_exception("both: ".gettype($inBoth)." number: ".count($inBoth)." json: ".json_encode($inBoth));
+		            //$e = new mb_exception("only ckan: ".gettype($onlyInCkan)." number: ".count($onlyInCkan)." json: ".json_encode($onlyInCkan));
+                            //$e = new mb_exception("only geoportal: ".gettype($onlyInGeoportal)." number: ".count($onlyInGeoportal)." json: ".json_encode($onlyInGeoportal));
+                            $syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->update = $inBoth;
+                            $syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->delete = $onlyInCkan;
+                            $syncListResultRemoteCkan->external_ckan[$numberOfCatalogue]->create = $onlyInRemoteCkan;
+                        }
+                        //then pull the ressources which are owned by the user but have no group defined - only if the primary group of the user is the corresponding group!
+                        //maybe the catalogue itself is the best way to pull all relevant data, cause the coupling is resolved already!
+                        //filter for datasets:
+                        //open data - defined by the licenses
+                        //transparency - no filter at all - licenses only of some exists - otherwise none or freetext
+                    }
+                }
+            } else {
+                $e = new mb_exception("classes/class_syncCkan.php: Coupled ckan cannot be synchronized, cause the required organization-id and/or API-Key is was not found for ".$organization["name"]);
+            }
+        $numberOfCatalogue++;
+        }
+	//TODO DEBUG
+	/*	foreach($syncListResultRemoteCkan->datasource_metadata[0]->create as $cswFileIdentifier) {
+			$e = new mb_exception($cswFileIdentifier);
+			$resultCkanRepresentation = $this->getCkanRepresentationFromRemoteCkan($syncListResultRemoteCkan->datasource_metadata[0]->id, $cswFileIdentifier, "orga_name", "orga_title", "orga_email", $this->topicDataThemeCategoryMap, "transparency_category_de_rp:environmental_information");	
+		}
+	//
+	*/
+	if (count($syncListResultRemoteCkan->external_ckan) >= 1) {
+	    $syncList->result = $syncListResultRemoteCkan;
+            $syncList->success = true;
+	}
+        return json_encode($syncList);    
+    }
+
+    public function getSyncListCswJson($departmentsArray, $orgaId, $listAllMetadataInJson = false) {
         $syncListCsw = new stdClass(); //should handle the returned json object
 	$syncListResultCsw = new stdClass();
 	$syncListCsw->help = "helptext";
@@ -234,9 +512,7 @@
 	$organization = $departmentsArray[0];
 	$catalogues = json_decode($departmentsArray[0]["csw_catalogues"])->csw_catalogues;
         foreach ($catalogues as $catalogue) { //only one in this case
-       
 	    $syncListResultCsw->external_csw[$numberOfCatalogue]->id = $catalogue->catalogue_id;
-
 	    $syncListResultCsw->external_csw[$numberOfCatalogue]->orga_filter = $catalogue->organisation_filter;
 	    $syncListResultCsw->external_csw[$numberOfCatalogue]->ckan_filter = $catalogue->ckan_filter;
 	    //things from mapbender database
@@ -250,6 +526,7 @@
                 //show organizations of the authorized user (from geoportal group table) via action api
                 $ckan = new ckanApi($organization["ckan_api_key"], CKAN_SERVER_IP);
                 $ckan->base_url = $this->ckanApiProtocol.'://'.$this->ckanApiUrl.'/api/3/';
+
                 $ckan->api_version = $this->ckanApiVersion;
                 $result = $ckan->action_organization_list_for_user();
                 foreach($result->result as $orga) {
@@ -260,6 +537,7 @@
                         //get list of ids for existing spatial datasets - category spatial should be defined!!!!!!
                         //http://localhost:5000/api/3/action/package_search?fq=transparency_category_de_rp:spatial_data
                         //with org: http://localhost:5000/api/3/action/package_search?fq=transparency_category_de_rp:spatial_data%20AND%20owner_org:81476cf5-6c52-4e99-8b9f-6150d63fcb32
+			
                         //TODO: define standard category filter in ckan.conf!
                         //$queryObject->fq = STANDARD_CKAN_FILTER." AND owner_org:".$organization["ckan_uuid"];
 			$queryObject->fq = $catalogue->ckan_filter." AND owner_org:".$organization["ckan_uuid"];
@@ -295,45 +573,25 @@
                         // only list http://localhost:5000/api/3/action/package_list?q=owner_org:81476cf5-6c52-4e99-8b9f-6150d63fcb32	
                         //pull all relevant information from mapbender database - first pull the resources which are owned by the corresponding group!
                         //only use metadata for which real licenses are defined !!!!!! - what should be done with the other metadata?- DO a left join!!!
-                        /*if ($organization['is_primary_group']) {
-                            $sql = "SELECT *, f_get_coupled_resources(metadata_id) from mb_metadata JOIN md_termsofuse ON mb_metadata.metadata_id = md_termsofuse.fkey_metadata_id WHERE fkey_mb_user_id = $1 AND (fkey_mb_group_id is null OR fkey_mb_group_id = 0) AND export2csw IS true";
-                            $v = array($this->mapbenderUserId);
-                            $t = array('i');
-                        } else {
-                            $sql = "SELECT *, f_get_coupled_resources(metadata_id) from mb_metadata JOIN md_termsofuse ON mb_metadata.metadata_id = md_termsofuse.fkey_metadata_id WHERE fkey_mb_group_id = $1 AND export2csw IS true";
-                            $v = array($organization['id']);
-                            $t = array('i');
-                        }*/
 			//get list of distributed metadata sets with their relevant arributes from csw search!!!!!
 			$cswMetadataArray = $this->getCswRecordList((integer)$catalogue->catalogue_id, $catalogue->organisation_filter, 'nonGeographicDataset');
 			$countMetadataArray = count($cswMetadataArray);
                         if ($countMetadataArray == 0) {
-                            //echo "No published metadata found for user with id ".$userId." <br>";
-                            //delete all records from external ckan instance
-                            /*foreach ($ckanMetadataArray as $ckanPackage) {
-                                //echo "Ckan package ".$ckanPackage['name']." should be deleted"."<br>";
-                            }*/
                         } else {
                             $numberCswMetadata = 0;
                             $cswUuids = array();
                             foreach($cswMetadataArray as $cswMetadata) {
-                                //if (count($layerArray) > 0  || count($featuretypeArray) > 0) {
-                                    //use only those that have resources!
-                                    //if ($geoportalMetadata['hasResource']) {
-                                        if ($listAllMetadataInJson == true) {
-                                            $syncListResultCsw->external_csw[$numberOfCatalogue]->csw_metadata[$numberCswMetadata]->id = $cswMetadata['uuid'];
-                                            $syncListResultCsw->external_csw[$numberOfCatalogue]->csw_metadata[$numberCswMetadata]->date_time = $cswMetadata['changedate'];
-                                        }
-                                        $cswUuids[] = $cswMetadata['uuid'];
-                                        $numberCswMetadata++;
-                                  //  }
-                                //}
+                                if ($listAllMetadataInJson == true) {
+                                    $syncListResultCsw->external_csw[$numberOfCatalogue]->datasource_metadata[$numberCswMetadata]->id = $cswMetadata['uuid'];
+                                    $syncListResultCsw->external_csw[$numberOfCatalogue]->datasource_metadata[$numberCswMetadata]->date_time = $cswMetadata['changedate'];
+                                }
+                                $cswUuids[] = $cswMetadata['uuid'];
+                                $numberCswMetadata++;
                             }
                             $syncListResultCsw->external_csw[$numberOfCatalogue]->count_csw_packages = $numberCswMetadata;
                             //build diffs for ckan and geoportal
                             //Arrays: ckanPackageNames / cswUuids
                             //Those which are only in ckan: delete them
-
 			    //TODO: Problem is that portalu/ingrid mix lowercase and uppercase uuids - to compare them always use lowercase
 			    $cswUuidsLower = array_map('strtolower', $cswUuids);
 //$e = new mb_exception(json_encode($cswUuidsLower));
@@ -349,7 +607,6 @@
 			    //rebuild key arrays to mixed case
 			    $onlyInCsw = $this->searchUpperLowerCase($onlyInCsw, $cswUuids);
 			    $inBoth = $this->searchUpperLowerCase($inBoth, $cswUuids);
-
                             //if the timestamps should be compared before
                             if ($this->compareTimestamps == true) { 
                                 foreach ($inBoth as $uuid) {
@@ -382,14 +639,13 @@
                 }
             } else {
                 $e = new mb_exception("classes/class_syncCkan.php: Coupled ckan cannot be synchronized, cause the required organization-id and/or API-Key is was not found for ".$organization["name"]);
-                //echo "<br> - ".$numberOfCatalogue."<br>";
             }
         $numberOfCatalogue++;
         }
 	//TODO DEBUG
-	/*	foreach($syncListResultCsw->external_csw[0]->create as $cswFileIdentifier) {
+	/*	foreach($syncListResultCsw->datasource_metadata[0]->create as $cswFileIdentifier) {
 			$e = new mb_exception($cswFileIdentifier);
-			$resultCkanRepresentation = $this->getCkanRepresentationFromCsw($syncListResultCsw->external_csw[0]->id, $cswFileIdentifier, "orga_name", "orga_title", "orga_email", $this->topicDataThemeCategoryMap, "transparency_category_de_rp:environmental_information");	
+			$resultCkanRepresentation = $this->getCkanRepresentationFromCsw($syncListResultCsw->datasource_metadata[0]->id, $cswFileIdentifier, "orga_name", "orga_title", "orga_email", $this->topicDataThemeCategoryMap, "transparency_category_de_rp:environmental_information");	
 		}
 	//
 	*/
@@ -428,7 +684,7 @@
                         //get list of ids for existing spatial datasets - category spatial should be defined!!!!!!
                         //http://localhost:5000/api/3/action/package_search?fq=transparency_category_de_rp:spatial_data
                         //with org: http://localhost:5000/api/3/action/package_search?fq=transparency_category_de_rp:spatial_data%20AND%20owner_org:81476cf5-6c52-4e99-8b9f-6150d63fcb32
-                        //TODO: define standard category filter in ckan.conf!
+                        //TODO: define standard category filter in ckan.conf for spatial data!
                         $queryObject->fq = STANDARD_CKAN_FILTER." AND owner_org:".$organization["ckan_uuid"];
 			//
 			$queryObject->rows = 1000;
@@ -458,7 +714,7 @@
                         } else {
                             $e = new mb_exception("classes/class_syncCkan.php: A problem while searching for datasets in ckan occured!");
                         }				
-                        // only list http://localhost:5000/api/3/action/package_list?q=owner_org:81476cf5-6c52-4e99-8b9f-6150d63fcb32	
+                        //only list http://localhost:5000/api/3/action/package_list?q=owner_org:81476cf5-6c52-4e99-8b9f-6150d63fcb32	
                         //pull all relevant information from mapbender database - first pull the resources which are owned by the corresponding group!
                         //only use metadata for which real licenses are defined !!!!!! - what should be done with the other metadata?- DO a left join!!!
 			//TODO: test what wents wrong, if fkey_mb_group_id is set in mapbender 
@@ -509,9 +765,9 @@
                                     //use only those that have resources!
                                     if ($geoportalMetadata['hasResource']) {
                                         if ($listAllMetadataInJson == true) {
-                                            $syncListResult->geoportal_organization[$numberGeoportalOrga]->geoportal_metadata[$numberGeoportalMetadata]->id = $geoportalMetadata['uuid'];
-                                            $syncListResult->geoportal_organization[$numberGeoportalOrga]->geoportal_metadata[$numberGeoportalMetadata]->date_time = $geoportalMetadata['changedate'];
-                                            $syncListResult->geoportal_organization[$numberGeoportalOrga]->geoportal_metadata[$numberGeoportalMetadata]->resources = json_decode($geoportalMetadata['resources']);
+                                            $syncListResult->geoportal_organization[$numberGeoportalOrga]->datasource_metadata[$numberGeoportalMetadata]->id = $geoportalMetadata['uuid'];
+                                            $syncListResult->geoportal_organization[$numberGeoportalOrga]->datasource_metadata[$numberGeoportalMetadata]->date_time = $geoportalMetadata['changedate'];
+                                            $syncListResult->geoportal_organization[$numberGeoportalOrga]->datasource_metadata[$numberGeoportalMetadata]->resources = json_decode($geoportalMetadata['resources']);
                                         }
                                         $geoportalUuids[] = $geoportalMetadata['uuid'];
                                         $numberGeoportalMetadata++;
@@ -571,143 +827,53 @@
         return json_encode($syncList);    
     }
 
-    public function syncSingleCsw($syncListJson) {
+    /***
+	syncListJson - Format:
+
+    */
+    public function syncSingleDataSource($syncListJson, $dataSourceType, $checkOrgaIdentity = false) {
+	$dataSourceTypeArrayWithViews = array("portalucsw","mapbender");
         $resultObject = new stdClass();
-        $resultObject->help = "Syncing external csw with ckan for organization with id: ".$this->syncOrgaId;
+        $resultObject->help = "Syncing datasource of type ".$dataSourceType." for organization with id: ".$this->syncOrgaId;
 	$syncList = json_decode($syncListJson);
-        //if ($syncList->id == $this->syncOrgaId) { - not relevant for csw sync, cause multiple csw may be synced for one ckan orga 
-            $numberOfDeletedPackages = 0;
-            $numberOfCreatedPackages = 0;
-            $numberOfUpdatedPackages = 0;
-	    //read ckan api-key from database again, because we wont transfer it via json thru the web ;-)
-	    $sql = "SELECT mb_group_ckan_api_key FROM mb_group WHERE mb_group_id = $1"; 
-            $v = array($this->syncOrgaId);
-	    $t = array('s');
-	    $res = db_prep_query($sql, $v, $t);
-	    if ($res) {
-	        $row = db_fetch_assoc($res);
-		$ckanApiKey = $row['mb_group_ckan_api_key'];
-		$ckan = new ckanApi($ckanApiKey, CKAN_SERVER_IP);
-                $ckan->base_url = $this->ckanApiProtocol.'://'.$this->ckanApiUrl.'/api/3/';
-                $ckan->api_version = $this->ckanApiVersion;
-	    } else {
-	        $e = new mb_exception("classes/class_syncCkan.php: No api-key found for mapbender group: ".$this->syncOrgaId);
-	        return false;
-	    }
-            foreach($syncList->delete as $ckanNameToDelete) {
-		//TODO - PortalU Problem lowercase/uppercase
-		$ckanNameToDelete = strtolower($ckanNameToDelete);
-
-                $result = $ckan->action_package_delete("{\"id\":\"".$ckanNameToDelete."\"}");
-                if ($result->success == true) {
-                    $e = new mb_notice("classes/class_syncCkan.php: Ckan package with name ".$ckanNameToDelete." successfully deleted!");
-                    $numberOfDeletedPackages++;
-                } else {
-                    $e = new mb_exception("classes/class_syncCkan.php: A problem occured while trying to delete ckan package with name ".$ckanNameToDelete);
-                }
-            }
-            foreach ($syncList->csw_metadata as $datasetMetadata) {
-                if (in_array($datasetMetadata->id, $syncList->update) || in_array($datasetMetadata->id, $syncList->create)) {
-                    //build resources array for each metadata
-                    /*$layerArrayMetadata = array();
-                    $featuretypeArrayMetadata = array();
-                    foreach ($datasetMetadata->resources->coupledResources->layerIds as $layerId) {
-                        $layerArrayMetadata[] = $layerId;
-                    }
-                    foreach ($datasetMetadata->resources->coupledResources->featuretypeIds as $featuretypeId) {
-                       $featuretypeArrayMetadata[] = $featuretypeId;
-                    }*/
-                    //check if dataset already exists in ckan - we know this, cause we want to update those
-                    //if (in_array($datasetMetadata['uuid'], $ckanMetadataArray['name'])) {
-                        //echo $datasetMetadata['uuid']." has to be updated!"."<br>";
-                    //} else {
-                        //echo $datasetMetadata['uuid']." has to be initial created!"."<br>";
-                        //check if deleted package with same uuid already exists, because the deleted packages only have the state deleted!
-			//TODO fix upper/lowercase problem
-                        $resourceJson->id = strtolower($datasetMetadata->id); //find by id and name name:http://docs.ckan.org/en/latest/api/
-                        $result = $ckan->action_package_show(json_encode($resourceJson));
-                        if ($result->success == true) {
-                            //update existing package
-                            //$resultCkanRepresentation = $this->getCkanRepresentation($datasetMetadata->id, $layerArrayMetadata, $featuretypeArrayMetadata, $syncList->ckan_orga_ident, $syncList->title, $syncList->email, $this->topicCkanCategoryMap);
-                            //TODO - define parameter
-			    $resultCkanRepresentation = $this->getCkanRepresentationFromCsw($syncList->id, $datasetMetadata->id, $syncList->ckan_orga_ident, $syncList->name, $syncList->email, $this->topicDataThemeCategoryMap, $syncList->ckan_filter);
-                            if ($resultCkanRepresentation != false) {
-                                $result = $ckan->action_package_update($resultCkanRepresentation['json']);
-                                if ($result->success == true) {
-                                    //echo "Creation of package successful!"."<br>";
-                                    //recreate views
-                                    $viewsUpdateProtocol = $this->recreateResourceViews($ckan, $result, $resultCkanRepresentation);
-                                    $numberOfUpdatedPackages++;
-                                    //$e = new mb_exception($viewsUpdateProtocol);
-                                } else {
-                                    //echo "A problem occured while trying to create the ckan package"."<br>";
-                                }
-                            } else {
-                                //echo "A problem occured while trying to create the json object from geoportal metadata!"."<br>";
-                            }
-                        } else {
-                            //create new package
-                            //echo "Package ".$datasetMetadata['uuid']." does not exist and has to be created!"."<br>";
-                            //$resultCkanRepresentation = $this->getCkanRepresentation($datasetMetadata->id, $layerArrayMetadata, $featuretypeArrayMetadata, $syncList->ckan_orga_ident, $syncList->title, $syncList->email, $this->topicCkanCategoryMap);
-			    //TODO
-			    $resultCkanRepresentation = $this->getCkanRepresentationFromCsw($syncList->id, $datasetMetadata->id, $syncList->ckan_orga_ident, $syncList->title, $syncList->email, $this->topicDataThemeCategoryMap, $syncList->ckan_filter);
-                            if ($resultCkanRepresentation != false) {
-                                $result = $ckan->action_package_create($resultCkanRepresentation['json']);
-                                if ($result->success == true) {
-                                    //echo "Creation of package successful!"."<br>";
-                                    //recreate views
-                                    $viewsUpdateProtocol = $this->recreateResourceViews($ckan, $result, $resultCkanRepresentation);
-                                    $numberOfCreatedPackages++;
-                                    //$e = new mb_exception($viewsUpdateProtocol);
-                                } else {
-                                    $e = new mb_exception("classes/class_syncCkan.php: A problem occured while trying to create the ckan package");
-                                }
-                            } else {
-                                $e = new mb_exception("classes/class_syncCkan.php: A problem occured while trying to create the json object from csw metadata!");
-                            }
-                        }
-                    //}
-                }
-            }
-        /*} else {
-            $e = new mb_exception("classes/class_syncCkan.php (syncSingleCsw): Id from json is not identical to id from class! Sync will not be started!");
+        $numberOfDeletedPackages = 0;
+        $numberOfCreatedPackages = 0;
+        $numberOfUpdatedPackages = 0; 
+	if ($checkOrgaIdentity == true && (integer)$syncList->id !== $this->syncOrgaId) {
+	    $e = new mb_exception("classes/class_syncCkan.php (syncSingleDataSource): Id from json is not identical to id from class! Sync will not be started!");
             $resultObject->success = false;
             $resultObject->error->message = "Organization id for the invoked sync process could not be obtained - please check the id!";
-            //$resultObject->help = "Sync of metadata for orga ";
             return json_encode($resultObject);
-        }*/     
-        $resultObject->success = true;
-        $resultObject->result->orga_id = $this->syncOrgaId;
-        $resultObject->result->numberOfDeletedPackages = $numberOfDeletedPackages;
-        $resultObject->result->numberOfUpdatedPackages = $numberOfUpdatedPackages;
-        $resultObject->result->numberOfCreatedPackages = $numberOfCreatedPackages;
-        return json_encode($resultObject);
-    }
-	
-    public function syncSingleOrga($syncListJson) {
-        $resultObject = new stdClass();
-        $resultObject->help = "Syncing mapbender with ckan for organization with id: ".$this->syncOrgaId;
-	$syncList = json_decode($syncListJson);
-        if ($syncList->id == $this->syncOrgaId) {
-            $numberOfDeletedPackages = 0;
-            $numberOfCreatedPackages = 0;
-            $numberOfUpdatedPackages = 0;
+	} else {
 	    //read ckan api-key from database again, because we wont transfer it via json thru the web ;-)
 	    $sql = "SELECT mb_group_ckan_api_key FROM mb_group WHERE mb_group_id = $1"; 
             $v = array($this->syncOrgaId);
 	    $t = array('s');
 	    $res = db_prep_query($sql, $v, $t);
+	    //$e = new mb_exception("classes/class_syncCkan.php (syncSingleDataSource): syncList->update: ".json_encode($syncList->update));
+	    //$e = new mb_exception("classes/class_syncCkan.php (syncSingleDataSource): syncList->delete: ".json_encode($syncList->delete));
+	    //$e = new mb_exception("classes/class_syncCkan.php (syncSingleDataSource): syncList->create: ".json_encode($syncList->create));
+
 	    if ($res) {
 	        $row = db_fetch_assoc($res);
-		$ckanApiKey = $row['mb_group_ckan_api_key'];
-		$ckan = new ckanApi($ckanApiKey, CKAN_SERVER_IP);
+	        $ckanApiKey = $row['mb_group_ckan_api_key'];
+	        $ckan = new ckanApi($ckanApiKey, CKAN_SERVER_IP);
                 $ckan->base_url = $this->ckanApiProtocol.'://'.$this->ckanApiUrl.'/api/3/';
                 $ckan->api_version = $this->ckanApiVersion;
 	    } else {
 	        $e = new mb_exception("classes/class_syncCkan.php: No api-key found for mapbender group: ".$this->syncOrgaId);
 	        return false;
 	    }
+	    //delete orphaned datasets
             foreach($syncList->delete as $ckanNameToDelete) {
+	        switch ($dataSourceType) {
+		    case "portalucsw":
+		        //TODO - PortalU Problem lowercase/uppercase
+		        $ckanNameToDelete = strtolower($ckanNameToDelete);
+		        break;
+		    case "ckan":
+		        break;
+	        }		
                 $result = $ckan->action_package_delete("{\"id\":\"".$ckanNameToDelete."\"}");
                 if ($result->success == true) {
                     $e = new mb_notice("classes/class_syncCkan.php: Ckan package with name ".$ckanNameToDelete." successfully deleted!");
@@ -716,81 +882,112 @@
                     $e = new mb_exception("classes/class_syncCkan.php: A problem occured while trying to delete ckan package with name ".$ckanNameToDelete);
                 }
             }
-            foreach ($syncList->geoportal_metadata as $datasetMetadata) {
+	    //$e = new mb_exception("classes/class_syncCkan.php: complete syncList as json: ".$syncListJson);
+            foreach ($syncList->datasource_metadata as $datasetMetadata) {
+		//$e = new mb_exception("classes/class_syncCkan.php: try to sync dataset with id: ".$datasetMetadata->id);
                 if (in_array($datasetMetadata->id, $syncList->update) || in_array($datasetMetadata->id, $syncList->create)) {
-                    //build resources array for each metadata
-                    $layerArrayMetadata = array();
-                    $featuretypeArrayMetadata = array();
-                    foreach ($datasetMetadata->resources->coupledResources->layerIds as $layerId) {
-                        $layerArrayMetadata[] = $layerId;
-                    }
-                    foreach ($datasetMetadata->resources->coupledResources->featuretypeIds as $featuretypeId) {
-                       $featuretypeArrayMetadata[] = $featuretypeId;
-                    }
-                    //check if dataset already exists in ckan - we know this, cause we want to update those
-                    //if (in_array($datasetMetadata['uuid'], $ckanMetadataArray['name'])) {
-                        //echo $datasetMetadata['uuid']." has to be updated!"."<br>";
-                    //} else {
-                        //echo $datasetMetadata['uuid']." has to be initial created!"."<br>";
-                        //check if deleted package with same uuid already exists, because the deleted packages only have the state deleted!
+		    //do some special preproccessing 
+		    switch ($dataSourceType) {
+		        case "mapbender":
+                            $layerArrayMetadata = array();
+                            $featuretypeArrayMetadata = array();
+                            foreach ($datasetMetadata->resources->coupledResources->layerIds as $layerId) {
+                                $layerArrayMetadata[] = $layerId;
+                            }
+                            foreach ($datasetMetadata->resources->coupledResources->featuretypeIds as $featuretypeId) {
+                               $featuretypeArrayMetadata[] = $featuretypeId;
+                            }
+			    $resourceJson->id = $datasetMetadata->id; //find by id and name name:http://docs.ckan.org/en/latest/api/
+		            break;
+		        case "portalucsw":
+			    //override uuid with lowercase representation of uuid - problem of portalu implementation
+			    $resourceJson->id = strtolower($datasetMetadata->id);
+                            break;
+                        default:
+			    $resourceJson->id = $datasetMetadata->id;
+			    break;
+		    }
+                    $result = $ckan->action_package_show(json_encode($resourceJson));
+                    if ($result->success == true) {
+		        //try to do an update
+			//first try to read from datasource
+		        switch ($dataSourceType) {
+		            case "mapbender":
+			        $resultCkanRepresentation = $this->getCkanRepresentation($datasetMetadata->id, $layerArrayMetadata, $featuretypeArrayMetadata, $syncList->ckan_orga_ident, $syncList->title, $syncList->email, $this->topicDataThemeCategoryMap);
+			        break;
+		            case "portalucsw":
+			        $resultCkanRepresentation = $this->getCkanRepresentationFromCsw($syncList->id, $datasetMetadata->id, $syncList->ckan_orga_ident, $syncList->name, $syncList->email, $this->topicDataThemeCategoryMap, $syncList->ckan_filter);
+		                break;
+			    default:
+                                //TODO: pull ckan json object from remote ckan source and transform it into central object - map attributes!!!!
+				//$e = new mb_exception("classes/class_syncCkan.php: try to pull json object from remote ckan - id: ".$datasetMetadata->id);
+				$resultCkanRepresentation = $this->getCkanRepresentationFromCkan($syncList->ckan_api_url, $syncList->ckan_api_version, $datasetMetadata->id, $syncList->central_filter, $syncList->ckan_orga_ident);
 			
-                        $resourceJson->id = $datasetMetadata->id; //find by id and name name:http://docs.ckan.org/en/latest/api/
-                        $result = $ckan->action_package_show(json_encode($resourceJson));
-                        if ($result->success == true) {
-                            //update existing package
-                            $resultCkanRepresentation = $this->getCkanRepresentation($datasetMetadata->id, $layerArrayMetadata, $featuretypeArrayMetadata, $syncList->ckan_orga_ident, $syncList->title, $syncList->email, $this->topicDataThemeCategoryMap);
-                            if ($resultCkanRepresentation != false) {
-                                $result = $ckan->action_package_update($resultCkanRepresentation['json']);
-                                if ($result->success == true) {
-                                    //echo "Creation of package successful!"."<br>";
-                                    //recreate views
+				//$resultCkanRepresentation = false;
+			        //$resultCkanRepresentation = $this->getCkanRepresentation($datasetMetadata->id, $layerArrayMetadata, $featuretypeArrayMetadata, $syncList->ckan_orga_ident, $syncList->title, $syncList->email, $this->topicDataThemeCategoryMap);
+			        break;
+		        }
+			//if reading was successful
+                        if ($resultCkanRepresentation != false) {
+			    //try to do an update via api
+                            $result = $ckan->action_package_update($resultCkanRepresentation['json']);
+			    //try to update views if they exists in this dataSourceType
+                            if ($result->success == true) {
+                                if (in_array($dataSourceType, $dataSourceTypeArrayWithViews)) {
                                     $viewsUpdateProtocol = $this->recreateResourceViews($ckan, $result, $resultCkanRepresentation);
-                                    $numberOfUpdatedPackages++;
-                                    //$e = new mb_exception($viewsUpdateProtocol);
-                                } else {
-                                    //echo "A problem occured while trying to create the ckan package"."<br>";
-                                }
+				}
+                                $numberOfUpdatedPackages++;
                             } else {
-                                //echo "A problem occured while trying to create the json object from geoportal metadata!"."<br>";
+                                $e = new mb_exception("classes/class_syncCkan.php: An error occured while trying to update ".$resultCkanRepresentation['json']);
                             }
                         } else {
-                            //create new package
-                            //echo "Package ".$datasetMetadata['uuid']." does not exist and has to be created!"."<br>";
-                            $resultCkanRepresentation = $this->getCkanRepresentation($datasetMetadata->id, $layerArrayMetadata, $featuretypeArrayMetadata, $syncList->ckan_orga_ident, $syncList->title, $syncList->email, $this->topicDataThemeCategoryMap);
-                            if ($resultCkanRepresentation != false) {
-                                $result = $ckan->action_package_create($resultCkanRepresentation['json']);
-                                if ($result->success == true) {
-                                    //echo "Creation of package successful!"."<br>";
-                                    //recreate views
-                                    $viewsUpdateProtocol = $this->recreateResourceViews($ckan, $result, $resultCkanRepresentation);
-                                    $numberOfCreatedPackages++;
-                                    //$e = new mb_exception($viewsUpdateProtocol);
-                                } else {
-                                    $e = new mb_exception("classes/class_syncCkan.php: A problem occured while trying to create the ckan package");
+                            $e = new mb_exception("classes/class_syncCkan.php: An error occured while generate json from external source");
+                        }
+                    } else {
+                        //create new package
+			//first read from external source 
+		        switch ($dataSourceType) {
+		            case "mapbender":
+			         $resultCkanRepresentation = $this->getCkanRepresentation($datasetMetadata->id, $layerArrayMetadata, $featuretypeArrayMetadata, $syncList->ckan_orga_ident, $syncList->title, $syncList->email, $this->topicDataThemeCategoryMap);
+			        break;
+		            case "portalucsw":
+			        $resultCkanRepresentation = $this->getCkanRepresentationFromCsw($syncList->id, $datasetMetadata->id, $syncList->ckan_orga_ident, $syncList->title, $syncList->email, $this->topicDataThemeCategoryMap, $syncList->ckan_filter);
+		                break;
+			    default:
+				//TODO: pull ckan json object from remote ckan source and transform it into central object - map attributes!!!!
+				//$e = new mb_exception("classes/class_syncCkan.php: try to pull json object from remote ckan - id: ".$datasetMetadata->id);
+				$resultCkanRepresentation = $this->getCkanRepresentationFromCkan($syncList->ckan_api_url, $syncList->ckan_api_version, $datasetMetadata->id, $syncList->central_filter, $syncList->ckan_orga_ident);
+				//$resultCkanRepresentation = false;
+			        //$resultCkanRepresentation = $this->getCkanRepresentation($datasetMetadata->id, $layerArrayMetadata, $featuretypeArrayMetadata, $syncList->ckan_orga_ident, $syncList->title, $syncList->email, $this->topicDataThemeCategoryMap);
+			        break;
+		        }
+			//if read from source was successful
+                        if ($resultCkanRepresentation != false) {
+                            $result = $ckan->action_package_create($resultCkanRepresentation['json']);
+			    //if creation from external source was successful - try to create
+                            if ($result->success == true) {
+				$numberOfCreatedPackages++;
+				if (in_array($dataSourceType, $dataSourceTypeArrayWithViews)) {
+                                   $viewsUpdateProtocol = $this->recreateResourceViews($ckan, $result, $resultCkanRepresentation);
                                 }
                             } else {
-                                $e = new mb_exception("classes/class_syncCkan.php: A problem occured while trying to create the json object from geoportal metadata!");
+                                $e = new mb_exception("classes/class_syncCkan.php: An error occured while trying to create ".$resultCkanRepresentation['json']);
                             }
+                        } else {
+                            $e = new mb_exception("classes/class_syncCkan.php: A problem occured while trying to create the json object from ".$dataSourceType." metadata!");
                         }
-                    //}
+                    }
                 }
             }
-        } else {
-            $e = new mb_exception("classes/class_syncCkan.php (syncSingleOrga): Id from json is not identical to id from class! Sync will not be started!");
-            $resultObject->success = false;
-            $resultObject->error->message = "Organization id for the invoked sync process could not be obtained - please check the id!";
-            //$resultObject->help = "Sync of metadata for orga ";
+            $resultObject->success = true;
+            $resultObject->result->orga_id = $this->syncOrgaId;
+            $resultObject->result->numberOfDeletedPackages = $numberOfDeletedPackages;
+            $resultObject->result->numberOfUpdatedPackages = $numberOfUpdatedPackages;
+            $resultObject->result->numberOfCreatedPackages = $numberOfCreatedPackages;
             return json_encode($resultObject);
-        }            
-        $resultObject->success = true;
-        $resultObject->result->orga_id = $this->syncOrgaId;
-        $resultObject->result->numberOfDeletedPackages = $numberOfDeletedPackages;
-        $resultObject->result->numberOfUpdatedPackages = $numberOfUpdatedPackages;
-        $resultObject->result->numberOfCreatedPackages = $numberOfCreatedPackages;
-        return json_encode($resultObject);
+        }
     }
 
-    //private function getCkanRepresentationFromCsw($syncList->external_csw[0]->id, uuid) {
     private function getCkanRepresentationFromCsw($cswId, $fileIdentifier, $ckan_orga_ident, $orgaTitle, $orgaEmail, $topicDataThemeCategoryMap, $ckanCategoryFilter) {
         if (defined("MAPBENDER_PATH") && MAPBENDER_PATH != '') { 
 	    $mapbenderUrl = MAPBENDER_PATH;

Modified: trunk/mapbender/http/php/mod_syncCkan_server.php
===================================================================
--- trunk/mapbender/http/php/mod_syncCkan_server.php	2017-10-19 12:26:18 UTC (rev 9805)
+++ trunk/mapbender/http/php/mod_syncCkan_server.php	2017-10-20 11:04:45 UTC (rev 9806)
@@ -11,8 +11,9 @@
 require_once(dirname(__FILE__) . '/../php/mod_getDownloadOptions.php');
 require_once(dirname(__FILE__).'/../../conf/ckan.conf');
 
-//http://localhost/mb_trunk/php/mod_syncCkan_new.php?userId=1&compareTimestamps=true&syncDepartment=24
-//http://localhost/mb_trunk/php/mod_syncCkan_new.php?userId=1&compareTimestamps=false
+//http://localhost/mb_trunk/php/mod_syncCkan_server.php?userId=1&compareTimestamps=true&syncDepartment=25&operation=syncCkan
+
+
 //TODO: Problem - datestamp for ckan_package - wrong????? Bug in 2.5.3??? Will the index not be updated???
 $registratingDepartments = false;
 $userId = false;
@@ -63,8 +64,8 @@
 
 if (isset($_REQUEST["operation"]) && $_REQUEST["operation"] !== "" && $_REQUEST["operation"] !== null) {
         $testMatch = $_REQUEST["operation"];
- 	if (!($testMatch == 'listCatalogues' or $testMatch == 'syncCatalogue' or $testMatch == 'syncCsw')){ 
-	 	$resultObject->error->message = 'Parameter operation is not valid (listCatalogues, syncCatalogue, syncCsw).'; 
+ 	if (!($testMatch == 'listCatalogues' or $testMatch == 'syncCatalogue' or $testMatch == 'syncCsw' or $testMatch == 'syncCkan')){ 
+	 	$resultObject->error->message = 'Parameter operation is not valid (listCatalogues, syncCatalogue, syncCsw, syncCkan).'; 
 		echo json_encode($resultObject);	
 		die();	
  	}
@@ -166,6 +167,43 @@
 	die();
 }
 
+if ($operation == 'syncCkan') {
+	//check if user is allowed to sync for requested organisation
+	//TODO - make code better
+	//overwrite organization from cswId with right orgaId
+	$test = $syncCkanClass->syncOrgaId;
+	$syncCkanClass->syncOrgaId = $orgaId;
+	//$e = new mb_exception($syncCkanClass->syncOrgaId);
+	$departmentsArray = $syncCkanClass->getMapbenderOrganizations();
+	//rewind orgaid to cswid to get right synclist
+	$syncCkanClass->syncOrgaId = $test;
+	//TODO: alter csw based sync to give orgaId as parameter as this is done here
+	$syncListJsonCkan = $syncCkanClass->getSyncListRemoteCkanJson($departmentsArray, $syncCkanClass->syncOrgaId, true);
+	//for synching use right orga id for getting apikey for invoking $syncCkanClass->syncSingleCsw
+
+	if (isset($orgaId) && $orgaId !== null) {
+		$syncCkanClass->syncOrgaId = $orgaId;
+	}
+	$syncList = json_decode($syncListJsonCkan);
+	if ($syncList->success = true) {
+    		foreach ($syncList->result->external_ckan as $orga) {
+        		//TODO try to sync single orga - the class has already set the syncOrgaId if wished!
+			//if ($syncDepartment == $orga->id) {
+            			//overwrite result with result from sync process
+            			//$syncList = json_decode($syncCkanClass->syncSingleCsw(json_encode($orga)));
+				//new function
+//$e = new mb_exception($syncListJsonCkan);
+				$syncList = json_decode($syncCkanClass->syncSingleDataSource(json_encode($orga), "ckan"));
+			//}
+    		}
+	}
+	//create new syncListJson
+	$syncListJson = json_encode($syncList);
+	header('Content-type:application/json;charset=utf-8');
+	echo $syncListJson;
+	die();
+}
+
 if ($operation == 'syncCsw') {
 	//check if user is allowed to sync for requested organisation
 	//TODO - make code better
@@ -176,7 +214,7 @@
 	$departmentsArray = $syncCkanClass->getMapbenderOrganizations();
 	//rewind orgaid to cswid to get right synclist
 	$syncCkanClass->syncOrgaId = $test;
-	$syncListJsonCsw = $syncCkanClass->getSyncListCswJson($departmentsArray, true);
+	$syncListJsonCsw = $syncCkanClass->getSyncListCswJson($departmentsArray, $syncCkanClass->syncOrgaId, true);
 	//for synching use right orga id for getting apikey for invoking $syncCkanClass->syncSingleCsw
 	$syncCkanClass->syncOrgaId = $orgaId;
 	$syncList = json_decode($syncListJsonCsw);
@@ -185,7 +223,10 @@
         		//TODO try to sync single orga - the class has already set the syncOrgaId if wished!
 			//if ($syncDepartment == $orga->id) {
             			//overwrite result with result from sync process
-            			$syncList = json_decode($syncCkanClass->syncSingleCsw(json_encode($orga)));
+            			//$syncList = json_decode($syncCkanClass->syncSingleCsw(json_encode($orga)));
+				//new function
+				
+				$syncList = json_decode($syncCkanClass->syncSingleDataSource(json_encode($orga), "portalucsw"));
 			//}
     		}
 	}
@@ -208,7 +249,8 @@
         //try to sync single orga - the class has already set the syncOrgaId if wished!
 	if ($syncDepartment == $orga->id) {
             //overwrite result with result from sync process
-            $syncList = json_decode($syncCkanClass->syncSingleOrga(json_encode($orga)));
+            //$syncList = json_decode($syncCkanClass->syncSingleOrga(json_encode($orga)));
+	    $syncList = json_decode($syncCkanClass->syncSingleDataSource(json_encode($orga), "mapbender", true));
 	}
     }
 }



More information about the Mapbender_commits mailing list