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\Object\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 - 2020 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 /* 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');
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);
96 $resultInstance = $this->doSelectByCriteria($searchInstance);
99 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-FRONTEND: resultInstance->valid()=%d', intval($resultInstance->valid())));
100 $GLOBALS[__METHOD__] = ($resultInstance->valid() ? $resultInstance : NULL);
104 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NODE-FRONTEND: resultInstance[]=%s - EXIT!', gettype($GLOBALS[__METHOD__])));
105 return $GLOBALS[__METHOD__];
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...
112 * @param $nodeInstance An instance of a Node class
115 public function registerNodeId (Node $nodeInstance) {
116 // Get a dataset instance
117 $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_INFORMATION));
119 // Set the primary key
120 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
122 // Add registration elements to the dataset
123 $nodeInstance->addElementsToDataSet($dataSetInstance);
125 // "Insert" this dataset instance completely into the database
126 $this->queryInsertDataSet($dataSetInstance);
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...
133 * @param $nodeInstance An instance of a Node class
134 * @param $searchInstance An instance of a LocalSearchCriteria class
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));
141 // Set search instance
142 $dataSetInstance->setSearchInstance($searchInstance);
144 // Set the primary key
145 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
147 // Add registration elements to the dataset
148 $nodeInstance->addElementsToDataSet($dataSetInstance);
150 // Update database record
151 $this->queryUpdateDataSet($dataSetInstance);
155 * 'Registers' a private key along with data provided in the node instance.
156 * This may sound confusing but avoids double code very nicely...
158 * @param $nodeInstance An instance of a Node class
159 * @param $searchInstance An instance of a LocalSearchCriteria class
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));
166 // Set the primary key
167 $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
169 // Set search instance
170 $dataSetInstance->setSearchInstance($searchInstance);
172 // Add registration elements to the dataset
173 $nodeInstance->addElementsToDataSet($dataSetInstance);
175 // Update database record
176 $this->queryUpdateDataSet($dataSetInstance);
180 * Removes non-public data from given array.
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.
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);
190 // Return cleaned data