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