Source for file RecordFieldData.class.php

Documentation is available at RecordFieldData.class.php

  1. <?php
  2.  
  3. /**
  4. * Holds information about a specific version of a value index of a field in a {@link Record}. Information held
  5. * includes: Date created/modified, active/not active (ie, deleted), and the actual value object (usually a {@link Primitive}).
  6. *
  7. * @package harmoni.datamanager
  8. *
  9. * @copyright Copyright &copy; 2005, Middlebury College
  10. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  11. *
  12. * @version $Id: RecordFieldData.class.php,v 1.26 2007/09/04 20:25:31 adamfranco Exp $
  13. * @author Gabe Schine
  14. */
  15. class RecordFieldData {
  16. var $_myID;
  17. var $_dataID;
  18. var $_date = null;
  19. var $_primitive = null;
  20. var $_active = false;
  21. var $_parent = null;
  22. var $_update = false;
  23. var $_prune = false;
  24. var $_recast = false;
  25. function RecordFieldData($parent, $active=false) {
  26. $this->_date = DateAndTime::now();
  27. $this->_active = $active;
  28. $this->_parent =$parent;
  29. }
  30. /**
  31. * Returns an exact data-specific replica of the current object.
  32. * @param ref object $parent The new parent object to use instead of our parent.
  33. * @return ref object
  34. * @access public
  35. */
  36. function replicate($parent) {
  37. $newObj = new RecordFieldData($parent, $this->_active);
  38. $date = $this->_date; // in PHP4 this will replicate the DateAndTime
  39. $newObj->setDate($date);
  40. $newObj->setValueFromPrimitive($this->_primitive->deepCopy());
  41. $newObj->update();
  42. return $newObj;
  43. }
  44. /**
  45. * Flags this RecordFieldData to be updated to the database upon commit().
  46. * @return void
  47. * @access public
  48. */
  49. function update() { $this->_update=true; }
  50. /**
  51. * Changes the active flag of this RecordFieldData to $active.
  52. * @param bool $active
  53. * @return void
  54. * @access public
  55. */
  56. function setActiveFlag($active) {
  57. // ArgumentValidator::validate($active, BooleanValidatorRule::getRule());
  58. $this->_active = $active;
  59. // if for some reason we are going to be pruned, and the new active flag is true, let's not prune
  60. if ($active===true) $this->cancelPrune();
  61. }
  62. /**
  63. * Returns the current active flag.
  64. * @access public
  65. * @return bool
  66. */
  67. function getActiveFlag()
  68. {
  69. return $this->_active;
  70. }
  71. /**
  72. * Returns the current active flag.
  73. * @return bool
  74. * @access public
  75. */
  76. function isActive() { return $this->_active; }
  77. /**
  78. * Takes a single database row and sets local data (like the modified timestamp, etc) variables
  79. * based on that row.
  80. * @param ref array $row The associative database row.
  81. * @return void
  82. * @access public
  83. */
  84. function populate( $row ) {
  85. $dbHandler = Services::getService("DatabaseManager");
  86. // $recordField =$this->_parent->getRecordField();
  87. // $record =$recordField->getRecord();
  88. // $dbID = $dataSet->getDatabaseId();
  89. $this->_myID = $row['record_field_id'];
  90. $this->_date =$dbHandler->fromDBDate($row['record_field_modified'],DATAMANAGER_DBID);
  91. // $this->_active was set by parent on construction
  92. // now we need to create the valueObj
  93. $recordField =$this->_parent->getRecordField();
  94. $schemaField =$recordField->getSchemaField();
  95. $type = $schemaField->getType();
  96. $dataTypeManager = Services::getService("DataTypeManager");
  97. $class = $dataTypeManager->storablePrimitiveClassForType($type);
  98. eval('$valueObj = '.$class.'::createAndPopulate($row);');
  99. $this->_dataID = $row['fk_data'];
  100. $this->_primitive =$valueObj;
  101. }
  102. /**
  103. * Flags us for deletion from the DB.
  104. * @return void
  105. * @access public
  106. */
  107. function prune() {
  108. // $this->_active = false;
  109. $this->_prune = true;
  110. // if we're pruning, there's no point in updating the DB
  111. $this->_update = false;
  112. }
  113. /**
  114. * Unflags us for deletion from the DB.
  115. * @return void
  116. * @access public
  117. */
  118. function cancelPrune() {
  119. $this->_prune = false;
  120. }
  121. /**
  122. * Re-casts our internal {@link Primitive} data object as a {@link StorablePrimitive} of the same data type.
  123. * @access public
  124. * @return void
  125. */
  126. function recastAsStorable()
  127. {
  128. if ($this->_recast) return;
  129. $dataTypeManager = Services::getService("DataTypeManager");
  130. $recordField =$this->_parent->getRecordField();
  131. $schemaField =$recordField->getSchemaField();
  132. $type = $schemaField->getType();
  133. $newObj =$dataTypeManager->recastAsStorablePrimitive($this->_primitive, $type);
  134. if ($newObj) {
  135. $this->_primitive =$newObj;
  136. $this->_recast = true;
  137. }
  138. }
  139. /**
  140. * Commits any changes that have been made to the database. If neither update() nor prune() have been
  141. * called, even if changes have been made, they will not be reflected in the database.
  142. * @return bool
  143. * @access public
  144. */
  145. function commit() {
  146. $dbHandler = Services::getService("DatabaseManager");
  147. if ($this->_update) {
  148. // let's re-cast our primitive to a storablePrimitive
  149. $this->recastAsStorable();
  150. // first we need to commit the actual Primitive value
  151. // so that we can get its ID
  152. if (!$this->_dataID)
  153. $this->_dataID = $this->_primitive->insert(DATAMANAGER_DBID);
  154. else
  155. $this->_primitive->update(DATAMANAGER_DBID,$this->_dataID);
  156. $this->_date = DateAndTime::now();
  157. if ($this->_myID) {
  158. // we're already in the DB. just update the entry
  159. $query = new UpdateQuery();
  160. $query->setWhere("id='".addslashes($this->_myID)."'");
  161. $query->setColumns(array("value_index","active", "modified"));
  162. $query->setValues(array($this->_parent->getIndex(),($this->_active)?1:0,
  163. $dbHandler->toDBDate($this->_date,DATAMANAGER_DBID)));
  164. } else {
  165. // we have to insert a new one
  166. $query = new InsertQuery();
  167. $idManager = Services::getService("Id");
  168. $newID =$idManager->createId();
  169. $this->_myID = $newID->getIdString();
  170. $query->setColumns(array(
  171. "id",
  172. "fk_record",
  173. "fk_schema_field",
  174. "value_index",
  175. "fk_data",
  176. "active",
  177. "modified"
  178. ));
  179. $schema =$this->_parent->_parent->_parent->getSchema();
  180. $schemaField =$this->_parent->_parent->getSchemaField();
  181. $query->addRowOfValues(array(
  182. "'".addslashes($this->_myID)."'",
  183. "'".addslashes($this->_parent->_parent->_parent->getID())."'",
  184. "'".addslashes($schemaField->getID())."'",
  185. $this->_parent->getIndex(),
  186. "'".addslashes($this->_dataID)."'",
  187. ($this->_active)?1:0,
  188. $dbHandler->toDBDate($this->_date,DATAMANAGER_DBID)
  189. ));
  190. }
  191. $query->setTable("dm_record_field");
  192.  
  193. $result =$dbHandler->query($query, DATAMANAGER_DBID);
  194. if (!$result) {
  195. throwError( new UnknownDBError("Record") );
  196. return false;
  197. }
  198. }
  199. if ($this->_prune && $this->_dataID) {
  200. if ($id = $this->getID()) {
  201. // ok, let's get rid of ourselves... completely!
  202. $query = new DeleteQuery;
  203. $query->setTable("dm_record_field");
  204. $query->setWhere("id='".addslashes($id)."'");
  205.  
  206. $res =$dbHandler->query($query, DATAMANAGER_DBID);
  207. if (!$res) throwError( new UnknownDBError("Record"));
  208. // now tell the data object to prune itself
  209. $this->recastAsStorable();
  210. $this->_primitive->prune(DATAMANAGER_DBID, $this->_dataID);
  211. // and we have to get rid of any tag mappings where we are included.
  212. $query = new DeleteQuery;
  213. $query->setTable("dm_tag_map");
  214. $query->setWhere("fk_record_field='".addslashes($id)."'");
  215. $res =$dbHandler->query($query, DATAMANAGER_DBID);
  216. if (!$res) throwError( new UnknownDBError("Record"));
  217. }
  218. }
  219. // reset the prune flag
  220. $this->_prune = false;
  221. // reset the update flag
  222. $this->_update = false;
  223. return true;
  224. }
  225. /**
  226. * Returns if this ValueVersion object is flagged to delete itself from the DB upon commit().
  227. * @return bool
  228. * @access public
  229. */
  230. function willPrune() {
  231. return $this->_prune;
  232. }
  233. /**
  234. * Takes a {@link Primitive} object and hands it to our local value object so that it can
  235. * set its own value based on $object.
  236. * @param ref object $object
  237. * @return void
  238. * @access public
  239. */
  240. function takeValueFromPrimitive($object) {
  241. $this->setValueFromPrimitive($object);
  242. }
  243. /**
  244. * Sets the local valueObject to be a reference to $object.
  245. * @param ref object $object A new {@link Primitive} object.
  246. * @return void
  247. * @access public
  248. */
  249. function setValueFromPrimitive($object) {
  250. $this->_primitive =$object;
  251. $this->_recast = false;
  252. }
  253. /**
  254. * Returns the current value object contained locally.
  255. * @return ref object A {@link Primitive}.
  256. * @access public
  257. */
  258. function getPrimitive() {
  259. return $this->_primitive;
  260. }
  261. /**
  262. * Returns the created/modified timestamp of this specific version.
  263. * @return ref object A {@link DateAndTime} object.
  264. * @access public
  265. */
  266. function getDate() {
  267. return $this->_date;
  268. }
  269. /**
  270. * Sets the date reflected within our local variables to $date.
  271. * @param ref object A {@link DateAndTime} object.
  272. * @return void
  273. * @access public
  274. */
  275. function setDate($date) {
  276. ArgumentValidator::validate($date,
  277. HasMethodsValidatorRule::getRule("asDateAndTime"));
  278. $this->_date =$date->asDateAndTime();
  279. }
  280. /**
  281. * Returns our ID in the database.
  282. * @return int
  283. * @access public
  284. */
  285. function getID() {
  286. return $this->_myID;
  287. }
  288. }

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