X-Git-Url: https://git.mxchange.org/?p=shipsimu.git;a=blobdiff_plain;f=ship-simu%2Fapplication%2Fship-simu%2Fmain%2Fpersonell%2Fclass_SimulatorPersonell.php;fp=ship-simu%2Fapplication%2Fship-simu%2Fmain%2Fpersonell%2Fclass_SimulatorPersonell.php;h=8581536f87e68abfc9998c9274534f54dceb8215;hp=0000000000000000000000000000000000000000;hb=85db0cda2cdb5a801db7020aa55b6f4d969f7674;hpb=6f7c99239b34b8ff5e05d719ea24dd213f03f955 diff --git a/ship-simu/application/ship-simu/main/personell/class_SimulatorPersonell.php b/ship-simu/application/ship-simu/main/personell/class_SimulatorPersonell.php new file mode 100644 index 0000000..8581536 --- /dev/null +++ b/ship-simu/application/ship-simu/main/personell/class_SimulatorPersonell.php @@ -0,0 +1,467 @@ +getDebugInstance()->output(sprintf("[%s:] Konstruktor erreicht.
\n", + $this->__toString() + )); + + // Set description + $this->setPartDescr("Simulationspersonal"); + + // Create unique ID + $this->createUniqueID(); + + // Clean-up a little + $this->removeSystemArray(); + } + + /** + * Magic wake-up method called when unserialize() is called. This is + * neccessary because in this case a personell does not need to know the + * min/max ages range and system classes. This would anyway use more RAM + * what is not required. + * + * @return void + */ + public function __wakeup () { + // Tidy up a little + $this->removePersonellList(); + $this->removeMinMaxAge(); + $this->removeCache(); + $this->removeSystemArray(); + } + + /** + * Generate a specified amount of personell and return the prepared instance + * + * @param $amountPersonell Number of personell we shall + * generate + * @return $personellInstance An instance of this object with a + * list of personells + */ + public static function createSimulatorPersonell ($amountPersonell) { + // Make sure only integer can pass + $amountPersonell = (int) $amountPersonell; + + // Get a new instance + $personellInstance = new SimulatorPersonell(); + + // Generate unique ID number + $personellInstance->createUniqueID(); + + // Debug message + if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $personellInstance->getDebugInstance()->output(sprintf("[%s:%d] Es werden %d Personal bereitgestellt.
\n", + __CLASS__, + __LINE__, + $amountPersonell + )); + + // Initialize the personell list + $personellInstance->createPersonellList(); + + // Create requested amount of personell + for ($idx = 0; $idx < $amountPersonell; $idx++) { + $personellInstance->addPersonell(); + } + + // Debug message + if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $personellInstance->getDebugInstance()->output(sprintf("[%s:%d] %d Personal bereitgestellt.
\n", + __CLASS__, + __LINE__, + $amountPersonell + )); + + // Tidy up a little + $personellInstance->removeGender(); + $personellInstance->removeNames(); + $personellInstance->removeBirthday(); + $personellInstance->removeSalary(); + $personellInstance->removeEmployed(); + $personellInstance->removeMarried(); + $personellInstance->removeNumberFormaters(); + //$personellInstance->removeCache(); + $personellInstance->removeSystemArray(); + + // Instanz zurueckgeben + return $personellInstance; + } + + /** + * Create a SimulatorPersonell object by loading the specified personell + * list from an existing database backend + * + * @param $idNumber The ID number (only right part) of the list + * @return $personellInstance An instance of + * @throws InvalidIDFormatException If the given id number + * $idNumber is invalid + * @throws NullPointerException If a null pointer (instance) + * has been returned. + * @throws NoObjectException If a non-object has been + * returned + * @throws MissingMethodException If a required method is missing + * @throws MissingSimulatorIDException If an ID number was not found + */ + public final static function createSimulatorPersonellByID ($idNumber) { + // Add the class name if it was not found + if (count(explode("@", $idNumber)) < 2) { + // Add class name in front of the incomplete ID number + $tempID = sprintf("%s@%s", __CLASS__, $idNumber); + } else { + // Use the direct ID number + $tempID = $idNumber; + } + + // Validate the ID number + if (!preg_match(sprintf("/%s\@([a-f0-9]){32}/i", __CLASS__), $tempID)) { + // Invalid format + throw new InvalidIDFormatException(new SimulatorPersonell(), self::EXCEPTION_ID_IS_INVALID_FORMAT); + } + + // Get instance + $personellInstance = new SimulatorPersonell(false); + + // Get database instance + $dbInstance = $personellInstance->getDatabaseInstance(); + + // Is this a valid database instance? + if (is_null($dbInstance)) { + // No class returned + throw new NullPointerException($personellInstance, self::EXCEPTION_IS_NULL_POINTER); + } elseif (!is_object($dbInstance)) { + // Not an object! ;-( + throw new NoObjectException($dbInstance, self::EXCEPTION_IS_NO_OBJECT); + } elseif (!method_exists($dbInstance, 'isUniqueIdUsed')) { + // Required method not found + throw new MissingMethodException(array($dbInstance, 'isUniqueIdUsed'), self::EXCEPTION_MISSING_METHOD); + } + + // Is the unique ID already used? Then it must be there! + if (!$dbInstance->isUniqueIdUsed($tempID)) { + // Entry not found! + throw new MissingSimulatorIDException(array($personellInstance, $idNumber), self::EXCEPTION_SIMULATOR_ID_INVALID); + } + + // Load the personell list and add it to this object + $personellInstance->loadPersonellList($tempID); + + // Clean-up a little + $personellInstance->removeGender(); + $personellInstance->removeNames(); + $personellInstance->removeBirthday(); + $personellInstance->removeSalary(); + $personellInstance->removeEmployed(); + $personellInstance->removeMarried(); + $personellInstance->removeNumberFormaters(); + //$personellInstance->removeCache(); + $personellInstance->removeSystemArray(); + + // Return instance + return $personellInstance; + } + + // Create personell list + public function createPersonellList () { + if (is_null($this->personellList)) { + if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Personell-Liste erstellt.
\n", + __CLASS__, + __LINE__ + )); + $this->personellList = new FrameworkArrayObject(); + } else { + throw new PersonellListAlreadyCreatedException($this, self::EXCEPTION_DIMENSION_ARRAY_INVALID); + } + } + + // Remove the personell list + private function removePersonellList () { + if (defined('DEBUG_PERSONELL')) $this->getDebugInstance()->output(sprintf("[%s:%d] Personell-Liste entfernt.
\n", + __CLASS__, + __LINE__ + )); + unset($this->personellList); + } + + // Add new personell object to our list + public function addPersonell () { + // Gender list... + $genders = array('M', 'F'); + + // Create new personell members + $personellInstance = new SimulatorPersonell(); + + // Set a randomized gender + $personellInstance->setGender($genders[mt_rand(0, 1)]); + + // Set a randomized birthday (maximum age required, see const MAX_AGE) + $personellInstance->createBirthday(); + + // Married? Same values means: married + if (mt_rand(0, 5) == mt_rand(0, 5)) $personellInstance->setMarried(true); + + // Tidy up a little + $personellInstance->removePersonellList(); + $personellInstance->removeMinMaxAge(); + $personellInstance->removeCache(); + $personellInstance->removeSystemArray(); + + // Add new member to the list + $this->personellList->append($personellInstance); + } + + /** + * Get a specifyable list of our people, null or empty string will be ignored! + * + * @return $cacheList A list of cached personells + */ + function getSpecialPersonellList ($isEmployed = null, $isMarried = null, $hasGender = "") { + // Serialize the conditions for checking if we can take the cache + $serialized = serialize(array($isEmployed, $isMarried, $hasGender)); + + // The same (last) conditions? + if (($serialized == $this->cacheCond) && (!is_null($this->cacheCond))) { + if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Gecachte Liste wird verwendet.
\n", + __CLASS__, + __LINE__ + )); + + // Return cached list + return $this->cacheList; + } + + // Output debug message + if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Personalliste wird nach Kriterien durchsucht...
\n", + __CLASS__, + __LINE__ + )); + + // Remember the conditions + $this->setCacheCond($serialized); + + // Create cached list + $this->setAllCacheList(new FrameworkArrayObject()); + + // Search all unemployed personells + for ($idx = $this->personellList->getIterator(); $idx->valid(); $idx->next()) { + // Element holen + $el = $idx->current(); + + // Check currenylt all single conditions (combined conditions are not yet supported) + if ((!is_null($isEmployed)) && ($el->isEmployed() == $isEmployed)) { + // Add this one (employed status asked) + $this->cacheList->append($el); + } elseif ((!is_null($isMarried)) && ($el->isMarried() == $isMarried)) { + // Add this one (marrital status asked) + $this->cacheList->append($el); + } elseif ((!empty($hasGender)) && ($el->getGender() == $hasGender)) { + // Add this one (specified gender) + $this->cacheList->append($el); + } + } + + // Return the completed list + return $this->cacheList; + } + + /** + * Get amount of unemployed personell + * + * @return $count Amount of unemployed personell + */ + public function getAllUnemployed () { + if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Es werden alle erwerbslosen Personen gesucht.
\n", + __CLASS__, + __LINE__ + )); + + // Get a temporary list + $list = $this->getSpecialPersonellList(false); + + // Anzahl zurueckliefern + return $list->count(); + } + + /** + * Remove cache things + * + * @return void + */ + private function removeCache () { + if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Cache-Attribute entfernt.
\n", + __CLASS__, + __LINE__ + )); + + // Remove cache data + unset($this->cacheList); + unset($this->cacheCond); + } + + /** + * Setter for cache list + * + * @param $cacheList The new cache list to set or null for initialization/reset + * @return void + */ + private function setAllCacheList (FrameworkArrayObject $cacheList = null) { + $this->cacheList = $cacheList; + } + + /** + * Setter for cache conditions + * + * @param $cacheCond The new cache conditions to set + * @return void + */ + private function setCacheCond ($cacheCond) { + $this->cacheCond = (string) $cacheCond; + } + + /** + * Reset cache list + * + * @return void + */ + public function resetCache () { + if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Cache-Liste zurückgesetzt.
\n", + __CLASS__, + __LINE__ + )); + $this->setAllCacheList(null); + $this->setCacheCond(""); + } + + /** + * Getter for surname. If no surname is set then default surnames are set + * for male and female personells. + * + * @return $surname The personell' surname + */ + public final function getSurname () { + $surname = parent::getSurname(); + // Make sure every one has a surname... + if (empty($surname)) { + if ($this->isMale()) { + // Typical male name + $surname = "John"; + } else { + // Typical female name + $surname = "Jennifer"; + } + + // Set typical family name + parent::setFamily("Smith"); + } + return $surname; + } + + /** + * Saves only the personell list to the database + * + * @return void + */ + public function saveObjectToDatabase () { + // Get the database + $dbInstance = $this->getDatabaseInstance(); + + // Prepare the limitation object. We just need the personellList array object. + $limitInstance = ObjectLimits::createObjectLimits(array("personellList")); + + // Limitate the saving amount + $dbInstance->limitObject($limitInstance); + + // Save this object + $dbInstance->saveObject($this); + } + + /** + * Getter for personell list + * + * @return $personellList The list of all personells + */ + public function getPersonellList () { + return $this->personellList; + } + + /** + * Loads the mostly pre-cached personell list + * + * @param $idNumber The ID number we shall use for looking up + * the right data. + * @return void + * @throws ContainerItemIsNullException If a container item is null + * @throws ContainerItemIsNoArrayException If a container item is + * not an array + * @throws ContainerMaybeDamagedException If the container item + * is missing the indexes + * 'name' and/or 'value' + * @see SerializationContainer A special container class which + * helps storing only some attributes + * of a class. + */ + public function loadPersonellList ($idNumber) { + // Get database instance + $dbInstance = $this->getDatabaseInstance(); + + // Get the serialization container within the personell list from + // the database layer + $containerInstance = $dbInstance->getObjectFromCachedData($idNumber); + + // Iterate through the whole container + for ($idx = $containerInstance->getIterator(); $idx->valid(); $idx->next()) { + // Get current item from container + $item = $idx->current(); + + // Validate it a bit + if (is_null($item)) { + // Is null + throw new ContainerItemIsNullException($this, self::EXCEPTION_CONTAINER_ITEM_IS_NULL); + } elseif (!is_array($item)) { + // Is not an array + throw new ContainerItemIsNoArrayException($this, self::EXCEPTION_ITEM_IS_NO_ARRAY); + } elseif ((!isset($item['name'])) || (!isset($item['value']))) { + // Missing elements + throw new ContainerMaybeDamagedException($this, self::EXCEPTION_CONTAINER_MAYBE_DAMAGED); + } + + // Okay, now we can get the item and generate a valid command for eval(). + // We need to convert the first letter to lower-case but keep all others intact + $eval = sprintf("\$this->%s = \$item['value'];", + strtolower(substr($item['name'], 0, 1)) + . + substr($item['name'], 1) + ); + + // Debug message + if ((defined('DEBUG_EVAL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Konstruierte PHP-Anweisung:
%s

\n", + __CLASS__, + __LINE__, + htmlentities($eval) + )); + + // Run the command + @eval($eval); + } + } +} + +// [EOF] +?>