Criteria added for abstract database programming
[shipsimu.git] / inc / classes / main / database / wrapper / class_UserDatabaseWrapper.php
index f9caae5220caacc2d5007db0171d901cfc43fdff..c8eb465c2b3e313b29cb753dae071d0b832708f3 100644 (file)
@@ -28,9 +28,15 @@ class UserDatabaseWrapper extends BaseDatabaseWrapper {
         */
        private $cacheInstance = null;
 
-       // Constants
+       // Constants for exceptions
        const EXCEPTION_CLIENT_USERNAME_NOT_FOUND = 0xe00;
 
+       // Constants for database columns
+       const DB_COLUMN_USERNAME = "username";
+
+       // Constants for database table names
+       const DB_TABLE_USER = "user";
+
        /**
         * Protected constructor
         *
@@ -45,31 +51,21 @@ class UserDatabaseWrapper extends BaseDatabaseWrapper {
 
                // Create unique ID number
                $this->createUniqueID();
-
-               // Initialize the cache instance
-               $this->initCacheInstance();
        }
 
        /**
         * Creates an instance of this database wrapper by a provided user class
         *
-        * @param       $userInstance           An instance of a user class
         * @return      $wrapperInstance        An instance of the created wrapper class
         * @throws      WrapperUserNameNotFoundException        If the supplied username
         *                                                                                              does not exist
         */
-       public final static function createUserDatabaseWrapper (ManageableUser $userInstance) {
+       public final static function createUserDatabaseWrapper () {
                // Get a new instance
                $wrapperInstance = new UserDatabaseWrapper();
 
-               // Does the username exists?
-               if (!$wrapperInstance->ifUserNameExists($userInstance->getUserName())) {
-                       // Throw an exception here
-                       throw new WrapperUserNameNotFoundException (array($wrapperInstance, $userInstance), self::EXCEPTION_CLIENT_USERNAME_NOT_FOUND);
-               }
-
-               // The user exists
-               $wrapperInstance->partialStub("Add loading of full user details");
+               // Initialize the cache instance
+               $wrapperInstance->initCacheInstance();
 
                // Return the instance
                return $wrapperInstance;
@@ -80,48 +76,42 @@ class UserDatabaseWrapper extends BaseDatabaseWrapper {
         *
         * @return      void
         */
-       private function initCacheInstance () {
+       protected function initCacheInstance () {
                // Set the new instance
                $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache();
        }
 
        /**
-        * Checks wether the given username is already used
+        * Do a "select" query on the user table with the given search criteria and
+        * store it in cache for later usage
         *
-        * @param       $userName       The username we shall look up
-        * @return      $exists         Wether the username exists
+        * @param       $criteriaInstance       An instance of a Criteria class
+        * @return      $resultInstance         An instance of a database result class
         */
-       public function ifUserNameExists ($userName) {
-               // By default no entry exist
-               $exists = false;
-
-               // Does this entry exist?
-               if ($this->cacheInstance->offsetExists($userName)) {
-                       // Then we have a user!
-                       $exists = true;
+       public function doSelectByCriteria (Criteria $criteriaInstance) {
+               // First get a key suitable for our cache and extend it with this class name
+               $cacheKey = sprintf("%s@%s",
+                       $this->__toString(),
+                       $criteriaInstance->getCacheKey()
+               );
+
+               // Does this key exists in cache?
+               if ($this->cacheInstance->offsetExists($cacheKey)) {
+                       // Then use this result
+                       $result = $cacheInstance->offsetGet($cacheKey);
                } else {
-                       // Create a search criteria
-                       $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria');
-
-                       // Add the username as a criteria and set limit to one entry
-                       $criteriaInstance->add(self::DB_USERNAME, $userName);
-                       $criteriaInstance->setLimit(1);
-
-                       // Get a search result
-                       $result = $this->doSelectByCriteria($criteriaInstance);
+                       // Now it's time to ask the database layer for this select statement
+                       $result = $this->getDatabaseInstance()->doSelectByTableCriteria(self::DB_TABLE_USER, $criteriaInstance);
 
-                       // Search for it
-                       if ($result->next()) {
-                               // Get the result
-                               $this->cacheInstance->add($userName, $result->getResultArray());
-
-                               // Entry found, so all is fine
-                               $exists = true;
-                       }
+                       // Cache the result
+                       $this->cacheInstance->offsetSet($cacheKey, $result);
                }
 
-               // Return the result
-               return $exists;
+               // Create an instance of a DatabaseResult class with the given result
+               $resultInstance = DatabaseResult::createDatabaseResult($result);
+
+               // And return the instance
+               return $resultInstance;
        }
 }