Git Repository Public Repository

namibia

URLs

Copy to Clipboard
 
df0489e1eeeeab5a9bd44e1d84fce49924fe1bac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?php
namespace Stock\Utility;

use Doctrine\ORM\EntityManager;
use Utility\Debug;
use Workspace\Service\DataBin as DataBinCore;


/**
 * Class ImportHelper
 * @package Stock\Utility
 */
class ImportHelper extends DataBinCore
{



	/**
	 * @var \Doctrine\ORM\EntityManager
	 */
	protected $em;
	/**
	 * @var \Stock\Utility\ImportUtility
	 */
	protected $util;
	/**
	 * @var \Stock\Utility\DataStore
	 */
	protected $store;
	/**
	 * @var bool
	 */
	protected $simulateExecution = false;
	/**
	 * @var array
	 */
	protected $categoryMap = array(
		'T' => 1,
		'A' => 2,
		'C' => 3,
		'B' => 4,
		'M' => 5,
		'H' => 6,
		'Z' => 7
	);
	/**
	 * @var array
	 */
	protected $typeFieldsToMatch = array(
		'cylinders', 'cubicCapacity', 'kilowatts', 'bodyType', 'doors'
	);
	/**
	 * @var array
	 */
	protected $numericFields = array(
		'cylinders', 'cubicCapacity', 'kilowatts', 'doors'
	);
	/**
	 * @var array
	 */
	protected $makes = array();
	/**
	 * @var array
	 */
	protected $models = array();


	public function __construct(EntityManager $em, DataStore &$store)
	{
		$this->em    = $em;
		$this->util  = new ImportUtility($this->em);
		$this->store = $store;
	}


	#-------------------------------------------------------------------------- SECTION
	/**
	 * @param $fileName
	 */
	public function executeOfflineTransunionDataImport($fileName)
	{
		if ($this->simulateExecution)
		{
			error_log('php /var/www/B4C2/public/index.php vehicle import '
			          . $fileName
			          . ' >>/log/php.log &');
		}
		else
		{
			exec(
				'php /var/www/B4C2/public/index.php vehicle import '
				. $fileName
				. ' >>/log/php.log &'
			);
		}
	}


	#-------------------------------------------------------------------------- SECTION
	/**
	 * Ensure that synchronization versions have a full sequence.
	 * return null
	 */
	public function ensureSynchronizationVersionSequence()
	{
		#-> Get current latest versions.
		$makeVersion  = $this->util->getLatestSynchVersion('Stock\\Entity\\Make');
		$modelVersion = $this->util->getLatestSynchVersion('Stock\\Entity\\Model');
		$typeVersion  = $this->util->getLatestSynchVersion('Stock\\Entity\\Type');

		#-> Establish highest current version.
		$maxVersion = $this->getMaxVersion($makeVersion, $modelVersion, $typeVersion);

		#-> Where do we need updates?
		if ($maxVersion > $makeVersion)
		{
			$this->updateSynchVersionSequence('Stock\\Entity\\Make', $makeVersion + 1, $maxVersion);
		}
		if ($maxVersion > $modelVersion)
		{
			$this->updateSynchVersionSequence('Stock\\Entity\\Model', $modelVersion + 1, $maxVersion);
		}
		if ($maxVersion > $typeVersion)
		{
			$this->updateSynchVersionSequence('Stock\\Entity\\Type', $typeVersion + 1, $maxVersion);
		}
	}

	/**
	 * @param $makeVersion
	 * @param $modelVersion
	 * @param $typeVersion
	 * @return mixed
	 */
	public function getMaxVersion($makeVersion, $modelVersion, $typeVersion)
	{
		$maxVersion = $makeVersion > $modelVersion
			? $makeVersion
			: $modelVersion;
		$maxVersion = $maxVersion > $typeVersion
			? $maxVersion
			: $typeVersion;
		return $maxVersion;
	}

	/**
	 * @param string  $entityName
	 * @param integer $startVersion
	 * @param integer $endVersion
	 * @throws \Exception
	 */
	public function updateSynchVersionSequence($entityName, $startVersion, $endVersion)
	{
		#-> Get entries we can update.
		$numEntriesNeeded     = ((int)$endVersion) - ((int)$startVersion) + 1;
		$entries              = $this->em->getRepository($entityName)
			->findBy(
				array('updateVersion' => null),
				array('id' => 'ASC'),
				$numEntriesNeeded,
				0
			);
		$this->store->version = ((int)$startVersion) + 1;
		if ($numEntriesNeeded != count($entries))
		{
			throw new \Exception(
				'Cannot synchronize data versions on ' . $entityName . '. Not enough entries available.'
			);
		}
		foreach ($entries as $entry)
		{
			$entry->updateVersion = $this->store->version;
			$this->store->version++;
		}
		$this->em->flush();
	}


	#-------------------------------------------------------------------------- SECTION
	/**
	 * MAKE THIS PUBLIC!!!!!!
	 * @param array $packet
	 * @return array
	 */
	public function cleanupNumericInputFields(array $packet)
	{
		foreach ($this->numericFields as $field)
		{
			if (array_key_exists($field, $packet))
			{
				$packet[$field] = ('' == $packet[$field])
					? null
					: $packet[$field];
			}
		}
		return $packet;
	}


	#-------------------------------------------------------------------------- SECTION
	/**
	 * MAKE THIS PUBLIC!!!!!!
	 * @param array $packet
	 * @return mixed
	 */
	public function createOrUpdateVehicleMakeEntry(array $packet)
	{
		if (!isset($this->makes[$packet['Make']]))
		{
			$makeId                       = $this->getOrCreateMakeEntry($packet);
			$this->makes[$packet['Make']] = $makeId;
		}
		return $this->makes[$packet['Make']];
	}

	/**
	 * MAKE THIS PUBLIC!!!!!!
	 * @param array $packet
	 * @return mixed
	 */
	public function getOrCreateMakeEntry($packet)
	{
		$makeId = $this->util->getId(
			'Stock\\Entity\\Make',
			array(
				'name' => $packet['Make']
			)
		);
		if (false == $makeId)
		{
			$makeId = $this->util->createEntry(
				'Stock\\Entity\\Make',
				array(
					'name'          => $packet['Make'],
					'createVersion' => $this->store->version,
					'updateVersion' => $this->store->version
				)
			);
			$this->store->numItems++;
			return $makeId;
		}
		return $makeId;
	}


	#-------------------------------------------------------------------------- SECTION
	/**
	 * MAKE THIS PUBLIC!!!!!!
	 * @param array $packet
	 * @param $makeId
	 * @return mixed
	 * @throws \Doctrine\ORM\ORMException
	 */
	public function createOrUpdateVehicleModel(array $packet, $makeId)
	{
		if (!isset($this->models[$makeId]) || !isset($this->models[$makeId][$packet['Model']]))
		{
			$modelId                                 = $this->getOrCreateModelEntry(
				$packet, $makeId
			);
			$this->models[$makeId][$packet['Model']] = $modelId;
		}
		return $this->models[$makeId][$packet['Model']];
	}

	/**
	 * @param array $packet
	 * @param       $makeId
	 * @return mixed
	 */
	public function getOrCreateModelEntry(array $packet, $makeId)
	{
		if (!isset($this->models[$makeId]))
		{
			$this->models[$makeId] = array();
		}
		$modelId = $this->util->getId(
			'Stock\\Entity\\Model',
			array(
				'make' => $makeId,
				'name' => $packet['Model']
			)
		);
		if (false == $modelId)
		{
			$modelId = $this->util->createEntry(
				'Stock\Entity\Model',
				array(
					'make'          => $this->em->getReference('Stock\\Entity\\Make', $makeId),
					'name'          => $packet['Model'],
					'createVersion' => $this->store->version,
					'updateVersion' => $this->store->version
				)
			);
			$this->store->numItems++;
			return $modelId;
		}
		return $modelId;
	}


	#-------------------------------------------------------------------------- SECTION
	/**
	 * @param array $packet
	 * @param $modelId
	 * @return mixed
	 * @throws \Doctrine\ORM\ORMException
	 */
	public function createOrUpdateVehicleType(array $packet, $modelId)
	{
		#-> Do data preparations.
		$packet['name'] = $packet['Variant'];
		$packet         = $this->prepareAdditionalVehicleTypeData($packet);
		$years          = $this->buildYearMapArray();
		$packet         = $this->prepareDateVehicleTypeData($packet, $years);

		/**
		 * Here we ensure that we remove spaces and retain leading zeroes to prevent data duplication.
		 * Previously, this field was cast to int which lead to duplications. - FP
		*/
		$packet['mmCode'] = (string)$packet['MMCode'];
		$packet['mmCode'] = preg_replace('/\s+/', '', $packet['mmCode']);

		#-> Find entry.
		$type             = $this->util->getEntry('Stock\\Entity\\Type', array(
			'model'  => $modelId,
			'mmCode' => $packet['mmCode']
		));
		if (false == $type)
		{
			$packet = $this->prepareVehicleTypeDataForNewEntry($packet, $modelId);
			$this->util->createEntry('Stock\\Entity\\Type', $packet);
			$this->store->numItems++;
		}
		else if ($this->vehicleTypeUpdateIsNeeded($packet, $type))
		{
			$type->updateVersion = $this->store->version;
			$this->em->flush($type);
			$this->store->numItems++;
		}
		return $packet;
	}

	/**
	 * @param array $packet
	 * @return array
	 */
	public function prepareAdditionalVehicleTypeData(array $packet)
	{
		$packet['cylinders']     = $packet['NoCylinders'];
		$packet['cubicCapacity'] = $packet['CubicCapacity'];
		$packet['kilowatts']     = $packet['Kilowatts'];
		$packet['bodyType']      = $packet['BodyType'];
		$packet['doors']         = $packet['NoOfDoors'];
		return $packet;
	}

	/**
	 * @return array
	 */
	public function buildYearMapArray()
	{
		$nextYear = ((int)date('Y')) + 1;
		$years    = array();
		$i        = 1;
		for ($y = 1970; $y < $nextYear; $y++)
		{
			$years[$y] = $i;
			$i++;
		}
		return $years;
	}

	/**
	 * @param array $packet
	 * @param array $years
	 * @return array
	 * @throws \Doctrine\ORM\ORMException
	 */
	public function prepareDateVehicleTypeData(array $packet, $years)
	{
		list($introMonth, $introYear) = explode('/', $packet['IntroDate']);
		$discontinueYear  = false;
		$discontinueMonth = false;
		if (!empty($packet['DiscDate']))
		{
			list($discontinueMonth, $discontinueYear) = explode('/', $packet['DiscDate']);
		}
		unset($packet['IntroDate']);
		unset($packet['DiscDate']);
		$packet['introYear']  = $this->em->getReference(
			'\Stock\Entity\Year',
			$years[$introYear]
		);
		$packet['introMonth'] = $introMonth;
		$packet['discYear']   = null;
		if ($discontinueYear)
		{
			$packet['discYear']  = $this->em->getReference(
				'Stock\\Entity\\Year', $years[$discontinueYear]
			);
			$packet['discMonth'] = $discontinueMonth;
			return $packet;
		}
		return $packet;
	}

	/**
	 * @param array $packet
	 * @param       $modelId
	 * @return array
	 * @throws \Doctrine\ORM\ORMException
	 */
	public function prepareVehicleTypeDataForNewEntry(array $packet, $modelId)
	{
		$packet['VehicleType']   = $this->em->getReference(
			'Stock\\Entity\\Category',
			$this->categoryMap[$packet['VehicleType']]
		);
		$packet['Model']         = $this->em->getReference(
			'\Stock\\Entity\\Model',
			$modelId
		);
		$packet['name']          = $packet['Variant'];
		$packet['createVersion'] = $this->store->version;
		$packet['updateVersion'] = $this->store->version;
		return $packet;
	}

	/**
	 * @param array $packet
	 * @param       $type
	 * @return bool
	 */
	public function vehicleTypeUpdateIsNeeded(array $packet, $type)
	{
		$needUpdate = false;
		foreach ($this->typeFieldsToMatch as $field)
		{
			if ($packet[$field] != $type->$field)
			{
				$needUpdate   = true;
				$type->$field = $packet[$field];
			}
			if (!$needUpdate)
			{
				if (false != $packet['discYear'] && is_null($type->discYear))
				{
					$needUpdate      = true;
					$type->discYear  = $packet['discYear'];
					$type->discMonth = $packet['discMonth'];
				}
			}
		}
		return $needUpdate;
	}


	#-------------------------------------------------------------------------- SECTION
	/**
	 * @return null
	 */
	public function processBatch()
	{
		usleep(500);
		$this->em->flush();
		$this->em->commit();
		$this->em->clear();
		$this->em->getConnection()->close();
		$this->em->beginTransaction();
		$this->store->version++;
		$this->store->numItems = 0;
	}

}

Commits for namibiamodule/Stock/src/Stock/Utility/ImportHelper.php

Diff revisions: vs.
Revision Author Commited Message
df0489 ... Mark Fri 14 Oct, 2016 10:01:00 +0000

initial commit