Source for file HarmoniRecord.class.php

Documentation is available at HarmoniRecord.class.php

  1. <?php
  2.  
  3. require_once(OKI2."/osid/repository/Record.php");
  4. require_once(HARMONI."/oki2/repository/HarmoniPart.class.php");
  5. require_once(HARMONI."/oki2/repository/HarmoniPartIterator.class.php");
  6.  
  7.  
  8. /**
  9. * Each Asset has one of the AssetType supported by the Repository. There are
  10. * also zero or more RecordStructures required by the Repository for each
  11. * AssetType. RecordStructures provide structural information. The values for
  12. * a given Asset's RecordStructure are stored in a Record. RecordStructures
  13. * can contain sub-elements which are referred to as PartStructures. The
  14. * structure defined in the RecordStructure and its PartStructures is used in
  15. * for any Records for the Asset. Records have Parts which parallel
  16. * PartStructures.
  17. *
  18. * <p>
  19. * OSID Version: 2.0
  20. * </p>
  21. *
  22. * @package harmoni.osid_v2.repository
  23. *
  24. * @copyright Copyright &copy;2005, Middlebury College
  25. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
  26. *
  27. * @version $Id: HarmoniRecord.class.php,v 1.24 2007/09/04 20:25:43 adamfranco Exp $
  28. */
  29.  
  30. class HarmoniRecord
  31. extends RecordInterface
  32. {
  33. var $_record;
  34. var $_recordStructure;
  35. var $_createdParts;
  36. function HarmoniRecord( $recordStructure, $record, $asset ) {
  37. $this->_record=$record;
  38. $this->_recordStructure =$recordStructure;
  39. $this->_createdParts = array();
  40. $this->_asset =$asset;
  41. }
  42. /**
  43. * Get the unique Id for this Record.
  44. *
  45. * @return object Id
  46. *
  47. * @throws object RepositoryException An exception with one of
  48. * the following messages defined in
  49. * org.osid.repository.RepositoryException may be thrown: {@link }
  50. * org.osid.repository.RepositoryException#OPERATION_FAILED
  51. * OPERATION_FAILED}, {@link }
  52. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  53. * PERMISSION_DENIED}, {@link }
  54. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  55. * CONFIGURATION_ERROR}, {@link }
  56. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  57. * UNIMPLEMENTED}
  58. *
  59. * @access public
  60. */
  61. function getId () {
  62. $idManager = Services::getService("Id");
  63. $id = $this->_record->getID();
  64. return $idManager->getId($id);
  65. }
  66.  
  67. /**
  68. * Create a Part. Records are composed of Parts. Parts can also contain
  69. * other Parts. Each Record is associated with a specific RecordStructure
  70. * and each Part is associated with a specific PartStructure.
  71. *
  72. * @param object Id $partStructureId
  73. * @param object mixed $value (original type: java.io.Serializable)
  74. *
  75. * @return object Part
  76. *
  77. * @throws object RepositoryException An exception with one of
  78. * the following messages defined in
  79. * org.osid.repository.RepositoryException may be thrown: {@link }
  80. * org.osid.repository.RepositoryException#OPERATION_FAILED
  81. * OPERATION_FAILED}, {@link }
  82. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  83. * PERMISSION_DENIED}, {@link }
  84. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  85. * CONFIGURATION_ERROR}, {@link }
  86. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  87. * UNIMPLEMENTED}, {@link }
  88. * org.osid.repository.RepositoryException#NULL_ARGUMENT
  89. * NULL_ARGUMENT}, {@link }
  90. * org.osid.repository.RepositoryException#UNKNOWN_ID UNKNOWN_ID}
  91. *
  92. * @access public
  93. */
  94. function createPart ( $partStructureId, $value ) {
  95. ArgumentValidator::validate($value, ExtendsValidatorRule::getRule("SObject"));
  96. $partID = $partStructureId->getIdString();
  97. // we need to find the label associated with this ID
  98. $schema =$this->_record->getSchema();
  99. $found = false;
  100. foreach ($schema->getAllIDs() as $id) {
  101. if ($partID == $id) {
  102. $found = true;
  103. $part =$schema->getField($id);
  104. break;
  105. }
  106. }
  107. if (!$found) {
  108. throwError(new Error(RepositoryException::UNKNOWN_ID().": $partID.", "HarmoniRecord", true));
  109. }
  110. $label = $schema->getFieldLabelFromID($id);
  111. $this->_record->makeFull(); // make sure we have a full data representation.
  112. // If the value is deleted, add a new version to it.
  113. if ($this->_record->numValues($label, true) && $this->_record->isValueDeleted($label)) {
  114. $this->_record->undeleteValue($label);
  115. $this->_record->setValue($label, $value);
  116. // If the field is not multi-valued AND has a value AND that value is not deleted,
  117. // throw an error.
  118. } else if (!$part->getMultFlag()
  119. && $this->_record->numValues($label)
  120. && $this->_record->getValue($label)) {
  121. throwError(new Error(RepositoryException::PERMISSION_DENIED().": Can't add another field to a
  122. non-multi-valued part.", "HarmoniRecord", true));
  123. // If we dont' have an existing, deleted field to add to, create a new index.
  124. } else {
  125. $this->_record->setValue($label, $value, NEW_VALUE);
  126. }
  127. $this->_record->commit(TRUE);
  128. $repository =$this->_asset->getRepository();
  129. $part = new HarmoniPart(new HarmoniPartStructure($this->_recordStructure, $part, $repository->getId()),
  130. $this->_record->getRecordFieldValue($id, $this->_record->numValues($label)-1),
  131. $this->_asset);
  132. $this->_asset->updateModificationDate();
  133. return $part;
  134. }
  135.  
  136. /**
  137. * Delete a Part and all its Parts.
  138. *
  139. * @param object Id $partId
  140. *
  141. * @throws object RepositoryException An exception with one of
  142. * the following messages defined in
  143. * org.osid.repository.RepositoryException may be thrown: {@link }
  144. * org.osid.repository.RepositoryException#OPERATION_FAILED
  145. * OPERATION_FAILED}, {@link }
  146. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  147. * PERMISSION_DENIED}, {@link }
  148. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  149. * CONFIGURATION_ERROR}, {@link }
  150. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  151. * UNIMPLEMENTED}, {@link }
  152. * org.osid.repository.RepositoryException#NULL_ARGUMENT
  153. * NULL_ARGUMENT}, {@link }
  154. * org.osid.repository.RepositoryException#UNKNOWN_ID UNKNOWN_ID}
  155. *
  156. * @access public
  157. */
  158. function deletePart ( $partId ) {
  159. $string = $partId->getIdString();
  160. if (ereg("(.+)::(.+)::([0-9]+)",$string,$r)) {
  161. $recordId = $r[1];
  162. $label = $r[2];
  163. $index = $r[3];
  164. if ($this->_record->getID() == $recordId) {
  165. $this->_record->deleteValue($label, $index);
  166. $this->_record->commit(TRUE);
  167. }
  168. } else {
  169. throwError(new Error(RepositoryException::UNKNOWN_ID().": $string", "HarmoniPart", true));
  170. }
  171. $this->_asset->updateModificationDate();
  172. }
  173.  
  174. /**
  175. * Get all the Parts in the Record. Iterators return a set, one at a time.
  176. *
  177. * @return object PartIterator
  178. *
  179. * @throws object RepositoryException An exception with one of
  180. * the following messages defined in
  181. * org.osid.repository.RepositoryException may be thrown: {@link }
  182. * org.osid.repository.RepositoryException#OPERATION_FAILED
  183. * OPERATION_FAILED}, {@link }
  184. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  185. * PERMISSION_DENIED}, {@link }
  186. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  187. * CONFIGURATION_ERROR}, {@link }
  188. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  189. * UNIMPLEMENTED}
  190. *
  191. * @access public
  192. */
  193. function getParts () {
  194. // Get all of the PartStructures in this structure
  195. $partStructures =$this->_recordStructure->getPartStructures();
  196. while ($partStructures->hasNext()) {
  197. $partStructure =$partStructures->next();
  198. $id = $partStructure->getId();
  199. $idString = $id->getIdString();
  200. $allRecordFieldValues =$this->_record->getRecordFieldValues($idString);
  201. // Create an Part for each valueVersionObj
  202. if (count($allRecordFieldValues)) {
  203. foreach (array_keys($allRecordFieldValues) as $key) {
  204. $activeValue = $allRecordFieldValues[$key]->getActiveVersion();
  205. if ($activeValue && !isset($this->_createdParts[$activeValue->getID()]))
  206. $this->_createdParts[$activeValue->getID()] = new HarmoniPart(
  207. $partStructure, $allRecordFieldValues[$key],
  208. $this->_asset);
  209. }
  210. }
  211. }
  212. // Create an iterator and return it.
  213. $partIterator = new HarmoniPartIterator($this->_createdParts);
  214. return $partIterator;
  215. }
  216.  
  217. /**
  218. * Get the part from the record that matches the passed Id
  219. *
  220. * WARNING: NOT IN OSID
  221. *
  222. * @param object HarmoniId
  223. * @return object HarmoniPart
  224. * @access public
  225. * @since 10/10/05
  226. */
  227. function getPart ($id) {
  228. $parts =$this->getParts();
  229. while ($parts->hasNext()) {
  230. $part =$parts->next();
  231. if ($part->getId() == $id)
  232. return $part;
  233. }
  234. $false = false;
  235. return $false;
  236. }
  237. /**
  238. * Return true if this Record is multi-valued; false otherwise. This is determined by the implementation.
  239. *
  240. * WARNING: NOT IN OSID
  241. *
  242. * @return boolean
  243. * @throws osid.dr.DigitalRepositoryException An exception with one of the following messages defined in osid.dr.DigitalRepositoryException may be thrown: {@link DigitalRepositoryException#OPERATION_FAILED OPERATION_FAILED}, {@link DigitalRepositoryException#PERMISSION_DENIED PERMISSION_DENIED}, {@link DigitalRepositoryException#CONFIGURATION_ERROR CONFIGURATION_ERROR}, {@link DigitalRepositoryException#UNIMPLEMENTED UNIMPLEMENTED}
  244. */
  245. function isMultivalued() {
  246. return true; // we allow as many Records of any RecordStructure as people want.
  247. }
  248.  
  249. /**
  250. * Get the RecordStructure associated with this Record.
  251. *
  252. * @return object RecordStructure
  253. *
  254. * @throws object RepositoryException An exception with one of
  255. * the following messages defined in
  256. * org.osid.repository.RepositoryException may be thrown: {@link }
  257. * org.osid.repository.RepositoryException#OPERATION_FAILED
  258. * OPERATION_FAILED}, {@link }
  259. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  260. * PERMISSION_DENIED}, {@link }
  261. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  262. * CONFIGURATION_ERROR}, {@link }
  263. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  264. * UNIMPLEMENTED}
  265. *
  266. * @access public
  267. */
  268. function getRecordStructure () {
  269. return $this->_recordStructure;
  270. }
  271. /**
  272. * Get the Parts of the Records for this Asset that are based on this
  273. * RecordStructure PartStructure's unique Id.
  274. *
  275. * WARNING: NOT IN OSID (as of July 2005)
  276. *
  277. * @param object Id $partStructureId
  278. *
  279. * @return object PartIterator
  280. *
  281. * @throws object RepositoryException An exception with one of
  282. * the following messages defined in
  283. * org.osid.repository.RepositoryException may be thrown: {@link }
  284. * org.osid.repository.RepositoryException#OPERATION_FAILED
  285. * OPERATION_FAILED}, {@link }
  286. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  287. * PERMISSION_DENIED}, {@link }
  288. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  289. * CONFIGURATION_ERROR}, {@link }
  290. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  291. * UNIMPLEMENTED}, {@link }
  292. * org.osid.repository.RepositoryException#NULL_ARGUMENT
  293. * NULL_ARGUMENT}, {@link }
  294. * org.osid.repository.RepositoryException#UNKNOWN_ID UNKNOWN_ID}
  295. *
  296. * @access public
  297. */
  298. function getPartsByPartStructure ( $partStructureId ) {
  299. $partStructure =$this->_recordStructure->getPartStructure($partStructureId);
  300. $partStructureId = $partStructure->getId();
  301. $idString = $partStructureId->getIdString();
  302. $partsToReturn = array();
  303. // Create an Part for each valueVersionObj
  304. $allRecordFieldValues =$this->_record->getRecordFieldValues($idString);
  305. if (count($allRecordFieldValues)) {
  306. foreach (array_keys($allRecordFieldValues) as $key) {
  307. if ($activeValue =$allRecordFieldValues[$key]->getActiveVersion()) {
  308. if (!isset($this->_createdParts[$activeValue->getID()])) {
  309. $this->_createdParts[$activeValue->getID()] = new HarmoniPart(
  310. $partStructure, $allRecordFieldValues[$key],
  311. $this->_asset);
  312. }
  313. $partsToReturn[] =$this->_createdParts[$activeValue->getID()];
  314. }
  315. }
  316. }
  317. $partsIterator = new HarmoniIterator($partsToReturn);
  318. return $partsIterator;
  319. }
  320. }

Documentation generated on Wed, 19 Sep 2007 10:24:13 -0400 by phpDocumentor 1.3.0RC3