public function save(Default_Model_Dataset $value)
 {
     global $application;
     $data = array();
     if (!isnull($value->getId())) {
         $data['id'] = $value->getId();
     }
     if (!isnull($value->getName())) {
         $data['name'] = $value->getName();
     }
     if (!isnull($value->getCategory())) {
         $data['category'] = $value->getCategory();
     }
     if (!isnull($value->getDescription())) {
         $data['description'] = $value->getDescription();
     }
     if (!isnull($value->getHomepage())) {
         $data['homepage'] = $value->getHomepage();
     }
     if (!isnull($value->getElixirURL())) {
         $data['elixir_url'] = $value->getElixirURL();
     }
     if (!isnull($value->getDisciplineID())) {
         $data['disciplineid'] = $value->getDisciplineID();
     }
     if (!isnull($value->getAddedByID())) {
         $data['addedby'] = $value->getAddedByID();
     }
     if (!isnull($value->getAddedon())) {
         $data['addedon'] = $value->getAddedon();
     }
     if (!isnull($value->getTags())) {
         $data['tags'] = $value->getTags();
     }
     if (!isnull($value->getGuID())) {
         $data['guid'] = $value->getGuID();
     }
     $q1 = 'id = ?';
     $q2 = $value->id;
     if (null === ($id = $value->id)) {
         unset($data['id']);
         $value->id = $this->getDbTable()->insert($data);
     } else {
         $s = $this->getDbTable()->getAdapter()->quoteInto($q1, $q2);
         $this->getDbTable()->update($data, $s);
     }
 }
Example #2
0
 public function save(Default_Model_Dataset $value)
 {
     $oldTags = $value->tags;
     if (!is_array($value->tags) || count($value->tags) == 0 || trim($value->tags[0]) == "") {
         $value->setTags("NULL");
     } else {
         $value->setTags(php_to_pg_array($value->tags));
     }
     //		parent::save($value);
     global $application;
     $data = array();
     if (!isnull($value->getId())) {
         $data['id'] = $value->getId();
     }
     if (!isnull($value->getName())) {
         $data['name'] = $value->getName();
     }
     if (!isnull($value->getCategory())) {
         $data['category'] = $value->getCategory();
     }
     if (!isnull($value->getDescription())) {
         $data['description'] = $value->getDescription();
     }
     if (!isnull($value->getHomepage())) {
         $data['homepage'] = $value->getHomepage();
     }
     if (!isnull($value->getElixirURL())) {
         $data['elixir_url'] = $value->getElixirURL();
     }
     if (!isnull($value->getDisciplineID())) {
         $data['disciplineid'] = $value->getDisciplineID();
     }
     if (!isnull($value->getAddedByID())) {
         $data['addedby'] = $value->getAddedByID();
     }
     if (!isnull($value->getAddedon())) {
         $data['addedon'] = $value->getAddedon();
     }
     if (!isnull($value->getTags())) {
         $data['tags'] = $value->getTags();
     }
     if (!isnull($value->getGuID())) {
         $data['guid'] = $value->getGuID();
     }
     if (!isnull($value->getParentID())) {
         $data['parentid'] = $value->getParentID();
     }
     if ($value->getParentID() == "0") {
         $data['parentid'] = null;
     }
     if (trim($value->getHomepage()) === "") {
         $data['homepage'] = null;
     }
     if (trim($value->getElixirURL()) === "") {
         $data['elixir_url'] = null;
     }
     if (trim($value->getDescription()) === "") {
         $data['description'] = null;
     }
     if ($value->getTags() === "NULL") {
         $data['tags'] = null;
     }
     $q1 = 'id = ?';
     $q2 = $value->id;
     if (null === ($id = $value->id)) {
         unset($data['id']);
         $value->id = $this->getDbTable()->insert($data);
     } else {
         $s = $this->getDbTable()->getAdapter()->quoteInto($q1, $q2);
         $this->getDbTable()->update($data, $s);
     }
     $value->setTags($oldTags);
 }
Example #3
0
 /**
  * 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;
     if (!is_null($this->_user)) {
         $ds = new Default_Model_Dataset();
         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:dataset');
         if (count($xmli) === 0) {
             $this->_error = RestErrorEnum::RE_INVALID_REPRESENTATION;
             return $ds;
         }
         $xml = $xmli[0];
         if ($this->_parent->getMethod() === RestMethodEnum::RM_PUT && (count($xml->xpath("//dataset:name")) == 0 || strval($xml->attributes()->category) == "")) {
             $this->_error = RestErrorEnum::RE_INVALID_REPRESENTATION;
             $this->_extError = "One ore more required entities are missing or contain no data.";
             return $ds;
         }
         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 datasets 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;
         }
         $newParentID = null;
         if (!is_null($this->el($xml, 'dataset:parent[@xsi:nil="true"]'))) {
             $newParentID = 0;
         } elseif (!is_null($this->el($xml, "dataset:parent"))) {
             $newParentID = $this->el($xml, "dataset:parent")->attributes()->id;
             if ($newParentID == "") {
                 $this->_error = RestErrorEnum::RE_INVALID_REPRESENTATION;
                 return $ds;
             } else {
                 $datasets = new Default_Model_Datasets();
                 $datasets->filter->id->numequals($newParentID);
                 if (count($datasets->items) == 0 || count($datasets->items) > 0 && $datasets->items[0]->parentID !== null) {
                     if (count($datasets->items) == 0) {
                         $this->_error = RestErrorEnum::RE_ITEM_NOT_FOUND;
                         $this->_extError = "Parent dataset not found";
                     } else {
                         $this->_error = RestErrorEnum::RE_BACKEND_ERROR;
                         $this->_extError = "Parent dataset must be a primary dataset";
                     }
                     return $ds;
                 }
             }
         }
         if (!is_null($newParentID)) {
             // a parentid has been supplied in the XML representation; act upon it
             if ($this->_parent->getMethod() === RestMethodEnum::RM_PUT) {
                 $ds->parentID = $newParentID;
             } elseif ($this->_parent->getMethod() === RestMethodEnum::RM_POST) {
                 // only allow setting parent on update if parent was NULL
                 $datasets = new Default_Model_Datasets();
                 $datasets->filter->id->numequals($ds->id);
                 if ($datasets->items[0]->parentID === null) {
                     $ds->parentID = $newParentID;
                 } elseif ($datasets->items[0]->parentID == $newParentID) {
                     $no_operation = "";
                     // do nothing
                 } else {
                     $this->_error = RestErrorEnum::RE_BACKEND_ERROR;
                     $this->_extError = "Cannot modify existing parent dataset";
                     return $ds;
                 }
             }
         }
         if ($newParentID === null) {
             // no parent id has been provided in the XML; look up database if this is an update
             if ($this->_parent->getMethod() == RestMethodEnum::RM_POST) {
                 $datasets = new Default_Model_Datasets();
                 $datasets->filter->id->numequals($ds->id);
                 $effectiveParentID = $datasets->items[0]->parentID;
             } else {
                 $effectiveParentID = null;
             }
         } else {
             $effectiveParentID = $newParentID === 0 ? null : $newParentID;
         }
         if (!is_null($this->el($xml, "dataset:name"))) {
             $ds->name = strval($this->el($xml, "dataset:name"));
         }
         if (!is_null($this->el($xml, "dataset:description"))) {
             $ds->description = strval($this->el($xml, "dataset:description"));
         }
         if (!is_null($this->el($xml, 'dataset:url[@type="homepage"]'))) {
             $ds->homepage = strval($this->el($xml, 'dataset:url[@type="homepage"]'));
         }
         if (!is_null($this->el($xml, 'dataset:url[@type="elixir"]'))) {
             $ds->elixirURL = strval($this->el($xml, 'dataset:url[@type="elixir"]'));
         }
         if (isset($xml->attributes()->tags)) {
             $ds->tags = explode(" ", strval($xml->attributes()->tags));
         }
         if (isset($xml->attributes()->category)) {
             $ds->category = strval($xml->attributes()->category);
         }
         $ds->save();
         // Discipline collection
         // only sync disciplines for primary datasets, assuming disciplines have been provided
         if (count($xml->xpath("//discipline:discipline")) > 0) {
             if ($effectiveParentID === null) {
                 $data = $this->buildCollection($xml, "//discipline:discipline", "disciplineID");
                 if (is_null($data)) {
                     $data = array();
                 }
                 $this->syncDBCollection("datasetid", $ds->id, "disciplineid", "DatasetDisciplines", "DatasetDiscipline", $data);
             } else {
                 $this->_error = RestErrorEnum::RE_BACKEND_ERROR;
                 $this->_extError = "Cannot explicitly modify disciplines of derived dataset; disciplines are inherited by the parent dataset";
                 return $ds;
             }
         }
         // License collection
         $data = $this->buildCollection($xml, "//dataset:license", "license");
         if (is_null($data)) {
             $data = array();
         }
         $this->syncDBCollection("datasetid", $ds->id, "licenseid", "DatasetLicenses", "DatasetLicense", $data, "license");
         // Also save versions if they exist
         if (count($xml->xpath('//dataset:version[@xsi:nil="true"]')) > 0) {
             db()->exec("SELECT delete_dataset_version(id) FROM datasets WHERE datasetid = " . $ds->id);
         } else {
             $xmlver = $xml->xpath("//dataset:version");
             if (count($xmlver) > 0) {
                 foreach ($xmlver as $x) {
                     $data = RestAPIHelper::responseHead("dataset") . $x->asXML() . RestAPIHelper::responseTail();
                     $verres = new RestDatasetVersionList(array_merge($this->_parent->getParams(), array('data' => $data, 'id' => $ds->id)));
                     if ($this->_parent->getMethod() === RestMethodEnum::RM_PUT) {
                         $verres->put();
                     } else {
                         if (isset($x->attributes()->id)) {
                             $verres->post();
                         } else {
                             $verres->put();
                         }
                     }
                     $this->_error = $verres->getError();
                     $this->_extError = $verres->getExtError();
                     if ($this->_error != RestErrorEnum::RE_OK) {
                         break;
                     }
                 }
             }
         }
     }
     return $ds;
 }