PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/pkp/classes/metadata/nlm/NlmCitationSchemaCitationAdapter.inc.php

https://github.com/lib-uoguelph-ca/ocs
PHP | 142 lines | 75 code | 17 blank | 50 comment | 7 complexity | f83ab61af45d21ed85ee97909a72a63b MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @file classes/metadata/NlmCitationSchemaCitationAdapter.inc.php
  4. *
  5. * Copyright (c) 2000-2012 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class NlmCitationSchemaCitationAdapter
  9. * @ingroup metadata_nlm
  10. * @see Citation
  11. * @see NlmCitationSchema
  12. *
  13. * @brief Class that injects/extracts NLM citation schema compliant
  14. * meta-data into/from a Citation object.
  15. */
  16. // $Id$
  17. import('metadata.MetadataDataObjectAdapter');
  18. import('metadata.nlm.NlmCitationSchema');
  19. import('metadata.nlm.NlmNameSchema');
  20. class NlmCitationSchemaCitationAdapter extends MetadataDataObjectAdapter {
  21. /**
  22. * Constructor
  23. */
  24. function NlmCitationSchemaCitationAdapter() {
  25. // Configure the adapter
  26. $metadataSchema = new NlmCitationSchema();
  27. parent::MetadataDataObjectAdapter($metadataSchema, 'Citation', ASSOC_TYPE_CITATION);
  28. }
  29. //
  30. // Implement template methods from MetadataDataObjectAdapter
  31. //
  32. /**
  33. * @see MetadataDataObjectAdapter::injectMetadataIntoDataObject()
  34. * @param $metadataDescription MetadataDescription
  35. * @param $dataObject Citation
  36. * @param $replace boolean whether existing meta-data should be replaced
  37. * @return DataObject
  38. */
  39. function &injectMetadataIntoDataObject(&$metadataDescription, &$dataObject, $replace) {
  40. // Did we get an existing citation object or should we create a new one?
  41. if (is_null($dataObject)) {
  42. import('citation.Citation');
  43. $dataObject = new Citation();
  44. }
  45. // Add new meta-data statements to the citation. Add the schema
  46. // name space to each property name so that it becomes unique
  47. // across schemas.
  48. $metadataSchemaNamespace = $this->getMetadataNamespace();
  49. $nullVar = null;
  50. foreach($metadataDescription->getPropertyNames() as $propertyName) {
  51. $dataObjectKey = $metadataSchemaNamespace.':'.$propertyName;
  52. if ($metadataDescription->hasStatement($propertyName)) {
  53. // Directly retrieve the internal data so that we don't
  54. // have to care about cardinality and translation.
  55. $value =& $metadataDescription->getData($propertyName);
  56. if (in_array($propertyName, array('person-group[@person-group-type="author"]', 'person-group[@person-group-type="editor"]'))) {
  57. assert(is_array($value));
  58. // Dereference the value to make sure that we don't destroy
  59. // the original MetadataDescription.
  60. $tmpValue = $value;
  61. unset($value);
  62. $value =& $tmpValue;
  63. // Convert MetadataDescription objects to simple key/value arrays.
  64. foreach($value as $key => $nameComposite) {
  65. assert(is_a($nameComposite, 'MetadataDescription'));
  66. $value[$key] =& $nameComposite->getAllData();
  67. }
  68. }
  69. $dataObject->setData($dataObjectKey, $value);
  70. unset($value);
  71. } elseif ($replace && $dataObject->hasData($dataObjectKey)) {
  72. // Delete existing property data
  73. $dataObject->setData($dataObjectKey, $nullVar);
  74. }
  75. }
  76. return $dataObject;
  77. }
  78. /**
  79. * @see MetadataDataObjectAdapter::extractMetadataFromDataObject()
  80. * @param $dataObject Citation
  81. * @return MetadataDescription
  82. */
  83. function &extractMetadataFromDataObject(&$dataObject) {
  84. $metadataDescription =& $this->instantiateMetadataDescription();
  85. // Identify the length of the name space prefix
  86. $namespacePrefixLength = strlen($this->getMetadataNamespace())+1;
  87. // Get all meta-data field names
  88. $fieldNames = array_merge($this->getDataObjectMetadataFieldNames(false),
  89. $this->getDataObjectMetadataFieldNames(true));
  90. // Retrieve the statements from the data object
  91. $statements = array();
  92. $nameSchema = new NlmNameSchema();
  93. foreach($fieldNames as $fieldName) {
  94. if ($dataObject->hasData($fieldName)) {
  95. // Remove the name space prefix
  96. $propertyName = substr($fieldName, $namespacePrefixLength);
  97. if (in_array($propertyName, array('person-group[@person-group-type="author"]', 'person-group[@person-group-type="editor"]'))) {
  98. // Convert key/value arrays to MetadataDescription objects.
  99. $names =& $dataObject->getData($fieldName);
  100. foreach($names as $key => $name) {
  101. switch($propertyName) {
  102. case 'person-group[@person-group-type="author"]':
  103. $assocType = ASSOC_TYPE_AUTHOR;
  104. break;
  105. case 'person-group[@person-group-type="editor"]':
  106. $assocType = ASSOC_TYPE_EDITOR;
  107. break;
  108. }
  109. $nameDescription = new MetadataDescription($nameSchema, $assocType);
  110. $nameDescription->setStatements($name);
  111. $names[$key] =& $nameDescription;
  112. unset($nameDescription);
  113. }
  114. $statements[$propertyName] =& $names;
  115. } else {
  116. $statements[$propertyName] =& $dataObject->getData($fieldName);
  117. }
  118. }
  119. }
  120. // Set the statements in the meta-data description
  121. $metadataDescription->setStatements($statements);
  122. return $metadataDescription;
  123. }
  124. }
  125. ?>