Skip to content

Commit

Permalink
Merge pull request #3 from ealcantara22/failed-synchronization-support
Browse files Browse the repository at this point in the history
Failed synchronization support
  • Loading branch information
ntidev authored Jan 24, 2018
2 parents 6e378c6 + c9daa55 commit f2922de
Show file tree
Hide file tree
Showing 10 changed files with 265 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# composer
/composer.phar

# Backup entities generated with doctrine:generate:entities command
**/Entity/*~

2 changes: 1 addition & 1 deletion Controller/SyncController.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function pushAction(Request $request) {

$syncClass = $mapping->getSyncService();

if(!class_exists($syncClass)) { continue; }
#if(!class_exists($syncClass)) { continue; }

/** @var SyncServiceInterface $service */
$service = $this->get($syncClass);
Expand Down
1 change: 0 additions & 1 deletion Entity/SyncDeleteState.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,3 @@ public function setTimestamp($timestamp)
}

}

191 changes: 191 additions & 0 deletions Entity/SyncFailedItemState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<?php

namespace NTI\SyncBundle\Entity;

use Doctrine\ORM\Mapping as ORM;


/**
* Class SyncFailedItemState
* @package NTI\SyncBundle\Entity
* @ORM\Table(name="nti_sync_failed_item_state")
* @ORM\Entity(repositoryClass="NTI\SyncBundle\Repository\SyncFailedItemStateRepository")
*
*/
class SyncFailedItemState {

/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @var string
*
* @ORM\Column(name="uuid", type="string", length=255, nullable=false, unique=true)
*/
private $uuid;

/**
* @var SyncMapping
*
* @ORM\ManyToOne(targetEntity="NTI\SyncBundle\Entity\SyncMapping")
*/
private $mapping;

/**
* @var integer
*
* @ORM\Column(name="class_id", type="integer", nullable=true)
*/
private $classId;

/**
* @var integer
*
* @ORM\Column(name="timestamp", type="integer", nullable=false)
*/
private $timestamp;

/**
* @var string
*
* @ORM\Column(name="errors", columnDefinition="TEXT", length=65535, nullable=true)
*/
private $errors;


/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}

/**
* Set uuid
*
* @param string $uuid
*
* @return SyncFailedItemState
*/
public function setUuid($uuid)
{
$this->uuid = $uuid;

return $this;
}

/**
* Get uuid
*
* @return string
*/
public function getUuid()
{
return $this->uuid;
}

/**
* Set classId
*
* @param integer $classId
*
* @return SyncFailedItemState
*/
public function setClassId($classId)
{
$this->classId = $classId;

return $this;
}

/**
* Get classId
*
* @return integer
*/
public function getClassId()
{
return $this->classId;
}

/**
* Set timestamp
*
* @param integer $timestamp
*
* @return SyncFailedItemState
*/
public function setTimestamp($timestamp)
{
$this->timestamp = $timestamp;

return $this;
}

/**
* Get timestamp
*
* @return integer
*/
public function getTimestamp()
{
return $this->timestamp;
}

/**
* Set errors
*
* @param string $errors
*
* @return SyncFailedItemState
*/
public function setErrors($errors)
{
$this->errors = $errors;

return $this;
}

/**
* Get errors
*
* @return string
*/
public function getErrors()
{
return $this->errors;
}

/**
* Set mapping
*
* @param \NTI\SyncBundle\Entity\SyncMapping $mapping
*
* @return SyncFailedItemState
*/
public function setMapping(\NTI\SyncBundle\Entity\SyncMapping $mapping = null)
{
$this->mapping = $mapping;

return $this;
}

/**
* Get mapping
*
* @return \NTI\SyncBundle\Entity\SyncMapping
*/
public function getMapping()
{
return $this->mapping;
}
}
1 change: 0 additions & 1 deletion Entity/SyncMapping.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,3 @@ public function setSyncService($syncService)


}

2 changes: 1 addition & 1 deletion Entity/SyncNewItemState.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ public function setTimestamp($timestamp)
return $this;
}

}
}
1 change: 0 additions & 1 deletion Entity/SyncState.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,3 @@ public function setTimestamp($timestamp)
}

}

21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,34 @@ After receiving the request, if a mapping with the specified name exists, the sy
},
...
],
"failedItems": [
{
"id": 7,
"uuid": "abcdefg-123456-hifgxyz-78901",
"mapping": {
"id": 9,
"name": "Product",
"class": "AppBundle\\Entity\\Product\\Product",
"sync_service": " ... "
},
"classId": 137,
"timestamp": 1512080747,
"errors": "{"has_error":true,"additional_errors":null,"code":403,"message":"This inventory report is already closed. Further operations are not allowed.","data":null,"redirect":null}"
},
...
],
"_real_last_timestamp": 1512092445
}
}

```
The server will return the both the `changes` , `newItems`, and the `deletes`. The `changes` will contain the `data` portion of the array returned by
The server will return the both the `changes` , `newItems`, `failedItems` ,and the `deletes`. The `changes` will contain the `data` portion of the array returned by
the repository's implementation of `SyncRepositoryInterface`. The `deletes` will contain the list of `SyncDeleteState` that were recorded since the
specified timestamp. The `newItems` will contain the list of `SyncNewItemState` which means the new items that were created since the provided timestamp
including the UUID that was given at the time (This is helpful to third party devices when first pulling the information they can verify if an item was already created
but they don't have the ID of that item in their local storage and avoid creating duplicates in the server)
but they don't have the ID of that item in their local storage and avoid creating duplicates in the server). The `failedItems` will contain the list of `SyncFailedItemState`, each item in this list
contains an `errors` JSON property with the list of errors founds processing the creation or update of the entity.
The `_real_last_timestamp` should be used as it can help with paginating the results for a full-sync and help the client
get the real last timestamp of the last object in the response. This has to be obtained in the repository and can be done
Expand Down
41 changes: 41 additions & 0 deletions Repository/SyncFailedItemStateRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace NTI\SyncBundle\Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NonUniqueResultException;

/**
* SyncFailedItemStateRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class SyncFailedItemStateRepository extends EntityRepository
{
public function findFromTimestampAndMapping($mappingName, $timestamp) {
$qb = $this->createQueryBuilder('s');
$qb ->innerJoin('s.mapping', 'm')
->andWhere('m.name = :mappingName')
->setParameter('mappingName', $mappingName)
->andWhere('s.timestamp >= :timestamp')
->setParameter('timestamp', $timestamp)
->orderBy('s.timestamp', 'asc');
return $qb->getQuery()->getResult();
}

public function findByUuId($id){
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('item')
->from('NTISyncBundle:SyncFailedItemState', 'item')
->andWhere(
$qb->expr()->eq('item.uuid', $qb->expr()->literal($id))
);

try {
return $qb->getQuery()->getOneOrNullResult();
}catch (NonUniqueResultException $e){
return null;
}
}
}
7 changes: 6 additions & 1 deletion Service/SyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class SyncService {
*/
public function __construct(ContainerInterface $container) {
$this->container = $container;

}

/**
Expand Down Expand Up @@ -61,6 +60,11 @@ public function getFromMappings($mappings) {
$deletes = $this->em->getRepository('NTISyncBundle:SyncDeleteState')->findFromTimestamp($mappingName, $timestamp);
$newItems = $this->em->getRepository('NTISyncBundle:SyncNewItemState')->findFromTimestampAndMapping($mappingName, $timestamp);

/**
* Failed Items Synchronization
*/
$failedItems = $this->em->getRepository('NTISyncBundle:SyncFailedItemState')->findFromTimestampAndMapping($mappingName, $timestamp);

/** @var SyncRepositoryInterface $repository */
$repository = $this->em->getRepository($syncMapping->getClass());
if(!($repository instanceof SyncRepositoryInterface)) {
Expand All @@ -74,6 +78,7 @@ public function getFromMappings($mappings) {
'changes' => $result["data"],
'deletes' => json_decode($this->container->get('jms_serializer')->serialize($deletes, 'json'), true),
'newItems' => json_decode($this->container->get('jms_serializer')->serialize($newItems, 'json'), true),
'failedItems' => json_decode($this->container->get('jms_serializer')->serialize($failedItems, 'json'), true),
SyncState::REAL_LAST_TIMESTAMP => $result[SyncState::REAL_LAST_TIMESTAMP],
);
}
Expand Down

0 comments on commit f2922de

Please sign in to comment.