public function run()
 {
     //Do not run this job if ReadPermissionSubscriptionUpdateComplete is already running
     try {
         JobInProcess::getByType('ReadPermissionSubscriptionComprehensiveUpdate');
     } catch (NotFoundException $e) {
         ReadPermissionsSubscriptionUtil::updateAllReadSubscriptionTables(true);
     }
     return true;
 }
 public function run()
 {
     $counter = 0;
     try {
         while (JobInProcess::getByType('ReadPermissionSubscriptionQuickUpdate') instanceof RedBeanModel && $counter <= self::MAX_NUMBER_OF_TRIES) {
             sleep(30);
             $counter++;
         }
     } catch (NotFoundException $e) {
         ReadPermissionsSubscriptionUtil::updateAllReadSubscriptionTables(false);
         return true;
     }
     return false;
 }
 /**
  * @param MessageLogger $messageLogger
  * @param null $modelClassName
  */
 public static function updateReadSubscriptionTableFromBuildTable(MessageLogger $messageLogger, $modelClassName = null)
 {
     if ($modelClassName == 'Account') {
         // ToDO: Add pagination - Ivica: I do not think we need it
         $accountIds = static::getAccountIdsArrayFromBuildTable();
         ReadPermissionsSubscriptionUtil::updateAllReadSubscriptionTables($messageLogger, array($modelClassName), $accountIds);
         if ($modelClassName == 'Account' && !empty($accountIds)) {
             foreach ($accountIds as $accountId) {
                 static::deleteAccountIdFromBuildTable((int) $accountId);
             }
         }
     }
 }
 public function testGetAddedOrDeletedModelsFromReadSubscriptionTable()
 {
     ReadPermissionsSubscriptionUtil::buildTables();
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     $messageLogger = new DebuggingMessageLogger();
     $task = TaskTestHelper::createTaskByNameForOwner('Test Task', $super);
     // Because ReadPermissionsSubscriptionUtil::updateAllReadSubscriptionTables completed in previous test
     // status need to be ReadPermissionsSubscriptionUtil::STATUS_COMPLETED
     $this->assertEquals(ReadPermissionsSubscriptionUtil::STATUS_COMPLETED, ReadPermissionsSubscriptionUtil::getReadPermissionUpdateStatus());
     $this->assertTrue(ReadPermissionsSubscriptionUtil::isReadPermissionSubscriptionUpdateCompleted());
     ReadPermissionsSubscriptionUtil::updateAllReadSubscriptionTables($messageLogger);
     $this->assertEquals(ReadPermissionsSubscriptionUtil::STATUS_COMPLETED, ReadPermissionsSubscriptionUtil::getReadPermissionUpdateStatus());
     $this->assertTrue(ReadPermissionsSubscriptionUtil::isReadPermissionSubscriptionUpdateCompleted());
     $sql = "SELECT * FROM task_read_subscription WHERE userid = " . $super->id;
     $permissionTableRows = ZurmoRedBean::getAll($sql);
     $this->assertEquals(1, count($permissionTableRows));
     $addedModelIds = ReadPermissionsSubscriptionUtil::getAddedOrDeletedModelsFromReadSubscriptionTable('TestService', 'Task', 0, ReadPermissionsSubscriptionUtil::TYPE_ADD, $super);
     $this->asserttrue(is_array($addedModelIds));
     $this->assertEquals(1, count($addedModelIds));
     ModelCreationApiSyncUtil::insertItem('TestService', $task->id, 'Task', '2013-05-03 15:16:06');
     $addedModelIds = ReadPermissionsSubscriptionUtil::getAddedOrDeletedModelsFromReadSubscriptionTable('TestService', 'Task', 0, ReadPermissionsSubscriptionUtil::TYPE_ADD, $super);
     $this->asserttrue(is_array($addedModelIds));
     $this->assertEquals(0, count($addedModelIds));
 }
 public function testGetDeletedModelIds()
 {
     ReadPermissionsSubscriptionUtil::recreateTable('account_read_subscription');
     ModelCreationApiSyncUtil::buildTable();
     $timestamp = time();
     sleep(1);
     $account1 = AccountTestHelper::createAccountByNameForOwner('First Test Delete Account', Yii::app()->user->userModel);
     ReadPermissionsSubscriptionUtil::updateAllReadSubscriptionTables();
     $models = ModelStateChangesSubscriptionUtil::getDeletedModelIds('apiTest', 'Account', 2, 0, $timestamp);
     $this->assertTrue(is_array($models));
     $this->assertTrue(empty($models));
     $account1Id = $account1->id;
     $this->assertTrue($account1->delete());
     $models = ModelStateChangesSubscriptionUtil::getDeletedModelIds('apiTest', 'Account', 2, 0, $timestamp);
     $this->assertTrue(is_array($models));
     $this->assertTrue(empty($models));
     ReadPermissionsSubscriptionUtil::updateAllReadSubscriptionTables();
     $models = ModelStateChangesSubscriptionUtil::getDeletedModelIds('apiTest', 'Account', 2, 0, $timestamp);
     $this->assertTrue(is_array($models));
     $this->assertEquals(1, count($models));
     $this->assertEquals($account1Id, $models[0]);
     // Check pagination
     sleep(1);
     $timestamp = time();
     sleep(1);
     $account2 = AccountTestHelper::createAccountByNameForOwner('Second Test Delete Account', Yii::app()->user->userModel);
     $account3 = AccountTestHelper::createAccountByNameForOwner('Third Test Delete Account', Yii::app()->user->userModel);
     $account4 = AccountTestHelper::createAccountByNameForOwner('Forth Test Delete Account', Yii::app()->user->userModel);
     ReadPermissionsSubscriptionUtil::updateAllReadSubscriptionTables();
     $account2Id = $account2->id;
     $account3Id = $account3->id;
     $account4Id = $account4->id;
     $this->assertTrue($account2->delete());
     $this->assertTrue($account3->delete());
     $this->assertTrue($account4->delete());
     ReadPermissionsSubscriptionUtil::updateAllReadSubscriptionTables();
     $models = ModelStateChangesSubscriptionUtil::getDeletedModelIds('apiTest', 'Account', 2, 0, $timestamp);
     $this->assertTrue(is_array($models));
     $this->assertEquals(2, count($models));
     $this->assertEquals($account2Id, $models[0]);
     $this->assertEquals($account3Id, $models[1]);
     // Second page
     $models = ModelStateChangesSubscriptionUtil::getDeletedModelIds('apiTest', 'Account', 2, 2, $timestamp);
     $this->assertTrue(is_array($models));
     $this->assertEquals(1, count($models));
     $this->assertEquals($account4Id, $models[0]);
 }
 public function testGetAddedOrDeletedModelsFromReadSubscriptionTable()
 {
     ReadPermissionsSubscriptionUtil::buildTables();
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     $task = TaskTestHelper::createTaskByNameForOwner('Test Task', $super);
     ReadPermissionsSubscriptionUtil::updateAllReadSubscriptionTables(false);
     $sql = "SELECT * FROM task_read_subscription WHERE userid = " . $super->id;
     $permissionTableRows = R::getAll($sql);
     $this->assertEquals(1, count($permissionTableRows));
     $addedModelIds = ReadPermissionsSubscriptionUtil::getAddedOrDeletedModelsFromReadSubscriptionTable('TestService', 'Task', 0, ReadPermissionsSubscriptionUtil::TYPE_ADD, $super);
     $this->asserttrue(is_array($addedModelIds));
     $this->assertEquals(1, count($addedModelIds));
     ModelCreationApiSyncUtil::insertItem('TestService', $task->id, 'Task', '2013-05-03 15:16:06');
     $addedModelIds = ReadPermissionsSubscriptionUtil::getAddedOrDeletedModelsFromReadSubscriptionTable('TestService', 'Task', 0, ReadPermissionsSubscriptionUtil::TYPE_ADD, $super);
     $this->asserttrue(is_array($addedModelIds));
     $this->assertEquals(0, count($addedModelIds));
 }
 public function run()
 {
     ReadPermissionsSubscriptionUtil::updateAllReadSubscriptionTables($this->getMessageLogger());
     return true;
 }