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

Language PHP Lines 300
MD5 Hash fa53e3e1ccba527c8cb9b98fd33c7e42
Repository https://github.com/lib-uoguelph-ca/ocs.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php

/**
 * @file classes/metadata/MetadataDataObjectAdapter.inc.php
 *
 * Copyright (c) 2000-2012 John Willinsky
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
 * @class MetadataDataObjectAdapter
 * @ingroup metadata
 * @see DataObject
 * @see MetadataSchema
 * @see MetadataDescription
 *
 * @brief Class that injects/extracts a meta-data description
 *  into/from an application entity object (DataObject).
 */

// $Id$

import('filter.Filter');
import('metadata.MetadataDescription');

class MetadataDataObjectAdapter extends Filter {
	/** @var MetadataSchema */
	var $_metadataSchema;

	/** @var string */
	var $_dataObjectName;

	/** @var integer */
	var $_assocType;

	/** @var array */
	var $_metadataFieldNames;

	/**
	 * Constructor
	 * @param $metadataSchema MetadataSchema
	 * @param $dataObjectName string
	 * @param $assocType integer
	 */
	function MetadataDataObjectAdapter(&$metadataSchema, $dataObjectName, $assocType) {
		assert(is_a($metadataSchema, 'MetadataSchema') && is_string($dataObjectName)
				&& is_integer($assocType));

		// Initialize the adapter
		$this->_metadataSchema =& $metadataSchema;
		$this->_dataObjectName = $dataObjectName;
		$this->_assocType = $assocType;
	}

	//
	// Getters and setters
	//
	/**
	 * Get the supported meta-data schema
	 * @return MetadataSchema
	 */
	function &getMetadataSchema() {
		return $this->_metadataSchema;
	}

	/**
	 * Convenience method that returns the
	 * meta-data name space.
	 * @return string
	 */
	function getMetadataNamespace() {
		$metadataSchema =& $this->getMetadataSchema();
		return $metadataSchema->getNamespace();
	}

	/**
	 * Get the supported application entity (class) name
	 * @return string
	 */
	function getDataObjectName() {
		return $this->_dataObjectName;
	}

	/**
	 * Get the association type corresponding to the data
	 * object type.
	 * @return integer
	 */
	function getAssocType() {
		return $this->_assocType;
	}

	//
	// Abstract template methods
	//
	/**
	 * Inject a MetadataDescription into a DataObject
	 * @param $metadataDescription MetadataDescription
	 * @param $dataObject DataObject
	 * @param $replace boolean whether to delete existing meta-data
	 * @return DataObject
	 */
	function &injectMetadataIntoDataObject(&$metadataDescription, &$dataObject, $replace) {
		// Must be implemented by sub-classes
		assert(false);
	}

	/**
	 * Extract a MetadataDescription from a DataObject.
	 * @param $dataObject DataObject
	 * @return MetadataDescription
	 */
	function &extractMetadataFromDataObject(&$dataObject) {
		// Must be implemented by sub-classes
		assert(false);
	}

	/**
	 * Return the additional field names introduced by the
	 * meta-data schema that need to be persisted in the
	 * ..._settings table corresponding to the DataObject
	 * which is supported by this adapter.
	 * NB: The field names must be prefixed with the meta-data
	 * schema namespace identifier.
	 * @param $translated boolean if true, return localized field
	 *  names, otherwise return additional field names.
	 * @return array an array of field names to be persisted.
	 */
	function getDataObjectMetadataFieldNames($translated = true) {
		// By default return all field names
		return $this->getMetadataFieldNames($translated);
	}


	//
	// Implement template methods from Filter
	//
	/**
	 * @see Filter::supports()
	 * @param $input mixed
	 * @param $output mixed
	 * @return boolean
	 */
	function supports(&$input, &$output) {
		// Check input tpye
		switch(true) {
			// Inject meta-data into an existing data object
			case is_array($input):
				// Check input type
				// We expect two array entries: a MetadataDescription and a target data object.
				if (count($input) != 3) return false;
				$metadataDescription =& $input[0];
				if (!is_a($metadataDescription, 'MetadataDescription')) return false;

				$dataObject =& $input[1];
				if (!is_a($dataObject, $this->_dataObjectName)) return false;

				$replace = $input[2];
				if (!is_bool($replace)) return false;

				// Check the the meta-data description compliance
				if (!$this->_complies($metadataDescription)) return false;
				break;

			// Inject meta-data into a new data object
			case is_a($input, 'MetadataDescription'):
				// We just need to check the meta-data description compliance.
				if (!$this->_complies($input)) return false;
				break;

			// Create a new meta-data description from a data object
			case is_a($input, $this->_dataObjectName):
				break;

			default:
				// A non-supported data-type
				return false;
		}

		// Check output type
		if (is_null($output)) return true;
		switch(true) {
			case is_array($input):
			case is_a($input, 'MetadataDescription'):
				// We expect an application object (DataObject)
				return is_a($output, $this->_dataObjectName);

			case is_a($input, $this->_dataObjectName):
				if (!is_a($output, 'MetadataDescription')) return false;

				// Check whether the the output
				// complies with the supported schema
				return $this->_complies($output);

			default:
				// The adapter mode must always be defined
				// when calling supports().
				assert(false);
		}
	}

	/**
	 * Convert a MetadataDescription to an application
	 * object or vice versa.
	 * @see Filter::process()
	 * @param $input mixed either a MetadataDescription or an application object
	 * @return mixed either a MetadataDescription or an application object
	 */
	function &process(&$input) {
		// Set the adapter mode and convert the input.
		switch (true) {
			case is_array($input):
				$output =& $this->injectMetadataIntoDataObject($input[0], $input[1], $input[2]);
				break;

			case is_a($input, 'MetadataDescription'):
				$nullVar = null;
				$output =& $this->injectMetadataIntoDataObject($input, $nullVar, false);
				break;

			case is_a($input, $this->_dataObjectName):
				$output =& $this->extractMetadataFromDataObject($input);
				break;

			default:
				// Input should be validated by now.
				assert(false);
		}

		return $output;
	}


	//
	// Protected helper methods
	//
	/**
	 * Instantiate a meta-data description that conforms to the
	 * settings of this adapter.
	 * @return MetadataDescription
	 */
	function &instantiateMetadataDescription() {
		$metadataDescription = new MetadataDescription($this->getMetadataSchema(), $this->getAssocType());
		return $metadataDescription;
	}

	/**
	 * Return all field names introduced by the
	 * meta-data schema that might have to be persisted.
	 * @param $translated boolean if true, return localized field
	 *  names, otherwise return additional field names.
	 * @return array an array of field names to be persisted.
	 */
	function getMetadataFieldNames($translated = true) {
		// Do we need to build the field name cache first?
		if (is_null($this->_metadataFieldNames)) {
			// Initialize the cache array
			$this->_metadataFieldNames = array();

			// Retrieve all properties and add
			// their names to the cache
			$metadataSchema =& $this->getMetadataSchema();
			$metadataSchemaNamespace = $metadataSchema->getNamespace();
			$properties =& $metadataSchema->getProperties();
			foreach($properties as $property) {
				$propertyAssocTypes = $property->getAssocTypes();
				if (in_array($this->_assocType, $propertyAssocTypes)) {
					// Separate translated and non-translated property names
					// and add the name space so that field names are unique
					// across various meta-data schemas.
					$this->_metadataFieldNames[$property->getTranslated()][] = $metadataSchemaNamespace.':'.$property->getName();
				}
			}
		}

		// Return the field names
		return $this->_metadataFieldNames[$translated];
	}

	//
	// Private helper methods
	//
	/**
	 * Check whether a given meta-data description complies with
	 * the meta-data schema configured for this adapter.
	 * @param $metadataDescription MetadataDescription
	 * @return boolean true if the given description complies, otherwise false
	 */
	function _complies($metadataDescription) {
		// Check that the description describes the correct resource
		if ($metadataDescription->getAssocType() != $this->_assocType) return false;

		// Check that the description complies with the correct schema
		$descriptionSchema =& $metadataDescription->getMetadataSchema();
		$supportedSchema =& $this->_metadataSchema;
		if ($descriptionSchema->getName() != $supportedSchema->getName()) return false;

		// Compliance was successfully checked
		return true;
	}
}
?>
Back to Top