3 namespace CoreFramework\Handler\Data;
6 * A general data Handler
8 * @author Roland Haeder <webmaster@shipsimu.org>
10 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
11 * @license GNU GPL 3.0 or any newer version
12 * @link http://www.shipsimu.org
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 abstract class BaseDataHandler extends BaseHandler {
29 * Last exception instance from database layer or NULL (default)
31 private $lastException = NULL;
34 * Array with search criteria elements
36 protected $searchData = array();
39 * Array with all data XML nodes (which hold the actual data) and their values
41 protected $messageDataElements = array();
44 * Array for translating message data elements (other node's data mostly)
45 * into configuration elements.
47 protected $messageToConfig = array();
50 * Array for copying configuration entries
52 protected $configCopy = array();
55 * Protected constructor
57 * @param $className Name of the class
60 protected function __construct ($className) {
61 // Call parent constructor
62 parent::__construct($className);
65 $dhtInstance = DhtObjectFactory::createDhtInstance('node');
68 $this->setDhtInstance($dhtInstance);
72 * Getter for search data array
74 * @return $searchData Search data array
76 public final function getSearchData () {
77 return $this->searchData;
81 * Getter for last exception
83 * @return $lastException Last thrown exception
85 public final function getLastException () {
86 return $this->lastException;
90 * Setter for last exception
92 * @param $lastException Last thrown exception
95 public final function setLastException (FrameworkException $exceptionInstance = NULL) {
96 $this->lastException = $exceptionInstance;
100 * Prepares a message as answer for given message data for delivery.
102 * @param $messageData An array with all message data
103 * @param $packageInstance An instance of a Deliverable instance
106 protected function prepareAnswerMessage (array $messageData, Deliverable $packageInstance) {
108 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('MESSAGE-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Going to send an answer message for ' . $this->getHandlerName() . ' ...');
110 // Get a helper instance based on this handler's name
111 $helperInstance = ObjectFactory::createObjectByConfiguredName('node_answer_' . $this->getHandlerName() . '_helper_class', array($messageData));
114 $nodeInstance = NodeObjectFactory::createNodeInstance();
116 // Load descriptor XML
117 $helperInstance->loadDescriptorXml($nodeInstance);
120 * Set missing (temporary) configuration data, mostly it needs to be
121 * copied from message data array.
123 $this->initMessageConfigurationData($messageData);
125 // Compile any configuration variables
126 $helperInstance->getTemplateInstance()->compileConfigInVariables();
128 // Deliver the package
129 $helperInstance->sendPackage($nodeInstance);
132 * Remove temporary configuration
134 $this->removeMessageConfigurationData($messageData);
137 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('MESSAGE-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Answer message has been prepared.');
141 * Prepares the next message
143 * @param $messageData An array with all message data
144 * @param $packageInstance An instance of a Deliverable instance
147 protected function prepareNextMessage (array $messageData, Deliverable $packageInstance) {
149 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('MESSAGE-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Going to send next message ...');
151 // Get a helper instance based on this handler's name
152 $helperInstance = ObjectFactory::createObjectByConfiguredName('node_next_' . $this->getHandlerName() . '_helper_class', array($messageData));
155 $nodeInstance = NodeObjectFactory::createNodeInstance();
157 // Load descriptor XML
158 $helperInstance->loadDescriptorXml($nodeInstance);
161 * Set missing (temporary) configuration data, mostly it needs to be
162 * copied from message data array.
164 $this->initMessageConfigurationData($messageData);
166 // Compile any configuration variables
167 $helperInstance->getTemplateInstance()->compileConfigInVariables();
169 // Deliver the package
170 $helperInstance->sendPackage($nodeInstance);
173 * Remove temporary configuration
175 $this->removeMessageConfigurationData($messageData);
178 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('MESSAGE-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Next message has been prepared.');
182 * Initializes configuration data from given message data array
184 * @param $messageData An array with all message data
187 abstract protected function initMessageConfigurationData (array $messageData);
190 * Removes configuration data with given message data array from global
193 * @param $messageData An array with all message data
196 abstract protected function removeMessageConfigurationData (array $messageData);