Debug mailer finished and debug messages removed:
[shipsimu.git] / application / ship-simu / main / class_WorksContract.php
index 1613d7432efc1761844e3f43943e195b67c77756..4a6005911552cce971e9537011f4c59afa048d23 100644 (file)
@@ -4,9 +4,9 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright(c) 2007, 2008 Roland Haeder, this is free software
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, this is free software
  * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.ship-simu.org
+ * @link               http://www.ship-simu.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
@@ -19,7 +19,7 @@
  * 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/>.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 class WorksContract extends BaseFrameworkSystem implements SignableContract {
        // Zukuenftiger Schiffsname
@@ -41,23 +41,15 @@ class WorksContract extends BaseFrameworkSystem implements SignableContract {
        private $merchantInstance = null;
 
        // Konstruktor
-       private function __construct () {
+       protected function __construct () {
                // Call parent constructor
-               parent::constructor(__CLASS__);
-
-               // Debug message
-               if (((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) && (defined('DEBUG_CONSTRUCT'))) {
-                       $this->getDebugInstance()->output(sprintf("[%s:%d] Konstruktor erreicht.<br />\n",
-                               __CLASS__,
-                               __LINE__
-                       ));
-               }
+               parent::__construct(__CLASS__);
 
                // Set description
-               $this->setPartDescr("Bauvertrag");
+               $this->setObjectDescription("Bauvertrag");
 
                // Unique-ID generieren
-               $this->createUniqueID();
+               $this->generateUniqueId();
 
                // Clean up a little
                $this->removeSystemArray();
@@ -69,27 +61,15 @@ class WorksContract extends BaseFrameworkSystem implements SignableContract {
                $shipType = (string) $shipType;
                $shipName = (string) $shipName;
 
-               // Is the other one a contract partner?
-               if (is_null($partnerInstance)) {
-                       // Opps! Empty partner instance?
-                       throw new NullPointerException($partnerInstance, self::EXCEPTION_IS_NULL_POINTER);
-               } elseif (!is_object($partnerInstance)) {
-                       // Not an object! ;-(
-                       throw new NoObjectException($partnerInstance, self::EXCEPTION_IS_NO_OBJECT);
-               } elseif (!method_exists($partnerInstance, 'isContractPartner')) {
-                       // Does not have the required feature (method)
-                       throw new MissingMethodException(array($partnerInstance, 'isContractPartner'), self::EXCEPTION_MISSING_METHOD);
-               }
-
                // Get new instance
                $contractInstance = new WorksContract();
 
                // Debug-Meldung ausgeben
-               if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $contractInstance->getDebugInstance()->output(sprintf("[%s:%d] Neuer Bauvertrag wird f&uuml;r das Schiff <strong>%s</strong> mit der <strong>%s</strong> <strong>%s</strong> erstellt.<br />\n",
+               if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $contractInstance->debugOutput(sprintf("[%s:%d] Neuer Bauvertrag wird f&uuml;r das Schiff <strong>%s</strong> mit der <strong>%s</strong> <strong>%s</strong> erstellt.",
                        __CLASS__,
                        __LINE__,
                        $shipName,
-                       $partnerInstance->getPartDescr(),
+                       $partnerInstance->getObjectDescription(),
                        $partnerInstance->getCompanyName()
                ));
 
@@ -99,25 +79,15 @@ class WorksContract extends BaseFrameworkSystem implements SignableContract {
                // Existiert die Klasse ueberhaupt?
                if (!class_exists($shipType)) {
                        // Klasse nicht gefunden
-                       throw new ClassNotFoundException ($shipType, 0);
+                       throw new ClassNotFoundException ($shipType, self::EXCEPTION_CLASS_NOT_FOUND);
                }
 
                // Schiff-Instanz temporaer erzeugen und in den Bauvertrag einfuegen
-               $eval = sprintf("\$contractInstance->setShipInstance(%s::create%s(\"%s\"));",
-                       $shipType,
-                       $shipType,
-                       $shipName
-               );
+               $shipInstance = ObjectFactory::createObjectByName($shipType, array($shipName));
+               $contractInstance->setShipInstance($shipInstance);
 
-               // Debug message
-               if ((defined('DEBUG_EVAL')) || (defined('DEBUG_ALL'))) $contractInstance->getDebugInstance()->output(sprintf("[%s:%d] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",
-                       __CLASS__,
-                       __LINE__,
-                       htmlentities($eval)
-               ));
-
-               // Execute constructed command
-               @eval($eval);
+               // Remove the ship instance
+               unset($shipInstance);
 
                // Set itself as contract partner
                $contractInstance->setContractPartner($partnerInstance);
@@ -127,41 +97,22 @@ class WorksContract extends BaseFrameworkSystem implements SignableContract {
        }
 
        // Setter for ship instance
-       private function setShipInstance (ConstructableShip $shipInstance) {
-               if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das Schiff <strong>%s</strong> wird in den Bauvertrag aufgenommen.<br />\n",
-                       __CLASS__,
-                       __LINE__,
-                       $shipInstance->getShipName()
-               ));
+       private final function setShipInstance (ConstructableShip $shipInstance) {
                $this->shipInstance = $shipInstance;
        }
 
        // Setter for ship name
-       private function setShipName ($shipName) {
-               if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das neue Schiff wird <strong>%s</strong> heissen.<br />\n",
-                       __CLASS__,
-                       __LINE__,
-                       $shipName
-               ));
+       private final function setShipName ($shipName) {
                $this->shipName = (string) $shipName;
        }
 
        // Getter for ship name
        public final function getShipName () {
-               if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Schiffsname <strong>%s</strong> angefordert.<br />\n",
-                       __CLASS__,
-                       __LINE__,
-                       $this->shipName
-               ));
                return $this->shipName;
        }
 
        // Getter for ship instance
        public final function getShipInstance () {
-               if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Schiff-Instanz angefordert.<br />\n",
-                       __CLASS__,
-                       __LINE__
-               ));
                return $this->shipInstance;
        }
 
@@ -172,7 +123,7 @@ class WorksContract extends BaseFrameworkSystem implements SignableContract {
                $parentPart = (string) $parentPart;
 
                // Debug message
-               if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Das Schiffsteil <strong>%s</strong> wird zusammen mit dem Konstruktionsteil <strong>%s</strong> in den Bauvertrag aufgenommen.<br />\n",
+               if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Das Schiffsteil <strong>%s</strong> wird zusammen mit dem Konstruktionsteil <strong>%s</strong> in den Bauvertrag aufgenommen.",
                        __CLASS__,
                        __LINE__,
                        $shipPart,
@@ -180,100 +131,57 @@ class WorksContract extends BaseFrameworkSystem implements SignableContract {
                ));
 
                // Initialize the instance (shall not be done within dynamic part)
-               $instance = null;
-
-               // Prepare the big command for everthing
-               $eval = "try {
-       \$instance = ".$shipPart."::create".$shipPart."(";
-               foreach ($dataArray as $pIdx => $parts) {
-                       if (is_string($parts)) {
-                               // String einbauen
-                               $eval .= "\"".$parts."\", ";
-                       } elseif (is_array($parts)) {
-                               // Ist ein weiteres Array!
-                               $eval .= "array(";
-                               foreach ($parts as $idx2 => $sub) {
-                                       $eval .= "'".$idx2."' => ";
-                                       if (is_string($sub)) {
-                                               // Ist erneut ein String
-                                               $eval .= "\"".$sub."\"";
-                                       } elseif (is_array($sub)) {
-                                               // Wird nicht mehr unterstuetzt!
-                                               ApplicationEntryPoint::app_die("No more arrays!");
-                                       } else {
-                                               // Direkt anhaengen
-                                               $eval .= $sub;
-                                       }
-                                       $eval .= ", ";
-                               }
-
-                               // Letztes Komma entfernen
-                               $eval = substr($eval, 0, -2);
-                               $eval .= "), ";
-                       } else {
-                               // Etwas anderes direkt!
-                               $eval .= $parts.", ";
-                       }
-               }
-
-               // Letztes Komma abhaengen
-               $eval = substr($eval, 0, -2);
-               $eval .= ");
-} catch (DimNotFoundInArrayException \$e) {
-       \$this->getDebugInstance()->output(sprintf(\"[main:] Die <strong>%s</strong> konnte nicht vervollst&auml;ndigt werden. Grund: <strong>%s</strong><br />\\n\",
-               \$this->getShipInstance()->getShipName(),
-               \$e->getMessage()
-       ));
-       \$instance = null;
-}";
+               $partInstance = null;
 
-               // Debug message
-               if ((defined('DEBUG_EVAL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[Ship:] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",
-                       htmlentities($eval)
-               ));
-
-               // Code ausfuhren
-               @eval($eval);
-
-               // Try to add the ship part to the contract
+               // Try to get an instance for this ship part
                try {
-                       if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Versuche ein Schiffsteil in den Bauvertrag aufzunehmen.<br />\n",
+                       $partInstance = ObjectFactory::createObjectByName($shipPart, $dataArray);
+               } catch (DimNotFoundInArrayException $e) {
+                       $this->debugOutput(sprintf("[main:] Die <strong>%s</strong> konnte nicht vervollst&auml;ndigt werden. Grund: <strong>%s</strong><br />",
+                               $this->getShipInstance()->getShipName(),
+                               $e->getMessage()
+                       ));
+
+                       // Debug message
+                       if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] Versuche ein Schiffsteil in den Bauvertrag aufzunehmen.",
                                __CLASS__,
                                __LINE__
                        ));
-                       if (!$instance instanceof ConstructableShipPart) {
+
+                       // Is this ship part constructable?
+                       if (!$partInstance instanceof ConstructableShipPart) {
                                // Ship part not constructable!
                                throw new ShipPartNotConstructableException(array($shipPart), self::EXCEPTION_NOT_CONSTRUCTABLE);
-                       } elseif (!$this->getShipInstance()->createShipPart($instance, $parentPart)) {
+                       } elseif (!$this->getShipInstance()->createShipPart($partInstance, $parentPart)) {
                                // Schiff konnte nicht gebaut werden!
                                throw new ShipNotConstructedException(sprintf("Das Schiff <strong>%s</strong> konnte wegen eines Fehlers nicht gebaut werden. Siehe obere Meldungen.",
                                        $this->getShipInstance()->getShipName()
                                ));
                        }
-               } catch(ClassNotFoundException $e) {
+               } catch (ClassNotFoundException $e) {
                        // Throw it again...
                        throw new ClassNotFoundException($e->getMessage(), $e->getCode());
                }
 
                // Get price for this item
-               $price = $this->getMerchantInstance()->getPriceFromList($instance);
+               $price = $this->getMerchantInstance()->getPriceFromList($partInstance);
 
                // Final debug message
-               if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] <strong>%s</strong> kostet <strong>%s</strong>.<br />\n",
+               if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] <strong>%s</strong> kostet <strong>%s</strong>.",
                        __CLASS__,
                        __LINE__,
-                       $instance->getPartDescr(),
+                       $partInstance->getObjectDescription(),
                        $this->getMerchantInstance()->formatCurrency($price)
                ));
 
                // Add price
-               $instance->setPrice($price);
+               $partInstance->setPrice($price);
 
                // Final debug message
-               if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] <strong>%s</strong> wurde in den Bauvertrag aufgenommen.<br />\n",
+               if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) $this->debugOutput(sprintf("[%s:%d] <strong>%s</strong> wurde in den Bauvertrag aufgenommen.",
                        __CLASS__,
                        __LINE__,
-                       $instance->getPartDescr()
+                       $partInstance->getObjectDescription()
                ));
        }
 
@@ -331,24 +239,24 @@ class WorksContract extends BaseFrameworkSystem implements SignableContract {
                if ((defined('DEBUG_CONTRACT')) || (defined('DEBUG_ALL'))) {
                        if ($partnerInstance->equals($partyInstance)) {
                                // With itself
-                               $this->getDebugInstance()->output(sprintf("[%s:%d] Die <strong>%s</strong> <em><strong>%s</strong></em> stimmt einem Bauvertrag &uuml;ber das <strong>%s</strong> <em><strong>%s</strong></em> zu.<br />\n",
+                               $this->debugOutput(sprintf("[%s:%d] Die <strong>%s</strong> <em><strong>%s</strong></em> stimmt einem Bauvertrag &uuml;ber das <strong>%s</strong> <em><strong>%s</strong></em> zu.",
                                        __CLASS__,
                                        __LINE__,
-                                       $partnerInstance->getPartDescr(),
+                                       $partnerInstance->getObjectDescription(),
                                        $partnerInstance->getCompanyName(),
-                                       $this->getShipInstance()->getPartDescr(),
+                                       $this->getShipInstance()->getObjectDescription(),
                                        $this->getShipInstance()->getShipName()
                                ));
                        } else {
                                // Other contract party
-                               $this->getDebugInstance()->output(sprintf("[%s:%d] Die <strong>%s</strong> <em><strong>%s</strong></em> geht mit der <strong>%s</strong> <em><strong>%s</strong></em> einen Bauvertrag &uuml;ber das <strong>%s</strong> <em><strong>%s</strong></em> ein.<br />\n",
+                               $this->debugOutput(sprintf("[%s:%d] Die <strong>%s</strong> <em><strong>%s</strong></em> geht mit der <strong>%s</strong> <em><strong>%s</strong></em> einen Bauvertrag &uuml;ber das <strong>%s</strong> <em><strong>%s</strong></em> ein.",
                                        __CLASS__,
                                        __LINE__,
-                                       $partnerInstance->getPartDescr(),
+                                       $partnerInstance->getObjectDescription(),
                                        $partnerInstance->getCompanyName(),
-                                       $partyInstance->getPartDescr(),
+                                       $partyInstance->getObjectDescription(),
                                        $partyInstance->getCompanyName(),
-                                       $this->getShipInstance()->getPartDescr(),
+                                       $this->getShipInstance()->getObjectDescription(),
                                        $this->getShipInstance()->getShipName()
                                ));
                        }
@@ -406,7 +314,7 @@ class WorksContract extends BaseFrameworkSystem implements SignableContract {
                        } elseif (!is_object($item)) {
                                // Not an object! ;-(
                                throw new NoObjectException($item, self::EXCEPTION_IS_NO_OBJECT);
-                       } elseif (!method_exists($item, 'getPartInstance')) {
+                       } elseif (!$item instanceof BaseSimulator) {
                                // Does not have the required feature (method)
                                throw new MissingMethodException(array($item, 'getPartInstance'), self::EXCEPTION_MISSING_METHOD);
                        }
@@ -448,16 +356,6 @@ class WorksContract extends BaseFrameworkSystem implements SignableContract {
                // Return total price
                return $totalPrice;
        }
-
-       /**
-        * Stub!
-        */
-       public function saveObjectToDatabase () {
-               $this->getDebugInstance()->output(sprintf("[%s:] Stub <strong>%s</strong> erreicht.",
-                       $this->__toString(),
-                       __FUNCTION__
-               ));
-       }
 }
 
 // [EOF]