Exemple #1
0
 public function onError($errorDesc)
 {
     echo RestAPIHelper::responseHead("unknown", $error = "Internal Server Error", $exterror = $errorDesc) . RestAPIHelper::responseTail();
 }
Exemple #2
0
 public static function wrapResponse($response, $datatype = null, $type = null, $count = null, $pageLength = null, $pageOffset = null, $error = null, $exterror = null)
 {
     $reqTime = null;
     $originalResponse = $response;
     $parent = null;
     if (is_object($response)) {
         $parent = $response->getParent();
         if (is_object($parent)) {
             $reqTime = $parent->getRequestTime();
         }
         if (!is_null($parent)) {
             if (is_null($error)) {
                 $error = $parent->error;
                 if ($parent->error != "" && $parent->extError != "") {
                     $exterror = $parent->extError;
                 }
             }
             if (is_null($datatype)) {
                 $datatype = $parent->dataType;
             }
             if (is_null($type)) {
                 $type = $parent->total === -1 ? "entry" : "list";
             }
             if (is_null($count) && $type === "list") {
                 $count = $parent->total;
             }
             if (is_null($pageLength) && $type === "list") {
                 $pageLength = $parent->pageLength;
             }
             if (is_null($pageOffset) && $type === "list") {
                 $pageOffset = $parent->pageOffset;
             }
             $response = strval($response);
         } else {
             $response = $response->getData();
         }
     }
     if (is_array($response)) {
         if (is_null($type)) {
             $type = "list";
         }
         if (is_null($count)) {
             $count = count($response);
         }
         $response = implode("\n", $response);
     } else {
         if (is_null($type)) {
             $type = "entry";
         }
     }
     $response = array();
     $response[] = RestAPIHelper::responseHead($datatype, $type, $count, $pageLength, $pageOffset, $error, $exterror, $reqTime);
     if (is_object($originalResponse)) {
         $originalResponse = $originalResponse->getData();
     }
     if (is_array($originalResponse)) {
         $response = array_merge($response, $originalResponse);
     } else {
         $response[] = $originalResponse;
     }
     $response[] = RestAPIHelper::responseTail();
     if (!is_array($originalResponse)) {
         $response = implode("\n", $response);
     }
     return new XMLRestResponse($response, $parent);
 }
 /**
  * implementation of abstract parse() operation from RestXMLParser.
  * @xml SimpleXMLElement the root element of the dataset XML representation
  * 
  * @return Default_Model_Dataset
  * @access public
  */
 public function parse($xml)
 {
     global $application;
     $ds = null;
     if (!is_null($this->_user)) {
         $ds = new Default_Model_DatasetVersion();
         try {
             $xml = new SimpleXMLElement($xml);
         } catch (Exception $e) {
             $this->_error = RestErrorEnum::RE_INVALID_REPRESENTATION;
             return $ds;
         }
         $this->_xml = $xml;
         // basic properties
         $xmli = $xml->xpath('//dataset:version');
         if (count($xmli) === 0) {
             $this->_error = RestErrorEnum::RE_INVALID_REPRESENTATION;
             return $ds;
         }
         $xml = $xmli[0];
         if ($this->_parent->getMethod() === RestMethodEnum::RM_PUT && (strval($xml->attributes()->version) == "" || strval($xml->attributes()->datasetid) == "" && $this->_parent->getParam("id") == "")) {
             $this->_error = RestErrorEnum::RE_INVALID_REPRESENTATION;
             $this->_extError = "One ore more required entities or attributes are missing or contain no data.";
             return $ds;
         }
         if ($this->_parent->getMethod() === RestMethodEnum::RM_PUT) {
             // do not modify version string, parent dataset for existing entry; set only for PUT (insert)
             $ds->version = strval($xml->attributes()->version);
             if ($this->_parent->getParam("id") == "") {
                 $ds->datasetID = intval(strval($xml->attributes()->datasetid));
             } else {
                 $ds->datasetID = intval($this->_parent->getParam("id"));
             }
         }
         if ($this->_parent->getMethod() === RestMethodEnum::RM_POST) {
             if ($xml->attributes()->id) {
                 $ds->id = intval(strval($xml->attributes()->id));
                 $db = $application->getBootstrap()->getResource('db');
                 $db->setFetchMode(Zend_Db::FETCH_OBJ);
                 $r = $db->query('SELECT guid FROM dataset_versions WHERE id = ' . $ds->id)->fetchAll();
                 if (count($r) > 0) {
                     $ds->guid = $r[0]->guid;
                 }
             } else {
                 $this->_error = RestErrorEnum::RE_INVALID_REPRESENTATION;
                 return $ds;
             }
         }
         if ($this->_parent->getMethod() === RestMethodEnum::RM_PUT) {
             $ds->addedByID = $this->_parent->getUser()->id;
         }
         if (!is_null($this->el($xml, "dataset:size"))) {
             $ds->size = strval($this->el($xml, "dataset:size"));
             if ($ds->size == "") {
                 $ds->size = null;
             }
         }
         if (!is_null($this->el($xml, "dataset:notes"))) {
             $ds->notes = strval($this->el($xml, "dataset:notes"));
         }
         if (!is_null($this->el($xml, 'dataset:parent_version[@xsi:nil="true"]'))) {
             $ds->parentID = 0;
         } elseif (!is_null($this->el($xml, "dataset:parent_version"))) {
             $ds->parentID = $this->el($xml, "dataset:parent_version")->attributes()->id;
             if ($ds->parentID == "") {
                 $this->_error = RestErrorEnum::RE_INVALID_REPRESENTATION;
                 return $ds;
             } else {
                 $datasets = new Default_Model_DatasetVersions();
                 $datasets->filter->id->numequals($ds->parentID);
                 if (count($datasets->items) == 0) {
                     $this->_error = RestErrorEnum::RE_ITEM_NOT_FOUND;
                     $this->_extError = "Parent dataset version not found";
                     return $ds;
                 }
             }
         }
         $ds->save();
         // Also save locations if they exist
         if (count($xml->xpath('//dataset:location[@xsi:nil="true"]')) > 0) {
             if ($ds->id != "") {
                 $locations = new Default_Model_DatasetLocations();
                 $locations->filter->dataset_version_id->numequals($ds->id);
                 if (count($locations->items) > 0) {
                     foreach ($locations->items as $loc) {
                         $locres = new RestDatasetLocationItem(array_merge($this->_parent->getParams(), array('data' => $data, 'vid' => $ds->id, 'lid' => $loc->id)));
                         $locres->delete();
                     }
                 }
             }
         } else {
             $xmlloc = $xml->xpath("//dataset:location");
             if (count($xmlloc) > 0) {
                 if ($ds->id != "") {
                     // keep track of existing locations
                     $db = $application->getBootstrap()->getResource('db');
                     $db->setFetchMode(Zend_Db::FETCH_OBJ);
                     $existing = $db->query("SELECT id FROM dataset_locations WHERE dataset_version_id = " . $ds->id)->fetchAll();
                     $eids = array();
                     foreach ($existing as $e) {
                         $eids[] = $e->id;
                     }
                 }
                 foreach ($xmlloc as $x) {
                     $data = RestAPIHelper::responseHead("dataset") . $x->asXML() . RestAPIHelper::responseTail();
                     $locres = new RestDatasetLocationList(array_merge($this->_parent->getParams(), array('data' => $data, 'vid' => $ds->id)));
                     if (isset($x->attributes()->id)) {
                         for ($eiter = count($eids) - 1; $eiter >= 0; $eiter = $eiter - 1) {
                             // remove submitted locations from array of existing locations
                             if (isset($eids[$eiter])) {
                                 if ($eids[$eiter] == $x->attributes()->id) {
                                     // DO NOT USE "unset"; does not work properly
                                     $eids[$eiter] = -1;
                                 }
                             }
                         }
                     }
                     if ($this->_parent->getMethod() === RestMethodEnum::RM_PUT) {
                         $locres->put();
                     } else {
                         if (isset($x->attributes()->id)) {
                             $locres->post();
                         } else {
                             $locres->put();
                         }
                     }
                     $this->_error = $locres->getError();
                     $this->_extError = $locres->getExtError();
                     if ($this->_error != RestErrorEnum::RE_OK) {
                         break;
                     }
                 }
                 if ($ds->id != "") {
                     // remove remaining existing locations, they were not submitted
                     foreach ($eids as $e) {
                         if ($e != "" && $e != -1) {
                             $locres = new RestDatasetLocationItem(array_merge($this->_parent->getParams(), array('data' => $data, 'vid' => $ds->id, 'lid' => $e)));
                             $locres->delete();
                         }
                     }
                 }
             }
         }
     }
     return $ds;
 }