/* 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.rest.dto.history;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response.Status;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.history.HistoricProcessInstanceQuery;
import org.camunda.bpm.engine.rest.dto.AbstractQueryDto;
import org.camunda.bpm.engine.rest.dto.CamundaQueryParam;
import org.camunda.bpm.engine.rest.dto.VariableQueryParameterDto;
import org.camunda.bpm.engine.rest.dto.converter.BooleanConverter;
import org.camunda.bpm.engine.rest.dto.converter.DateConverter;
import org.camunda.bpm.engine.rest.dto.converter.StringListConverter;
import org.camunda.bpm.engine.rest.dto.converter.StringSetConverter;
import org.camunda.bpm.engine.rest.dto.converter.VariableListConverter;
import org.camunda.bpm.engine.rest.exception.InvalidRequestException;
import org.codehaus.jackson.map.ObjectMapper;
public class HistoricProcessInstanceQueryDto extends AbstractQueryDto<HistoricProcessInstanceQuery> {
private static final String SORT_BY_PROCESS_INSTANCE_ID_VALUE = "instanceId";
private static final String SORT_BY_PROCESS_DEFINITION_ID_VALUE = "definitionId";
private static final String SORT_BY_PROCESS_INSTANCE_BUSINESS_KEY_VALUE = "businessKey";
private static final String SORT_BY_PROCESS_INSTANCE_START_TIME_VALUE = "startTime";
private static final String SORT_BY_PROCESS_INSTANCE_END_TIME_VALUE = "endTime";
private static final String SORT_BY_PROCESS_INSTANCE_DURATION_VALUE = "duration";
private static final List<String> VALID_SORT_BY_VALUES;
static {
VALID_SORT_BY_VALUES = new ArrayList<String>();
VALID_SORT_BY_VALUES.add(SORT_BY_PROCESS_INSTANCE_ID_VALUE);
VALID_SORT_BY_VALUES.add(SORT_BY_PROCESS_DEFINITION_ID_VALUE);
VALID_SORT_BY_VALUES.add(SORT_BY_PROCESS_INSTANCE_BUSINESS_KEY_VALUE);
VALID_SORT_BY_VALUES.add(SORT_BY_PROCESS_INSTANCE_START_TIME_VALUE);
VALID_SORT_BY_VALUES.add(SORT_BY_PROCESS_INSTANCE_END_TIME_VALUE);
VALID_SORT_BY_VALUES.add(SORT_BY_PROCESS_INSTANCE_DURATION_VALUE);
}
private String processInstanceId;
private Set<String> processInstanceIds;
private String processDefinitionId;
private String processDefinitionKey;
private String processDefinitionName;
private String processDefinitionNameLike;
private List<String> processDefinitionKeyNotIn;
private String processInstanceBusinessKey;
private String processInstanceBusinessKeyLike;
private Boolean finished;
private Boolean unfinished;
private Date startedBefore;
private Date startedAfter;
private Date finishedBefore;
private Date finishedAfter;
private String startedBy;
private String superProcessInstanceId;
private String subProcessInstanceId;
private String caseInstanceId;
private List<VariableQueryParameterDto> variables;
public HistoricProcessInstanceQueryDto() {}
public HistoricProcessInstanceQueryDto(ObjectMapper objectMapper, MultivaluedMap<String, String> queryParameters) {
super(objectMapper, queryParameters);
}
@CamundaQueryParam("processInstanceId")
public void setProcessInstanceId(String processInstanceId) {
this.processInstanceId = processInstanceId;
}
@CamundaQueryParam(value = "processInstanceIds", converter = StringSetConverter.class)
public void setProcessInstanceIds(Set<String> processInstanceIds) {
this.processInstanceIds = processInstanceIds;
}
@CamundaQueryParam("processDefinitionId")
public void setProcessDefinitionId(String processDefinitionId) {
this.processDefinitionId = processDefinitionId;
}
@CamundaQueryParam("processDefinitionName")
public void setProcessDefinitionName(String processDefinitionName) {
this.processDefinitionName = processDefinitionName;
}
@CamundaQueryParam("processDefinitionNameLike")
public void setProcessDefinitionNameLike(String processDefinitionNameLike) {
this.processDefinitionNameLike = processDefinitionNameLike;
}
@CamundaQueryParam("processDefinitionKey")
public void setProcessDefinitionKey(String processDefinitionKey) {
this.processDefinitionKey = processDefinitionKey;
}
@CamundaQueryParam(value = "processDefinitionKeyNotIn", converter = StringListConverter.class)
public void setProcessDefinitionKeyNotIn(List<String> processDefinitionKeys) {
this.processDefinitionKeyNotIn = processDefinitionKeys;
}
@CamundaQueryParam("processInstanceBusinessKey")
public void setProcessInstanceBusinessKey(String processInstanceBusinessKey) {
this.processInstanceBusinessKey = processInstanceBusinessKey;
}
@CamundaQueryParam("processInstanceBusinessKeyLike")
public void setProcessInstanceBusinessKeyLike(String processInstanceBusinessKeyLike) {
this.processInstanceBusinessKeyLike = processInstanceBusinessKeyLike;
}
@CamundaQueryParam(value = "finished", converter = BooleanConverter.class)
public void setFinished(Boolean finished) {
this.finished = finished;
}
@CamundaQueryParam(value = "unfinished", converter = BooleanConverter.class)
public void setUnfinished(Boolean unfinished) {
this.unfinished = unfinished;
}
@CamundaQueryParam(value = "startedBefore", converter = DateConverter.class)
public void setStartedBefore(Date startedBefore) {
this.startedBefore = startedBefore;
}
@CamundaQueryParam(value = "startedAfter", converter = DateConverter.class)
public void setStartedAfter(Date startedAfter) {
this.startedAfter = startedAfter;
}
@CamundaQueryParam(value = "finishedBefore", converter = DateConverter.class)
public void setFinishedBefore(Date finishedBefore) {
this.finishedBefore = finishedBefore;
}
@CamundaQueryParam(value = "finishedAfter", converter = DateConverter.class)
public void setFinishedAfter(Date finishedAfter) {
this.finishedAfter = finishedAfter;
}
@CamundaQueryParam("startedBy")
public void setStartedBy(String startedBy) {
this.startedBy = startedBy;
}
@CamundaQueryParam("superProcessInstanceId")
public void setSuperProcessInstanceId(String superProcessInstanceId) {
this.superProcessInstanceId = superProcessInstanceId;
}
@CamundaQueryParam("subProcessInstanceId")
public void setSubProcessInstanceId(String subProcessInstanceId) {
this.subProcessInstanceId = subProcessInstanceId;
}
@CamundaQueryParam("caseInstanceId")
public void setCaseInstanceId(String caseInstanceId) {
this.caseInstanceId = caseInstanceId;
}
@CamundaQueryParam(value = "variables", converter = VariableListConverter.class)
public void setVariables(List<VariableQueryParameterDto> variables) {
this.variables = variables;
}
@Override
protected boolean isValidSortByValue(String value) {
return VALID_SORT_BY_VALUES.contains(value);
}
@Override
protected HistoricProcessInstanceQuery createNewQuery(ProcessEngine engine) {
return engine.getHistoryService().createHistoricProcessInstanceQuery();
}
@Override
protected void applyFilters(HistoricProcessInstanceQuery query) {
if (processInstanceId != null) {
query.processInstanceId(processInstanceId);
}
if (processInstanceIds != null) {
query.processInstanceIds(processInstanceIds);
}
if (processDefinitionId != null) {
query.processDefinitionId(processDefinitionId);
}
if (processDefinitionKey != null) {
query.processDefinitionKey(processDefinitionKey);
}
if (processDefinitionName != null) {
query.processDefinitionName(processDefinitionName);
}
if (processDefinitionNameLike != null) {
query.processDefinitionNameLike(processDefinitionNameLike);
}
if (processDefinitionKeyNotIn != null) {
query.processDefinitionKeyNotIn(processDefinitionKeyNotIn);
}
if (processInstanceBusinessKey != null) {
query.processInstanceBusinessKey(processInstanceBusinessKey);
}
if (processInstanceBusinessKeyLike != null) {
query.processInstanceBusinessKeyLike(processInstanceBusinessKeyLike);
}
if (finished != null && finished) {
query.finished();
}
if (unfinished != null && unfinished) {
query.unfinished();
}
if (startedBefore != null) {
query.startedBefore(startedBefore);
}
if (startedAfter != null) {
query.startedAfter(startedAfter);
}
if (finishedBefore != null) {
query.finishedBefore(finishedBefore);
}
if (finishedAfter != null) {
query.finishedAfter(finishedAfter);
}
if (startedBy != null) {
query.startedBy(startedBy);
}
if (superProcessInstanceId != null) {
query.superProcessInstanceId(superProcessInstanceId);
}
if (subProcessInstanceId != null) {
query.subProcessInstanceId(subProcessInstanceId);
}
if (caseInstanceId != null) {
query.caseInstanceId(caseInstanceId);
}
if (variables != null) {
for (VariableQueryParameterDto variableQueryParam : variables) {
String variableName = variableQueryParam.getName();
String op = variableQueryParam.getOperator();
Object variableValue = variableQueryParam.resolveValue(objectMapper);
if (op.equals(VariableQueryParameterDto.EQUALS_OPERATOR_NAME)) {
query.variableValueEquals(variableName, variableValue);
} else if (op.equals(VariableQueryParameterDto.GREATER_THAN_OPERATOR_NAME)) {
query.variableValueGreaterThan(variableName, variableValue);
} else if (op.equals(VariableQueryParameterDto.GREATER_THAN_OR_EQUALS_OPERATOR_NAME)) {
query.variableValueGreaterThanOrEqual(variableName, variableValue);
} else if (op.equals(VariableQueryParameterDto.LESS_THAN_OPERATOR_NAME)) {
query.variableValueLessThan(variableName, variableValue);
} else if (op.equals(VariableQueryParameterDto.LESS_THAN_OR_EQUALS_OPERATOR_NAME)) {
query.variableValueLessThanOrEqual(variableName, variableValue);
} else if (op.equals(VariableQueryParameterDto.NOT_EQUALS_OPERATOR_NAME)) {
query.variableValueNotEquals(variableName, variableValue);
} else if (op.equals(VariableQueryParameterDto.LIKE_OPERATOR_NAME)) {
query.variableValueLike(variableName, String.valueOf(variableValue));
} else {
throw new InvalidRequestException(Status.BAD_REQUEST, "Invalid variable comparator specified: " + op);
}
}
}
}
@Override
protected void applySortingOptions(HistoricProcessInstanceQuery query) {
if (sortBy != null) {
if (sortBy.equals(SORT_BY_PROCESS_INSTANCE_ID_VALUE)) {
query.orderByProcessInstanceId();
} else if (sortBy.equals(SORT_BY_PROCESS_DEFINITION_ID_VALUE)) {
query.orderByProcessDefinitionId();
} else if (sortBy.equals(SORT_BY_PROCESS_INSTANCE_BUSINESS_KEY_VALUE)) {
query.orderByProcessInstanceBusinessKey();
} else if (sortBy.equals(SORT_BY_PROCESS_INSTANCE_START_TIME_VALUE)) {
query.orderByProcessInstanceStartTime();
} else if (sortBy.equals(SORT_BY_PROCESS_INSTANCE_END_TIME_VALUE)) {
query.orderByProcessInstanceEndTime();
} else if (sortBy.equals(SORT_BY_PROCESS_INSTANCE_DURATION_VALUE)) {
query.orderByProcessInstanceDuration();
}
}
if (sortOrder != null) {
if (sortOrder.equals(SORT_ORDER_ASC_VALUE)) {
query.asc();
} else if (sortOrder.equals(SORT_ORDER_DESC_VALUE)) {
query.desc();
}
}
}
}