f1caa49379c4c604261fbd39ab84486b1ab3b2cb
[shipsimu.git] / inc / classes / main / database / wrapper / class_UserDatabaseWrapper.php
1 <?php
2 /**
3  * A database wrapper for the User class
4  *
5  * @see                 DatabaseFrontendInterface - An interface for database frontends (front-end to the application)
6  * @author              Roland Haeder <webmaster@ship-simu.org>
7  * @version             0.0.0
8  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
9  * @license             GNU GPL 3.0 or any newer version
10  * @link                http://www.ship-simu.org
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  */
25 class UserDatabaseWrapper extends BaseDatabaseWrapper {
26         /**
27          * Cache instance
28          */
29         private $cacheInstance = null;
30
31         // Constants for exceptions
32         const EXCEPTION_CLIENT_USERNAME_NOT_FOUND = 0x800;
33
34         // Constants for database columns
35         const DB_COLUMN_USERNAME = "username";
36         const DB_COLUMN_EMAIL    = "email";
37
38         // Constants for database table names
39         const DB_TABLE_USER = "user";
40
41         /**
42          * Protected constructor
43          *
44          * @return      void
45          */
46         protected function __construct() {
47                 // Call parent constructor
48                 parent::__construct(__CLASS__);
49
50                 // Set part description
51                 $this->setObjectDescription("Database wrapper for user objects");
52
53                 // Create unique ID number
54                 $this->generateUniqueId();
55         }
56
57         /**
58          * Creates an instance of this database wrapper by a provided user class
59          *
60          * @return      $wrapperInstance        An instance of the created wrapper class
61          * @throws      WrapperUserNameNotFoundException        If the supplied username
62          *                                                                                              does not exist
63          */
64         public final static function createUserDatabaseWrapper () {
65                 // Get a new instance
66                 $wrapperInstance = new UserDatabaseWrapper();
67
68                 // Initialize the cache instance
69                 $wrapperInstance->initCacheInstance();
70
71                 // Return the instance
72                 return $wrapperInstance;
73         }
74
75         /**
76          * Initializes the cache instance with a new object
77          *
78          * @return      void
79          */
80         protected function initCacheInstance () {
81                 // Set the new instance
82                 $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache();
83         }
84
85         /**
86          * Do a "select" query on the user table with the given search criteria and
87          * store it in cache for later usage
88          *
89          * @param       $criteriaInstance       An instance of a Criteria class
90          * @return      $resultInstance         An instance of a database result class
91          */
92         public function doSelectByCriteria (Criteria $criteriaInstance) {
93                 // First get a key suitable for our cache and extend it with this class name
94                 $cacheKey = sprintf("%s@%s",
95                         $this->__toString(),
96                         $criteriaInstance->getCacheKey()
97                 );
98
99                 // Does this key exists in cache?
100                 if ($this->cacheInstance->offsetExists($cacheKey)) {
101                         // Then use this result
102                         $result = $cacheInstance->offsetGet($cacheKey);
103                 } else {
104                         // Now it's time to ask the database layer for this select statement
105                         $result = $this->getDatabaseInstance()->doSelectByTableCriteria(self::DB_TABLE_USER, $criteriaInstance);
106
107                         // Cache the result if not null
108                         if (!is_null($result)) {
109                                 // A valid result has returned from the database layer
110                                 $this->cacheInstance->offsetSet($cacheKey, $result);
111                         } else {
112                                 // This invalid result must be wrapped
113                                 $result = array(
114                                         'status'                => "invalid",
115                                         'exception'             => $this->getDatabaseInstance()->getLastException()
116                                 );
117                         }
118                 }
119
120                 // Create an instance of a DatabaseResult class with the given result
121                 $resultInstance = DatabaseResult::createDatabaseResult($result);
122
123                 // And return the instance
124                 return $resultInstance;
125         }
126
127         /**
128          * Handles inserting the registration data from a registration instance into the database
129          *
130          * @param       $registrationInstance   An instance of a registration class
131          * @return      void
132          */
133         public function insertRegistrationObject (UserRegister $registrationInstance) {
134                 // Generate a data set for the request
135                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class');
136                 $dataSetInstance->setTableName(self::DB_TABLE_USER);
137
138                 // Add registration elements to the dataset
139                 $registrationInstance->addElementsToDataSet($dataSetInstance);
140
141                 // "Insert" this request instance completely into the database
142                 $this->getDatabaseInstance()->queryInsertDataSet($dataSetInstance);
143         }
144
145         /**
146          * Updates an user database entry with given result
147          *
148          * @param       $resultInstance         An instance of a Updateable database result
149          * @return      void
150          */
151         public function doUpdateByResult (UpdateableResult $resultInstance) {
152                 // Generate a data set object
153                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class');
154                 $dataSetInstance->setTableName(self::DB_TABLE_USER);
155
156                 // Add all update criteria to the database set
157                 $resultInstance->addElementsToDataSet($dataSetInstance);
158
159                 // Add seach criteria
160                 $dataSetInstance->setSearchInstance($resultInstance->getSearchInstance());
161
162                 // Add the primary key
163                 $dataSetInstance->setUniqueKey('username');
164
165                 // "Update" this request with the database
166                 $this->getDatabaseInstance()->queryUpdateDataSet($dataSetInstance);
167         }
168 }
169
170 // [EOF]
171 ?>