public function save(Default_Model_DatasetVersion $value) { global $application; $data = array(); if (!isnull($value->getId())) { $data['id'] = $value->getId(); } if (!isnull($value->getDatasetID())) { $data['datasetid'] = $value->getDatasetID(); } if (!isnull($value->getVersion())) { $data['version'] = $value->getVersion(); } if (!isnull($value->getNotes())) { $data['notes'] = $value->getNotes(); } if (!isnull($value->getSize())) { $data['size'] = $value->getSize(); } if (!isnull($value->getAddedByID())) { $data['addedby'] = $value->getAddedByID(); } if (!isnull($value->getParentID())) { $data['parentid'] = $value->getParentID(); } if (!isnull($value->getAddedOn())) { $data['addedon'] = $value->getAddedOn(); } if (!isnull($value->getGuID())) { $data['guid'] = $value->getGuID(); } if ($value->getParentID() == "0") { $data['parentid'] = 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); } }
/** * 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; }