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