]> git.mxchange.org Git - hub.git/blob
ba9e0e37a3d35a70211905e640107b7165a999c5
[hub.git] /
1 <?php
2 // Own namespace
3 namespace Org\Shipsimu\Hub\Database\Frontend\Node\Information;
4
5 // Import application-specific stuff
6 use Org\Shipsimu\Hub\Database\Frontend\BaseHubDatabaseFrontend;
7 use Org\Shipsimu\Hub\Database\Frontend\Node\NodeInformationFrontend;
8 use Org\Shipsimu\Hub\Node\Node;
9
10 // Import framework stuff
11 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
12 use Org\Mxchange\CoreFramework\Criteria\Local\LocalSearchCriteria;
13 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
14 use Org\Mxchange\CoreFramework\Registry\Registerable;
15
16 /**
17  * A database frontend for node informations
18  *
19  * @author              Roland Haeder <webmaster@shipsimu.org>
20  * @version             0.0.0
21  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Hub Developer Team
22  * @license             GNU GPL 3.0 or any newer version
23  * @link                http://www.shipsimu.org
24  *
25  * This program is free software: you can redistribute it and/or modify
26  * it under the terms of the GNU General Public License as published by
27  * the Free Software Foundation, either version 3 of the License, or
28  * (at your option) any later version.
29  *
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  * GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with this program. If not, see <http://www.gnu.org/licenses/>.
37  */
38 class NodeInformationDatabaseFrontend extends BaseHubDatabaseFrontend implements NodeInformationFrontend, Registerable {
39         // Constants for database table names
40         const DB_TABLE_NODE_INFORMATION = 'node_data';
41
42         // Constants for database column names
43         const DB_COLUMN_NODE_NR          = 'node_nr';
44         const DB_COLUMN_NODE_ID          = 'node_id';
45         const DB_COLUMN_SESSION_ID       = 'session_id';
46         const DB_COLUMN_PRIVATE_KEY      = 'private_key';
47         const DB_COLUMN_PRIVATE_KEY_HASH = 'private_key_hash';
48         const DB_COLUMN_NODE_MODE        = 'node_mode';
49         const DB_COLUMN_INTERNAL_UNL     = 'internal_unl';
50         const DB_COLUMN_EXTERNAL_UNL     = 'external_unl';
51
52         /**
53          * Protected constructor
54          *
55          * @return      void
56          */
57         protected function __construct () {
58                 // Call parent constructor
59                 parent::__construct(__CLASS__);
60         }
61
62         /**
63          * Creates an instance of this database frontend by a provided user class
64          *
65          * @return      $frontendInstance       An instance of the created frontend class
66          */
67         public static final function createNodeInformationDatabaseFrontend () {
68                 // Get a new instance
69                 $frontendInstance = new NodeInformationDatabaseFrontend();
70
71                 // Set (primary!) table name
72                 $frontendInstance->setTableName(self::DB_TABLE_NODE_INFORMATION);
73
74                 // Return the instance
75                 return $frontendInstance;
76         }
77
78         /**
79          * Tries to find a node instance by it's nodeId field
80          *
81          * @return      $resultInstance         An instance of a SearchableResult class
82          */
83         public function findFirstNodeData () {
84                 // Is there cache?
85                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-FRONTEND: CALLED!');
86                 if (!isset($GLOBALS[__METHOD__])) {
87                         // Now get a search criteria instance
88                         $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
89
90                         // Search for the node number one which is hard-coded the default
91                         $searchInstance->addCriteria(NodeInformationDatabaseFrontend::DB_COLUMN_NODE_NR  , 1);
92                         $searchInstance->addCriteria(NodeInformationDatabaseFrontend::DB_COLUMN_NODE_MODE, FrameworkBootstrap::getRequestInstance()->getRequestElement('mode'));
93                         $searchInstance->setLimit(1);
94
95                         // Get a result back
96                         $resultInstance = $this->doSelectByCriteria($searchInstance);
97
98                         // Is it valid?
99                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-FRONTEND: resultInstance->valid()=%d', intval($resultInstance->valid())));
100                         $GLOBALS[__METHOD__] = ($resultInstance->valid() ? $resultInstance : NULL);
101                 }
102
103                 // Return it
104                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-FRONTEND: resultInstance[]=%s - EXIT!', gettype($GLOBALS[__METHOD__])));
105                 return $GLOBALS[__METHOD__];
106         }
107
108         /**
109          * 'Registers' a new node id along with data provided in the node instance.
110          * This may sound confusing but avoids double code very nicely...
111          *
112          * @param       $nodeInstance   An instance of a Node class
113          * @return      void
114          */
115         public function registerNodeId (Node $nodeInstance) {
116                 // Get a dataset instance
117                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_INFORMATION));
118
119                 // Set the primary key
120                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
121
122                 // Add registration elements to the dataset
123                 $nodeInstance->addElementsToDataSet($dataSetInstance);
124
125                 // "Insert" this dataset instance completely into the database
126                 $this->queryInsertDataSet($dataSetInstance);
127         }
128
129         /**
130          * 'Registers' a new session id along with data provided in the node instance.
131          * This may sound confusing but avoids double code very nicely...
132          *
133          * @param       $nodeInstance           An instance of a Node class
134          * @param       $searchInstance         An instance of a LocalSearchCriteria class
135          * @return      void
136          */
137         public function registerSessionId (Node $nodeInstance, LocalSearchCriteria $searchInstance) {
138                 // Get a dataset instance
139                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_INFORMATION));
140
141                 // Set search instance
142                 $dataSetInstance->setSearchInstance($searchInstance);
143
144                 // Set the primary key
145                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
146
147                 // Add registration elements to the dataset
148                 $nodeInstance->addElementsToDataSet($dataSetInstance);
149
150                 // Update database record
151                 $this->queryUpdateDataSet($dataSetInstance);
152         }
153
154         /**
155          * 'Registers' a private key along with data provided in the node instance.
156          * This may sound confusing but avoids double code very nicely...
157          *
158          * @param       $nodeInstance           An instance of a Node class
159          * @param       $searchInstance         An instance of a LocalSearchCriteria class
160          * @return      void
161          */
162         public function registerPrivateKey (Node $nodeInstance, LocalSearchCriteria $searchInstance) {
163                 // Get a dataset instance
164                 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_INFORMATION));
165
166                 // Set the primary key
167                 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
168
169                 // Set search instance
170                 $dataSetInstance->setSearchInstance($searchInstance);
171
172                 // Add registration elements to the dataset
173                 $nodeInstance->addElementsToDataSet($dataSetInstance);
174
175                 // Update database record
176                 $this->queryUpdateDataSet($dataSetInstance);
177         }
178
179         /**
180          * Removes non-public data from given array.
181          *
182          * @param       $data   An array with possible non-public data that needs to be removed.
183          * @return      $data   A cleaned up array with only public data.
184          */
185         public function removeNonPublicDataFromArray(array $data) {
186                 // Currently call only inner method
187                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-FRONTEND: Calling parent::removeNonPublicDataFromArray(data) ...');
188                 $data = parent::removeNonPublicDataFromArray($data);
189
190                 // Return cleaned data
191                 return $data;
192         }
193
194 }