// Copyright (C) 2017 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.common.TimeUtil;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.webui.UiAction;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.ChangeMessagesUtil;
import com.google.gerrit.server.permissions.GlobalPermission;
import com.google.gerrit.server.permissions.PermissionBackend;
import com.google.gerrit.server.project.ChangeControl;
import com.google.gerrit.server.update.BatchUpdate;
import com.google.gerrit.server.update.RetryHelper;
import com.google.gerrit.server.update.RetryingRestModifyView;
import com.google.gerrit.server.update.UpdateException;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;

@Singleton
public class PostPrivate
    extends RetryingRestModifyView<ChangeResource, SetPrivateOp.Input, Response<String>>
    implements UiAction<ChangeResource> {
  private final ChangeMessagesUtil cmUtil;
  private final Provider<ReviewDb> dbProvider;
  private final PermissionBackend permissionBackend;

  @Inject
  PostPrivate(
      Provider<ReviewDb> dbProvider,
      RetryHelper retryHelper,
      ChangeMessagesUtil cmUtil,
      PermissionBackend permissionBackend) {
    super(retryHelper);
    this.dbProvider = dbProvider;
    this.cmUtil = cmUtil;
    this.permissionBackend = permissionBackend;
  }

  @Override
  public Response<String> applyImpl(
      BatchUpdate.Factory updateFactory, ChangeResource rsrc, SetPrivateOp.Input input)
      throws RestApiException, UpdateException {
    if (!canSetPrivate(rsrc)) {
      throw new AuthException("not allowed to mark private");
    }

    if (rsrc.getChange().isPrivate()) {
      return Response.ok("");
    }

    ChangeControl control = rsrc.getControl();
    SetPrivateOp op = new SetPrivateOp(cmUtil, true, input);
    try (BatchUpdate u =
        updateFactory.create(
            dbProvider.get(),
            control.getProject().getNameKey(),
            control.getUser(),
            TimeUtil.nowTs())) {
      u.addOp(control.getId(), op).execute();
    }

    return Response.created("");
  }

  @Override
  public Description getDescription(ChangeResource rsrc) {
    Change change = rsrc.getChange();
    return new UiAction.Description()
        .setLabel("Mark private")
        .setTitle("Mark change as private")
        .setVisible(!change.isPrivate() && canSetPrivate(rsrc));
  }

  private boolean canSetPrivate(ChangeResource rsrc) {
    PermissionBackend.WithUser user = permissionBackend.user(rsrc.getUser());
    return user.testOrFalse(GlobalPermission.ADMINISTRATE_SERVER)
        || (rsrc.isUserOwner() && rsrc.getChange().getStatus() != Change.Status.MERGED);
  }
}

@@ -655,41 +655,258 @@
 ort 
-com.google.gerrit.common.TimeUtil
+static com.google.gerrit.extensions.conditions.BooleanCondition.and;%0Aimport static com.google.gerrit.extensions.conditions.BooleanCondition.or;%0A%0Aimport com.google.gerrit.common.TimeUtil;%0Aimport com.google.gerrit.extensions.conditions.BooleanCondition
 ;%0Aim
@@ -2623,16 +2623,24 @@
 te(rsrc)
+.value()
 ) %7B%0A    
@@ -3421,16 +3421,20 @@
 Visible(
+and(
 !change.
@@ -3444,19 +3444,17 @@
 rivate()
- &&
+,
  canSetP
@@ -3466,16 +3466,17 @@
 e(rsrc))
+)
 ;%0A  %7D%0A%0A 
@@ -3488,15 +3488,24 @@
 ate 
-b
+B
 oolean
+Condition
  can
@@ -3631,62 +3631,11 @@
 urn 
-user.testOrFalse(GlobalPermission.ADMINISTRATE_SERVER)
+or(
 %0A   
@@ -3643,12 +3643,8 @@
     
-%7C%7C (
 rsrc
@@ -3713,16 +3713,77 @@
 s.MERGED
+,%0A        user.testCond(GlobalPermission.ADMINISTRATE_SERVER)
 );%0A  %7D%0A%7D

Use BooleanCondition for #setVisible