]> git.mxchange.org Git - hub.git/blob - application/hub/main/database/frontend/node/class_NodeInformationDatabaseWrapper.php
Updated 'core'.
[hub.git] / application / hub / main / database / frontend / node / class_NodeInformationDatabaseWrapper.php
1 <?php
2 /**
3  * A database wrapper for node informations
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.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 NodeInformationDatabaseWrapper extends BaseDatabaseWrapper implements NodeInformationWrapper, Registerable {
25         // Constants for database table names
26         const DB_TABLE_NODE_INFORMATION = 'node_data';
27
28         // Constants for database column names
29         const DB_COLUMN_NODE_NR          = 'node_nr';
30         const DB_COLUMN_NODE_ID          = 'node_id';
31         const DB_COLUMN_SESSION_ID       = 'session_id';
32         const DB_COLUMN_PRIVATE_KEY      = 'private_key';
33         const DB_COLUMN_PRIVATE_KEY_HASH = 'private_key_hash';
34         const DB_COLUMN_NODE_MODE        = 'node_mode';
35         const DB_COLUMN_INTERNAL_UNL     = 'internal_unl';
36         const DB_COLUMN_EXTERNAL_UNL     = 'external_unl';
37
38         /**
39          * Protected constructor
40          *
41          * @return      void
42          */
43         protected function __construct () {
44                 // Call parent constructor
45                 parent::__construct(__CLASS__);
46         }
47
48         /**
49          * Creates an instance of this database wrapper by a provided user class
50          *
51          * @return      $wrapperInstance        An instance of the created wrapper class
52          */
53         public static final function createNodeInformationDatabaseWrapper () {
54                 // Get a new instance
55                 $wrapperInstance = new NodeInformationDatabaseWrapper();
56
57                 // Set (primary!) table name
58                 $wrapperInstance->setTableName(self::DB_TABLE_NODE_INFORMATION);
59
60                 // Return the instance
61                 return $wrapperInstance;
62         }
63
64         /**
65          * Checks whether there is an entry for given node instance
66          *
67          * @param       $nodeInstance   An instance of a NodeHelper class
68          * @return      $isFound                Whether a node id has been found for this node
69          */
70         public function ifNodeDataIsFound (NodeHelper $nodeInstance) {
71                 // Is there cache?
72                 if (!isset($GLOBALS[__METHOD__])) {
73                         // Now get a search criteria instance
74                         $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
75
76                         // Search for the node number one which is hard-coded the default
77                         $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_NR  , 1);
78                         $searchInstance->addCriteria(NodeInformationDatabaseWrapper::DB_COLUMN_NODE_MODE, $nodeInstance->getRequestInstance()->getRequestElement('mode'));
79                         $searchInstance->setLimit(1);
80
81                         // Get a result back
82                         $resultInstance = $this->doSelectByCriteria($searchInstance);
83
84                         // Set result instance in node instance
85                         $nodeInstance->setResultInstance($resultInstance);
86
87                         // Is it valid?
88                         $GLOBALS[__METHOD__] = $resultInstance->next();
89                 } // END - if
90
91                 // Return it
92                 return $GLOBALS[__METHOD__];
93         }
94
95         /**
96          * 'Registers' a new node id along with data provided in the node instance.
97          * This may sound confusing but avoids double code very nicely...
98          *
99          * @param       $nodeInstance           A node instance
100          * @param       $requestInstance        An instance of a Requestable class
101          * @return      void
102          */
103         public function registerNodeId (BaseHubNode $nodeInstance, Requestable $requestInstance) {
104                 // Get a dataset instance
105                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_INFORMATION));
106
107                 // Set the primary key
108                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
109
110                 // Add registration elements to the dataset
111                 $nodeInstance->addElementsToDataSet($dataSetInstance, $requestInstance);
112
113                 // "Insert" this dataset instance completely into the database
114                 $this->queryInsertDataSet($dataSetInstance);
115         }
116
117         /**
118          * 'Registers' a new session id along with data provided in the node instance.
119          * This may sound confusing but avoids double code very nicely...
120          *
121          * @param       $nodeInstance           An instance of a BaseHubNode class
122          * @param       $requestInstance        An instance of a Requestable class
123          * @param       $searchInstance         An instance of a LocalSearchCriteria class
124          * @return      void
125          */
126         public function registerSessionId (BaseHubNode $nodeInstance, Requestable $requestInstance, LocalSearchCriteria $searchInstance) {
127                 // Get a dataset instance
128                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_INFORMATION));
129
130                 // Set search instance
131                 $dataSetInstance->setSearchInstance($searchInstance);
132
133                 // Set the primary key
134                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
135
136                 // Add registration elements to the dataset
137                 $nodeInstance->addElementsToDataSet($dataSetInstance, $requestInstance);
138
139                 // Update database record
140                 $this->queryUpdateDataSet($dataSetInstance);
141         }
142
143         /**
144          * 'Registers' a private key along with data provided in the node instance.
145          * This may sound confusing but avoids double code very nicely...
146          *
147          * @param       $nodeInstance           An instance of a BaseHubNode class
148          * @param       $requestInstance        An instance of a Requestable class
149          * @param       $searchInstance         An instance of a LocalSearchCriteria class
150          * @return      void
151          */
152         public function registerPrivateKey (BaseHubNode $nodeInstance, Requestable $requestInstance, LocalSearchCriteria $searchInstance) {
153                 // Get a dataset instance
154                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_INFORMATION));
155
156                 // Set the primary key
157                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
158
159                 // Set search instance
160                 $dataSetInstance->setSearchInstance($searchInstance);
161
162                 // Add registration elements to the dataset
163                 $nodeInstance->addElementsToDataSet($dataSetInstance, $requestInstance);
164
165                 // Update database record
166                 $this->queryUpdateDataSet($dataSetInstance);
167         }
168
169         /**
170          * Removes non-public data from given array.
171          *
172          * @param       $data   An array with possible non-public data that needs to be removed.
173          * @return      $data   A cleaned up array with only public data.
174          */
175         public function removeNonPublicDataFromArray(array $data) {
176                 // Currently call only inner method
177                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NODE-WRAPPER[' . __METHOD__ . ':' . __LINE__ . ']: Calling parent::removeNonPublicDataFromArray(data) ...');
178                 $data = parent::removeNonPublicDataFromArray($data);
179
180                 // Return cleaned data
181                 return $data;
182         }
183 }
184
185 // [EOF]
186 ?>