<commit_before><?php
/**
 * Class which parses the bulk upload CSV and creates the objects listed in it.
 * This engine class parses CSVs which describe vendor catalog item.
 *
 * @package plugins.bulkUploadCsv
 * @subpackage batch
 */
class BulkUploadVendorCatalogItemEngineCsv extends BulkUploadEngineCsv
{
	const OBJECT_TYPE_TITLE = 'vendor catalog item';
	const MANDATORY_COLUMN_MISSING = 'Mandatory Column missing from CSV';
	const EXCEEDED_MAX_RECORDS = 'Exceeded max records count per bulk';
	const NA = 'N\A';
	const PRICING_PER_UNIT = 'pricing:pricePerUnit';
	const PRICING_FUNCTION = 'pricing:priceFunction';
	const UTF = 'UTF-8';



	/**
	 * (non-PHPdoc)
	 * @see BulkUploadGeneralEngineCsv::createUploadResult()
	 */
	protected function createUploadResult($values, $columns)
	{
		$bulkUploadResult = parent::createUploadResult($values, $columns);
		if (!$bulkUploadResult)
		{
			return;
		}

		$bulkUploadResult->bulkUploadResultObjectType = KalturaBulkUploadObjectType::VENDOR_CATALOG_ITEM;
		array_walk($values, array('BulkUploadVendorCatalogItemEngineCsv', 'trimArray'));
		$this->setResultValues($columns, $values, $bulkUploadResult);

		$bulkUploadResult->status = KalturaBulkUploadResultStatus::IN_PROGRESS;
		$bulkUploadResult->objectStatus = KalturaVendorCatalogItemStatus::ACTIVE;

		if (!$bulkUploadResult->action)
		{
			$bulkUploadResult->action = KalturaBulkUploadAction::ADD;
		}

		$bulkUploadResult = $this->validateBulkUploadResult($bulkUploadResult);
		if($bulkUploadResult)
		{
			$this->bulkUploadResults[] = $bulkUploadResult;
		}
	}

	protected function setResultValues($columns, $values, &$bulkUploadResult)
	{
		$shouldConvertValueToEnum = array('serviceFeature', 'serviceType', 'turnAroundTime', 'outputFormat');
		$pricing = null;

		foreach($columns as $index => $column)
		{
			if(!is_numeric($index) || $values[$index] === self::NA || $values[$index] === '')
			{
				continue;
			}
			if (in_array($column, $shouldConvertValueToEnum) && isset($values[$index]))
			{
				self::handleEnumColumns($values[$index], $column, $bulkUploadResult);
			}
			else if(($column === self::PRICING_PER_UNIT || $column === self::PRICING_FUNCTION) && isset($values[$index]))
			{
				self::handlePriceColumns($pricing, $bulkUploadResult, $column, $values[$index]);
			}
			else if ($column === 'id' && isset($values[$index]))
			{
				$bulkUploadResult->vendorCatalogItemId = $values[$index];
				KalturaLog::info("Set value vendorCatalogItemId [{$bulkUploadResult->vendorCatalogItemId}]");
			}
			else if(iconv_strlen($values[$index], self::UTF))
			{
				$bulkUploadResult->$column = $values[$index];
				KalturaLog::info("Set value $column [{$bulkUploadResult->$column}]");
			}
			else
			{
				KalturaLog::info("Value $column is empty");
			}
		}
	}

	protected static function handlePriceColumns(&$pricing, $bulkUploadResult, $column, $value)
	{
		if (!$pricing)
		{
			$pricing = new KalturaVendorCatalogItemPricing();
			$bulkUploadResult->pricing = $pricing;
		}

		$columnName = substr($column,8); //removing 'pricing:'
		$bulkUploadPricing = $bulkUploadResult->pricing;
		$bulkUploadPricing->$columnName = $value;
		KalturaLog::info("Set value $column [{$bulkUploadPricing->$columnName}]");
	}

	protected static function handleEnumColumns($value, $column, $bulkUploadResult)
	{
		switch($column)
		{
			case 'serviceFeature':
				$enumValue = self::getEnumValue('KalturaVendorServiceFeature', $value);
				break;

			case 'serviceType':
				$enumValue = self::getEnumValue('KalturaVendorServiceType', $value);
				break;

			case 'turnAroundTime':
				$enumValue = self::getEnumValue('KalturaVendorServiceTurnAroundTime', $value);
				break;

			case 'outputFormat':
				$enumValue = self::getEnumValue('KalturaVendorCatalogItemOutputFormat', $value);
				break;

			default:
				$enumValue = null;
		}

		if ($enumValue)
		{
			$bulkUploadResult->$column = $enumValue;
			KalturaLog::info("Set value $column [{$bulkUploadResult->$column}]");
		}
	}

	protected function validateBulkUploadResult(KalturaBulkUploadResultVendorCatalogItem $bulkUploadResult)
	{
		$this->validateBulkUploadResultByAction($bulkUploadResult);

		if($this->maxRecords && $this->lineNumber > $this->maxRecords) // check max records
		{
			$this->handleResultError($bulkUploadResult, KalturaBatchJobErrorTypes::APP, self::EXCEEDED_MAX_RECORDS);
		}

		if($bulkUploadResult->status == KalturaBulkUploadResultStatus::ERROR)
		{
			$this->addBulkUploadResult($bulkUploadResult);
			return null;
		}
		return $bulkUploadResult;
	}

	protected function validateBulkUploadResultByAction($bulkUploadResult)
	{
		switch ($bulkUploadResult->action)
		{
			case KalturaBulkUploadAction::ADD:
				if (!$bulkUploadResult->vendorPartnerId && !$bulkUploadResult->serviceType &&
					!$bulkUploadResult->turnAroundTime && !$bulkUploadResult->pricing)
				{
					$this->handleResultError($bulkUploadResult, KalturaBatchJobErrorTypes::APP, self::MANDATORY_COLUMN_MISSING .' :vendorPartnerId, serviceType, turnAroundTime, pricing');
				}
				self::validateResultsByServiceFeature($bulkUploadResult);
				break;

			case KalturaBulkUploadAction::UPDATE:
				if (!$bulkUploadResult->vendorPartnerId)
				{
					$this->handleResultError($bulkUploadResult, KalturaBatchJobErrorTypes::APP, self::MANDATORY_COLUMN_MISSING . ' : vendorPartnerId');
				}
				break;

			default:
				break;
		}
	}

	protected function validateResultsByServiceFeature(KalturaBulkUploadResultVendorCatalogItem $bulkUploadResult)
	{
		switch ($bulkUploadResult->serviceFeature)
		{
			case VendorServiceFeature::CAPTIONS:<commit_msg>REACH2-840: make serviceFeature a mandatory param<commit_after><?php
/**
 * Class which parses the bulk upload CSV and creates the objects listed in it.
 * This engine class parses CSVs which describe vendor catalog item.
 *
 * @package plugins.bulkUploadCsv
 * @subpackage batch
 */
class BulkUploadVendorCatalogItemEngineCsv extends BulkUploadEngineCsv
{
	const OBJECT_TYPE_TITLE = 'vendor catalog item';
	const MANDATORY_COLUMN_MISSING = 'Mandatory Column missing from CSV';
	const EXCEEDED_MAX_RECORDS = 'Exceeded max records count per bulk';
	const NA = 'N\A';
	const PRICING_PER_UNIT = 'pricing:pricePerUnit';
	const PRICING_FUNCTION = 'pricing:priceFunction';
	const UTF = 'UTF-8';



	/**
	 * (non-PHPdoc)
	 * @see BulkUploadGeneralEngineCsv::createUploadResult()
	 */
	protected function createUploadResult($values, $columns)
	{
		$bulkUploadResult = parent::createUploadResult($values, $columns);
		if (!$bulkUploadResult)
		{
			return;
		}

		$bulkUploadResult->bulkUploadResultObjectType = KalturaBulkUploadObjectType::VENDOR_CATALOG_ITEM;
		array_walk($values, array('BulkUploadVendorCatalogItemEngineCsv', 'trimArray'));
		$this->setResultValues($columns, $values, $bulkUploadResult);

		$bulkUploadResult->status = KalturaBulkUploadResultStatus::IN_PROGRESS;
		$bulkUploadResult->objectStatus = KalturaVendorCatalogItemStatus::ACTIVE;

		if (!$bulkUploadResult->action)
		{
			$bulkUploadResult->action = KalturaBulkUploadAction::ADD;
		}

		$bulkUploadResult = $this->validateBulkUploadResult($bulkUploadResult);
		if($bulkUploadResult)
		{
			$this->bulkUploadResults[] = $bulkUploadResult;
		}
	}

	protected function setResultValues($columns, $values, &$bulkUploadResult)
	{
		$shouldConvertValueToEnum = array('serviceFeature', 'serviceType', 'turnAroundTime', 'outputFormat');
		$pricing = null;

		foreach($columns as $index => $column)
		{
			if(!is_numeric($index) || $values[$index] === self::NA || $values[$index] === '')
			{
				continue;
			}
			if (in_array($column, $shouldConvertValueToEnum) && isset($values[$index]))
			{
				self::handleEnumColumns($values[$index], $column, $bulkUploadResult);
			}
			else if(($column === self::PRICING_PER_UNIT || $column === self::PRICING_FUNCTION) && isset($values[$index]))
			{
				self::handlePriceColumns($pricing, $bulkUploadResult, $column, $values[$index]);
			}
			else if ($column === 'id' && isset($values[$index]))
			{
				$bulkUploadResult->vendorCatalogItemId = $values[$index];
				KalturaLog::info("Set value vendorCatalogItemId [{$bulkUploadResult->vendorCatalogItemId}]");
			}
			else if(iconv_strlen($values[$index], self::UTF))
			{
				$bulkUploadResult->$column = $values[$index];
				KalturaLog::info("Set value $column [{$bulkUploadResult->$column}]");
			}
			else
			{
				KalturaLog::info("Value $column is empty");
			}
		}
	}

	protected static function handlePriceColumns(&$pricing, $bulkUploadResult, $column, $value)
	{
		if (!$pricing)
		{
			$pricing = new KalturaVendorCatalogItemPricing();
			$bulkUploadResult->pricing = $pricing;
		}

		$columnName = substr($column,8); //removing 'pricing:'
		$bulkUploadPricing = $bulkUploadResult->pricing;
		$bulkUploadPricing->$columnName = $value;
		KalturaLog::info("Set value $column [{$bulkUploadPricing->$columnName}]");
	}

	protected static function handleEnumColumns($value, $column, $bulkUploadResult)
	{
		switch($column)
		{
			case 'serviceFeature':
				$enumValue = self::getEnumValue('KalturaVendorServiceFeature', $value);
				break;

			case 'serviceType':
				$enumValue = self::getEnumValue('KalturaVendorServiceType', $value);
				break;

			case 'turnAroundTime':
				$enumValue = self::getEnumValue('KalturaVendorServiceTurnAroundTime', $value);
				break;

			case 'outputFormat':
				$enumValue = self::getEnumValue('KalturaVendorCatalogItemOutputFormat', $value);
				break;

			default:
				$enumValue = null;
		}

		if ($enumValue)
		{
			$bulkUploadResult->$column = $enumValue;
			KalturaLog::info("Set value $column [{$bulkUploadResult->$column}]");
		}
	}

	protected function validateBulkUploadResult(KalturaBulkUploadResultVendorCatalogItem $bulkUploadResult)
	{
		$this->validateBulkUploadResultByAction($bulkUploadResult);

		if($this->maxRecords && $this->lineNumber > $this->maxRecords) // check max records
		{
			$this->handleResultError($bulkUploadResult, KalturaBatchJobErrorTypes::APP, self::EXCEEDED_MAX_RECORDS);
		}

		if($bulkUploadResult->status == KalturaBulkUploadResultStatus::ERROR)
		{
			$this->addBulkUploadResult($bulkUploadResult);
			return null;
		}
		return $bulkUploadResult;
	}

	protected function validateBulkUploadResultByAction($bulkUploadResult)
	{
		if (($bulkUploadResult->action == KalturaBulkUploadAction::ADD || $bulkUploadResult->action == KalturaBulkUploadAction::UPDATE)
		&& !$bulkUploadResult->serviceFeature)
		{
			return $this->handleResultError($bulkUploadResult, KalturaBatchJobErrorTypes::APP, self::MANDATORY_COLUMN_MISSING .' :serviceFeature');

		}

		switch ($bulkUploadResult->action)
		{
			case KalturaBulkUploadAction::ADD:
				if (!$bulkUploadResult->vendorPartnerId && !$bulkUploadResult->serviceType &&
					!$bulkUploadResult->turnAroundTime && !$bulkUploadResult->pricing)
				{
					$this->handleResultError($bulkUploadResult, KalturaBatchJobErrorTypes::APP, self::MANDATORY_COLUMN_MISSING .' :vendorPartnerId, serviceType, turnAroundTime, pricing');
				}
				self::validateResultsByServiceFeature($bulkUploadResult);
				break;

			case KalturaBulkUploadAction::UPDATE:
				if (!$bulkUploadResult->vendorPartnerId)
				{
					$this->handleResultError($bulkUploadResult, KalturaBatchJobErrorTypes::APP, self::MANDATORY_COLUMN_MISSING . ' : vendorPartnerId');
				}
				break;

			default:
				break;
		}
	}

	protected function validateResultsByServiceFeature(KalturaBulkUploadResultVendorCatalogItem $bulkUploadResult)
	{
		switch ($bulkUploadResult->serviceFeature)
		{
			case VendorServiceFeature::CAPTIONS:
{'fraction_non_alphanumeric': 0.13503460207612455, 'fraction_numerical': 0.0006920415224913495, 'mean_word_length': 5.65572826712723, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3404056', 'n_tokens_mistral': 4235, 'n_tokens_neox': 3762, 'n_words': 597}