PageRenderTime 118ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/pkp/classes/metadata/MetadataDataObjectAdapter.inc.php

https://github.com/lib-uoguelph-ca/ocs
PHP | 300 lines | 114 code | 41 blank | 145 comment | 18 complexity | fa53e3e1ccba527c8cb9b98fd33c7e42 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @file classes/metadata/MetadataDataObjectAdapter.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 MetadataDataObjectAdapter
  9. * @ingroup metadata
  10. * @see DataObject
  11. * @see MetadataSchema
  12. * @see MetadataDescription
  13. *
  14. * @brief Class that injects/extracts a meta-data description
  15. * into/from an application entity object (DataObject).
  16. */
  17. // $Id$
  18. import('filter.Filter');
  19. import('metadata.MetadataDescription');
  20. class MetadataDataObjectAdapter extends Filter {
  21. /** @var MetadataSchema */
  22. var $_metadataSchema;
  23. /** @var string */
  24. var $_dataObjectName;
  25. /** @var integer */
  26. var $_assocType;
  27. /** @var array */
  28. var $_metadataFieldNames;
  29. /**
  30. * Constructor
  31. * @param $metadataSchema MetadataSchema
  32. * @param $dataObjectName string
  33. * @param $assocType integer
  34. */
  35. function MetadataDataObjectAdapter(&$metadataSchema, $dataObjectName, $assocType) {
  36. assert(is_a($metadataSchema, 'MetadataSchema') && is_string($dataObjectName)
  37. && is_integer($assocType));
  38. // Initialize the adapter
  39. $this->_metadataSchema =& $metadataSchema;
  40. $this->_dataObjectName = $dataObjectName;
  41. $this->_assocType = $assocType;
  42. }
  43. //
  44. // Getters and setters
  45. //
  46. /**
  47. * Get the supported meta-data schema
  48. * @return MetadataSchema
  49. */
  50. function &getMetadataSchema() {
  51. return $this->_metadataSchema;
  52. }
  53. /**
  54. * Convenience method that returns the
  55. * meta-data name space.
  56. * @return string
  57. */
  58. function getMetadataNamespace() {
  59. $metadataSchema =& $this->getMetadataSchema();
  60. return $metadataSchema->getNamespace();
  61. }
  62. /**
  63. * Get the supported application entity (class) name
  64. * @return string
  65. */
  66. function getDataObjectName() {
  67. return $this->_dataObjectName;
  68. }
  69. /**
  70. * Get the association type corresponding to the data
  71. * object type.
  72. * @return integer
  73. */
  74. function getAssocType() {
  75. return $this->_assocType;
  76. }
  77. //
  78. // Abstract template methods
  79. //
  80. /**
  81. * Inject a MetadataDescription into a DataObject
  82. * @param $metadataDescription MetadataDescription
  83. * @param $dataObject DataObject
  84. * @param $replace boolean whether to delete existing meta-data
  85. * @return DataObject
  86. */
  87. function &injectMetadataIntoDataObject(&$metadataDescription, &$dataObject, $replace) {
  88. // Must be implemented by sub-classes
  89. assert(false);
  90. }
  91. /**
  92. * Extract a MetadataDescription from a DataObject.
  93. * @param $dataObject DataObject
  94. * @return MetadataDescription
  95. */
  96. function &extractMetadataFromDataObject(&$dataObject) {
  97. // Must be implemented by sub-classes
  98. assert(false);
  99. }
  100. /**
  101. * Return the additional field names introduced by the
  102. * meta-data schema that need to be persisted in the
  103. * ..._settings table corresponding to the DataObject
  104. * which is supported by this adapter.
  105. * NB: The field names must be prefixed with the meta-data
  106. * schema namespace identifier.
  107. * @param $translated boolean if true, return localized field
  108. * names, otherwise return additional field names.
  109. * @return array an array of field names to be persisted.
  110. */
  111. function getDataObjectMetadataFieldNames($translated = true) {
  112. // By default return all field names
  113. return $this->getMetadataFieldNames($translated);
  114. }
  115. //
  116. // Implement template methods from Filter
  117. //
  118. /**
  119. * @see Filter::supports()
  120. * @param $input mixed
  121. * @param $output mixed
  122. * @return boolean
  123. */
  124. function supports(&$input, &$output) {
  125. // Check input tpye
  126. switch(true) {
  127. // Inject meta-data into an existing data object
  128. case is_array($input):
  129. // Check input type
  130. // We expect two array entries: a MetadataDescription and a target data object.
  131. if (count($input) != 3) return false;
  132. $metadataDescription =& $input[0];
  133. if (!is_a($metadataDescription, 'MetadataDescription')) return false;
  134. $dataObject =& $input[1];
  135. if (!is_a($dataObject, $this->_dataObjectName)) return false;
  136. $replace = $input[2];
  137. if (!is_bool($replace)) return false;
  138. // Check the the meta-data description compliance
  139. if (!$this->_complies($metadataDescription)) return false;
  140. break;
  141. // Inject meta-data into a new data object
  142. case is_a($input, 'MetadataDescription'):
  143. // We just need to check the meta-data description compliance.
  144. if (!$this->_complies($input)) return false;
  145. break;
  146. // Create a new meta-data description from a data object
  147. case is_a($input, $this->_dataObjectName):
  148. break;
  149. default:
  150. // A non-supported data-type
  151. return false;
  152. }
  153. // Check output type
  154. if (is_null($output)) return true;
  155. switch(true) {
  156. case is_array($input):
  157. case is_a($input, 'MetadataDescription'):
  158. // We expect an application object (DataObject)
  159. return is_a($output, $this->_dataObjectName);
  160. case is_a($input, $this->_dataObjectName):
  161. if (!is_a($output, 'MetadataDescription')) return false;
  162. // Check whether the the output
  163. // complies with the supported schema
  164. return $this->_complies($output);
  165. default:
  166. // The adapter mode must always be defined
  167. // when calling supports().
  168. assert(false);
  169. }
  170. }
  171. /**
  172. * Convert a MetadataDescription to an application
  173. * object or vice versa.
  174. * @see Filter::process()
  175. * @param $input mixed either a MetadataDescription or an application object
  176. * @return mixed either a MetadataDescription or an application object
  177. */
  178. function &process(&$input) {
  179. // Set the adapter mode and convert the input.
  180. switch (true) {
  181. case is_array($input):
  182. $output =& $this->injectMetadataIntoDataObject($input[0], $input[1], $input[2]);
  183. break;
  184. case is_a($input, 'MetadataDescription'):
  185. $nullVar = null;
  186. $output =& $this->injectMetadataIntoDataObject($input, $nullVar, false);
  187. break;
  188. case is_a($input, $this->_dataObjectName):
  189. $output =& $this->extractMetadataFromDataObject($input);
  190. break;
  191. default:
  192. // Input should be validated by now.
  193. assert(false);
  194. }
  195. return $output;
  196. }
  197. //
  198. // Protected helper methods
  199. //
  200. /**
  201. * Instantiate a meta-data description that conforms to the
  202. * settings of this adapter.
  203. * @return MetadataDescription
  204. */
  205. function &instantiateMetadataDescription() {
  206. $metadataDescription = new MetadataDescription($this->getMetadataSchema(), $this->getAssocType());
  207. return $metadataDescription;
  208. }
  209. /**
  210. * Return all field names introduced by the
  211. * meta-data schema that might have to be persisted.
  212. * @param $translated boolean if true, return localized field
  213. * names, otherwise return additional field names.
  214. * @return array an array of field names to be persisted.
  215. */
  216. function getMetadataFieldNames($translated = true) {
  217. // Do we need to build the field name cache first?
  218. if (is_null($this->_metadataFieldNames)) {
  219. // Initialize the cache array
  220. $this->_metadataFieldNames = array();
  221. // Retrieve all properties and add
  222. // their names to the cache
  223. $metadataSchema =& $this->getMetadataSchema();
  224. $metadataSchemaNamespace = $metadataSchema->getNamespace();
  225. $properties =& $metadataSchema->getProperties();
  226. foreach($properties as $property) {
  227. $propertyAssocTypes = $property->getAssocTypes();
  228. if (in_array($this->_assocType, $propertyAssocTypes)) {
  229. // Separate translated and non-translated property names
  230. // and add the name space so that field names are unique
  231. // across various meta-data schemas.
  232. $this->_metadataFieldNames[$property->getTranslated()][] = $metadataSchemaNamespace.':'.$property->getName();
  233. }
  234. }
  235. }
  236. // Return the field names
  237. return $this->_metadataFieldNames[$translated];
  238. }
  239. //
  240. // Private helper methods
  241. //
  242. /**
  243. * Check whether a given meta-data description complies with
  244. * the meta-data schema configured for this adapter.
  245. * @param $metadataDescription MetadataDescription
  246. * @return boolean true if the given description complies, otherwise false
  247. */
  248. function _complies($metadataDescription) {
  249. // Check that the description describes the correct resource
  250. if ($metadataDescription->getAssocType() != $this->_assocType) return false;
  251. // Check that the description complies with the correct schema
  252. $descriptionSchema =& $metadataDescription->getMetadataSchema();
  253. $supportedSchema =& $this->_metadataSchema;
  254. if ($descriptionSchema->getName() != $supportedSchema->getName()) return false;
  255. // Compliance was successfully checked
  256. return true;
  257. }
  258. }
  259. ?>