// Copyright (C) 2012 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.change;

import com.google.common.base.Strings;
import com.google.gerrit.common.ChangeHooks;
import com.google.gerrit.common.TimeUtil;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.DefaultInput;
import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.extensions.webui.UiAction;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.ChangeMessage;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.ChangeMessagesUtil;
import com.google.gerrit.server.ChangeUtil;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.change.PutTopic.Input;
import com.google.gerrit.server.git.BatchUpdate;
import com.google.gerrit.server.git.BatchUpdate.ChangeContext;
import com.google.gerrit.server.git.BatchUpdate.Context;
import com.google.gerrit.server.git.UpdateException;
import com.google.gerrit.server.notedb.ChangeUpdate;
import com.google.gerrit.server.project.ChangeControl;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;

@Singleton
public class PutTopic implements RestModifyView<ChangeResource, Input>,
    UiAction<ChangeResource> {
  private final Provider<ReviewDb> dbProvider;
  private final ChangeHooks hooks;
  private final ChangeMessagesUtil cmUtil;
  private final BatchUpdate.Factory batchUpdateFactory;

  public static class Input {
    @DefaultInput
    public String topic;
  }

  @Inject
  PutTopic(Provider<ReviewDb> dbProvider,
      ChangeHooks hooks,
      ChangeMessagesUtil cmUtil,
      BatchUpdate.Factory batchUpdateFactory) {
    this.dbProvider = dbProvider;
    this.hooks = hooks;
    this.cmUtil = cmUtil;
    this.batchUpdateFactory = batchUpdateFactory;
  }

  @Override
  public Response<String> apply(ChangeResource req, Input input)
      throws UpdateException, RestApiException {
    ChangeControl ctl = req.getControl();
    if (!ctl.canEditTopicName()) {
      throw new AuthException("changing topic not permitted");
    }

    Op op = new Op(ctl, input != null ? input : new Input());
    try (BatchUpdate u = batchUpdateFactory.create(dbProvider.get(),
        req.getChange().getProject(), ctl.getUser(), TimeUtil.nowTs())) {
      u.addOp(req.getId(), op);
      u.execute();
    }
    return Strings.isNullOrEmpty(op.newTopicName)
        ? Response.<String> none()
        : Response.ok(op.newTopicName);
  }

  private class Op extends BatchUpdate.Op {
    private final Input input;
    private final IdentifiedUser caller;

    private Change change;
    private String oldTopicName;
    private String newTopicName;

    public Op(ChangeControl ctl, Input input) {
      this.input = input;
      this.caller = ctl.getUser().asIdentifiedUser();
    }

    @Override
    public boolean updateChange(ChangeContext ctx) throws OrmException {
      change = ctx.getChange();
      ChangeUpdate update = ctx.getUpdate(change.currentPatchSetId());
      newTopicName = Strings.nullToEmpty(input.topic);
      oldTopicName = Strings.nullToEmpty(change.getTopic());
      if (oldTopicName.equals(newTopicName)) {
        return false;
      }
      String summary;
      if (oldTopicName.isEmpty()) {
        summary = "Topic set to " + newTopicName;
      } else if (newTopicName.isEmpty()) {
        summary = "Topic " + oldTopicName + " removed";
      } else {
        summary = String.format("Topic changed from %s to %s",
            oldTopicName, newTopicName);
      }
      change.setTopic(Strings.emptyToNull(newTopicName));
      update.setTopic(change.getTopic());
      ctx.saveChange();

      ChangeMessage cmsg = new ChangeMessage(
          new ChangeMessage.Key(
              change.getId(),
              ChangeUtil.messageUUID(ctx.getDb())),
          caller.getAccountId(), ctx.getWhen(),
          change.currentPatchSetId());
      cmsg.setMessage(summary);
      cmUtil.addChangeMessage(ctx.getDb(), update, cmsg);
      return true;
    }

    @Override
    public void postUpdate(Context ctx) throws OrmException {
      if (change != null) {
        hooks.doTopicChangedHook(change, caller.getAccount(),
            oldTopicName, ctx.getDb());
      }
    }
  }

  @Override
  public UiAction.Description getDescription(ChangeResource resource) {
    return new UiAction.Description()
      .setLabel("Edit Topic")
      .setVisible(resource.getControl().canEditTopicName());
  }
}

@@ -1371,56 +1371,8 @@
 il;%0A
-import com.google.gerrit.server.IdentifiedUser;%0A
 impo
@@ -2866,21 +2866,16 @@
  new Op(
-ctl, 
 input !=
@@ -3312,49 +3312,8 @@
 put;
-%0A    private final IdentifiedUser caller;
 %0A%0A  
@@ -3422,27 +3422,8 @@
  Op(
-ChangeControl ctl, 
 Inpu
@@ -3463,62 +3463,8 @@
 ut;%0A
-      this.caller = ctl.getUser().asIdentifiedUser();%0A
     
@@ -4480,21 +4480,28 @@
        c
-aller
+tx.getUser()
 .getAcco
@@ -4820,22 +4820,73 @@
 ook(
-change, caller
+%0A            change,%0A            ctx.getUser().asIdentifiedUser()
 .get
@@ -4921,16 +4921,28 @@
 picName,
+%0A           
  ctx.get

Use user from context for change message and hook