Open Journal Systems  3.0.0
 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  return $returner;
58  }
59 
65  function &getByDataObjectId($dataObjectId) {
66  $result = $this->retrieve(
67  'SELECT * FROM data_object_tombstones WHERE data_object_id = ?', (int) $dataObjectId
68  );
69 
70  $dataObjectTombstone = $this->_fromRow($result->GetRowAssoc(false));
71 
72  $result->Close();
73  return $dataObjectTombstone;
74  }
75 
81  function _fromRow($row) {
82  $dataObjectTombstone = $this->newDataObject();
83  $dataObjectTombstone->setId($row['tombstone_id']);
84  $dataObjectTombstone->setDataObjectId($row['data_object_id']);
85  $dataObjectTombstone->setDateDeleted($this->datetimeFromDB($row['date_deleted']));
86  $dataObjectTombstone->setSetSpec($row['set_spec']);
87  $dataObjectTombstone->setSetName($row['set_name']);
88  $dataObjectTombstone->setOAIIdentifier($row['oai_identifier']);
89 
90  $OAISetObjectsIds = $this->getOAISetObjectsIds($dataObjectTombstone->getId());
91  $dataObjectTombstone->setOAISetObjectsIds($OAISetObjectsIds);
92 
93  return $dataObjectTombstone;
94  }
95 
103  function deleteById($tombstoneId, $assocType = null, $assocId = null) {
104  $tombstone = $this->getById($tombstoneId, $assocType, $assocId);
105  if (!$tombstone) return false; // Did not exist
106 
107  assert(is_a($tombstone, 'DataObjectTombstone'));
108 
109  $this->update(
110  'DELETE FROM data_object_tombstones WHERE tombstone_id = ?',
111  (int) $tombstoneId
112  );
113  if ($this->getAffectedRows()) {
114  $dataObjectTombstoneSettingsDao = DAORegistry::getDAO('DataObjectTombstoneSettingsDAO');
115  $settingsDeleted = $dataObjectTombstoneSettingsDao->deleteSettings($tombstoneId);
116  $setObjectsDeleted = $this->deleteOAISetObjects($tombstoneId);
117  if ($settingsDeleted && $setObjectsDeleted) {
118  return true;
119  }
120  }
121  return false;
122  }
123 
129  function deleteByDataObjectId($dataObjectId) {
130  $dataObjectTombstone =& $this->getByDataObjectId($dataObjectId);
131  return $this->deleteById($dataObjectTombstone->getId());
132  }
133 
139  function insertObject(&$dataObjectTombstone) {
140  $this->update(
141  sprintf('INSERT INTO data_object_tombstones
142  (data_object_id, date_deleted, set_spec, set_name, oai_identifier)
143  VALUES
144  (?, %s, ?, ?, ?)',
145  $this->datetimeToDB(date('Y-m-d H:i:s'))
146  ),
147  array(
148  (int) $dataObjectTombstone->getDataObjectId(),
149  $dataObjectTombstone->getSetSpec(),
150  $dataObjectTombstone->getSetName(),
151  $dataObjectTombstone->getOAIIdentifier()
152  )
153  );
154 
155  $dataObjectTombstone->setId($this->getInsertId());
156  $this->insertOAISetObjects($dataObjectTombstone);
157 
158  return $dataObjectTombstone->getId();
159  }
160 
166  function updateObject(&$dataObjectTombstone) {
167  $returner = $this->update(
168  sprintf('UPDATE data_object_tombstones SET
169  data_object_id = ?,
170  date_deleted = %s,
171  set_spec = ?,
172  set_name = ?,
173  oai_identifier = ?
174  WHERE tombstone_id = ?',
175  $this->datetimeToDB(date('Y-m-d H:i:s'))
176  ),
177  array(
178  (int) $publicationFormatTombstone->getDataObjectId(),
179  $publicationFormatTombstone->getSetSpec(),
180  $publicationFormatTombstone->getSetName(),
181  $publicationFormatTombstone->getOAIIdentifier(),
182  (int) $publicationFormatTombstone->getId()
183  )
184  );
185 
186  $this->updateOAISetObjects($dataObjectTombstone);
187 
188  return $returner;
189  }
190 
195  function getInsertId() {
196  return $this->_getInsertId('data_object_tombstones', 'tombstone_id');
197  }
198 
206  function &getSets($assocType, $assocId) {
207  $result = $this->retrieve(
208  'SELECT DISTINCT dot.set_spec, dot.set_name FROM data_object_tombstones dot
209  LEFT JOIN data_object_tombstone_oai_set_objects oso ON (dot.tombstone_id = oso.tombstone_id)
210  WHERE oso.assoc_type = ? AND oso.assoc_id = ?',
211  array((int) $assocType, (int) $assocId)
212  );
213 
214  $returner = array();
215  while (!$result->EOF) {
216  $returner[$result->fields[0]] = $result->fields[1];
217  $result->MoveNext();
218  }
219 
220  $result->Close();
221  return $returner;
222  }
223 
230  function getOAISetObjectsIds($tombstoneId) {
231  $result = $this->retrieve(
232  'SELECT * FROM data_object_tombstone_oai_set_objects WHERE tombstone_id = ?',
233  (int) $tombstoneId
234  );
235 
236  $oaiSetObjectsIds = array();
237  while ($row = $result->FetchRow()) {
238  $oaiSetObjectsIds[$row['assoc_type']] = $row['assoc_id'];
239  }
240 
241  $result->Close();
242  return $oaiSetObjectsIds;
243  }
244 
249  function deleteOAISetObjects($tombstoneId) {
250  $this->update(
251  'DELETE FROM data_object_tombstone_oai_set_objects WHERE tombstone_id = ?',
252  (int) $tombstoneId
253  );
254 
255  return $this->getAffectedRows();
256  }
257 
262  function insertOAISetObjects($dataObjectTombstone) {
263  foreach ($dataObjectTombstone->getOAISetObjectsIds() as $assocType => $assocId) {
264  $this->update('INSERT INTO data_object_tombstone_oai_set_objects
265  (tombstone_id, assoc_type, assoc_id)
266  VALUES
267  (?, ?, ?)',
268  array(
269  (int) $dataObjectTombstone->getId(),
270  (int) $assocType,
271  (int) $assocId
272  )
273  );
274  }
275  }
276 
282  function updateOAISetObjects($dataObjectTombstone) {
283  foreach ($dataObjectTombstone->getOAISetObjectsIds() as $assocType => $assocId) {
284  $this->update('UPDATE data_object_tombstone_oai_set_objects SET
285  assoc_type = ?,
286  assoc_id = ?
287  WHERE tombstone_id = ?',
288  array(
289  (int) $assocType,
290  (int) $assocId,
291  (int) $dataObjectTombstone->getId()
292  )
293  );
294  }
295  }
296 
297 
298  //
299  // Private helper methods.
300  //
301 
307  function _getSelectTombstoneSql($assocType, $assocId) {
308  return 'FROM data_object_tombstones dot
309  LEFT JOIN data_object_tombstone_oai_set_objects oso ON (dot.tombstone_id = oso.tombstone_id)
310  WHERE dot.tombstone_id = ?' .
311  (isset($assocId) && isset($assocType) ? 'AND oso.assoc_type = ? AND oso.assoc_id = ?' : '');
312  }
313 }
314 
315 ?>
static & getDAO($name, $dbconn=null)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
_getSelectTombstoneSql($assocType, $assocId)
insertOAISetObjects($dataObjectTombstone)
updateObject(&$dataObjectTombstone)
deleteById($tombstoneId, $assocType=null, $assocId=null)
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
getById($tombstoneId, $assocType=null, $assocId=null)
datetimeFromDB($dt)
Definition: DAO.inc.php:313
getAffectedRows()
Definition: DAO.inc.php:258
updateOAISetObjects($dataObjectTombstone)
insertObject(&$dataObjectTombstone)
Base class for data object tombstones.
Base class for retrieving and modifying DataObjectTombstone objects.
datetimeToDB($dt)
Definition: DAO.inc.php:293
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
& getSets($assocType, $assocId)