Mark changes as submitted when the submit action is pushed

A submitted change is a different status, so it can lay around
pending until its dependencies are also submitted and they can
be merged into the destination branch.

Currently we don't actually do the merge, we just mark the state
on the change and record who did it.

Signed-off-by: Shawn O. Pearce <sop@google.com>
diff --git a/appjar/src/main/java/com/google/gerrit/Gerrit.gwt.xml b/appjar/src/main/java/com/google/gerrit/Gerrit.gwt.xml
index 71b0040..660c939 100644
--- a/appjar/src/main/java/com/google/gerrit/Gerrit.gwt.xml
+++ b/appjar/src/main/java/com/google/gerrit/Gerrit.gwt.xml
@@ -26,6 +26,8 @@
            class='com.google.gerrit.server.ChangeDetailServiceSrv'/>
   <servlet path='/rpc/ChangeListService'
            class='com.google.gerrit.server.ChangeListServiceSrv'/>
+  <servlet path='/rpc/ChangeManageService'
+           class='com.google.gerrit.server.ChangeManageServiceSrv'/>
   <servlet path='/rpc/GroupAdminService'
            class='com.google.gerrit.server.GroupAdminServiceSrv'/>
   <servlet path='/rpc/PatchDetailService'
diff --git a/appjar/src/main/java/com/google/gerrit/client/changes/ChangeManageService.java b/appjar/src/main/java/com/google/gerrit/client/changes/ChangeManageService.java
new file mode 100644
index 0000000..a2a7b18
--- /dev/null
+++ b/appjar/src/main/java/com/google/gerrit/client/changes/ChangeManageService.java
@@ -0,0 +1,28 @@
+// Copyright 2009 Google Inc.
+//
+// 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.client.changes;
+
+import com.google.gerrit.client.reviewdb.ApprovalCategoryValue;
+import com.google.gerrit.client.reviewdb.PatchSet;
+import com.google.gerrit.client.rpc.SignInRequired;
+import com.google.gwt.user.client.rpc.AsyncCallback;
+import com.google.gwtjsonrpc.client.RemoteJsonService;
+import com.google.gwtjsonrpc.client.VoidResult;
+
+public interface ChangeManageService extends RemoteJsonService {
+  @SignInRequired
+  void patchSetAction(ApprovalCategoryValue.Id value, PatchSet.Id patchSetId,
+      AsyncCallback<VoidResult> callback);
+}
diff --git a/appjar/src/main/java/com/google/gerrit/client/changes/PatchSetPanel.java b/appjar/src/main/java/com/google/gerrit/client/changes/PatchSetPanel.java
index 0c36040..4026899 100644
--- a/appjar/src/main/java/com/google/gerrit/client/changes/PatchSetPanel.java
+++ b/appjar/src/main/java/com/google/gerrit/client/changes/PatchSetPanel.java
@@ -19,18 +19,24 @@
 import com.google.gerrit.client.data.ApprovalType;
 import com.google.gerrit.client.data.ChangeDetail;
 import com.google.gerrit.client.data.PatchSetDetail;
+import com.google.gerrit.client.reviewdb.ApprovalCategory;
+import com.google.gerrit.client.reviewdb.ApprovalCategoryValue;
 import com.google.gerrit.client.reviewdb.PatchSet;
 import com.google.gerrit.client.rpc.Common;
 import com.google.gerrit.client.rpc.GerritCallback;
-import com.google.gwt.core.client.GWT;
 import com.google.gwt.user.client.ui.Button;
+import com.google.gwt.user.client.ui.ClickListener;
 import com.google.gwt.user.client.ui.Composite;
 import com.google.gwt.user.client.ui.DisclosureEvent;
 import com.google.gwt.user.client.ui.DisclosureHandler;
 import com.google.gwt.user.client.ui.FlowPanel;
 import com.google.gwt.user.client.ui.Grid;
 import com.google.gwt.user.client.ui.Panel;
+import com.google.gwt.user.client.ui.Widget;
 import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
+import com.google.gwtjsonrpc.client.VoidResult;
+
+import java.util.Set;
 
 class PatchSetPanel extends Composite implements DisclosureHandler {
   private static final int R_DOWNLOAD = 0;
@@ -115,16 +121,38 @@
   }
 
   private void populateActions(final PatchSetDetail detail) {
-    if (changeDetail.getCurrentActions() != null
-        && !changeDetail.getCurrentActions().isEmpty()) {
-      for (final ApprovalType at : Common.getGerritConfig().getActionTypes()) {
-        if (changeDetail.getCurrentActions().contains(at.getCategory().getId())) {
-          final Button b =
-              new Button(Util.M.patchSetAction(at.getCategory().getName(),
-                  detail.getPatchSet().getPatchSetId()));
-          actionsPanel.add(b);
-        }
+    final Set<ApprovalCategory.Id> allowed = changeDetail.getCurrentActions();
+    if (allowed == null) {
+      // No set of actions, perhaps the user is not signed in?
+      return;
+    }
+
+    for (final ApprovalType at : Common.getGerritConfig().getActionTypes()) {
+      final ApprovalCategoryValue max = at.getMax();
+      if (max == null || max.getValue() <= 0) {
+        // No positive assertion, don't draw a button.
+        continue;
       }
+      if (!allowed.contains(at.getCategory().getId())) {
+        // User isn't permitted to invoke this.
+        continue;
+      }
+
+      final Button b =
+          new Button(Util.M.patchSetAction(at.getCategory().getName(), detail
+              .getPatchSet().getPatchSetId()));
+      b.addClickListener(new ClickListener() {
+        public void onClick(Widget sender) {
+          Util.MANAGE_SVC.patchSetAction(max.getId(), patchSet.getId(),
+              new GerritCallback<VoidResult>() {
+                public void onSuccess(VoidResult result) {
+                  // TODO refresh change screen
+                  actionsPanel.remove(b);
+                }
+              });
+        }
+      });
+      actionsPanel.add(b);
     }
   }
 
diff --git a/appjar/src/main/java/com/google/gerrit/client/changes/Util.java b/appjar/src/main/java/com/google/gerrit/client/changes/Util.java
index f8dd5f8..44795c6 100644
--- a/appjar/src/main/java/com/google/gerrit/client/changes/Util.java
+++ b/appjar/src/main/java/com/google/gerrit/client/changes/Util.java
@@ -24,6 +24,7 @@
 
   public static final ChangeDetailService DETAIL_SVC;
   public static final ChangeListService LIST_SVC;
+  public static final ChangeManageService MANAGE_SVC;
 
   static {
     DETAIL_SVC = GWT.create(ChangeDetailService.class);
@@ -31,6 +32,9 @@
 
     LIST_SVC = GWT.create(ChangeListService.class);
     JsonUtil.bind(LIST_SVC, "rpc/ChangeListService");
+
+    MANAGE_SVC = GWT.create(ChangeManageService.class);
+    JsonUtil.bind(MANAGE_SVC, "rpc/ChangeManageService");
   }
 
   public static String toLongString(final Change.Status status) {
diff --git a/appjar/src/main/java/com/google/gerrit/client/reviewdb/ApprovalCategoryValue.java b/appjar/src/main/java/com/google/gerrit/client/reviewdb/ApprovalCategoryValue.java
index 05bc913..c37c29d 100644
--- a/appjar/src/main/java/com/google/gerrit/client/reviewdb/ApprovalCategoryValue.java
+++ b/appjar/src/main/java/com/google/gerrit/client/reviewdb/ApprovalCategoryValue.java
@@ -66,6 +66,10 @@
     this.name = name;
   }
 
+  public ApprovalCategoryValue.Id getId() {
+    return key;
+  }
+
   public ApprovalCategory.Id getCategoryId() {
     return key.categoryId;
   }
diff --git a/appjar/src/main/java/com/google/gerrit/client/reviewdb/Change.java b/appjar/src/main/java/com/google/gerrit/client/reviewdb/Change.java
index 6ed6c54..47fb303 100644
--- a/appjar/src/main/java/com/google/gerrit/client/reviewdb/Change.java
+++ b/appjar/src/main/java/com/google/gerrit/client/reviewdb/Change.java
@@ -51,12 +51,18 @@
     }
   }
 
+  protected static final char MIN_OPEN = 'a';
   protected static final char STATUS_NEW = 'n';
+  protected static final char STATUS_SUBMITTED = 's';
+  protected static final char MAX_OPEN = 'z';
+
   protected static final char STATUS_MERGED = 'M';
 
   public static enum Status {
     NEW(STATUS_NEW, false),
 
+    SUBMITTED(STATUS_SUBMITTED, false),
+
     MERGED(STATUS_MERGED, true),
 
     ABANDONED('A', true);
diff --git a/appjar/src/main/java/com/google/gerrit/client/reviewdb/ChangeAccess.java b/appjar/src/main/java/com/google/gerrit/client/reviewdb/ChangeAccess.java
index a9a522c..483bd23 100644
--- a/appjar/src/main/java/com/google/gerrit/client/reviewdb/ChangeAccess.java
+++ b/appjar/src/main/java/com/google/gerrit/client/reviewdb/ChangeAccess.java
@@ -24,8 +24,8 @@
   @PrimaryKey("changeId")
   Change get(Change.Id id) throws OrmException;
 
-  @Query("WHERE owner = ? AND status = '" + Change.STATUS_NEW
-      + "' ORDER BY lastUpdatedOn DESC")
+  @Query("WHERE owner = ? AND status >= '" + Change.MIN_OPEN
+      + "' AND status <= '" + Change.MAX_OPEN + "' ORDER BY lastUpdatedOn DESC")
   ResultSet<Change> byOwnerOpen(Account.Id id) throws OrmException;
 
   @Query("WHERE owner = ? AND status = '" + Change.STATUS_MERGED
diff --git a/appjar/src/main/java/com/google/gerrit/client/reviewdb/ChangeApproval.java b/appjar/src/main/java/com/google/gerrit/client/reviewdb/ChangeApproval.java
index 5b6c3ba..71c4c33 100644
--- a/appjar/src/main/java/com/google/gerrit/client/reviewdb/ChangeApproval.java
+++ b/appjar/src/main/java/com/google/gerrit/client/reviewdb/ChangeApproval.java
@@ -87,6 +87,10 @@
     setGranted();
   }
 
+  public ChangeApproval.Key getKey() {
+    return key;
+  }
+
   public Change.Id getChangeId() {
     return key.changeId;
   }
diff --git a/appjar/src/main/java/com/google/gerrit/server/ChangeManageServiceImpl.java b/appjar/src/main/java/com/google/gerrit/server/ChangeManageServiceImpl.java
new file mode 100644
index 0000000..f46eea9
--- /dev/null
+++ b/appjar/src/main/java/com/google/gerrit/server/ChangeManageServiceImpl.java
@@ -0,0 +1,135 @@
+// Copyright 2009 Google Inc.
+//
+// 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;
+
+import com.google.gerrit.client.changes.ChangeManageService;
+import com.google.gerrit.client.data.ApprovalType;
+import com.google.gerrit.client.reviewdb.Account;
+import com.google.gerrit.client.reviewdb.ApprovalCategory;
+import com.google.gerrit.client.reviewdb.ApprovalCategoryValue;
+import com.google.gerrit.client.reviewdb.Change;
+import com.google.gerrit.client.reviewdb.ChangeApproval;
+import com.google.gerrit.client.reviewdb.PatchSet;
+import com.google.gerrit.client.reviewdb.ReviewDb;
+import com.google.gerrit.client.rpc.BaseServiceImplementation;
+import com.google.gerrit.client.rpc.Common;
+import com.google.gerrit.client.rpc.NoSuchEntityException;
+import com.google.gerrit.client.workflow.FunctionState;
+import com.google.gwt.user.client.rpc.AsyncCallback;
+import com.google.gwtjsonrpc.client.VoidResult;
+import com.google.gwtorm.client.OrmException;
+import com.google.gwtorm.client.Transaction;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class ChangeManageServiceImpl extends BaseServiceImplementation
+    implements ChangeManageService {
+
+  public void patchSetAction(final ApprovalCategoryValue.Id value,
+      final PatchSet.Id patchSetId, final AsyncCallback<VoidResult> callback) {
+    run(callback, new Action<VoidResult>() {
+      public VoidResult run(final ReviewDb db) throws OrmException, Failure {
+        final Change change = db.changes().get(patchSetId.getParentKey());
+        if (change == null) {
+          throw new Failure(new NoSuchEntityException());
+        }
+
+        if (!patchSetId.equals(change.currentPatchSetId())) {
+          throw new Failure(new IllegalStateException("Patch set " + patchSetId
+              + " not current"));
+        }
+        if (change.getStatus().isClosed()) {
+          throw new Failure(new IllegalStateException("Change" + change.getId()
+              + " is closed"));
+        }
+
+        final List<ChangeApproval> allApprovals =
+            new ArrayList<ChangeApproval>(db.changeApprovals().byChange(
+                change.getId()).toList());
+
+        final Account.Id me = Common.getAccountId();
+        final ChangeApproval.Key ak =
+            new ChangeApproval.Key(change.getId(), me, value.getParentKey());
+        ChangeApproval myAction = null;
+        boolean isnew = true;
+        for (final ChangeApproval ca : allApprovals) {
+          if (ak.equals(ca.getKey())) {
+            isnew = false;
+            myAction = ca;
+            myAction.setValue(value.get());
+            myAction.setGranted();
+            break;
+          }
+        }
+        if (myAction == null) {
+          myAction = new ChangeApproval(ak, value.get());
+          allApprovals.add(myAction);
+        }
+
+        final ApprovalType actionType =
+            Common.getGerritConfig().getApprovalType(myAction.getCategoryId());
+        if (actionType == null || !actionType.getCategory().isAction()) {
+          throw new Failure(new IllegalArgumentException(actionType
+              .getCategory().getName()
+              + " not an action"));
+        }
+
+        final FunctionState fs =
+            new FunctionState(Common.getProjectCache().get(
+                change.getDest().getParentKey()), allApprovals);
+        for (ApprovalType c : Common.getGerritConfig().getApprovalTypes()) {
+          c.getCategory().getFunction().run(c, fs);
+        }
+        if (!actionType.getCategory().getFunction().isValid(me, actionType, fs)) {
+          throw new Failure(new IllegalStateException(actionType.getCategory()
+              .getName()
+              + " not permitted"));
+        }
+        fs.normalize(actionType, myAction);
+        if (myAction.getValue() <= 0) {
+          throw new Failure(new IllegalStateException(actionType.getCategory()
+              .getName()
+              + " not permitted"));
+        }
+
+        if (ApprovalCategory.SUBMIT.equals(actionType.getCategory().getId())) {
+          if (change.getStatus() == Change.Status.NEW) {
+            change.setStatus(Change.Status.SUBMITTED);
+          }
+        } else {
+          throw new Failure(new IllegalArgumentException(actionType
+              .getCategory().getName()
+              + " cannot be perfomed by Gerrit"));
+        }
+
+        final Transaction txn = db.beginTransaction();
+        db.changes().update(Collections.singleton(change), txn);
+        if (change.getStatus().isClosed()) {
+          db.changeApprovals().update(fs.getDirtyChangeApprovals(), txn);
+        }
+        if (isnew) {
+          db.changeApprovals().insert(Collections.singleton(myAction), txn);
+        } else {
+          db.changeApprovals().update(Collections.singleton(myAction), txn);
+        }
+        txn.commit();
+
+        return VoidResult.INSTANCE;
+      }
+    });
+  }
+}
diff --git a/appjar/src/main/java/com/google/gerrit/server/ChangeManageServiceSrv.java b/appjar/src/main/java/com/google/gerrit/server/ChangeManageServiceSrv.java
new file mode 100644
index 0000000..d76e0e7
--- /dev/null
+++ b/appjar/src/main/java/com/google/gerrit/server/ChangeManageServiceSrv.java
@@ -0,0 +1,24 @@
+// Copyright 2009 Google Inc.
+//
+// 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;
+
+
+/** Publishes {@link ChangeManageServiceImpl} over JSON. */
+public class ChangeManageServiceSrv extends GerritJsonServlet {
+  @Override
+  protected Object createServiceHandle() throws Exception {
+    return new ChangeManageServiceImpl();
+  }
+}
diff --git a/appwar/src/main/webapp/WEB-INF/web.xml b/appwar/src/main/webapp/WEB-INF/web.xml
index af7efe5..5bed37e 100644
--- a/appwar/src/main/webapp/WEB-INF/web.xml
+++ b/appwar/src/main/webapp/WEB-INF/web.xml
@@ -115,6 +115,16 @@
   </servlet-mapping>
 
   <servlet>
+    <servlet-name>ChangeManageService</servlet-name>
+    <servlet-class>com.google.gerrit.server.ChangeManageServiceSrv</servlet-class>
+    <load-on-startup>1</load-on-startup>
+  </servlet>
+  <servlet-mapping>
+    <servlet-name>ChangeManageService</servlet-name>
+    <url-pattern>/rpc/ChangeManageService</url-pattern>
+  </servlet-mapping>
+
+  <servlet>
     <servlet-name>PatchDetailService</servlet-name>
     <servlet-class>com.google.gerrit.server.PatchDetailServiceSrv</servlet-class>
     <load-on-startup>1</load-on-startup>