//Copyright (C) 2015 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.googlesource.gerrit.plugins.importer;

import com.google.gerrit.common.errors.NoSuchAccountException;
import com.google.gerrit.extensions.common.ChangeInfo;
import com.google.gerrit.extensions.common.ChangeMessageInfo;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.ChangeMessage;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.ChangeMessagesUtil;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.notedb.ChangeUpdate;
import com.google.gerrit.server.project.ChangeControl;
import com.google.gerrit.server.project.NoSuchChangeException;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;

import java.io.IOException;
import java.sql.Timestamp;

class ReplayMessagesStep {

  interface Factory {
    ReplayMessagesStep create(Change change, ChangeInfo changeInfo);
  }

  private final AccountUtil accountUtil;
  private final ChangeUpdate.Factory updateFactory;
  private final ChangeMessagesUtil cmUtil;
  private final ReviewDb db;
  private final IdentifiedUser.GenericFactory genericUserFactory;
  private final ChangeControl.GenericFactory changeControlFactory;
  private final Change change;
  private final ChangeInfo changeInfo;

  @Inject
  public ReplayMessagesStep(AccountUtil accountUtil,
      ChangeUpdate.Factory updateFactory,
      ChangeMessagesUtil cmUtil,
      IdentifiedUser.GenericFactory genericUserFactory,
      ChangeControl.GenericFactory changeControlFactory,
      ReviewDb db,
      @Assisted Change change,
      @Assisted ChangeInfo changeInfo) {
    this.accountUtil = accountUtil;
    this.updateFactory = updateFactory;
    this.cmUtil = cmUtil;
    this.db = db;
    this.genericUserFactory = genericUserFactory;
    this.changeControlFactory = changeControlFactory;
    this.change = change;
    this.changeInfo = changeInfo;
  }

  void replay() throws NoSuchAccountException, NoSuchChangeException,
      OrmException, IOException {
    for (ChangeMessageInfo msg : changeInfo.messages) {
      Account.Id userId = accountUtil.resolveUser(msg.author);
      Timestamp ts = msg.date;
      ChangeUpdate update = updateFactory.create(control(change, userId), ts);
      ChangeMessage cmsg =
          new ChangeMessage(new ChangeMessage.Key(change.getId(), msg.id),
              userId, ts, new PatchSet.Id(change.getId(), msg._revisionNumber));
      cmsg.setMessage(msg.message);
      cmUtil.addChangeMessage(db, update, cmsg);
      update.commit();
    }
  }

  private ChangeControl control(Change change, Account.Id id)
      throws NoSuchChangeException {
    return changeControlFactory.controlFor(change,
        genericUserFactory.create(id));
  }
}

@@ -1535,16 +1535,46 @@
 mestamp;
+%0Aimport java.util.Collections;
 %0A%0Aclass 
@@ -2847,24 +2847,89 @@
 messages) %7B%0A
+      Timestamp ts = msg.date;%0A      if (msg.author != null) %7B%0A  
       Accoun
@@ -2985,37 +2985,8 @@
 ;%0A  
-    Timestamp ts = msg.date;%0A
     
@@ -3062,24 +3062,26 @@
  ts);%0A      
+  
 ChangeMessag
@@ -3089,16 +3089,18 @@
  cmsg =%0A
+  
         
@@ -3180,16 +3180,18 @@
         
+  
 userId, 
@@ -3249,16 +3249,18 @@
 mber));%0A
+  
       cm
@@ -3293,16 +3293,18 @@
 ;%0A      
+  
 cmUtil.a
@@ -3344,16 +3344,18 @@
 ;%0A      
+ 
+ 
 update.c
@@ -3363,16 +3363,385 @@
 mmit();%0A
+      %7D else %7B%0A        // Message create by the GerritPersonIdent user%0A        ChangeMessage cmsg =%0A            new ChangeMessage(new ChangeMessage.Key(change.getId(), msg.id),%0A                null, ts, new PatchSet.Id(change.getId(), msg._revisionNumber));%0A        cmsg.setMessage(msg.message);%0A        db.changeMessages().insert(Collections.singleton(cmsg));%0A      %7D%0A
     %7D%0A  

Fix the NPE when importing merged changes