/** * Function returns metadata object which needs to be set with the new metadata XML * @param BaseObject $object * @param MetadataProfile $metadataProfileId */ protected static function createOrFindMetadataObject(BaseObject $object, MetadataProfile $metadataProfile) { $c = new Criteria(); $c->addAnd(MetadataPeer::PARTNER_ID, $object->getPartnerId(), Criteria::EQUAL); $c->addAnd(MetadataPeer::OBJECT_ID, $object->getId(), Criteria::EQUAL); $c->addAnd(MetadataPeer::METADATA_PROFILE_ID, $metadataProfile->getId(), Criteria::EQUAL); $c->addAnd(MetadataPeer::METADATA_PROFILE_VERSION, $metadataProfile->getVersion(), Criteria::EQUAL); $c->addAnd(MetadataPeer::OBJECT_TYPE, kMetadataManager::getTypeNameFromObject($object), Criteria::EQUAL); $c->addAnd(MetadataPeer::STATUS, Metadata::STATUS_VALID); $dbMetadata = MetadataPeer::doSelectOne($c); if (!$dbMetadata) { $dbMetadata = new Metadata(); $dbMetadata->setPartnerId($object->getPartnerId()); $dbMetadata->setMetadataProfileId($metadataProfile->getId()); $dbMetadata->setMetadataProfileVersion($metadataProfile->getVersion()); $dbMetadata->setObjectType(kMetadataManager::getTypeNameFromObject($object)); $dbMetadata->setObjectId($object->getId()); $dbMetadata->setStatus(Metadata::STATUS_VALID); $dbMetadata->save(); } else { $dbMetadata->incrementVersion(); $dbMetadata->save(); } return $dbMetadata; }
/** * Check if transforming required and create job if needed * * @param MetadataProfile $metadataProfile * @param int $prevVersion * @param string $prevXsd */ public static function diffMetadataProfile(MetadataProfile $metadataProfile, $prevXsd, $newXsd) { $xsl = true; if (!PermissionPeer::isValidForPartner(MetadataPermissionName::FEATURE_METADATA_NO_VALIDATION, $metadataProfile->getPartnerId())) { $xsl = kXsd::compareXsd($prevXsd, $newXsd); } if ($xsl === true) { return; } if (PermissionPeer::isValidForPartner(MetadataPermissionName::FEATURE_METADATA_NO_TRANSFORMATION, $metadataProfile->getPartnerId())) { throw new kXsdException(kXsdException::TRANSFORMATION_REQUIRED); } $prevVersion = $metadataProfile->getVersion(); $metadataProfile->incrementVersion(); $newVersion = $metadataProfile->getVersion(); $metadataProfile->save(); //save has to be before we create a batch to make sure there is no race-condition where XSD is not updated before the batch runs. return self::addTransformMetadataJob($metadataProfile->getPartnerId(), $metadataProfile->getId(), $prevVersion, $newVersion, $xsl); }
/** * Parse the XSD and update the list of search fields * * @param MetadataProfile $metadataProfile * * @return TBD */ public static function parseProfileSearchFields(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]; $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']); } $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(); } }