// 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.client.changes;

import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.rpc.GerritCallback;
import com.google.gerrit.client.rpc.Natives;
import com.google.gerrit.client.ui.InlineHyperlink;
import com.google.gerrit.common.PageLinks;
import com.google.gerrit.common.changes.ListChangesOption;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.http.client.URL;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.ListIterator;

public class DashboardTable extends ChangeTable2 {
  private List<Section> sections;
  private String title;
  private List<String> titles;
  private List<String> queries;

  public DashboardTable(String params) {
    titles = new ArrayList<String>();
    queries = new ArrayList<String>();
    String foreach = null;
    for (String kvPair : params.split("[,;&]")) {
      String[] kv = kvPair.split("=", 2);
      if (kv.length != 2 || kv[0].isEmpty()) {
        continue;
      }

      if ("title".equals(kv[0])) {
        title = URL.decodeQueryString(kv[1]);
      } else if ("foreach".equals(kv[0])) {
        foreach = URL.decodeQueryString(kv[1]);
      } else {
        titles.add(URL.decodeQueryString(kv[0]));
        queries.add(URL.decodeQueryString(kv[1]));
      }
    }

    if (foreach != null) {
      ListIterator<String> it = queries.listIterator();
      while (it.hasNext()) {
        it.set(it.next() + " " + foreach);
      }
    }

    addStyleName(Gerrit.RESOURCES.css().accountDashboard());

    sections = new ArrayList<ChangeTable2.Section>();
    int i = 0;
    for (String title : titles) {
      Section s = new Section();
      s.setTitleWidget(new InlineHyperlink(title, PageLinks.toChangeQuery(queries.get(i++))));
      addSection(s);
      sections.add(s);
    }
  }

  public String getTitle() {
    return title;
  }

  @Override
  protected void onLoad() {
    super.onLoad();

    if (queries.size() == 1) {
      ChangeList.next(queries.get(0),
          0, PagedSingleListScreen.MAX_SORTKEY,
          new GerritCallback<ChangeList>() {
            @Override
            public void onSuccess(ChangeList result) {
              updateColumnsForLabels(result);
              sections.get(0).display(result);
              finishDisplay();
            }
        });
    } else if (! queries.isEmpty()) {
      ChangeList.query(
          new GerritCallback<JsArray<ChangeList>>() {
            @Override
            public void onSuccess(JsArray<ChangeList> result) {
              List<ChangeList> cls = Natives.asList(result);
              updateColumnsForLabels(cls.toArray(new ChangeList[cls.size()]));
              for (int i = 0; i < cls.size(); i++) {
                sections.get(i).display(cls.get(i));
              }
              finishDisplay();
            }
          },
          EnumSet.noneOf(ListChangesOption.class),
          queries.toArray(new String[queries.size()]));
    }
  }
}

@@ -2280,24 +2280,76 @@
  Section();%0A
+      String query = removeLimit(queries.get(i++));%0A
       s.setT
@@ -2414,28 +2414,17 @@
 ery(quer
-ies.get(i++)
+y
 )));%0A   
@@ -2475,16 +2475,340 @@
  %7D%0A  %7D%0A%0A
+  private String removeLimit(String query) %7B%0A    StringBuilder unlimitedQuery = new StringBuilder();%0A    String%5B%5D operators = query.split(%22 %22);%0A    for (String o : operators) %7B%0A      if (!o.startsWith(%22limit:%22)) %7B%0A        unlimitedQuery.append(o).append(%22 %22);%0A      %7D%0A    %7D%0A    return unlimitedQuery.toString().trim();%0A  %7D%0A%0A
   public

Remove query limit when drilling down from a dashboard to a query