/** * Code to be run after inserting to database * @param PropelPDO $con */ public function postInsert(PropelPDO $con = null) { MetadataProfileFieldPeer::setUseCriteriaFilter(false); $this->reload(); MetadataProfileFieldPeer::setUseCriteriaFilter(true); kEventsManager::raiseEvent(new kObjectCreatedEvent($this)); if ($this->copiedFrom) { kEventsManager::raiseEvent(new kObjectCopiedEvent($this->copiedFrom, $this)); } }
/** * @param string $entryId the new created entry * @param array $data key => value pairs */ public static function handleBulkUploadData($entryId, array $data) { KalturaLog::debug("Handle metadata bulk upload data:\n" . print_r($data, true)); if (!isset($data[self::BULK_UPLOAD_COLUMN_PROFILE_ID])) { return; } $metadataProfileId = $data[self::BULK_UPLOAD_COLUMN_PROFILE_ID]; $xmlData = null; $entry = entryPeer::retrieveByPK($entryId); if (!$entry) { return; } // $criteriaFilter = FileSyncPeer::getCriteriaFilter(); // $criteria = $criteriaFilter->getFilter(); // $criteria->add(FileSyncPeer::PARTNER_ID, $entry->getPartnerId()); $metadataProfile = MetadataProfilePeer::retrieveById($metadataProfileId); if (!$metadataProfile) { $errorMessage = "Metadata profile [{$metadataProfileId}] not found"; KalturaLog::err($errorMessage); self::addBulkUploadResultDescription($entryId, $entry->getBulkUploadId(), $errorMessage); return; } if (isset($data[self::BULK_UPLOAD_COLUMN_URL])) { try { $xmlData = file_get_contents($data[self::BULK_UPLOAD_COLUMN_URL]); KalturaLog::debug("Metadata downloaded [" . $data[self::BULK_UPLOAD_COLUMN_URL] . "]"); } catch (Exception $e) { $errorMessage = "Download metadata[" . $data[self::BULK_UPLOAD_COLUMN_URL] . "] error: " . $e->getMessage(); KalturaLog::err($errorMessage); self::addBulkUploadResultDescription($entryId, $entry->getBulkUploadId(), $errorMessage); $xmlData = null; } } elseif (isset($data[self::BULK_UPLOAD_COLUMN_XML])) { $xmlData = $data[self::BULK_UPLOAD_COLUMN_XML]; } else { $metadataProfileFields = array(); MetadataProfileFieldPeer::setUseCriteriaFilter(false); $tmpMetadataProfileFields = MetadataProfileFieldPeer::retrieveByMetadataProfileId($metadataProfileId); MetadataProfileFieldPeer::setUseCriteriaFilter(true); foreach ($tmpMetadataProfileFields as $metadataProfileField) { $metadataProfileFields[$metadataProfileField->getKey()] = $metadataProfileField; } KalturaLog::debug("Found fields [" . count($metadataProfileFields) . "] for metadata profile [{$metadataProfileId}]"); $xml = new DOMDocument(); $dataFound = false; foreach ($data as $key => $value) { if (!$value || !strlen($value)) { continue; } if (!preg_match('/^' . self::BULK_UPLOAD_COLUMN_FIELD_PREFIX . '(.+)$/', $key, $matches)) { continue; } $key = $matches[1]; if (!isset($metadataProfileFields[$key])) { $errorMessage = "Field [{$key}] does not exist"; KalturaLog::debug($errorMessage); self::addBulkUploadResultDescription($entryId, $entry->getBulkUploadId(), $errorMessage); continue; } $metadataProfileField = $metadataProfileFields[$key]; KalturaLog::debug("Found field [" . $metadataProfileField->getXpath() . "] for value [{$value}]"); $fieldValues = explode(self::BULK_UPLOAD_MULTI_VALUES_DELIMITER, $value); foreach ($fieldValues as $fieldValue) { if ($metadataProfileField->getType() == MetadataSearchFilter::KMC_FIELD_TYPE_DATE && !is_numeric($fieldValue)) { $value = self::parseFormatedDate($fieldValue); if (!$value || !strlen($value)) { $errorMessage = "Could not parse date format [{$fieldValue}] for field [{$key}]"; KalturaLog::debug($errorMessage); self::addBulkUploadResultDescription($entryId, $entry->getBulkUploadId(), $errorMessage); continue; } $fieldValue = $value; } self::addXpath($xml, $metadataProfileField->getXpath(), $fieldValue); } $dataFound = true; } if ($dataFound) { $xmlData = $xml->saveXML($xml->firstChild); $xmlData = trim($xmlData, " \n\r\t"); } } if (!$xmlData) { return; } $dbMetadata = new Metadata(); $dbMetadata->setPartnerId($entry->getPartnerId()); $dbMetadata->setMetadataProfileId($metadataProfileId); $dbMetadata->setMetadataProfileVersion($metadataProfile->getVersion()); $dbMetadata->setObjectType(Metadata::TYPE_ENTRY); $dbMetadata->setObjectId($entryId); $dbMetadata->setStatus(Metadata::STATUS_INVALID); $dbMetadata->save(); KalturaLog::debug("Metadata [" . $dbMetadata->getId() . "] saved [{$xmlData}]"); $key = $dbMetadata->getSyncKey(Metadata::FILE_SYNC_METADATA_DATA); kFileSyncUtils::file_put_contents($key, $xmlData); $errorMessage = ''; $status = kMetadataManager::validateMetadata($dbMetadata, $errorMessage); if ($status == Metadata::STATUS_VALID) { kEventsManager::raiseEvent(new kObjectDataChangedEvent($dbMetadata)); } else { self::addBulkUploadResultDescription($entryId, $entry->getBulkUploadId(), $errorMessage); } }
/** * Parse the XSD and update the list of search fields * * @param MetadataProfile $metadataProfile * @param partnerId * * @return TBD */ public static function parseProfileSearchFields($partnerId, MetadataProfile $metadataProfile) { $key = $metadataProfile->getSyncKey(MetadataProfile::FILE_SYNC_METADATA_DEFINITION); $xsdPath = kFileSyncUtils::getLocalFilePathForKey($key); $xPaths = kXsd::findXpathsByAppInfo($xsdPath, self::APP_INFO_SEARCH, 'true'); MetadataProfileFieldPeer::setUseCriteriaFilter(false); $profileFields = MetadataProfileFieldPeer::retrieveByMetadataProfileId($metadataProfile->getId()); MetadataProfileFieldPeer::setUseCriteriaFilter(true); // check all existing fields foreach ($profileFields as $profileField) { /** @var MetadataProfileField $profileField*/ $xPath = $profileField->getXpath(); // field removed if (!isset($xPaths[$xPath])) { $profileField->setStatus(MetadataProfileField::STATUS_DEPRECATED); $profileField->save(); continue; } $xPathData = $xPaths[$xPath]; $profileField->setStatus(MetadataProfileField::STATUS_ACTIVE); $profileField->setMetadataProfileVersion($metadataProfile->getFileSyncVersion()); if (isset($xPathData['name'])) { $profileField->setKey($xPathData['name']); } if (isset($xPathData['label'])) { $profileField->setLabel($xPathData['label']); } if (isset($xPathData['type'])) { $profileField->setType($xPathData['type']); } self::setAdditionalProfileFieldData($metadataProfile, $profileField, $xPathData); $profileField->save(); unset($xPaths[$xPath]); } // add new searchable fields foreach ($xPaths as $xPath => $xPathData) { $profileField = new MetadataProfileField(); $profileField->setMetadataProfileId($metadataProfile->getId()); $profileField->setMetadataProfileVersion($metadataProfile->getVersion()); $profileField->setPartnerId($metadataProfile->getPartnerId()); $profileField->setStatus(MetadataProfileField::STATUS_ACTIVE); $profileField->setXpath($xPath); if (isset($xPathData['name'])) { $profileField->setKey($xPathData['name']); } if (isset($xPathData['label'])) { $profileField->setLabel($xPathData['label']); } if (isset($xPathData['type'])) { $profileField->setType($xPathData['type']); self::setAdditionalProfileFieldData($metadataProfile, $profileField, $xPathData); $profileField->save(); } } // set none searchable existing fields $xPaths = kXsd::findXpathsByAppInfo($xsdPath, self::APP_INFO_SEARCH, 'false'); foreach ($profileFields as $profileField) { $xPath = $profileField->getXpath(); if (!isset($xPaths[$xPath])) { continue; } $xPathData = $xPaths[$xPath]; if (isset($xPathData['name'])) { $profileField->setKey($xPathData['name']); } if (isset($xPathData['label'])) { $profileField->setLabel($xPathData['label']); } if (isset($xPathData['type'])) { $profileField->setType($xPathData['type']); } $profileField->setStatus(MetadataProfileField::STATUS_NONE_SEARCHABLE); $profileField->setMetadataProfileVersion($metadataProfile->getVersion()); $profileField->save(); self::setAdditionalProfileFieldData($metadataProfile, $profileField, $xPathData); unset($xPaths[$xPath]); } // add new none searchable fields foreach ($xPaths as $xPath => $xPathData) { $profileField = new MetadataProfileField(); $profileField->setMetadataProfileId($metadataProfile->getId()); $profileField->setMetadataProfileVersion($metadataProfile->getVersion()); $profileField->setPartnerId($metadataProfile->getPartnerId()); $profileField->setStatus(MetadataProfileField::STATUS_NONE_SEARCHABLE); $profileField->setXpath($xPath); if (isset($xPathData['name'])) { $profileField->setKey($xPathData['name']); } if (isset($xPathData['label'])) { $profileField->setLabel($xPathData['label']); } if (isset($xPathData['type'])) { $profileField->setType($xPathData['type']); self::setAdditionalProfileFieldData($metadataProfile, $profileField, $xPathData); } $profileField->save(); } }
/** * Reloads this object from datastore based on primary key and (optionally) resets all associated objects. * * This will only work if the object has been saved and has a valid primary key set. * * @param boolean $deep (optional) Whether to also de-associated any related objects. * @param PropelPDO $con (optional) The PropelPDO connection to use. * @return void * @throws PropelException - if this object is deleted, unsaved or doesn't have pk match in db */ public function reload($deep = false, PropelPDO $con = null) { if ($this->isDeleted()) { throw new PropelException("Cannot reload a deleted object."); } if ($this->isNew()) { throw new PropelException("Cannot reload an unsaved object."); } if ($con === null) { $con = Propel::getConnection(MetadataProfileFieldPeer::DATABASE_NAME, Propel::CONNECTION_READ); } // We don't need to alter the object instance pool; we're just modifying this instance // already in the pool. MetadataProfileFieldPeer::setUseCriteriaFilter(false); $criteria = $this->buildPkeyCriteria(); entryPeer::addSelectColumns($criteria); $stmt = BasePeer::doSelect($criteria, $con); MetadataProfileFieldPeer::setUseCriteriaFilter(true); $row = $stmt->fetch(PDO::FETCH_NUM); $stmt->closeCursor(); if (!$row) { throw new PropelException('Cannot find matching row in the database to reload object values.'); } $this->hydrate($row, 0, true); // rehydrate if ($deep) { // also de-associate any related objects? } // if (deep) }
/** * Read multiple metadata schemas * @param BaseObject $object * @param array $data */ protected static function addMetadataWithProfilesSystemNames(BaseObject $object, array $data) { $newFieldValuesMap = array(); $xmlDataArray = array(); //Construct mapping of all metadata profile system names, their fields and the field values. foreach ($data as $key => $value) { if (strpos($key, self::BULK_UPLOAD_METADATA_FIELD_PREFIX) === 0) { $prefix = null; $metadataProfileSystemName = null; $metadataProfileFieldName = null; list($prefix, $metadataProfileSystemName, $metadataProfileFieldName) = explode(self::BULK_UPLOAD_METADATA_SYSTEMNAME_SEPARATOR, $key); if (!$prefix || !$metadataProfileSystemName || !$metadataProfileFieldName) { $errorMessage = "Unexpected key structure. Expected metadata::ProfileSystemName::FieldSystemName."; KalturaLog::err($errorMessage); self::addBulkUploadResultDescription($object, $object->getBulkUploadId(), $errorMessage); continue; } if (!isset($newFieldValuesMap[$metadataProfileSystemName])) { $newFieldValuesMap[$metadataProfileSystemName] = array(); } $newFieldValuesMap[$metadataProfileSystemName][$metadataProfileFieldName] = $value; } } foreach ($newFieldValuesMap as $metadataProfileSystemName => $fieldsArray) { /* @var array $fieldsArray */ if (!$fieldsArray || !count($fieldsArray)) { continue; } $metadataProfile = MetadataProfilePeer::retrieveBySystemName($metadataProfileSystemName, $object->getPartnerId()); if (!$metadataProfile) { $errorMessage = "Metadata profile with system name [{$metadataProfileSystemName}] could not be found."; KalturaLog::err($errorMessage); self::addBulkUploadResultDescription($object, $object->getBulkUploadId(), $errorMessage); continue; } if ($metadataProfile->getObjectType() != kMetadataManager::getTypeNameFromObject($object)) { $errorMessage = "Metadata profile [{$metadataProfileSystemName}] object type [" . $metadataProfile->getObjectType() . "] is not compatible with object type [" . kMetadataManager::getTypeNameFromObject($object) . "]"; KalturaLog::err($errorMessage); self::addBulkUploadResultDescription($object, $object->getBulkUploadId(), $errorMessage); continue; } $metadataProfileId = $metadataProfile->getId(); $xml = new DOMDocument(); $metadataProfileFields = array(); MetadataProfileFieldPeer::setUseCriteriaFilter(false); $tmpMetadataProfileFields = MetadataProfileFieldPeer::retrieveByMetadataProfileId($metadataProfileId); MetadataProfileFieldPeer::setUseCriteriaFilter(true); foreach ($tmpMetadataProfileFields as $metadataProfileField) { /* @var $metadataProfileField MetadataProfileField */ $metadataProfileFields[$metadataProfileField->getKey()] = $metadataProfileField; } foreach ($fieldsArray as $fieldSysName => $fieldValue) { if (!isset($metadataProfileFields[$fieldSysName])) { $errorMessage = "Metadata profile field with system name [{$fieldSysName}] missing from metadata profile with id [{$metadataProfileId}]"; KalturaLog::err($errorMessage); self::addBulkUploadResultDescription($object, $object->getBulkUploadId(), $errorMessage); continue; } $metadataProfileField = $metadataProfileFields[$fieldSysName]; KalturaLog::debug("Found field [" . $metadataProfileField->getXpath() . "] for value [{$fieldValue}]"); $fieldValues = explode(self::BULK_UPLOAD_MULTI_VALUES_DELIMITER, $fieldValue); foreach ($fieldValues as $fieldSingleValue) { if ($fieldSingleValue) { if ($metadataProfileField->getType() == MetadataSearchFilter::KMC_FIELD_TYPE_DATE && !is_numeric($fieldSingleValue)) { $valueAsDate = self::parseFormatedDate($fieldSingleValue); if (!$valueAsDate || !strlen($valueAsDate)) { $errorMessage = "Could not parse date format [{$fieldValue}] for field [{$key}]"; KalturaLog::err($errorMessage); self::addBulkUploadResultDescription($object, $object->getBulkUploadId(), $errorMessage); continue; } $fieldSingleValue = $valueAsDate; } if ($metadataProfileField->getType() == MetadataSearchFilter::KMC_FIELD_TYPE_INT && !is_numeric($fieldSingleValue)) { $errorMessage = "Could not parse int format [{$fieldSingleValue}] for field [{$key}]"; KalturaLog::err($errorMessage); self::addBulkUploadResultDescription($object, $object->getBulkUploadId(), $errorMessage); continue; } self::addXpath($xml, $metadataProfileField->getXpath(), $fieldSingleValue); } } $dataFound = true; if ($dataFound && $xml->hasChildNodes()) { $xmlDataArray[$metadataProfileId] = $xml->saveXML($xml->firstChild); $xmlDataArray[$metadataProfileId] = trim($xmlDataArray[$metadataProfileId], " \n\r\t"); } } } foreach ($xmlDataArray as $metadataProfileId => $xmlData) { $errorMessage = ''; if (!kMetadataManager::validateMetadata($metadataProfileId, $xmlData, $errorMessage)) { self::addBulkUploadResultDescription($object, $object->getBulkUploadId(), $errorMessage); continue; } $metadataProfile = MetadataProfilePeer::retrieveByPK($metadataProfileId); $dbMetadata = self::createOrFindMetadataObject($object, $metadataProfile); KalturaLog::debug("Metadata [" . $dbMetadata->getId() . "] saved [{$xmlData}]"); $key = $dbMetadata->getSyncKey(Metadata::FILE_SYNC_METADATA_DATA); kFileSyncUtils::file_put_contents($key, $xmlData); kEventsManager::raiseEvent(new kObjectDataChangedEvent($dbMetadata)); } }
/** * Parse the XSD and update the list of search fields * * @param MetadataProfile $metadataProfile * @param partnerId * * @return TBD */ public static function parseProfileSearchFields($partnerId, MetadataProfile $metadataProfile) { $key = $metadataProfile->getSyncKey(MetadataProfile::FILE_SYNC_METADATA_DEFINITION); $xsdPath = kFileSyncUtils::getLocalFilePathForKey($key); $xPaths = kXsd::findXpathsByAppInfo($xsdPath, self::APP_INFO_SEARCH, 'true'); MetadataProfileFieldPeer::setUseCriteriaFilter(false); $profileFields = MetadataProfileFieldPeer::retrieveByMetadataProfileId($metadataProfile->getId()); MetadataProfileFieldPeer::setUseCriteriaFilter(true); // check all existing fields foreach ($profileFields as $profileField) { $xPath = $profileField->getXpath(); // field removed if (!isset($xPaths[$xPath])) { $profileField->setStatus(MetadataProfileField::STATUS_DEPRECATED); $profileField->save(); continue; } $xPathData = $xPaths[$xPath]; if ($profileField->getStatus() != MetadataProfileField::STATUS_ACTIVE && isset($xPathData['type']) && ($xPathData['type'] == MetadataSearchFilter::KMC_FIELD_TYPE_DATE || $xPathData['type'] == MetadataSearchFilter::KMC_FIELD_TYPE_INT)) { $availableSearchIndex = self::getAvailableSearchIndex($partnerId, $metadataProfile->getObjectType()); if (!isset($availableSearchIndex)) { throw new Exception('could not find available search index'); } $profileField->setSearchIndex($availableSearchIndex); } $profileField->setStatus(MetadataProfileField::STATUS_ACTIVE); $profileField->setMetadataProfileVersion($metadataProfile->getVersion()); if (isset($xPathData['name'])) { $profileField->setKey($xPathData['name']); } if (isset($xPathData['label'])) { $profileField->setLabel($xPathData['label']); } if (isset($xPathData['type'])) { $profileField->setType($xPathData['type']); } $profileField->save(); unset($xPaths[$xPath]); } // add new searchable fields foreach ($xPaths as $xPath => $xPathData) { $profileField = new MetadataProfileField(); $profileField->setMetadataProfileId($metadataProfile->getId()); $profileField->setMetadataProfileVersion($metadataProfile->getVersion()); $profileField->setPartnerId($metadataProfile->getPartnerId()); $profileField->setStatus(MetadataProfileField::STATUS_ACTIVE); $profileField->setXpath($xPath); if (isset($xPathData['name'])) { $profileField->setKey($xPathData['name']); } if (isset($xPathData['label'])) { $profileField->setLabel($xPathData['label']); } if (isset($xPathData['type'])) { $profileField->setType($xPathData['type']); if ($xPathData['type'] == MetadataSearchFilter::KMC_FIELD_TYPE_DATE || $xPathData['type'] == MetadataSearchFilter::KMC_FIELD_TYPE_INT) { $availableSearchIndex = self::getAvailableSearchIndex($partnerId, $metadataProfile->getObjectType()); if (!isset($availableSearchIndex)) { throw new Exception('could not find available search index'); } $profileField->setSearchIndex($availableSearchIndex); } $profileField->save(); } } // set none searchable existing fields $xPaths = kXsd::findXpathsByAppInfo($xsdPath, self::APP_INFO_SEARCH, 'false'); foreach ($profileFields as $profileField) { $xPath = $profileField->getXpath(); if (!isset($xPaths[$xPath])) { continue; } $xPathData = $xPaths[$xPath]; if (isset($xPathData['name'])) { $profileField->setKey($xPathData['name']); } if (isset($xPathData['label'])) { $profileField->setLabel($xPathData['label']); } if (isset($xPathData['type'])) { $profileField->setType($xPathData['type']); } $profileField->setStatus(MetadataProfileField::STATUS_NONE_SEARCHABLE); $profileField->setMetadataProfileVersion($metadataProfile->getVersion()); $profileField->save(); unset($xPaths[$xPath]); } // add new none searchable fields foreach ($xPaths as $xPath => $xPathData) { $profileField = new MetadataProfileField(); $profileField->setMetadataProfileId($metadataProfile->getId()); $profileField->setMetadataProfileVersion($metadataProfile->getVersion()); $profileField->setPartnerId($metadataProfile->getPartnerId()); $profileField->setStatus(MetadataProfileField::STATUS_NONE_SEARCHABLE); $profileField->setXpath($xPath); if (isset($xPathData['name'])) { $profileField->setKey($xPathData['name']); } if (isset($xPathData['label'])) { $profileField->setLabel($xPathData['label']); } if (isset($xPathData['type'])) { $profileField->setType($xPathData['type']); } $profileField->save(); } }