Commit 34ef8b048fc27e7419caeb8de0ef070dd2caa6a3

Authored by Michael Joseph
1 parent 2487b4d8

added ArchiveSettings class and test


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@1836 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/documentmanagement/ArchiveSettings.inc 0 → 100644
  1 +<?php
  2 +
  3 +/**
  4 + * $Id$
  5 + *
  6 + * Represents archive settings for a document
  7 + *
  8 + * Licensed under the GNU GPL. For full terms see the file COPYING.
  9 + *
  10 + * @version $Revision$
  11 + * @author Michael Joseph <michael@jamwarehouse.com>, Jam Warehouse (Pty) Ltd, South Africa
  12 + * @package lib.dashboard
  13 + */
  14 +
  15 +class ArchiveSettings {
  16 +
  17 + /**
  18 + * The primary key
  19 + */
  20 + var $iId;
  21 + /**
  22 + * The document ID
  23 + */
  24 + var $iDocumentID;
  25 + /**
  26 + * The expiration date
  27 + */
  28 + var $dExpirationDate;
  29 + /**
  30 + * The utilisation threshold
  31 + */
  32 + var $iUtilisationThreshold;
  33 +
  34 + /**
  35 + * Constructs an archive settings instance
  36 + *
  37 + * @param integer the document id
  38 + * @param date the expiration date
  39 + * @param integer the utilisation threshold
  40 + */
  41 + function ArchiveSettings($iNewDocumentID, $dNewExpirationDate, $iNewUtilisationThreshold) {
  42 + global $default;
  43 +
  44 + // primary key not set as this is not stored yet
  45 + $this->iId = -1;
  46 + $this->iDocumentID = $iNewDocumentID;
  47 + $this->dExpirationDate = $dNewExpirationDate;
  48 + $this->iUtilisationThreshold = $iNewUtilisationThreshold;
  49 + }
  50 +
  51 + /**
  52 + * Gets the primary key
  53 + */
  54 + function getID(){
  55 + return $this->iId;
  56 + }
  57 +
  58 + /**
  59 + * Gets the document id
  60 + */
  61 + function getDocumentID(){
  62 + return $this->iDocumentID;
  63 + }
  64 +
  65 + /**
  66 + * Sets the document id
  67 + *
  68 + * @param string the new document id
  69 + */
  70 + function setDocumentID($iNewDocumentID){
  71 + $this->iDocumentID = $iNewDocumentID;
  72 + }
  73 +
  74 + /**
  75 + * Gets the expiration date
  76 + */
  77 + function getExpirationDate(){
  78 + return $this->dExpirationDate;
  79 + }
  80 +
  81 + /**
  82 + * Sets the expiration date
  83 + *
  84 + * @param date the new expiration date
  85 + */
  86 + function setExpirationDate($dNewExpirationDate){
  87 + $this->dExpirationDate = $dNewExpirationDate;
  88 + }
  89 +
  90 + /**
  91 + * Gets the utilisation threshold
  92 + */
  93 + function getUtilisationThreshold(){
  94 + return $this->iUtilisationThreshold;
  95 + }
  96 +
  97 + /**
  98 + * Sets the utilisation threshold
  99 + *
  100 + * @param integer the utilisation threshold
  101 + */
  102 + function setUtilisationThreshold($iNewUtilisationThreshold){
  103 + $this->iUtilisationThreshold = $iNewUtilisationThreshold;
  104 + }
  105 +
  106 + /**
  107 + * Inserts the archive settings into the database
  108 + *
  109 + * @return boolean true on successful update, false otherwise
  110 + */
  111 + function create(){
  112 + global $default;
  113 + //if the id >= 0, then the object has already been created
  114 + if ($this->iId < 0) {
  115 + $sql = $default->db;
  116 + $result = $sql->query("INSERT INTO $default->owl_archive_settings_table (document_id, expiration_date, utilisation_threshold) " .
  117 + "VALUES ($this->iDocumentID, '$this->dExpirationDate', $this->iUtilisationThreshold)");
  118 + if ($result) {
  119 + //set the current primary key
  120 + $this->iId = $sql->insert_id();
  121 + return true;
  122 + }
  123 + return false;
  124 + }
  125 + return false;
  126 + }
  127 +
  128 + /**
  129 + * Update the archive settings current values in the database
  130 + *
  131 + * @return boolean true on successful update, false otherwise
  132 + */
  133 + function update(){
  134 + global $default;
  135 + if ($this->iId >= 0) {
  136 + $sql = $default->db;
  137 + $sQuery = "UPDATE " . $default->owl_archive_settings_table . " SET " .
  138 + "document_id = " . $this->iDocumentID . ", " .
  139 + "expiration_date = '" . $this->dExpirationDate . "', " .
  140 + "utilisation_theshold = $this->iUtilisationThreshold)" .
  141 + "WHERE id = $this->iId";
  142 + $result = $sql->query($sQuery);
  143 + if ($result) {
  144 + return true;
  145 + }
  146 + return false;
  147 + }
  148 + return false;
  149 + }
  150 +
  151 + /**
  152 + * Delete the current archive settings from the database. Set the primary key to -1
  153 + * on successful deletion
  154 + *
  155 + * @return boolean true and reset id to -1 on successful deletion, false otherwise
  156 + */
  157 + function delete() {
  158 + global $default;
  159 + if ($this->iId >= 0) {
  160 + $sql = $default->db;
  161 + $result = $sql->query("DELETE FROM " . $default->owl_archive_settings_table . " WHERE id = $this->iId");
  162 + if ($result) {
  163 + $this->iId = -1;
  164 + return true;
  165 + }
  166 + return false;
  167 + }
  168 + return false;
  169 + }
  170 +
  171 + /**
  172 + * Static function. Given a news item primary key will create
  173 + * a ArchiveSettings object and populate it with the corresponding
  174 + * database values
  175 + *
  176 + * @return ArchiveSettings populated ArchiveSettings object on success, false otherwise
  177 + */
  178 + function & get($iArchiveSettingsID) {
  179 + global $default;
  180 + $sql = $default->db;
  181 + $sql->query("SELECT * FROM $default->owl_archive_settings_table WHERE id = $iArchiveSettingsID");
  182 + if ($sql->next_record()) {
  183 + $oArchiveSettings = & new ArchiveSettings($sql->f("document_id"), $sql->f("expiration_date"), $sql->f("utilisation_threshold"));
  184 + $oArchiveSettings->iId = $iArchiveSettingsID;
  185 + return $oArchiveSettings;
  186 + }
  187 + return false;
  188 + }
  189 +
  190 + /**
  191 + * Static function
  192 + * Get a list of ArchiveSettings objects
  193 + *
  194 + * @param String Where clause (optional)
  195 + * @return Array array of ArchiveSettings objects, false otherwise
  196 + */
  197 + function getList($sWhereClause = null) {
  198 + global $default;
  199 + $aArchiveSettingsArray = array();
  200 + $sql = $default->db;
  201 + $result = $sql->query("SELECT * FROM " . $default->owl_archive_settings_table . (isset($sWhereClause) ? " WHERE " . $sWhereClause : ""));
  202 + if ($result) {
  203 + $iCount = 0;
  204 + while ($sql->next_record()) {
  205 + $oArchiveSettings = & ArchiveSettings::get($sql->f("id"));
  206 + $aArchiveSettingsArray[$iCount++] = $oArchiveSettings;
  207 + }
  208 + return $aArchiveSettingsArray;
  209 + }
  210 + return false;
  211 + }
  212 +}
0 \ No newline at end of file 213 \ No newline at end of file
tests/documentmanagement/ArchiveSettings.php 0 → 100644
  1 +<?php
  2 +
  3 +require_once("../../config/dmsDefaults.php");
  4 +
  5 +/**
  6 + * Contains unit tests for class ArchiveSettings in /lib/documentmanagement/ArchiveSettings.inc
  7 + *
  8 + * Tests are:
  9 + * o creation of archive settings object
  10 + * o setting/getting of values
  11 + * o storing of object
  12 + * o updating of object
  13 + * o deletion of object
  14 + * o retrieval of object by primary key
  15 + * o retrieval of an array of objects
  16 + * @package tests.documentmanagement
  17 + */
  18 +
  19 +require_once("$default->fileSystemRoot/lib/documentmanagement/ArchiveSettings.inc");
  20 +echo "<pre>";
  21 +//test creation of archive settings
  22 +echo "<b>Testing archive settings creation</b><br>";
  23 +$oArchiveSettings = & new ArchiveSettings(6, "2003-06-14", -1);
  24 +if (isset($oArchiveSettings)) {
  25 + echo "Passed archive settings creation test<br><br>";
  26 +
  27 + echo "<b>Testing setting and getting of values</b><br>";
  28 +
  29 + echo "Current document ID: " . $oArchiveSettings->getDocumentID() . "<br>";
  30 + echo "Setting document ID to: 5<br>";
  31 + $oArchiveSettings->setDocumentID(5);
  32 + echo "New document id: " . $oArchiveSettings->getDocumentID() . "<br><br>";
  33 +
  34 + echo "Current expiration date: " . $oArchiveSettings->getExpirationDate() . "<br>";
  35 + echo "Setting expiration date to: '2003-07-14'<br>";
  36 + $oArchiveSettings->setExpirationDate("2003-07-14");
  37 + echo "New expiration date: " . $oArchiveSettings->getExpirationDate() . "<br><br>";
  38 +
  39 + echo "Current utilisation threshold : " . $oArchiveSettings->getUtilisationThreshold() . "<br>";
  40 + echo "Setting expiration date to: '2003-07-14'<br>";
  41 + $oArchiveSettings->setExpirationDate("2003-07-14");
  42 + echo "New expiration date: " . $oArchiveSettings->getExpirationDate() . "<br><br>";
  43 +
  44 + echo "<b>Testing archive settings storage</b><br>";
  45 + if ($oArchiveSettings->create()) {
  46 + echo "Passed archive settings storage test<br><br>";
  47 +
  48 + echo "<b>Testing archive settings retrieval</b><br>";
  49 + $oNewArchiveSettings = ArchiveSettings::get(1);
  50 + if ($oNewArchiveSettings) {
  51 + echo "Passed archive settings retrieval test:" . arrayToString($oNewArchiveSettings) . "<br>";
  52 + } else {
  53 + echo "Failed archive settings retrieval test.<br>";
  54 + }
  55 +
  56 + echo "<b>Testing archive settings array retrieval</b><br>";
  57 + $aArchiveSettings = ArchiveSettings::getList();
  58 + echo "Archive Settings array=" . arrayToString($aArchiveSettings) . "<br><br>";
  59 +
  60 + echo "<b>Testing archive settings deletion</b><br>";
  61 + if ($oArchiveSettings->delete()) {
  62 + echo "Passed archive settings deletion test<br>";
  63 + } else {
  64 + echo "Failed archive settings deletion test";
  65 + }
  66 + } else {
  67 + echo "Failed archive settings storage test<br>";
  68 + echo "Tests NOT run: (a)archive settings deletion<br>";
  69 + }
  70 +} else {
  71 + echo "Failed archive settings creation test<br>";
  72 + echo "Tests NOT run: (a)getting and setting (b)archive settings storage (c)archive settings deletion<br>";
  73 +}
  74 +echo "</pre>";
  75 +
  76 +?>