Open Journal Systems  2.4.3
 All Classes Namespaces Functions Variables Groups Pages
DataObjectTombstoneDAO.inc.php
1 <?php
2 
17 import('lib.pkp.classes.tombstone.DataObjectTombstone');
18 
19 class DataObjectTombstoneDAO extends DAO {
24  parent::DAO();
25  }
26 
31  function newDataObject() {
32  return new DataObjectTombstone();
33  }
34 
42  function &getById($tombstoneId, $assocType = null, $assocId = null) {
43  $params = array((int) $tombstoneId);
44  if ($assocId !== null && $assocType !== null) {
45  $params[] = (int) $assocType;
46  $params[] = (int) $assocId;
47  }
48  $result =& $this->retrieve(
49  'SELECT DISTINCT * ' . $this->_getSelectTombstoneSql($assocType, $assocId), $params);
50 
51  $returner = null;
52  if ($result->RecordCount() != 0) {
53  $returner =& $this->_fromRow($result->GetRowAssoc(false));
54  }
55 
56  $result->Close();
57  unset($result);
58 
59  return $returner;
60  }
61 
67  function &getByDataObjectId($dataObjectId) {
68  $result =& $this->retrieve(
69  'SELECT * FROM data_object_tombstones WHERE data_object_id = ?', (int) $dataObjectId
70  );
71 
72  $dataObjectTombstone =& $this->_fromRow($result->GetRowAssoc(false));
73 
74  $result->Close();
75  unset($result);
76 
77  return $dataObjectTombstone;
78  }
79 
85  function &_fromRow($row) {
86  $dataObjectTombstone = $this->newDataObject();
87  $dataObjectTombstone->setId($row['tombstone_id']);
88  $dataObjectTombstone->setDataObjectId($row['data_object_id']);
89  $dataObjectTombstone->setDateDeleted($this->datetimeFromDB($row['date_deleted']));
90  $dataObjectTombstone->setSetSpec($row['set_spec']);
91  $dataObjectTombstone->setSetName($row['set_name']);
92  $dataObjectTombstone->setOAIIdentifier($row['oai_identifier']);
93 
94  $OAISetObjectsIds = $this->getOAISetObjectsIds($dataObjectTombstone->getId());
95  $dataObjectTombstone->setOAISetObjectsIds($OAISetObjectsIds);
96 
97  return $dataObjectTombstone;
98  }
99 
107  function deleteById($tombstoneId, $assocType = null, $assocId = null) {
108  $tombstone = $this->getById($tombstoneId, $assocType, $assocId);
109  if (!$tombstone) return false; // Did not exist
110 
111  assert(is_a($tombstone, 'DataObjectTombstone'));
112 
113  $this->update(
114  'DELETE FROM data_object_tombstones WHERE tombstone_id = ?',
115  (int) $tombstoneId
116  );
117  if ($this->getAffectedRows()) {
118  $dataObjectTombstoneSettingsDao =& DAORegistry::getDAO('DataObjectTombstoneSettingsDAO');
119  $settingsDeleted = $dataObjectTombstoneSettingsDao->deleteSettings($tombstoneId);
120  $setObjectsDeleted = $this->deleteOAISetObjects($tombstoneId);
121  if ($settingsDeleted && $setObjectsDeleted) {
122  return true;
123  }
124  }
125  return false;
126  }
127 
133  function deleteByDataObjectId($dataObjectId) {
134  $dataObjectTombstone =& $this->getByDataObjectId($dataObjectId);
135  return $this->deleteById($dataObjectTombstone->getId());
136  }
137 
143  function insertObject(&$dataObjectTombstone) {
144  $this->update(
145  sprintf('INSERT INTO data_object_tombstones
146  (data_object_id, date_deleted, set_spec, set_name, oai_identifier)
147  VALUES
148  (?, %s, ?, ?, ?)',
149  $this->datetimeToDB(date('Y-m-d H:i:s'))
150  ),
151  array(
152  (int) $dataObjectTombstone->getDataObjectId(),
153  $dataObjectTombstone->getSetSpec(),
154  $dataObjectTombstone->getSetName(),
155  $dataObjectTombstone->getOAIIdentifier()
156  )
157  );
158 
159  $dataObjectTombstone->setId($this->getInsertTombstoneId());
160  $this->insertOAISetObjects($dataObjectTombstone);
161 
162  return $dataObjectTombstone->getId();
163  }
164 
170  function updateObject(&$dataObjectTombstone) {
171  $returner = $this->update(
172  sprintf('UPDATE data_object_tombstones SET
173  data_object_id = ?,
174  date_deleted = %s,
175  set_spec = ?,
176  set_name = ?,
177  oai_identifier = ?
178  WHERE tombstone_id = ?',
179  $this->datetimeToDB(date('Y-m-d H:i:s'))
180  ),
181  array(
182  (int) $publicationFormatTombstone->getDataObjectId(),
183  $publicationFormatTombstone->getSetSpec(),
184  $publicationFormatTombstone->getSetName(),
185  $publicationFormatTombstone->getOAIIdentifier(),
186  (int) $publicationFormatTombstone->getId()
187  )
188  );
189 
190  $this->updateOAISetObjects($dataObjectTombstone);
191 
192  return $returner;
193  }
194 
199  function getInsertTombstoneId() {
200  return $this->getInsertId('data_object_tombstones', 'tombstone_id');
201  }
202 
210  function &getSets($assocType, $assocId) {
211  $result =& $this->retrieve(
212  'SELECT DISTINCT dot.set_spec, dot.set_name FROM data_object_tombstones dot
213  LEFT JOIN data_object_tombstone_oai_set_objects oso ON (dot.tombstone_id = oso.tombstone_id)
214  WHERE oso.assoc_type = ? AND oso.assoc_id = ?',
215  array((int) $assocType, (int) $assocId)
216  );
217 
218  $returner = array();
219  while (!$result->EOF) {
220  $returner[$result->fields[0]] = $result->fields[1];
221  $result->moveNext();
222  }
223 
224  $result->Close();
225  unset($result);
226 
227  return $returner;
228  }
229 
236  function getOAISetObjectsIds($tombstoneId) {
237  $result =& $this->retrieve(
238  'SELECT * FROM data_object_tombstone_oai_set_objects WHERE tombstone_id = ?',
239  (int) $tombstoneId
240  );
241 
242  $oaiSetObjectsIds = array();
243  while ($row = $result->FetchRow()) {
244  $oaiSetObjectsIds[$row['assoc_type']] = $row['assoc_id'];
245  }
246 
247  $result->Close();
248  unset($result);
249 
250  return $oaiSetObjectsIds;
251  }
252 
257  function deleteOAISetObjects($tombstoneId) {
258  $this->update(
259  'DELETE FROM data_object_tombstone_oai_set_objects WHERE tombstone_id = ?',
260  (int) $tombstoneId
261  );
262 
263  return $this->getAffectedRows();
264  }
265 
270  function insertOAISetObjects($dataObjectTombstone) {
271  foreach ($dataObjectTombstone->getOAISetObjectsIds() as $assocType => $assocId) {
272  $this->update('INSERT INTO data_object_tombstone_oai_set_objects
273  (tombstone_id, assoc_type, assoc_id)
274  VALUES
275  (?, ?, ?)',
276  array(
277  (int) $dataObjectTombstone->getId(),
278  (int) $assocType,
279  (int) $assocId
280  )
281  );
282  }
283  }
284 
290  function updateOAISetObjects($dataObjectTombstone) {
291  foreach ($dataObjectTombstone->getOAISetObjectsIds() as $assocType => $assocId) {
292  $returner = $this->update('UPDATE data_object_tombstone_oai_set_objects SET
293  assoc_type = ?,
294  assoc_id = ?
295  WHERE tombstone_id = ?',
296  array(
297  (int) $assocType,
298  (int) $assocId,
299  (int) $publicationFormatTombstone->getId()
300  )
301  );
302  }
303  }
304 
305 
306  //
307  // Private helper methods.
308  //
309 
315  function _getSelectTombstoneSql($assocType, $assocId) {
316  return 'FROM data_object_tombstones dot
317  LEFT JOIN data_object_tombstone_oai_set_objects oso ON (dot.tombstone_id = oso.tombstone_id)
318  WHERE dot.tombstone_id = ?' .
319  (isset($assocId) && isset($assocType) ? 'AND oso.assoc_type = ? AND oso.assoc_id = ?' : '');
320  }
321 }
322 
323 ?>
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:29
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:83
_getSelectTombstoneSql($assocType, $assocId)
getInsertId($table= '', $id= '', $callHooks=true)
Definition: DAO.inc.php:252
insertOAISetObjects($dataObjectTombstone)
updateObject(&$dataObjectTombstone)
deleteById($tombstoneId, $assocType=null, $assocId=null)
datetimeFromDB($dt)
Definition: DAO.inc.php:316
getAffectedRows()
Definition: DAO.inc.php:261
updateOAISetObjects($dataObjectTombstone)
insertObject(&$dataObjectTombstone)
& getDAO($name, $dbconn=null)
Base class for data object tombstones.
Base class for retrieving and modifying DataObjectTombstone objects.
datetimeToDB($dt)
Definition: DAO.inc.php:296
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:211
& getById($tombstoneId, $assocType=null, $assocId=null)
& getSets($assocType, $assocId)