2 namespace Stock\Utility;
4 use Doctrine\ORM\EntityManager;
6 use Workspace\Service\DataBin as DataBinCore;
11 * @package Stock\Utility
13 class ImportHelper extends DataBinCore
19 * @var \Doctrine\ORM\EntityManager
23 * @var \Stock\Utility\ImportUtility
27 * @var \Stock\Utility\DataStore
33 protected $simulateExecution = false;
37 protected $categoryMap = array(
49 protected $typeFieldsToMatch = array(
50 'cylinders', 'cubicCapacity', 'kilowatts', 'bodyType', 'doors'
55 protected $numericFields = array(
56 'cylinders', 'cubicCapacity', 'kilowatts', 'doors'
61 protected $makes = array();
65 protected $models = array();
68 public function __construct(EntityManager $em, DataStore &$store)
71 $this->util = new ImportUtility($this->em);
72 $this->store = $store;
76 #-------------------------------------------------------------------------- SECTION
80 public function executeOfflineTransunionDataImport($fileName)
82 if ($this->simulateExecution)
84 error_log('php /var/www/namibia/public/index.php vehicle import '
86 . ' >>/log/php.log &');
91 'php /var/www/namibia/public/index.php vehicle import '
99 #-------------------------------------------------------------------------- SECTION
101 * Ensure that synchronization versions have a full sequence.
104 public function ensureSynchronizationVersionSequence()
106 #-> Get current latest versions.
107 $makeVersion = $this->util->getLatestSynchVersion('Stock\\Entity\\Make');
108 $modelVersion = $this->util->getLatestSynchVersion('Stock\\Entity\\Model');
109 $typeVersion = $this->util->getLatestSynchVersion('Stock\\Entity\\Type');
111 #-> Establish highest current version.
112 $maxVersion = $this->getMaxVersion($makeVersion, $modelVersion, $typeVersion);
114 #-> Where do we need updates?
115 if ($maxVersion > $makeVersion)
117 $this->updateSynchVersionSequence('Stock\\Entity\\Make', $makeVersion + 1, $maxVersion);
119 if ($maxVersion > $modelVersion)
121 $this->updateSynchVersionSequence('Stock\\Entity\\Model', $modelVersion + 1, $maxVersion);
123 if ($maxVersion > $typeVersion)
125 $this->updateSynchVersionSequence('Stock\\Entity\\Type', $typeVersion + 1, $maxVersion);
130 * @param $makeVersion
131 * @param $modelVersion
132 * @param $typeVersion
135 public function getMaxVersion($makeVersion, $modelVersion, $typeVersion)
137 $maxVersion = $makeVersion > $modelVersion
140 $maxVersion = $maxVersion > $typeVersion
147 * @param string $entityName
148 * @param integer $startVersion
149 * @param integer $endVersion
152 public function updateSynchVersionSequence($entityName, $startVersion, $endVersion)
154 #-> Get entries we can update.
155 $numEntriesNeeded = ((int)$endVersion) - ((int)$startVersion) + 1;
156 $entries = $this->em->getRepository($entityName)
158 array('updateVersion' => null),
159 array('id' => 'ASC'),
163 $this->store->version = ((int)$startVersion) + 1;
164 if ($numEntriesNeeded != count($entries))
166 throw new \Exception(
167 'Cannot synchronize data versions on ' . $entityName . '. Not enough entries available.'
170 foreach ($entries as $entry)
172 $entry->updateVersion = $this->store->version;
173 $this->store->version++;
179 #-------------------------------------------------------------------------- SECTION
181 * MAKE THIS PUBLIC!!!!!!
182 * @param array $packet
185 public function cleanupNumericInputFields(array $packet)
187 foreach ($this->numericFields as $field)
189 if (array_key_exists($field, $packet))
191 $packet[$field] = ('' == $packet[$field])
200 #-------------------------------------------------------------------------- SECTION
202 * MAKE THIS PUBLIC!!!!!!
203 * @param array $packet
206 public function createOrUpdateVehicleMakeEntry(array $packet)
208 if (!isset($this->makes[$packet['Make']]))
210 $makeId = $this->getOrCreateMakeEntry($packet);
211 $this->makes[$packet['Make']] = $makeId;
213 return $this->makes[$packet['Make']];
217 * MAKE THIS PUBLIC!!!!!!
218 * @param array $packet
221 public function getOrCreateMakeEntry($packet)
223 $makeId = $this->util->getId(
224 'Stock\\Entity\\Make',
226 'name' => $packet['Make']
229 if (false == $makeId)
231 $makeId = $this->util->createEntry(
232 'Stock\\Entity\\Make',
234 'name' => $packet['Make'],
235 'createVersion' => $this->store->version,
236 'updateVersion' => $this->store->version
239 $this->store->numItems++;
246 #-------------------------------------------------------------------------- SECTION
248 * MAKE THIS PUBLIC!!!!!!
249 * @param array $packet
252 * @throws \Doctrine\ORM\ORMException
254 public function createOrUpdateVehicleModel(array $packet, $makeId)
256 if (!isset($this->models[$makeId]) || !isset($this->models[$makeId][$packet['Model']]))
258 $modelId = $this->getOrCreateModelEntry(
261 $this->models[$makeId][$packet['Model']] = $modelId;
263 return $this->models[$makeId][$packet['Model']];
267 * @param array $packet
271 public function getOrCreateModelEntry(array $packet, $makeId)
273 if (!isset($this->models[$makeId]))
275 $this->models[$makeId] = array();
277 $modelId = $this->util->getId(
278 'Stock\\Entity\\Model',
281 'name' => $packet['Model']
284 if (false == $modelId)
286 $modelId = $this->util->createEntry(
287 'Stock\Entity\Model',
289 'make' => $this->em->getReference('Stock\\Entity\\Make', $makeId),
290 'name' => $packet['Model'],
291 'createVersion' => $this->store->version,
292 'updateVersion' => $this->store->version
295 $this->store->numItems++;
302 #-------------------------------------------------------------------------- SECTION
304 * @param array $packet
307 * @throws \Doctrine\ORM\ORMException
309 public function createOrUpdateVehicleType(array $packet, $modelId)
311 #-> Do data preparations.
312 $packet['name'] = $packet['Variant'];
313 $packet = $this->prepareAdditionalVehicleTypeData($packet);
314 $years = $this->buildYearMapArray();
315 $packet = $this->prepareDateVehicleTypeData($packet, $years);
318 * Here we ensure that we remove spaces and retain leading zeroes to prevent data duplication.
319 * Previously, this field was cast to int which lead to duplications. - FP
321 $packet['mmCode'] = (string)$packet['MMCode'];
322 $packet['mmCode'] = preg_replace('/\s+/', '', $packet['mmCode']);
325 $type = $this->util->getEntry('Stock\\Entity\\Type', array(
327 'mmCode' => $packet['mmCode']
331 $packet = $this->prepareVehicleTypeDataForNewEntry($packet, $modelId);
332 $this->util->createEntry('Stock\\Entity\\Type', $packet);
333 $this->store->numItems++;
335 else if ($this->vehicleTypeUpdateIsNeeded($packet, $type))
337 $type->updateVersion = $this->store->version;
338 $this->em->flush($type);
339 $this->store->numItems++;
345 * @param array $packet
348 public function prepareAdditionalVehicleTypeData(array $packet)
350 $packet['cylinders'] = $packet['NoCylinders'];
351 $packet['cubicCapacity'] = $packet['CubicCapacity'];
352 $packet['kilowatts'] = $packet['Kilowatts'];
353 $packet['bodyType'] = $packet['BodyType'];
354 $packet['doors'] = $packet['NoOfDoors'];
361 public function buildYearMapArray()
363 $nextYear = ((int)date('Y')) + 1;
366 for ($y = 1970; $y < $nextYear; $y++)
375 * @param array $packet
376 * @param array $years
378 * @throws \Doctrine\ORM\ORMException
380 public function prepareDateVehicleTypeData(array $packet, $years)
382 list($introMonth, $introYear) = explode('/', $packet['IntroDate']);
383 $discontinueYear = false;
384 $discontinueMonth = false;
385 if (!empty($packet['DiscDate']))
387 list($discontinueMonth, $discontinueYear) = explode('/', $packet['DiscDate']);
389 unset($packet['IntroDate']);
390 unset($packet['DiscDate']);
391 $packet['introYear'] = $this->em->getReference(
392 '\Stock\Entity\Year',
395 $packet['introMonth'] = $introMonth;
396 $packet['discYear'] = null;
397 if ($discontinueYear)
399 $packet['discYear'] = $this->em->getReference(
400 'Stock\\Entity\\Year', $years[$discontinueYear]
402 $packet['discMonth'] = $discontinueMonth;
409 * @param array $packet
412 * @throws \Doctrine\ORM\ORMException
414 public function prepareVehicleTypeDataForNewEntry(array $packet, $modelId)
416 $packet['VehicleType'] = $this->em->getReference(
417 'Stock\\Entity\\Category',
418 $this->categoryMap[$packet['VehicleType']]
420 $packet['Model'] = $this->em->getReference(
421 '\Stock\\Entity\\Model',
424 $packet['name'] = $packet['Variant'];
425 $packet['createVersion'] = $this->store->version;
426 $packet['updateVersion'] = $this->store->version;
431 * @param array $packet
435 public function vehicleTypeUpdateIsNeeded(array $packet, $type)
438 foreach ($this->typeFieldsToMatch as $field)
440 if ($packet[$field] != $type->$field)
443 $type->$field = $packet[$field];
447 if (false != $packet['discYear'] && is_null($type->discYear))
450 $type->discYear = $packet['discYear'];
451 $type->discMonth = $packet['discMonth'];
459 #-------------------------------------------------------------------------- SECTION
463 public function processBatch()
469 $this->em->getConnection()->close();
470 $this->em->beginTransaction();
471 $this->store->version++;
472 $this->store->numItems = 0;