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