Some updates:
[core.git] / inc / main / classes / handler / raw_data / class_BaseDataHandler.php
diff --git a/inc/main/classes/handler/raw_data/class_BaseDataHandler.php b/inc/main/classes/handler/raw_data/class_BaseDataHandler.php
new file mode 100644 (file)
index 0000000..24f2913
--- /dev/null
@@ -0,0 +1,197 @@
+<?php
+/**
+ * A general data Handler
+ *
+ * @author             Roland Haeder <webmaster@shipsimu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.shipsimu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+abstract class BaseDataHandler extends BaseHandler {
+       /**
+        * Last exception instance from database layer or NULL (default)
+        */
+       private $lastException = NULL;
+
+       /**
+        * Array with search criteria elements
+        */
+       protected $searchData = array();
+
+       /**
+        * Array with all data XML nodes (which hold the actual data) and their values
+        */
+       protected $messageDataElements = array();
+
+       /**
+        * Array for translating message data elements (other node's data mostly)
+        * into configuration elements.
+        */
+       protected $messageToConfig = array();
+
+       /**
+        * Array for copying configuration entries
+        */
+       protected $configCopy = array();
+
+       /**
+        * Protected constructor
+        *
+        * @param       $className      Name of the class
+        * @return      void
+        */
+       protected function __construct ($className) {
+               // Call parent constructor
+               parent::__construct($className);
+
+               // Get a DHT instance
+               $dhtInstance = DhtObjectFactory::createDhtInstance('node');
+
+               // Set it here
+               $this->setDhtInstance($dhtInstance);
+       }
+
+       /**
+        * Getter for search data array
+        *
+        * @return      $searchData             Search data array
+        */
+       public final function getSearchData () {
+               return $this->searchData;
+       }
+
+       /**
+        * Getter for last exception
+        *
+        * @return      $lastException  Last thrown exception
+        */
+       public final function getLastException () {
+               return $this->lastException;
+       }
+
+       /**
+        * Setter for last exception
+        *
+        * @param       $lastException  Last thrown exception
+        * @return      void
+        */
+       public final function setLastException (FrameworkException $exceptionInstance = NULL) {
+               $this->lastException = $exceptionInstance;
+       }
+
+       /**
+        * Prepares a message as answer for given message data for delivery.
+        *
+        * @param       $messageData            An array with all message data
+        * @param       $packageInstance        An instance of a Deliverable instance
+        * @return      void
+        */
+       protected function prepareAnswerMessage (array $messageData, Deliverable $packageInstance) {
+               // Debug message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('MESSAGE-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Going to send an answer message for ' . $this->getHandlerName() . ' ...');
+
+               // Get a helper instance based on this handler's name
+               $helperInstance = ObjectFactory::createObjectByConfiguredName('node_answer_' . $this->getHandlerName() . '_helper_class', array($messageData));
+
+               // Get node instance
+               $nodeInstance = NodeObjectFactory::createNodeInstance();
+
+               // Load descriptor XML
+               $helperInstance->loadDescriptorXml($nodeInstance);
+
+               /*
+                * Set missing (temporary) configuration data, mostly it needs to be
+                * copied from message data array.
+                */
+               $this->initMessageConfigurationData($messageData);
+
+               // Compile any configuration variables
+               $helperInstance->getTemplateInstance()->compileConfigInVariables();
+
+               // Deliver the package
+               $helperInstance->sendPackage($nodeInstance);
+
+               /*
+                * Remove temporary configuration
+                */
+               $this->removeMessageConfigurationData($messageData);
+
+               // Debug message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('MESSAGE-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Answer message has been prepared.');
+       }
+
+       /**
+        * Prepares the next message
+        *
+        * @param       $messageData            An array with all message data
+        * @param       $packageInstance        An instance of a Deliverable instance
+        * @return      void
+        */
+       protected function prepareNextMessage (array $messageData, Deliverable $packageInstance) {
+               // Debug message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('MESSAGE-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Going to send next message ...');
+
+               // Get a helper instance based on this handler's name
+               $helperInstance = ObjectFactory::createObjectByConfiguredName('node_next_' . $this->getHandlerName() . '_helper_class', array($messageData));
+
+               // Get node instance
+               $nodeInstance = NodeObjectFactory::createNodeInstance();
+
+               // Load descriptor XML
+               $helperInstance->loadDescriptorXml($nodeInstance);
+
+               /*
+                * Set missing (temporary) configuration data, mostly it needs to be
+                * copied from message data array.
+                */
+               $this->initMessageConfigurationData($messageData);
+
+               // Compile any configuration variables
+               $helperInstance->getTemplateInstance()->compileConfigInVariables();
+
+               // Deliver the package
+               $helperInstance->sendPackage($nodeInstance);
+
+               /*
+                * Remove temporary configuration
+                */
+               $this->removeMessageConfigurationData($messageData);
+
+               // Debug message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('MESSAGE-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Next message has been prepared.');
+       }
+
+       /**
+        * Initializes configuration data from given message data array
+        *
+        * @param       $messageData    An array with all message data
+        * @return      void
+        */
+       abstract protected function initMessageConfigurationData (array $messageData);
+
+       /**
+        * Removes configuration data with given message data array from global
+        * configuration
+        *
+        * @param       $messageData    An array with all message data
+        * @return      void
+        */
+       abstract protected function removeMessageConfigurationData (array $messageData);
+}
+
+// [EOF]
+?>