09574b1857ab380d28f225086e940ad837aa9cac
[shipsimu.git] / inc / classes / main / database / wrapper / class_NewsDatabaseWrapper.php
1 <?php
2 /**
3  * A database wrapper for news classes
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class NewsDatabaseWrapper extends BaseDatabaseWrapper {
25         /**
26          * Cache instance
27          */
28         private $cacheInstance = null;
29
30         // Constants for exceptions
31         const EXCEPTION_CLIENT_USERNAME_NOT_FOUND = 0x160;
32
33         // Constants for database columns
34
35         // Constants for database table names
36         const DB_TABLE_NEWS = "news";
37
38         /**
39          * Protected constructor
40          *
41          * @return      void
42          */
43         protected function __construct() {
44                 // Call parent constructor
45                 parent::__construct(__CLASS__);
46
47                 // Set part description
48                 $this->setObjectDescription("Database wrapper for user objects");
49
50                 // Create unique ID number
51                 $this->generateUniqueId();
52         }
53
54         /**
55          * Creates an instance of this database wrapper by a provided user class
56          *
57          * @return      $wrapperInstance        An instance of the created wrapper class
58          */
59         public final static function createNewsDatabaseWrapper () {
60                 // Get a new instance
61                 $wrapperInstance = new NewsDatabaseWrapper();
62
63                 // Initialize the cache instance
64                 $wrapperInstance->initCacheInstance();
65
66                 // Return the instance
67                 return $wrapperInstance;
68         }
69
70         /**
71          * Initializes the cache instance with a new object
72          *
73          * @return      void
74          */
75         protected function initCacheInstance () {
76                 // Set the new instance
77                 $this->cacheInstance = CacheFactory::getFactory()->createConfiguredCache();
78         }
79
80         /**
81          * Do a "select" query on the user table with the given search criteria and
82          * store it in cache for later usage
83          *
84          * @param       $criteriaInstance       An instance of a Criteria class
85          * @return      $resultInstance         An instance of a database result class
86          */
87         public function doSelectByCriteria (Criteria $criteriaInstance) {
88                 // First get a key suitable for our cache and extend it with this class name
89                 $cacheKey = sprintf("%s@%s",
90                         $this->__toString(),
91                         $criteriaInstance->getCacheKey()
92                 );
93
94                 // Does this key exists in cache?
95                 if ($this->cacheInstance->offsetExists($cacheKey)) {
96                         // Then use this result
97                         $result = $cacheInstance->offsetGet($cacheKey);
98                 } else {
99                         // Now it's time to ask the database layer for this select statement
100                         $result = $this->getDatabaseInstance()->doSelectByTableCriteria(self::DB_TABLE_NEWS, $criteriaInstance);
101
102                         // Cache the result if not null
103                         if (!is_null($result)) {
104                                 // A valid result has returned from the database layer
105                                 $this->cacheInstance->offsetSet($cacheKey, $result);
106                         } else {
107                                 // This invalid result must be wrapped
108                                 $result = array(
109                                         'status'                => "invalid",
110                                         'exception'             => $this->getDatabaseInstance()->getLastException()
111                                 );
112                         }
113                 }
114
115                 // Create an instance of a DatabaseResult class with the given result
116                 $resultInstance = DatabaseResult::createDatabaseResult($result);
117
118                 // And return the instance
119                 return $resultInstance;
120         }
121 }
122
123 // [EOF]
124 ?>