724980be21c988f28f0374ec3c4d1392449b103e
[shipsimu.git] / application / ship-simu / main / personell / class_SimulatorPersonell.php
1 <?php
2 /**
3  * The general simulator personell class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.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 class SimulatorPersonell extends BasePersonell {
25         // Personell list
26         private $personellList = null;
27
28         // A cache for lists
29         private $cacheList = null;
30
31         // A string for cached conditions
32         private $cacheCond = null;
33
34         /**
35          * Protected constructor
36          *
37          * @return      void
38          */
39         protected function __construct () {
40                 // Call parent constructor
41                 parent::__construct(__CLASS__);
42
43                 // Set description
44                 $this->setObjectDescription("Simulationspersonal");
45
46                 // Create unique ID
47                 $this->generateUniqueId();
48
49                 // Clean-up a little
50                 $this->removeSystemArray();
51         }
52
53         /**
54          * Magic wake-up method called when unserialize() is called. This is
55          * neccessary because in this case a personell does not need to know the
56          * min/max ages range and system classes. This would anyway use more RAM
57          * what is not required.
58          *
59          * @return      void
60          */
61         public function __wakeup () {
62                 // Tidy up a little
63                 $this->removePersonellList();
64                 $this->removeMinMaxAge();
65                 $this->removeCache();
66                 $this->removeSystemArray();
67         }
68
69         /**
70          * Generate a specified amount of personell and return the prepared instance
71          *
72          * @param               $amountPersonell                Number of personell we shall
73          *                                                              generate
74          * @return      $personellInstance              An instance of this object with a
75          *                                                              list of personells
76          */
77         public final static function createSimulatorPersonell ($amountPersonell) {
78                 // Make sure only integer can pass
79                 $amountPersonell = (int) $amountPersonell;
80
81                 // Get a new instance
82                 $personellInstance = new SimulatorPersonell();
83
84                 // Debug message
85                 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $personellInstance->getDebugInstance()->output(sprintf("[%s:%d] Es werden <strong>%d</strong> Personal bereitgestellt.",
86                         __CLASS__,
87                         __LINE__,
88                         $amountPersonell
89                 ));
90
91                 // Initialize the personell list
92                 $personellInstance->createPersonellList();
93
94                 // Create requested amount of personell
95                 for ($idx = 0; $idx < $amountPersonell; $idx++) {
96                         $personellInstance->addRandomPersonell();
97                 }
98
99                 // Debug message
100                 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $personellInstance->getDebugInstance()->output(sprintf("[%s:%d] <strong>%d</strong> Personal bereitgestellt.",
101                         __CLASS__,
102                         __LINE__,
103                         $amountPersonell
104                 ));
105
106                 // Tidy up a little
107                 $personellInstance->removeGender();
108                 $personellInstance->removeNames();
109                 $personellInstance->removeBirthday();
110                 $personellInstance->removeSalary();
111                 $personellInstance->removeEmployed();
112                 $personellInstance->removeMarried();
113                 $personellInstance->removeNumberFormaters();
114                 //$personellInstance->removeCache();
115                 $personellInstance->removeSystemArray();
116
117                 // Instanz zurueckgeben
118                 return $personellInstance;
119         }
120
121         /**
122          * Create a SimulatorPersonell object by loading the specified personell
123          * list from an existing database backend
124          *
125          * @param               $idNumber               The ID number (only right part) of the list
126          * @return      $personellInstance      An instance of
127          * @throws      InvalidIDFormatException                If the given id number
128          *                                                                      $idNumber is invalid
129          * @throws      NullPointerException            If a null pointer (instance)
130          *                                                                      has been returned.
131          * @throws      NoObjectException                       If a non-object has been
132          *                                                                      returned
133          * @throws      MissingMethodException          If a required method is missing
134          * @throws      MissingSimulatorIDException     If an ID number was not found
135          */
136         public final static function createSimulatorPersonellByID ($idNumber) {
137                 // Add the class name if it was not found
138                 if (count(explode("@", $idNumber)) < 2) {
139                         // Add class name in front of the incomplete ID number
140                         $tempID = sprintf("%s@%s", __CLASS__, $idNumber);
141                 } else {
142                         // Use the direct ID number
143                         $tempID = $idNumber;
144                 }
145
146                 // Validate the ID number
147                 if (!preg_match(sprintf("/%s\@([a-f0-9]){32}/i", __CLASS__), $tempID)) {
148                         // Invalid format
149                         throw new InvalidIDFormatException(new SimulatorPersonell(), self::EXCEPTION_ID_IS_INVALID_FORMAT);
150                 }
151
152                 // Get instance
153                 $personellInstance = new SimulatorPersonell(false);
154
155                 // Get database instance
156                 $dbInstance = $personellInstance->getDatabaseInstance();
157
158                 // Is this a valid database instance?
159                 if (is_null($dbInstance)) {
160                         // No class returned
161                         throw new NullPointerException($personellInstance, self::EXCEPTION_IS_NULL_POINTER);
162                 } elseif (!is_object($dbInstance)) {
163                         // Not an object! ;-(
164                         throw new NoObjectException($dbInstance, self::EXCEPTION_IS_NO_OBJECT);
165                 } elseif (!method_exists($dbInstance, 'isUniqueIdUsed')) {
166                         // Required method not found
167                         throw new MissingMethodException(array($dbInstance, 'isUniqueIdUsed'), self::EXCEPTION_MISSING_METHOD);
168                 }
169
170                 // Is the unique ID already used? Then it must be there!
171                 if (!$dbInstance->isUniqueIdUsed($tempID))  {
172                         // Entry not found!
173                         throw new MissingSimulatorIDException(array($personellInstance, $idNumber), self::EXCEPTION_SIMULATOR_ID_INVALID);
174                 }
175
176                 // Load the personell list and add it to this object
177                 $personellInstance->loadPersonellList($tempID);
178
179                 // Clean-up a little
180                 $personellInstance->removeGender();
181                 $personellInstance->removeNames();
182                 $personellInstance->removeBirthday();
183                 $personellInstance->removeSalary();
184                 $personellInstance->removeEmployed();
185                 $personellInstance->removeMarried();
186                 $personellInstance->removeNumberFormaters();
187                 //$personellInstance->removeCache();
188                 $personellInstance->removeSystemArray();
189
190                 // Return instance
191                 return $personellInstance;
192         }
193
194         // Create personell list
195         public function createPersonellList () {
196                 if (is_null($this->personellList)) {
197                         $this->personellList = new FrameworkArrayObject("FakedPersonellList");
198                 } else {
199                         throw new PersonellListAlreadyCreatedException($this, self::EXCEPTION_DIMENSION_ARRAY_INVALID);
200                 }
201         }
202
203         // Remove the personell list
204         private function removePersonellList () {
205                 unset($this->personellList);
206         }
207
208         // Add new personell object to our list
209         public function addRandomPersonell () {
210                 // Gender list...
211                 $genders = array('M', 'F');
212
213                 // Create new personell members
214                 $personellInstance = new SimulatorPersonell();
215
216                 // Set a randomized gender
217                 $personellInstance->setGender($genders[mt_rand(0, 1)]);
218
219                 // Set a randomized birthday (maximum age required, see const MAX_AGE)
220                 $personellInstance->createBirthday();
221
222                 // Married? Same values means: married
223                 if (mt_rand(0, 5) == mt_rand(0, 5)) $personellInstance->setMarried(true);
224
225                 // Tidy up a little
226                 $personellInstance->removePersonellList();
227                 $personellInstance->removeMinMaxAge();
228                 $personellInstance->removeCache();
229                 $personellInstance->removeSystemArray();
230
231                 // Add new member to the list
232                 $this->personellList->append($personellInstance);
233         }
234
235         /**
236          * Get a specifyable list of our people, null or empty string will be ignored!
237          *
238          * @return      $cacheList      A list of cached personells
239          */
240         function getSpecialPersonellList ($isEmployed = null, $isMarried = null, $hasGender = "") {
241                 // Serialize the conditions for checking if we can take the cache
242                 $serialized = serialize(array($isEmployed, $isMarried, $hasGender));
243
244                 // The same (last) conditions?
245                 if (($serialized == $this->cacheCond) && (!is_null($this->cacheCond))) {
246                         if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Gecachte Liste wird verwendet.",
247                                 __CLASS__,
248                                 __LINE__
249                         ));
250
251                         // Return cached list
252                         return $this->cacheList;
253                 }
254
255                 // Output debug message
256                 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Personalliste wird nach Kriterien durchsucht...",
257                         __CLASS__,
258                         __LINE__
259                 ));
260
261                 // Remember the conditions
262                 $this->setCacheCond($serialized);
263
264                 // Create cached list
265                 $this->setAllCacheList(new FrameworkArrayObject("FakedCacheList"));
266
267                 // Search all unemployed personells
268                 for ($idx = $this->personellList->getIterator(); $idx->valid(); $idx->next()) {
269                         // Element holen
270                         $el = $idx->current();
271
272                         // Check currenylt all single conditions (combined conditions are not yet supported)
273                         if ((!is_null($isEmployed)) && ($el->isEmployed() == $isEmployed)) {
274                                 // Add this one (employed status asked)
275                                 $this->cacheList->append($el);
276                         } elseif ((!is_null($isMarried)) && ($el->isMarried() == $isMarried)) {
277                                 // Add this one (marrital status asked)
278                                 $this->cacheList->append($el);
279                         } elseif ((!empty($hasGender)) && ($el->getGender() == $hasGender)) {
280                                 // Add this one (specified gender)
281                                 $this->cacheList->append($el);
282                         }
283                 }
284
285                 // Return the completed list
286                 return $this->cacheList;
287         }
288
289         /**
290          * Get amount of unemployed personell
291          *
292          * @return      $count  Amount of unemployed personell
293          */
294         public final function getAllUnemployed () {
295                 // Get a temporary list
296                 $list = $this->getSpecialPersonellList(false);
297
298                 // Anzahl zurueckliefern
299                 return $list->count();
300         }
301
302         /**
303          * Remove cache things
304          *
305          * @return      void
306          */
307         private function removeCache () {
308                 // Remove cache data
309                 unset($this->cacheList);
310                 unset($this->cacheCond);
311         }
312
313         /**
314          * Setter for cache list
315          *
316          * @param               $cacheList      The new cache list to set or null for initialization/reset
317          * @return      void
318          */
319         private final function setAllCacheList (FrameworkArrayObject $cacheList = null) {
320                 $this->cacheList = $cacheList;
321         }
322
323         /**
324          * Setter for cache conditions
325          *
326          * @param               $cacheCond      The new cache conditions to set
327          * @return      void
328          */
329         private final function setCacheCond ($cacheCond) {
330                 $this->cacheCond = (string) $cacheCond;
331         }
332
333         /**
334          * Reset cache list
335          *
336          * @return      void
337          */
338         public function resetCache () {
339                 $this->setAllCacheList(null);
340                 $this->setCacheCond("");
341         }
342
343         /**
344          * Getter for surname. If no surname is set then default surnames are set
345          * for male and female personells.
346          *
347          * @return      $surname                The personell' surname
348          */
349         public final function getSurname () {
350                 $surname = parent::getSurname();
351                 // Make sure every one has a surname...
352                 if (empty($surname)) {
353                         if ($this->isMale()) {
354                                 // Typical male name
355                                 $surname = "John";
356                         } else {
357                                 // Typical female name
358                                 $surname = "Jennifer";
359                         }
360
361                         // Set typical family name
362                         parent::setFamily("Smith");
363                 }
364                 return $surname;
365         }
366
367         /**
368          * Getter for personell list
369          *
370          * @return      $personellList          The list of all personells
371          */
372         public final function getPersonellList () {
373                 return $this->personellList;
374         }
375
376         /**
377          * Loads the mostly pre-cached personell list
378          *
379          * @param               $idNumber               The ID number we shall use for looking up
380          *                                              the right data.
381          * @return      void
382          * @throws      ContainerItemIsNullException    If a container item is null
383          * @throws      ContainerItemIsNoArrayException If a container item is
384          *                                                                              not an array
385          * @throws      ContainerMaybeDamagedException  If the container item
386          *                                                                              is missing the indexes
387          *                                                                              'name' and/or 'value'
388          * @see         SerializationContainer  A special container class which
389          *                                                              helps storing only some attributes
390          *                                                              of a class.
391          */
392         public function loadPersonellList ($idNumber) {
393                 // Cleared because old code
394                 $this->partialStub("Clear because of old lost code was usaged.");
395         }
396 }
397
398 // [EOF]
399 ?>