b9b55e91490b844a43cd4049df94dc5a75567cf8
[core.git] / framework / 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 use CoreFramework\Generic\FrameworkException;
8 use CoreFramework\Handler\BaseHandler;
9
10 /**
11  * A general data Handler
12  *
13  * @author              Roland Haeder <webmaster@shipsimu.org>
14  * @version             0.0.0
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
18  *
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.
23  *
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.
28  *
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/>.
31  */
32 abstract class BaseDataHandler extends BaseHandler {
33         /**
34          * Last exception instance from database layer or NULL (default)
35          */
36         private $lastException = NULL;
37
38         /**
39          * Array with search criteria elements
40          */
41         protected $searchData = array();
42
43         /**
44          * Array with all data XML nodes (which hold the actual data) and their values
45          */
46         protected $messageDataElements = array();
47
48         /**
49          * Array for translating message data elements (other node's data mostly)
50          * into configuration elements.
51          */
52         protected $messageToConfig = array();
53
54         /**
55          * Array for copying configuration entries
56          */
57         protected $configCopy = array();
58
59         /**
60          * Protected constructor
61          *
62          * @param       $className      Name of the class
63          * @return      void
64          */
65         protected function __construct ($className) {
66                 // Call parent constructor
67                 parent::__construct($className);
68
69                 // Get a DHT instance
70                 $dhtInstance = DhtObjectFactory::createDhtInstance('node');
71
72                 // Set it here
73                 $this->setDhtInstance($dhtInstance);
74         }
75
76         /**
77          * Getter for search data array
78          *
79          * @return      $searchData             Search data array
80          */
81         public final function getSearchData () {
82                 return $this->searchData;
83         }
84
85         /**
86          * Getter for last exception
87          *
88          * @return      $lastException  Last thrown exception
89          */
90         public final function getLastException () {
91                 return $this->lastException;
92         }
93
94         /**
95          * Setter for last exception
96          *
97          * @param       $lastException  Last thrown exception
98          * @return      void
99          */
100         public final function setLastException (FrameworkException $exceptionInstance = NULL) {
101                 $this->lastException = $exceptionInstance;
102         }
103
104         /**
105          * Prepares a message as answer for given message data for delivery.
106          *
107          * @param       $messageData            An array with all message data
108          * @param       $packageInstance        An instance of a Deliverable instance
109          * @return      void
110          */
111         protected function prepareAnswerMessage (array $messageData, Deliverable $packageInstance) {
112                 // Debug message
113                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MESSAGE-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Going to send an answer message for ' . $this->getHandlerName() . ' ...');
114
115                 // Get a helper instance based on this handler's name
116                 $helperInstance = ObjectFactory::createObjectByConfiguredName('node_answer_' . $this->getHandlerName() . '_helper_class', array($messageData));
117
118                 // Get node instance
119                 $nodeInstance = NodeObjectFactory::createNodeInstance();
120
121                 // Load descriptor XML
122                 $helperInstance->loadDescriptorXml($nodeInstance);
123
124                 /*
125                  * Set missing (temporary) configuration data, mostly it needs to be
126                  * copied from message data array.
127                  */
128                 $this->initMessageConfigurationData($messageData);
129
130                 // Compile any configuration variables
131                 $helperInstance->getTemplateInstance()->compileConfigInVariables();
132
133                 // Deliver the package
134                 $helperInstance->sendPackage($nodeInstance);
135
136                 /*
137                  * Remove temporary configuration
138                  */
139                 $this->removeMessageConfigurationData($messageData);
140
141                 // Debug message
142                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MESSAGE-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Answer message has been prepared.');
143         }
144
145         /**
146          * Prepares the next message
147          *
148          * @param       $messageData            An array with all message data
149          * @param       $packageInstance        An instance of a Deliverable instance
150          * @return      void
151          */
152         protected function prepareNextMessage (array $messageData, Deliverable $packageInstance) {
153                 // Debug message
154                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MESSAGE-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Going to send next message ...');
155
156                 // Get a helper instance based on this handler's name
157                 $helperInstance = ObjectFactory::createObjectByConfiguredName('node_next_' . $this->getHandlerName() . '_helper_class', array($messageData));
158
159                 // Get node instance
160                 $nodeInstance = NodeObjectFactory::createNodeInstance();
161
162                 // Load descriptor XML
163                 $helperInstance->loadDescriptorXml($nodeInstance);
164
165                 /*
166                  * Set missing (temporary) configuration data, mostly it needs to be
167                  * copied from message data array.
168                  */
169                 $this->initMessageConfigurationData($messageData);
170
171                 // Compile any configuration variables
172                 $helperInstance->getTemplateInstance()->compileConfigInVariables();
173
174                 // Deliver the package
175                 $helperInstance->sendPackage($nodeInstance);
176
177                 /*
178                  * Remove temporary configuration
179                  */
180                 $this->removeMessageConfigurationData($messageData);
181
182                 // Debug message
183                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MESSAGE-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Next message has been prepared.');
184         }
185
186         /**
187          * Initializes configuration data from given message data array
188          *
189          * @param       $messageData    An array with all message data
190          * @return      void
191          */
192         abstract protected function initMessageConfigurationData (array $messageData);
193
194         /**
195          * Removes configuration data with given message data array from global
196          * configuration
197          *
198          * @param       $messageData    An array with all message data
199          * @return      void
200          */
201         abstract protected function removeMessageConfigurationData (array $messageData);
202
203 }