3 * A general data Handler
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 abstract class BaseDataHandler extends BaseHandler {
26 * Last exception instance from database layer or NULL (default)
28 private $lastException = NULL;
31 * Array with search criteria elements
33 protected $searchData = array();
36 * Array with all data XML nodes (which hold the actual data) and their values
38 protected $messageDataElements = array();
41 * Array for translating message data elements (other node's data mostly)
42 * into configuration elements.
44 protected $messageToConfig = array();
47 * Array for copying configuration entries
49 protected $configCopy = array();
52 * Protected constructor
54 * @param $className Name of the class
57 protected function __construct ($className) {
58 // Call parent constructor
59 parent::__construct($className);
62 $dhtInstance = DhtObjectFactory::createDhtObjectInstance('node');
65 $this->setDhtInstance($dhtInstance);
69 * Getter for search data array
71 * @return $searchData Search data array
73 public final function getSearchData () {
74 return $this->searchData;
78 * Getter for last exception
80 * @return $lastException Last thrown exception
82 public final function getLastException () {
83 return $this->lastException;
87 * Setter for last exception
89 * @param $lastException Last thrown exception
92 public final function setLastException (FrameworkException $exceptionInstance = NULL) {
93 $this->lastException = $exceptionInstance;
97 * Prepares a message as answer for given message data for delivery.
99 * @param $messageData An array with all message data
100 * @param $packageInstance An instance of a Deliverable instance
103 protected function prepareAnswerMessage (array $messageData, Deliverable $packageInstance) {
105 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('MESSAGE-HANDLER[' . $this->__toString() . ']: Going to send an answer message ...');
107 // Get a helper instance based on this handler's name
108 $helperInstance = ObjectFactory::createObjectByConfiguredName('node_answer_' . $this->getHandlerName() . '_helper_class', array($messageData));
111 $nodeInstance = Registry::getRegistry()->getInstance('node');
113 // Load descriptor XML
114 $helperInstance->loadDescriptorXml($nodeInstance);
117 * Set missing (temporary) configuration data, mostly it needs to be
118 * copied from message data array.
120 $this->initMessageConfigurationData($messageData);
122 // Compile any configuration variables
123 $helperInstance->getTemplateInstance()->compileConfigInVariables();
125 // Deliver the package
126 $helperInstance->sendPackage($nodeInstance);
129 * Remove temporary configuration
131 $this->removeMessageConfigurationData($messageData);
134 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('MESSAGE-HANDLER[' . $this->__toString() . ']: Answer message has been prepared.');
138 * Prepares the next message
140 * @param $messageData An array with all message data
141 * @param $packageInstance An instance of a Deliverable instance
144 protected function prepareNextMessage (array $messageData, Deliverable $packageInstance) {
146 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('MESSAGE-HANDLER[' . $this->__toString() . ']: Going to send next message ...');
148 // Get a helper instance based on this handler's name
149 $helperInstance = ObjectFactory::createObjectByConfiguredName('node_next_' . $this->getHandlerName() . '_helper_class', array($messageData));
152 $nodeInstance = Registry::getRegistry()->getInstance('node');
154 // Load descriptor XML
155 $helperInstance->loadDescriptorXml($nodeInstance);
158 * Set missing (temporary) configuration data, mostly it needs to be
159 * copied from message data array.
161 $this->initMessageConfigurationData($messageData);
163 // Compile any configuration variables
164 $helperInstance->getTemplateInstance()->compileConfigInVariables();
166 // Deliver the package
167 $helperInstance->sendPackage($nodeInstance);
170 * Remove temporary configuration
172 $this->removeMessageConfigurationData($messageData);
175 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('MESSAGE-HANDLER[' . $this->__toString() . ']: Next message has been prepared.');
179 * Initializes configuration data from given message data array
181 * @param $messageData An array with all message data
184 abstract protected function initMessageConfigurationData (array $messageData);
187 * Removes configuration data with given message data array from global
190 * @param $messageData An array with all message data
193 abstract protected function removeMessageConfigurationData (array $messageData);