]> git.mxchange.org Git - shipsimu.git/blobdiff - application/ship-simu/main/companies/class_ShippingCompany.php
Following things are changed: (in order of class names)
[shipsimu.git] / application / ship-simu / main / companies / class_ShippingCompany.php
index 381aa4ce8f665e37cbf4457d291d277da0b7b12e..d3ab6985dec4c5d08e602f645dcaa330de337044 100644 (file)
@@ -63,7 +63,15 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
        private $contractList    = null;
 
        /**
-        * Main constructor
+        * Database result instance
+        */
+       private $resultInstance = null;
+
+       // Exception constants
+       const EXCEPTION_USER_OWNS_NO_COMPANY = 0x200;
+
+       /**
+        * Protected constructor
         *
         * @return      void
         */
@@ -72,7 +80,7 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
                parent::__construct(__CLASS__);
 
                // Set description
-               $this->setObjectDescription("Reederei");
+               $this->setObjectDescription("A shipping company class");
 
                // Generate unique ID number
                $this->generateUniqueId();
@@ -81,76 +89,79 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
                $this->removeSystemArray();
        }
 
-       // Reederei gruenden (create wegen Namenskonvention)
-       public final static function createShippingCompany ($companyName, Harbor $hqInstance) {
+       /**
+        * Creates an instance of this company class or throws an exception if the
+        * given user owns no company.
+        *
+        * @param       $userInstance           A user class
+        * @return      $companyInstance        Prepared company instance
+        * @throws      NoShippingCompanyOwnedException         If the user owns no shipping companies
+        */
+       public final static function createShippingCompany (BaseUser $userInstance) {
                // Get new instance
                $companyInstance = new ShippingCompany();
 
-               if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $companyInstance->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> wird gegr&uuml;ndet.",
-                       __CLASS__,
-                       __LINE__,
-                       $companyName
-               ));
+               // Does the given user owns a company?
+               if (!$companyInstance->ifUserOwnsShippingCompany($userInstance)) {
+                       // Throw an exception here
+                       throw new NoShippingCompanyOwnedException(array($companyInstance, $userInstance), self::EXCEPTION_USER_OWNS_NO_COMPANY);
+               } // END - if
 
-               // Firmennamen setzen
-               $companyInstance->setCompanyName($companyName);
+               // Set the user instance. We don't care here if he is founder or employee.
+               $companyInstance->setUserInstance($userInstance);
 
-               // Kuerzel setzen
-               $companyInstance->createShortName();
+               // Init all lists
+               $companyInstance->initCompanyLists();
 
-               // Sitz festlegen
-               $companyInstance->setHQInstance($hqInstance);
+               // Return instance
+               return $companyInstance;
+       }
 
-               // Werftenliste erstellen
-               $companyInstance->createshipyardList();
+       /**
+        * Checks wether the given user owns a company or not
+        *
+        * @param       $userInstance   An instance of a user class
+        * @return      $ownsCompany    Wether the user owns at least one company
+        */
+       protected function ifUserOwnsShippingCompany (BaseUser $userInstance)  {
+               // By default no user owns any company... ;)
+               $ownsCompany = false;
 
-               // Angestellten-Liste erstellen
-               $companyInstance->createEmployeeList();
+               // Get a company database wrapper class
+               $wrapperInstance = ObjectFactory::createObjectByConfiguredName('company_db_wrapper_class');
 
-               // Auftragsliste erstellen
-               $companyInstance->createContractList();
+               // Get a search criteria class
+               $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
 
-               // Clean up a little
-               $companyInstance->removeWidth();
-               $companyInstance->removeHeight();
-               $companyInstance->removeLength();
-               $companyInstance->removeDraught();
-               $companyInstance->removePartInstance();
+               // Add the user primary key as a search criteria
+               $criteriaInstance->addCriteria('company_owner', $userInstance->getPrimaryKey());
+               $criteriaInstance->setLimit(1);
 
-               // Instanz zurueckgeben
-               return $companyInstance;
+               // Get the result back
+               $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
+               $resultInstance->debugInstance();
        }
 
-       // Angestellten-Liste erstellen
-       private function createEmployeeList () {
-               if ((defined('DEBUG_COMPANY_EMPLOYEE')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> erh&auml;lt eine Angestelltenliste.",
-                       __CLASS__,
-                       __LINE__,
-                       $this->getCompanyName()
-               ));
+       /**
+        * Intialize all lists
+        *
+        * @return      void
+        */
+       protected function initCompanyLists () {
+               // Employees
                $this->employeeList = new FrameworkArrayObject("FakedEmployeeList");
-       }
 
-       // Werftenliste erstellen
-       public function createShipyardList () {
-               if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> erh&auml;lt eine Werftsliste.",
-                       __CLASS__,
-                       __LINE__,
-                       $this->getCompanyName()
-               ));
+               // Ship yards
                $this->shipyardList = new FrameworkArrayObject("FakedShipyardList");
-       }
 
-       // Auftragsliste erstellen
-       public function createContractList () {
-               if ((defined('DEBUG_COMPANY')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Die Reederei <strong>%s</strong> erh&auml;lt eine Auftragsliste.",
-                       __CLASS__,
-                       __LINE__,
-                       $this->getCompanyName()
-               ));
+               // Contracts
                $this->contractList = new FrameworkArrayObject("FakedContractList");
        }
 
+       //----------------------------------------------------------------------------
+       // From here is very old code which needs to be translated and changed heavily
+       //----------------------------------------------------------------------------
+
        // Setter-Methode fuer Firmennamen
        public final function setCompanyName ($companyName) {
                $this->companyName = (string) $companyName;
@@ -167,12 +178,12 @@ class ShippingCompany extends BaseSimulator implements Customer, ContractPartner
        }
 
        // Kuerzel setzen
-       private function createShortName () {
+       private function initShortName () {
                // Mindestens eine Leerstelle?
                $dummy = explode(" ", $this->getCompanyName());
                foreach ($dummy as $part) {
                        $this->shortName .= substr($part, 0, 1);
-               }
+               } // END - if
        }
 
        // Reedereien Werften bauen lassen