Debugging continued:
[core.git] / inc / main / classes / handler / raw_data / class_BaseDataHandler.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Handler\Data;
4
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7
8 /**
9  * A general data Handler
10  *
11  * @author              Roland Haeder <webmaster@shipsimu.org>
12  * @version             0.0.0
13  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14  * @license             GNU GPL 3.0 or any newer version
15  * @link                http://www.shipsimu.org
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29  */
30 abstract class BaseDataHandler extends BaseHandler {
31         /**
32          * Last exception instance from database layer or NULL (default)
33          */
34         private $lastException = NULL;
35
36         /**
37          * Array with search criteria elements
38          */
39         protected $searchData = array();
40
41         /**
42          * Array with all data XML nodes (which hold the actual data) and their values
43          */
44         protected $messageDataElements = array();
45
46         /**
47          * Array for translating message data elements (other node's data mostly)
48          * into configuration elements.
49          */
50         protected $messageToConfig = array();
51
52         /**
53          * Array for copying configuration entries
54          */
55         protected $configCopy = array();
56
57         /**
58          * Protected constructor
59          *
60          * @param       $className      Name of the class
61          * @return      void
62          */
63         protected function __construct ($className) {
64                 // Call parent constructor
65                 parent::__construct($className);
66
67                 // Get a DHT instance
68                 $dhtInstance = DhtObjectFactory::createDhtInstance('node');
69
70                 // Set it here
71                 $this->setDhtInstance($dhtInstance);
72         }
73
74         /**
75          * Getter for search data array
76          *
77          * @return      $searchData             Search data array
78          */
79         public final function getSearchData () {
80                 return $this->searchData;
81         }
82
83         /**
84          * Getter for last exception
85          *
86          * @return      $lastException  Last thrown exception
87          */
88         public final function getLastException () {
89                 return $this->lastException;
90         }
91
92         /**
93          * Setter for last exception
94          *
95          * @param       $lastException  Last thrown exception
96          * @return      void
97          */
98         public final function setLastException (FrameworkException $exceptionInstance = NULL) {
99                 $this->lastException = $exceptionInstance;
100         }
101
102         /**
103          * Prepares a message as answer for given message data for delivery.
104          *
105          * @param       $messageData            An array with all message data
106          * @param       $packageInstance        An instance of a Deliverable instance
107          * @return      void
108          */
109         protected function prepareAnswerMessage (array $messageData, Deliverable $packageInstance) {
110                 // Debug message
111                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MESSAGE-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Going to send an answer message for ' . $this->getHandlerName() . ' ...');
112
113                 // Get a helper instance based on this handler's name
114                 $helperInstance = ObjectFactory::createObjectByConfiguredName('node_answer_' . $this->getHandlerName() . '_helper_class', array($messageData));
115
116                 // Get node instance
117                 $nodeInstance = NodeObjectFactory::createNodeInstance();
118
119                 // Load descriptor XML
120                 $helperInstance->loadDescriptorXml($nodeInstance);
121
122                 /*
123                  * Set missing (temporary) configuration data, mostly it needs to be
124                  * copied from message data array.
125                  */
126                 $this->initMessageConfigurationData($messageData);
127
128                 // Compile any configuration variables
129                 $helperInstance->getTemplateInstance()->compileConfigInVariables();
130
131                 // Deliver the package
132                 $helperInstance->sendPackage($nodeInstance);
133
134                 /*
135                  * Remove temporary configuration
136                  */
137                 $this->removeMessageConfigurationData($messageData);
138
139                 // Debug message
140                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MESSAGE-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Answer message has been prepared.');
141         }
142
143         /**
144          * Prepares the next message
145          *
146          * @param       $messageData            An array with all message data
147          * @param       $packageInstance        An instance of a Deliverable instance
148          * @return      void
149          */
150         protected function prepareNextMessage (array $messageData, Deliverable $packageInstance) {
151                 // Debug message
152                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MESSAGE-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Going to send next message ...');
153
154                 // Get a helper instance based on this handler's name
155                 $helperInstance = ObjectFactory::createObjectByConfiguredName('node_next_' . $this->getHandlerName() . '_helper_class', array($messageData));
156
157                 // Get node instance
158                 $nodeInstance = NodeObjectFactory::createNodeInstance();
159
160                 // Load descriptor XML
161                 $helperInstance->loadDescriptorXml($nodeInstance);
162
163                 /*
164                  * Set missing (temporary) configuration data, mostly it needs to be
165                  * copied from message data array.
166                  */
167                 $this->initMessageConfigurationData($messageData);
168
169                 // Compile any configuration variables
170                 $helperInstance->getTemplateInstance()->compileConfigInVariables();
171
172                 // Deliver the package
173                 $helperInstance->sendPackage($nodeInstance);
174
175                 /*
176                  * Remove temporary configuration
177                  */
178                 $this->removeMessageConfigurationData($messageData);
179
180                 // Debug message
181                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MESSAGE-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Next message has been prepared.');
182         }
183
184         /**
185          * Initializes configuration data from given message data array
186          *
187          * @param       $messageData    An array with all message data
188          * @return      void
189          */
190         abstract protected function initMessageConfigurationData (array $messageData);
191
192         /**
193          * Removes configuration data with given message data array from global
194          * configuration
195          *
196          * @param       $messageData    An array with all message data
197          * @return      void
198          */
199         abstract protected function removeMessageConfigurationData (array $messageData);
200
201 }