From 0304aae1c59aed35e20242f45fd39f9c43d4ea0a Mon Sep 17 00:00:00 2001 From: Timo Paananen Date: Fri, 9 Mar 2012 16:32:12 +0200 Subject: [PATCH 1/2] Added SaveException and Error classes Modified Mapper::Save so that it catches the client's SaveException Added Mapper::checkResult method which throws a SaveException if there are errors --- Exception/Error.php | 60 +++++++++++++++++++++++++++++++++++ Exception/SaveException.php | 57 +++++++++++++++++++++++++++++++++ Mapper.php | 63 +++++++++++++++++++++++++++++++++++-- 3 files changed, 177 insertions(+), 3 deletions(-) create mode 100755 Exception/Error.php create mode 100755 Exception/SaveException.php mode change 100644 => 100755 Mapper.php diff --git a/Exception/Error.php b/Exception/Error.php new file mode 100755 index 0000000..896be72 --- /dev/null +++ b/Exception/Error.php @@ -0,0 +1,60 @@ +errors; + } + + /** + * setErrors + * + * @param array $errors + */ + public function setErrors(array $errors) + { + return $this->errors = $errors; + } + + /** + * getModel + * + * @return mixed + */ + public function getModel() + { + return $this->model; + } + + /** + * setModel + * + * @param mixed $model + */ + public function setModel($model) + { + $this->model = $model; + } +} \ No newline at end of file diff --git a/Exception/SaveException.php b/Exception/SaveException.php new file mode 100755 index 0000000..4f7378f --- /dev/null +++ b/Exception/SaveException.php @@ -0,0 +1,57 @@ +okModels; + } + + /** + * setOkModels + * @param array $okModels + */ + public function setOkModels(array $okModels) + { + $this->okModels = $okModels; + } + + /** + * getErrors + * @return array + */ + public function getErrors() + { + return $this->errors; + } + + /** + * setErrors + * @param array $errors + */ + public function setErrors(array $errors) + { + $this->errors = $errors; + } +} +?> \ No newline at end of file diff --git a/Mapper.php b/Mapper.php old mode 100644 new mode 100755 index f4e9f54..e85a77e --- a/Mapper.php +++ b/Mapper.php @@ -11,6 +11,9 @@ use Ddeboer\Salesforce\MapperBundle\Event\BeforeSaveEvent; use Doctrine\Common\Cache\Cache; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Ddeboer\Salesforce\MapperBundle\Exception\SaveException; +use Ddeboer\Salesforce\MapperBundle\Exception\Error; +use Ddeboer\Salesforce\ClientBundle\Exception\SaveException as ClientSaveException; /** * This mapper makes interaction with the Salesforce API using full objects @@ -245,12 +248,14 @@ public function save($model) $objectsToBeCreated = array(); $objectsToBeUpdated = array(); $modelsWithoutId = array(); + $modelsToBeUpdated = array(); foreach ($models as $model) { $object = $this->annotationReader->getSalesforceObject($model); $sObject = $this->mapToSalesforceObject($model); if (isset($sObject->Id) && null !== $sObject->Id) { $objectsToBeUpdated[$object->name][] = $sObject; + $modelsToBeUpdated[$object->name][] = $model; } else { $objectsToBeCreated[$object->name][] = $sObject; $modelsWithoutId[$object->name][] = $model; @@ -258,6 +263,7 @@ public function save($model) } $results = array(); + $checkModels = array(); foreach ($objectsToBeCreated as $objectName => $sObjects) { $reflClass = new \ReflectionClass(current( $modelsWithoutId[$objectName] @@ -265,22 +271,73 @@ public function save($model) $reflProperty = $reflClass->getProperty('id'); $reflProperty->setAccessible(true); - $saveResults = $this->client->create($sObjects, $objectName); + try + { + $saveResults = $this->client->create($sObjects, $objectName); + } catch( ClientSaveException $e ) + { + $saveResults = $e->getResults(); + } for ($i = 0; $i < count($saveResults); $i++) { $newId = $saveResults[$i]->id; $model = $modelsWithoutId[$objectName][$i]; $reflProperty->setValue($model, $newId); } - $results[] = $saveResults; + $checkModels = array_merge($checkModels, $modelsWithoutId[$objectName]); + $results = array_merge($results, $saveResults); } foreach ($objectsToBeUpdated as $objectName => $sObjects) { - $results[] = $this->client->update($sObjects, $objectName); + try + { + $updateResults = $this->client->update($sObjects, $objectName); + } catch( ClientSaveException $e ) + { + $updateResults = $e->getResults(); + } + $checkModels = array_merge($checkModels, $modelsToBeUpdated[$objectName]); + $results = array_merge($results, $updateResults); } + $this->checkResult($results,$checkModels); + return $results; } + + /** + * Checks client results and throws SaveException if errors are found + * + * @param array $results + * @param array $models + * @return bool + * @throws Ddeboer\Salesforce\MapperBundle\Exception\SaveException + */ + private function checkResult(array $results, array $models) + { + $okModels = array(); + $errors = array(); + foreach($results as $key => $result) + { + if ($result->success) + $okModels[] = $models[$key]; + else + { + $error = new Error(); + $error->model = $models[$key]; + $error->errors = $result->errors; + $errors[] = $error; + } + } + if (count($errors) > 0) + { + $saveException = new SaveException(); + $saveException->setOkModels($okModels); + $saveException->setErrors($errors); + throw $saveException; + } + return true; + } /** * Map a Salesforce object to a domain model object From b50d4c69660feb9ee7a7ef362042055f1e3f5f97 Mon Sep 17 00:00:00 2001 From: Timo Paananen Date: Wed, 28 Mar 2012 09:43:47 +0300 Subject: [PATCH 2/2] Added the first error message to the exception when throwing SaveException --- Mapper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Mapper.php b/Mapper.php index e85a77e..980d96b 100755 --- a/Mapper.php +++ b/Mapper.php @@ -331,7 +331,7 @@ private function checkResult(array $results, array $models) } if (count($errors) > 0) { - $saveException = new SaveException(); + $saveException = new SaveException($errors[0]->errors[0]->message); $saveException->setOkModels($okModels); $saveException->setErrors($errors); throw $saveException;