// 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.gerrit.extensions.restapi.RestResource;
import com.google.gerrit.extensions.restapi.RestResource.HasETag;
import com.google.gerrit.extensions.restapi.RestView;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.server.project.ChangeControl;
import com.google.inject.TypeLiteral;

public class ChangeResource implements RestResource, HasETag {
  public static final TypeLiteral<RestView<ChangeResource>> CHANGE_KIND =
      new TypeLiteral<RestView<ChangeResource>>() {};

  private final ChangeControl control;

  public ChangeResource(ChangeControl control) {
    this.control = control;
  }

  protected ChangeResource(ChangeResource copy) {
    this.control = copy.control;
  }

  public ChangeControl getControl() {
    return control;
  }

  public Change getChange() {
    return getControl().getChange();
  }

  @Override
  public String getETag() {
    return String.format("%x-%x",
        getChange().getLastUpdatedOn().getTime(),
        getChange().getRowVersion());
  }
}

@@ -644,16 +644,132 @@
 hange;%0A%0A
+import com.google.common.base.Objects;%0Aimport com.google.common.hash.Hasher;%0Aimport com.google.common.hash.Hashing;%0A
 import c
@@ -1023,67 +1023,253 @@
 ver.
-project.ChangeControl;%0Aimport com.google.inject.TypeLiteral
+CurrentUser;%0Aimport com.google.gerrit.server.IdentifiedUser;%0Aimport com.google.gerrit.server.project.ChangeControl;%0Aimport com.google.gerrit.server.project.ProjectState;%0Aimport com.google.inject.TypeLiteral;%0A%0Aimport org.eclipse.jgit.lib.ObjectId
 ;%0A%0Ap
@@ -1852,46 +1852,109 @@
     
-return String.format(%22%25x-%25x%22,%0A        
+CurrentUser user = control.getCurrentUser();%0A    Hasher h = Hashing.md5().newHasher()%0A      .putLong(
 getC
@@ -1993,18 +1993,24 @@
 me()
-,
+)
 %0A      
-  
+.putInt(
 getC
@@ -2033,16 +2033,426 @@
 rsion())
+%0A      .putInt(user instanceof IdentifiedUser%0A          ? ((IdentifiedUser) user).getAccountId().get()%0A          : 0);%0A%0A    byte%5B%5D buf = new byte%5B20%5D;%0A    for (ProjectState p : control.getProjectControl().getProjectState().tree()) %7B%0A      ObjectId id = p.getConfig().getRevision();%0A      Objects.firstNonNull(id, ObjectId.zeroId()).copyRawTo(buf, 0);%0A      h.putBytes(buf);%0A    %7D%0A    return h.hash().toString()
 ;%0A  %7D%0A%7D%0A

Fix /detail caching to include project ACLs and user id