3 namespace Org\Shipsimu\Hub\Database\Frontend\Node\Information;
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\BaseHubNode;
9 use Org\Shipsimu\Hub\Node\Node;
11 // Import framework stuff
12 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
13 use Org\Mxchange\CoreFramework\Criteria\Local\LocalSearchCriteria;
14 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
15 use Org\Mxchange\CoreFramework\Registry\Registerable;
18 * A database frontend for node informations
20 * @author Roland Haeder <webmaster@shipsimu.org>
22 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2018 Hub Developer Team
23 * @license GNU GPL 3.0 or any newer version
24 * @link http://www.shipsimu.org
26 * This program is free software: you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation, either version 3 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 class NodeInformationDatabaseFrontend extends BaseHubDatabaseFrontend implements NodeInformationFrontend, Registerable {
40 // Constants for database table names
41 const DB_TABLE_NODE_INFORMATION = 'node_data';
43 // Constants for database column names
44 const DB_COLUMN_NODE_NR = 'node_nr';
45 const DB_COLUMN_NODE_ID = 'node_id';
46 const DB_COLUMN_SESSION_ID = 'session_id';
47 const DB_COLUMN_PRIVATE_KEY = 'private_key';
48 const DB_COLUMN_PRIVATE_KEY_HASH = 'private_key_hash';
49 const DB_COLUMN_NODE_MODE = 'node_mode';
50 const DB_COLUMN_INTERNAL_UNL = 'internal_unl';
51 const DB_COLUMN_EXTERNAL_UNL = 'external_unl';
54 * Protected constructor
58 protected function __construct () {
59 // Call parent constructor
60 parent::__construct(__CLASS__);
64 * Creates an instance of this database frontend by a provided user class
66 * @return $frontendInstance An instance of the created frontend class
68 public static final function createNodeInformationDatabaseFrontend () {
70 $frontendInstance = new NodeInformationDatabaseFrontend();
72 // Set (primary!) table name
73 $frontendInstance->setTableName(self::DB_TABLE_NODE_INFORMATION);
75 // Return the instance
76 return $frontendInstance;
80 * Checks whether there is an entry for given node instance
82 * @param $nodeInstance An instance of a Node class
83 * @return $isFound Whether a node id has been found for this node
85 public function ifNodeDataIsFound (Node $nodeInstance) {
87 if (!isset($GLOBALS[__METHOD__])) {
88 // Now get a search criteria instance
89 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
91 // Search for the node number one which is hard-coded the default
92 $searchInstance->addCriteria(NodeInformationDatabaseFrontend::DB_COLUMN_NODE_NR , 1);
93 $searchInstance->addCriteria(NodeInformationDatabaseFrontend::DB_COLUMN_NODE_MODE, FrameworkBootstrap::getRequestInstance()->getRequestElement('mode'));
94 $searchInstance->setLimit(1);
97 $resultInstance = $this->doSelectByCriteria($searchInstance);
100 $GLOBALS[__METHOD__] = $resultInstance->next();
104 return $GLOBALS[__METHOD__];
108 * 'Registers' a new node id along with data provided in the node instance.
109 * This may sound confusing but avoids double code very nicely...
111 * @param $nodeInstance A node instance
114 public function registerNodeId (BaseHubNode $nodeInstance) {
115 // Get a dataset instance
116 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_INFORMATION));
118 // Set the primary key
119 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
121 // Add registration elements to the dataset
122 $nodeInstance->addElementsToDataSet($dataSetInstance);
124 // "Insert" this dataset instance completely into the database
125 $this->queryInsertDataSet($dataSetInstance);
129 * 'Registers' a new session id along with data provided in the node instance.
130 * This may sound confusing but avoids double code very nicely...
132 * @param $nodeInstance An instance of a BaseHubNode class
133 * @param $searchInstance An instance of a LocalSearchCriteria class
136 public function registerSessionId (BaseHubNode $nodeInstance, LocalSearchCriteria $searchInstance) {
137 // Get a dataset instance
138 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_INFORMATION));
140 // Set search instance
141 $dataSetInstance->setSearchInstance($searchInstance);
143 // Set the primary key
144 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
146 // Add registration elements to the dataset
147 $nodeInstance->addElementsToDataSet($dataSetInstance);
149 // Update database record
150 $this->queryUpdateDataSet($dataSetInstance);
154 * 'Registers' a private key along with data provided in the node instance.
155 * This may sound confusing but avoids double code very nicely...
157 * @param $nodeInstance An instance of a BaseHubNode class
158 * @param $searchInstance An instance of a LocalSearchCriteria class
161 public function registerPrivateKey (BaseHubNode $nodeInstance, LocalSearchCriteria $searchInstance) {
162 // Get a dataset instance
163 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_INFORMATION));
165 // Set the primary key
166 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
168 // Set search instance
169 $dataSetInstance->setSearchInstance($searchInstance);
171 // Add registration elements to the dataset
172 $nodeInstance->addElementsToDataSet($dataSetInstance);
174 // Update database record
175 $this->queryUpdateDataSet($dataSetInstance);
179 * Removes non-public data from given array.
181 * @param $data An array with possible non-public data that needs to be removed.
182 * @return $data A cleaned up array with only public data.
184 public function removeNonPublicDataFromArray(array $data) {
185 // Currently call only inner method
186 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-WRAPPER: Calling parent::removeNonPublicDataFromArray(data) ...');
187 $data = parent::removeNonPublicDataFromArray($data);
189 // Return cleaned data