/* 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 org.camunda.bpm.engine.impl.metrics;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import org.camunda.bpm.engine.ProcessEngineException;
import org.camunda.bpm.engine.impl.db.ListQueryParameterObject;
import org.camunda.bpm.engine.impl.interceptor.Command;
import org.camunda.bpm.engine.impl.interceptor.CommandContext;
import org.camunda.bpm.engine.impl.interceptor.CommandExecutor;
import static org.camunda.bpm.engine.impl.util.EnsureUtil.ensureNotNull;
import org.camunda.bpm.engine.management.Metric;
import org.camunda.bpm.engine.management.MetricsQuery;

/**
 * @author Daniel Meyer
 *
 */
public class MetricsQueryImpl extends ListQueryParameterObject implements Serializable, Command<Object>, MetricsQuery {

  public static final int DEFAULT_LIMIT_SELECT_INTERVAL = 200;
  public static final long DEFAULT_SELECT_INTERVAL = 15 * 60;

  private static final long serialVersionUID = 1L;

  protected String name;
  protected String reporter;
  protected Date startDate;
  protected Date endDate;
  protected Long interval;

  protected transient CommandExecutor commandExecutor;

  public MetricsQueryImpl(CommandExecutor commandExecutor) {
    this.commandExecutor = commandExecutor;
    this.maxResults = DEFAULT_LIMIT_SELECT_INTERVAL;
    this.interval = DEFAULT_SELECT_INTERVAL;
  }

  public MetricsQueryImpl name(String name) {
    ensureNotNull("name", name);
    this.name = name;
    return this;
  }

  public MetricsQuery reporter(String reporter) {
    ensureNotNull("reporter", reporter);
    this.reporter = reporter;
    return this;
  }

  public MetricsQueryImpl startDate(Date startDate) {
    ensureNotNull("startDate", startDate);
    this.startDate = startDate;
    return this;
  }

  public MetricsQueryImpl endDate(Date endDate) {
    ensureNotNull("endDate", endDate);
    this.endDate = endDate;
    return this;
  }

  /**
   * Contains the command implementation which should be executed either
   * metric sum or select metric grouped by time interval.
   *
   * Note: this enables to quit with the enum distinction
   */
  protected Command<Object> callback;

  @Override
  public List<Metric> interval() {
    callback = new Command() {
      @Override
      public Object execute(CommandContext commandContext) {
        return commandContext.getMeterLogManager()
          .executeSelectInterval(MetricsQueryImpl.this);
      }
    };

    return (List<Metric>) commandExecutor.execute(this);
  }

  @Override
  public List<Metric> interval(long interval) {
    this.interval = interval;
    return interval();
  }

  public long sum() {
    callback = new Command() {
      @Override
      public Object execute(CommandContext commandContext) {
        return commandContext.getMeterLogManager()
          .executeSelectSum(MetricsQueryImpl.this);
      }
    };

    return (Long) commandExecutor.execute(this);
  }

  @Override
  public Object execute(CommandContext commandContext) {
    if (callback != null) {
      return callback.execute(commandContext);
    }
    throw new ProcessEngineException("Query can't be executed. Use either sum or interval to query the metrics.");
  }

  @Override
  public MetricsQuery offset(int offset) {
    setFirstResult(offset);
    return this;
  }

  @Override
  public MetricsQuery limit(int maxResults) {
    setMaxResults(maxResults);
    return this;
  }

  @Override
  public void setMaxResults(int maxResults) {
    if (maxResults > DEFAULT_LIMIT_SELECT_INTERVAL) {
      throw new ProcessEngineException("Metrics interval query row limit can't be set larger than 200.");
    }
    this.maxResults = maxResults;
  }

  public Date getStartDate() {
    return startDate;
  }

  public Date getEndDate() {
    return endDate;
  }

  public String getName() {
    return name;
  }

  public String getReporter() {
    return reporter;
  }

  public Long getInterval() {
    if (interval == null) {
      return DEFAULT_SELECT_INTERVAL;
    }
    return interval;
  }

  @Override
  public int getMaxResults() {
    if (maxResults > DEFAULT_LIMIT_SELECT_INTERVAL) {
      return DEFAULT_LIMIT_SELECT_INTERVAL;
    }
    return super.getMaxResults();
  }

}

@@ -979,81 +979,8 @@
 or;%0A
-import static org.camunda.bpm.engine.impl.util.EnsureUtil.ensureNotNull;%0A
 impo
@@ -1864,41 +1864,8 @@
 ) %7B%0A
-    ensureNotNull(%22name%22, name);%0A
     
@@ -1882,16 +1882,16 @@
 = name;%0A
+
     retu
@@ -1958,49 +1958,8 @@
 ) %7B%0A
-    ensureNotNull(%22reporter%22, reporter);%0A
     
@@ -2064,51 +2064,8 @@
 ) %7B%0A
-    ensureNotNull(%22startDate%22, startDate);%0A
     
@@ -2106,32 +2106,32 @@
 turn this;%0A  %7D%0A%0A
+
   public Metrics
@@ -2168,47 +2168,8 @@
 ) %7B%0A
-    ensureNotNull(%22endDate%22, endDate);%0A
     

remove not null check from metrics query