/*
 * Copyright (c) 2013 VMware, Inc. All Rights Reserved.
 */
package com.vmware.gerrit.owners.common;

import com.google.gerrit.extensions.api.GerritApi;
import com.google.gerrit.extensions.api.changes.ChangeApi;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Change;
import com.google.inject.Inject;
import com.google.inject.Singleton;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;

@Singleton
public class ReviewerManager {
  private static final Logger log = LoggerFactory
      .getLogger(ReviewerManager.class);

  private final GerritApi gApi;
  @Inject
  public ReviewerManager(GerritApi gApi) {
    this.gApi = gApi;
  }
  public void addReviewers(Change change, Collection<Account.Id> reviewers) throws ReviewerManagerException {
    try {
      ChangeApi cApi = gApi.changes().id(change.getId().get());
      for (Account.Id account : reviewers) {
        cApi.addReviewer(account.toString());
      }
    } catch (RestApiException e) {
      log.error("Couldn't add reviewers to the change", e);
      throw new ReviewerManagerException(e);
    }
  }

}
@@ -369,16 +369,180 @@
 Change;%0A
+import com.google.gerrit.server.util.ManualRequestContext;%0Aimport com.google.gerrit.server.util.OneOffRequestContext;%0Aimport com.google.gwtorm.server.OrmException;%0A
 import c
@@ -863,85 +863,219 @@
 ;%0A  
-@Inject%0A  public ReviewerManager(GerritApi gApi) %7B%0A    this.gApi = gApi
+private final OneOffRequestContext requestContext;%0A%0A  @Inject%0A  public ReviewerManager(GerritApi gApi, OneOffRequestContext requestContext) %7B%0A    this.gApi = gApi;%0A    this.requestContext = requestContext
 ;%0A  %7D%0A
+%0A
   pu
@@ -1145,16 +1145,22 @@
 viewers)
+%0A     
  throws 
@@ -1194,17 +1194,88 @@
     try 
-%7B
+(ManualRequestContext ctx = requestContext.openAs(change.getOwner())) %7B%0A
 %0A      C
@@ -1430,16 +1430,17 @@
       %7D%0A
+%0A
     %7D ca
@@ -1461,16 +1461,31 @@
 ception 
+%7C OrmException 
 e) %7B%0A   
@@ -1600,6 +1600,6 @@
   %7D%0A
-%0A
 %7D
+%0A

Set change owners execution context