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(); /** * @var array */ public $errors = array(); public function __construct(EntityManager $em, DataStore &$store) { $this->em = $em; $this->util = new ImportUtility($this->em); $this->store = $store; $this->errors = array(); } #-------------------------------------------------------------------------- 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 getYearEntry($vehicleYear) { $yearId = $this->util->getId( 'Stock\\Entity\\Year', array( 'name' => $vehicleYear ) ); if (false == $yearId) { array_push($this->errors,array('Could not find Year ' . $vehicleYear)); return $this->errors; // throw new \Exception( // 'Could not find Year ' . $vehicleYear // ); } return $yearId; } #-------------------------------------------------------------------------- SECTION /** * MAKE THIS PUBLIC!!!!!! * @param array $packet * @return mixed */ public function getMakeEntry($vehicleMmCode,$modelId) { $makeId = false; $makeEntity = $this->em->createQuery( 'SELECT model.id as MODELID,model.name as MODELNAME,make.id as MAKEID ' . 'FROM Stock\\Entity\\Model model ' . 'LEFT JOIN model.make make ' . 'WHERE model.id = :ModelId ' . 'AND model.archived = :Archived ' ) ->setParameter('ModelId', $modelId) ->setParameter('Archived', false) ->getScalarResult(); foreach($makeEntity as $make) { // \Utility\Debug::errorLog('$model', $model); $makeId = $make['MAKEID']; } if (false == $makeId) { array_push($this->errors,array('Could not find Make for MM Code ' . $vehicleMmCode)); return $this->errors; // throw new \Exception( // 'Could not find Model ' . $vehicleModel // ); } return $makeId; } #-------------------------------------------------------------------------- SECTION /** * @param array $packet * @param $makeId * @return mixed */ public function getCategoryEntry($vehicleMmCode,$typeId) { $categoryId = false; $categoryEntity = $this->em->createQuery( 'SELECT type.id as TYPEID,category.id as CATEGORYID,category.name as CATEGORYNAME ' . 'FROM Stock\\Entity\\Type type ' . 'LEFT JOIN type.category category ' . 'WHERE type.id = :TypeId ' . 'AND category.archived = :Archived ' ) ->setParameter('TypeId', $typeId) ->setParameter('Archived', false) ->getScalarResult(); foreach($categoryEntity as $category) { // \Utility\Debug::errorLog('$model', $model); $categoryId = $category['CATEGORYID']; } if (false == $categoryId) { array_push($this->errors,array('Could not find Category for MM Code ' . $vehicleMmCode)); return $this->errors; // throw new \Exception( // 'Could not find Model ' . $vehicleModel // ); } return $categoryId; } #-------------------------------------------------------------------------- SECTION /** * @param array $packet * @param $makeId * @return mixed */ public function getModelEntry($vehicleMmCode,$typeId) { $modelId = false; $modelEntity = $this->em->createQuery( 'SELECT type.id as TYPEID,model.id as MODELID,model.name as MODELNAME ' . 'FROM Stock\\Entity\\Type type ' . 'LEFT JOIN type.model model ' . 'WHERE type.id = :TypeId ' . 'AND model.archived = :Archived ' ) ->setParameter('TypeId', $typeId) ->setParameter('Archived', false) ->getScalarResult(); foreach($modelEntity as $model) { // \Utility\Debug::errorLog('$model', $model); $modelId = $model['MODELID']; } if (false == $modelId) { array_push($this->errors,array('Could not find Model for MM Code' . $vehicleMmCode)); return $this->errors; // throw new \Exception( // 'Could not find Model ' . $vehicleModel // ); } return $modelId; } #-------------------------------------------------------------------------- SECTION /** * @param array $packet * @param $modelId * @return mixed * @throws \Doctrine\ORM\ORMException */ public function getVehicleType($vehicleMmCode,$vehicleMake) { $typeId = false; #-> Do data preparations. /** * 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 */ $vehicleMmCode = (string)$vehicleMmCode; $vehicleMmCode = preg_replace('/\s+/', '', $vehicleMmCode); // #-> Find entry. // $typeId = $this->util->getId( // 'Stock\\Entity\\Type', // array( // 'mmCode' => $vehicleMmCode // ) // ); $typeEntity = $this->em->createQuery( 'SELECT type.id as TYPEID,type.name as TYPENAME ' . 'FROM Stock\\Entity\\Type type ' . 'WHERE type.mmCode = :MmCode ' . 'AND type.archived = :Archived ' ) ->setParameter('MmCode', $vehicleMmCode) ->setParameter('Archived', false) ->getScalarResult(); foreach($typeEntity as $type) { // \Utility\Debug::errorLog('$type', $type); $typeId = $type['TYPEID']; } if (false == $typeId) { array_push($this->errors,array('Could not find Type for MM Code ' .$vehicleMmCode)); return $this->errors; // throw new \Exception( // 'Could not find Type for MM Code ' .$vehicleMmCode // ); } return $typeId; } #-------------------------------------------------------------------------- SECTION /** * @param array $packet * @param $makeId * @return mixed */ public function getInteriorColourEntry($colour) { $colourId = false; $colourEntity = $this->em->createQuery( 'SELECT interiorColour.id,interiorColour.name ' . 'FROM Stock\\Entity\\InteriorColour interiorColour ' . 'WHERE interiorColour.archived = :Archived ' ) ->setParameter('Archived', false) ->getScalarResult(); foreach($colourEntity as $colours) { if (strpos(strtolower($colour), strtolower($colours['name'])) !== false) { $colourId = $colours['id']; } } if(false != $colourId) { return $colourId; } // if (false == $colourId) // { // array_push($this->errors,array('Could not find colour ' . $colour)); // // return $this->errors; // //// throw new \Exception( //// 'Could not find colour ' . $colour //// ); // } // return $colourId; } #-------------------------------------------------------------------------- SECTION /** * @param array $packet * @param $makeId * @return mixed */ public function getUpholsteryEntry($upholstery) { if('cloth' == strtolower($upholstery)) { $upholstery = 'Material'; } $upholsteryId = false; $upholsteryEntity = $this->em->createQuery( 'SELECT upholstery.id,upholstery.name ' . 'FROM Stock\\Entity\\Upholstery upholstery ' . 'WHERE upholstery.archived = :Archived ' ) ->setParameter('Archived', false) ->getScalarResult(); foreach($upholsteryEntity as $upholsteries) { if (strpos(strtolower($upholstery), strtolower($upholsteries['name'])) !== false) { $upholsteryId = $upholsteries['id']; } } if(false != $upholsteryId) { return $upholsteryId; } // if (false == $upholsteryId) // { // array_push($this->errors,array('Could not find upholstery ' . $upholstery)); // // return $this->errors; // //// throw new \Exception( //// 'Could not find colour ' . $upholstery //// ); // } // return $upholsteryId; } #-------------------------------------------------------------------------- SECTION /** * @param array $packet * @param $makeId * @return mixed */ public function getConditionEntry($condition) { $condition = substr($condition, 1); $conditionId = false; $conditionEntity = $this->em->createQuery( 'SELECT condition.id,condition.name ' . 'FROM Stock\\Entity\\Condition condition ' . 'WHERE condition.name LIKE :Condition ' . 'AND condition.archived = :Archived ' ) ->setParameter('Condition', $condition.'%') ->setParameter('Archived', false) ->getScalarResult(); foreach($conditionEntity as $conditions) { // \Utility\Debug::errorLog('$conditions', $conditions); $conditionId = $conditions['id']; } if(false != $conditionId) { return $conditionId; } // if (false == $conditionId) // { // array_push($this->errors,array('Could not find condition ' . $condition)); // // return $this->errors; // //// throw new \Exception( //// 'Could not find condition ' . $condition //// ); // } // return $conditionId; } #-------------------------------------------------------------------------- SECTION /** * @param array $packet * @param $makeId * @return mixed */ public function getTransmissionEntry($transmission) { $transmissionId = false; $transmissionEntity = $this->em->createQuery( 'SELECT transmissionType.id,transmissionType.name ' . 'FROM Stock\\Entity\\TransmissionType transmissionType ' . 'WHERE transmissionType.name = :TransmissionType ' . 'AND transmissionType.archived = :Archived ' ) ->setParameter('TransmissionType', $transmission) ->setParameter('Archived', false) ->getScalarResult(); foreach($transmissionEntity as $transmissions) { // \Utility\Debug::errorLog('$transmissions', $transmissions); $transmissionId = $transmissions['id']; } if(false != $transmissionId) { return $transmissionId; } // if (false == $transmissionId) // { // array_push($this->errors,array('Could not find transmission ' . $transmission)); // // return $this->errors; // // } // return $transmissionId; } #-------------------------------------------------------------------------- SECTION /** * @param array $packet * @param $makeId * @return mixed */ public function getFuelTypeEntry($fuelType) { $fuelTypeId = false; $fuelTypeEntity = $this->em->createQuery( 'SELECT fuelType.id,fuelType.name ' . 'FROM Stock\\Entity\\FuelType fuelType ' . 'WHERE fuelType.name = :FuelType ' . 'AND fuelType.archived = :Archived ' ) ->setParameter('FuelType', $fuelType) ->setParameter('Archived', false) ->getScalarResult(); foreach($fuelTypeEntity as $fuelTypes) { // \Utility\Debug::errorLog('$fuelTypes', $fuelTypes); $fuelTypeId = $fuelTypes['id']; } if(false != $fuelTypeId) { return $fuelTypeId; } // if (false == $fuelTypeId) // { // array_push($this->errors,array('Could not find fuel type ' . $fuelType)); // // return $this->errors; // } // return $fuelTypeId; } #-------------------------------------------------------------------------- SECTION /** * @param array $packet * @param $makeId * @return mixed */ public function getExteriorColourEntry($colour) { $colourId = false; $colourEntity = $this->em->createQuery( 'SELECT exteriorColour.id,exteriorColour.name ' . 'FROM Stock\\Entity\\ExteriorColour exteriorColour ' . 'WHERE exteriorColour.archived = :Archived ' ) ->setParameter('Archived', false) ->getScalarResult(); foreach($colourEntity as $colours) { if (strpos(strtolower($colour), strtolower($colours['name'])) !== false) { $colourId = $colours['id']; } } if(false != $colourId) { return $colourId; } // if (false == $colourId) // { // array_push($this->errors,array('Could not find colour ' . $colour)); // // return $this->errors; // //// throw new \Exception( //// 'Could not find colour ' . $colour //// ); // } // return $colourId; } /** * @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; } }