Esempio n. 1
0
 public function testSettersAndGetters()
 {
     $this->assertNull($this->entity->getId());
     $this->assertNull($this->entity->getName());
     $this->assertNull($this->entity->getDefinition());
     $this->assertNull($this->entity->getDescription());
     $this->assertNull($this->entity->getType());
     $this->assertNull($this->entity->getEntity());
     $this->assertNull($this->entity->getOwner());
     $this->assertNull($this->entity->getCreatedAt());
     $this->assertNull($this->entity->getUpdatedAt());
     $this->assertNull($this->entity->getLastRun());
     $this->assertNull($this->entity->getOrganization());
     $testData = uniqid('name');
     $this->entity->setName($testData);
     $this->assertEquals($testData, $this->entity->getName());
     $testData = uniqid('definition');
     $this->entity->setDefinition($testData);
     $this->assertEquals($testData, $this->entity->getDefinition());
     $testData = uniqid('description');
     $this->entity->setDescription($testData);
     $this->assertEquals($testData, $this->entity->getDescription());
     $testData = $this->getMock('Oro\\Bundle\\SegmentBundle\\Entity\\SegmentType', [], ['testTypeName']);
     $this->entity->setType($testData);
     $this->assertSame($testData, $this->entity->getType());
     $testData = $this->getMock('Oro\\Bundle\\OrganizationBundle\\Entity\\BusinessUnit');
     $this->entity->setOwner($testData);
     $this->assertSame($testData, $this->entity->getOwner());
     $testData = uniqid('entity');
     $this->entity->setEntity($testData);
     $this->assertEquals($testData, $this->entity->getEntity());
     $testData = new \DateTime();
     $this->entity->setCreatedAt($testData);
     $this->assertSame($testData, $this->entity->getCreatedAt());
     $testData = new \DateTime();
     $this->entity->setUpdatedAt($testData);
     $this->assertSame($testData, $this->entity->getUpdatedAt());
     $testData = new \DateTime();
     $this->entity->setLastRun($testData);
     $this->assertSame($testData, $this->entity->getLastRun());
     $testData = $this->getMock('Oro\\Bundle\\OrganizationBundle\\Entity\\Organization');
     $this->entity->setOrganization($testData);
     $this->assertSame($testData, $this->entity->getOrganization());
 }
Esempio n. 2
0
 /**
  * Runs static repository restriction query and stores it state into snapshot entity
  * Doctrine does not supports insert in DQL. To increase the speed of query here uses plain sql query.
  *
  * @param Segment $segment
  *
  * @throws \LogicException
  * @throws \Exception
  */
 public function run(Segment $segment)
 {
     if ($segment->getType()->getName() !== SegmentType::TYPE_STATIC) {
         throw new \LogicException('Only static segments could have snapshots.');
     }
     $entityMetadata = $this->em->getClassMetadata($segment->getEntity());
     if (count($entityMetadata->getIdentifierFieldNames()) > 1) {
         throw new \LogicException('Only entities with single identifier supports.');
     }
     $this->em->getRepository('OroSegmentBundle:SegmentSnapshot')->removeBySegment($segment);
     try {
         $this->em->beginTransaction();
         $date = new \DateTime('now', new \DateTimeZone('UTC'));
         $dateString = '\'' . $date->format('Y-m-d H:i:s') . '\'';
         if ($this->em->getConnection()->getDriver()->getName() === DatabaseDriverInterface::DRIVER_POSTGRESQL) {
             $dateString = sprintf('TIMESTAMP %s', $dateString);
         }
         $insertString = sprintf(', %d, %s ', $segment->getId(), $dateString);
         $qb = $this->dynamicSegmentQB->getQueryBuilder($segment);
         $this->applyOrganizationLimit($segment, $qb);
         $query = $qb->getQuery();
         $segmentQuery = $query->getSQL();
         $segmentQuery = substr_replace($segmentQuery, $insertString, stripos($segmentQuery, 'from'), 0);
         $fieldToSelect = 'entity_id';
         if ($entityMetadata->getTypeOfField($entityMetadata->getSingleIdentifierFieldName()) === 'integer') {
             $fieldToSelect = 'integer_entity_id';
         }
         $dbQuery = 'INSERT INTO oro_segment_snapshot (' . $fieldToSelect . ', segment_id, createdat) (%s)';
         $dbQuery = sprintf($dbQuery, $segmentQuery);
         $statement = $this->em->getConnection()->prepare($dbQuery);
         $this->bindParameters($statement, $query->getParameters());
         $statement->execute();
         $this->em->commit();
     } catch (\Exception $exception) {
         $this->em->rollback();
         throw $exception;
     }
     $segment = $this->em->merge($segment);
     $segment->setLastRun(new \DateTime('now', new \DateTimeZone('UTC')));
     $this->em->persist($segment);
     $this->em->flush();
 }
Esempio n. 3
0
 /**
  * Runs static repository restriction query and stores it state into snapshot entity
  *
  * @param Segment $segment
  *
  * @throws \LogicException
  * @throws \Exception
  */
 public function run(Segment $segment)
 {
     if ($segment->getType()->getName() !== SegmentType::TYPE_STATIC) {
         throw new \LogicException('Only static segments could have snapshots.');
     }
     $this->em->getRepository('OroSegmentBundle:SegmentSnapshot')->removeBySegment($segment);
     $qb = $this->dynamicSegmentQB->build($segment);
     $iterator = new BufferedQueryResultIterator($qb);
     $writeCount = 0;
     try {
         $this->em->beginTransaction();
         $this->em->clear(ClassUtils::getClass($segment));
         foreach ($iterator as $data) {
             // only not composite identifiers are supported
             $id = reset($data);
             $writeCount++;
             /** @var Segment $reference */
             $reference = $this->em->getReference(ClassUtils::getClass($segment), $segment->getId());
             $snapshot = new SegmentSnapshot($reference);
             $snapshot->setEntityId($id);
             $this->toWrite[] = $snapshot;
             if (0 === $writeCount % $this->batchSize) {
                 $this->write($this->toWrite);
                 $this->toWrite = [];
             }
         }
         if (count($this->toWrite) > 0) {
             $this->write($this->toWrite);
         }
         $this->em->commit();
     } catch (\Exception $exception) {
         $this->em->rollback();
         throw $exception;
     }
     $segment = $this->em->merge($segment);
     $segment->setLastRun(new \DateTime('now', new \DateTimeZone('UTC')));
     $this->em->persist($segment);
     $this->em->flush();
 }
Esempio n. 4
0
 /**
  * @param Segment $segment
  *
  * @return bool
  */
 protected function isDynamic(Segment $segment)
 {
     return $segment->getType()->getName() === SegmentType::TYPE_DYNAMIC;
 }