3 namespace CoreFramework\Handler\Data;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Generic\FrameworkException;
8 use CoreFramework\Handler\BaseHandler;
11 * A general data Handler
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 abstract class BaseDataHandler extends BaseHandler {
34 * Last exception instance from database layer or NULL (default)
36 private $lastException = NULL;
39 * Array with search criteria elements
41 protected $searchData = array();
44 * Array with all data XML nodes (which hold the actual data) and their values
46 protected $messageDataElements = array();
49 * Array for translating message data elements (other node's data mostly)
50 * into configuration elements.
52 protected $messageToConfig = array();
55 * Array for copying configuration entries
57 protected $configCopy = array();
60 * Protected constructor
62 * @param $className Name of the class
65 protected function __construct ($className) {
66 // Call parent constructor
67 parent::__construct($className);
70 $dhtInstance = DhtObjectFactory::createDhtInstance('node');
73 $this->setDhtInstance($dhtInstance);
77 * Getter for search data array
79 * @return $searchData Search data array
81 public final function getSearchData () {
82 return $this->searchData;
86 * Getter for last exception
88 * @return $lastException Last thrown exception
90 public final function getLastException () {
91 return $this->lastException;
95 * Setter for last exception
97 * @param $lastException Last thrown exception
100 public final function setLastException (FrameworkException $exceptionInstance = NULL) {
101 $this->lastException = $exceptionInstance;
105 * Prepares a message as answer for given message data for delivery.
107 * @param $messageData An array with all message data
108 * @param $packageInstance An instance of a Deliverable instance
111 protected function prepareAnswerMessage (array $messageData, Deliverable $packageInstance) {
113 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MESSAGE-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Going to send an answer message for ' . $this->getHandlerName() . ' ...');
115 // Get a helper instance based on this handler's name
116 $helperInstance = ObjectFactory::createObjectByConfiguredName('node_answer_' . $this->getHandlerName() . '_helper_class', array($messageData));
119 $nodeInstance = NodeObjectFactory::createNodeInstance();
121 // Load descriptor XML
122 $helperInstance->loadDescriptorXml($nodeInstance);
125 * Set missing (temporary) configuration data, mostly it needs to be
126 * copied from message data array.
128 $this->initMessageConfigurationData($messageData);
130 // Compile any configuration variables
131 $helperInstance->getTemplateInstance()->compileConfigInVariables();
133 // Deliver the package
134 $helperInstance->sendPackage($nodeInstance);
137 * Remove temporary configuration
139 $this->removeMessageConfigurationData($messageData);
142 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MESSAGE-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Answer message has been prepared.');
146 * Prepares the next message
148 * @param $messageData An array with all message data
149 * @param $packageInstance An instance of a Deliverable instance
152 protected function prepareNextMessage (array $messageData, Deliverable $packageInstance) {
154 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MESSAGE-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Going to send next message ...');
156 // Get a helper instance based on this handler's name
157 $helperInstance = ObjectFactory::createObjectByConfiguredName('node_next_' . $this->getHandlerName() . '_helper_class', array($messageData));
160 $nodeInstance = NodeObjectFactory::createNodeInstance();
162 // Load descriptor XML
163 $helperInstance->loadDescriptorXml($nodeInstance);
166 * Set missing (temporary) configuration data, mostly it needs to be
167 * copied from message data array.
169 $this->initMessageConfigurationData($messageData);
171 // Compile any configuration variables
172 $helperInstance->getTemplateInstance()->compileConfigInVariables();
174 // Deliver the package
175 $helperInstance->sendPackage($nodeInstance);
178 * Remove temporary configuration
180 $this->removeMessageConfigurationData($messageData);
183 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MESSAGE-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Next message has been prepared.');
187 * Initializes configuration data from given message data array
189 * @param $messageData An array with all message data
192 abstract protected function initMessageConfigurationData (array $messageData);
195 * Removes configuration data with given message data array from global
198 * @param $messageData An array with all message data
201 abstract protected function removeMessageConfigurationData (array $messageData);