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\Node;
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;
17 * A database frontend for node informations
19 * @author Roland Haeder <webmaster@shipsimu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2018 Hub Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.shipsimu.org
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.
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.
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/>.
38 class NodeInformationDatabaseFrontend extends BaseHubDatabaseFrontend implements NodeInformationFrontend, Registerable {
39 // Constants for database table names
40 const DB_TABLE_NODE_INFORMATION = 'node_data';
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';
53 * Protected constructor
57 protected function __construct () {
58 // Call parent constructor
59 parent::__construct(__CLASS__);
63 * Creates an instance of this database frontend by a provided user class
65 * @return $frontendInstance An instance of the created frontend class
67 public static final function createNodeInformationDatabaseFrontend () {
69 $frontendInstance = new NodeInformationDatabaseFrontend();
71 // Set (primary!) table name
72 $frontendInstance->setTableName(self::DB_TABLE_NODE_INFORMATION);
74 // Return the instance
75 return $frontendInstance;
79 * Tries to find a node instance by it's nodeId field
81 * @return $resultInstance An instance of a SearchableResult class
83 public function findFirstNodeData () {
85 if (!isset($GLOBALS[__METHOD__])) {
86 // Now get a search criteria instance
87 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
89 // Search for the node number one which is hard-coded the default
90 $searchInstance->addCriteria(NodeInformationDatabaseFrontend::DB_COLUMN_NODE_NR , 1);
91 $searchInstance->addCriteria(NodeInformationDatabaseFrontend::DB_COLUMN_NODE_MODE, FrameworkBootstrap::getRequestInstance()->getRequestElement('mode'));
92 $searchInstance->setLimit(1);
95 $resultInstance = $this->doSelectByCriteria($searchInstance);
98 $GLOBALS[__METHOD__] = ($resultInstance->next() ? $resultInstance : NULL);
102 return $GLOBALS[__METHOD__];
106 * 'Registers' a new node id along with data provided in the node instance.
107 * This may sound confusing but avoids double code very nicely...
109 * @param $nodeInstance An instance of a Node class
112 public function registerNodeId (Node $nodeInstance) {
113 // Get a dataset instance
114 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_INFORMATION));
116 // Set the primary key
117 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
119 // Add registration elements to the dataset
120 $nodeInstance->addElementsToDataSet($dataSetInstance);
122 // "Insert" this dataset instance completely into the database
123 $this->queryInsertDataSet($dataSetInstance);
127 * 'Registers' a new session id along with data provided in the node instance.
128 * This may sound confusing but avoids double code very nicely...
130 * @param $nodeInstance An instance of a Node class
131 * @param $searchInstance An instance of a LocalSearchCriteria class
134 public function registerSessionId (Node $nodeInstance, LocalSearchCriteria $searchInstance) {
135 // Get a dataset instance
136 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_INFORMATION));
138 // Set search instance
139 $dataSetInstance->setSearchInstance($searchInstance);
141 // Set the primary key
142 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
144 // Add registration elements to the dataset
145 $nodeInstance->addElementsToDataSet($dataSetInstance);
147 // Update database record
148 $this->queryUpdateDataSet($dataSetInstance);
152 * 'Registers' a private key along with data provided in the node instance.
153 * This may sound confusing but avoids double code very nicely...
155 * @param $nodeInstance An instance of a Node class
156 * @param $searchInstance An instance of a LocalSearchCriteria class
159 public function registerPrivateKey (Node $nodeInstance, LocalSearchCriteria $searchInstance) {
160 // Get a dataset instance
161 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_INFORMATION));
163 // Set the primary key
164 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
166 // Set search instance
167 $dataSetInstance->setSearchInstance($searchInstance);
169 // Add registration elements to the dataset
170 $nodeInstance->addElementsToDataSet($dataSetInstance);
172 // Update database record
173 $this->queryUpdateDataSet($dataSetInstance);
177 * Removes non-public data from given array.
179 * @param $data An array with possible non-public data that needs to be removed.
180 * @return $data A cleaned up array with only public data.
182 public function removeNonPublicDataFromArray(array $data) {
183 // Currently call only inner method
184 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NODE-FRONTEND: Calling parent::removeNonPublicDataFromArray(data) ...');
185 $data = parent::removeNonPublicDataFromArray($data);
187 // Return cleaned data