提交 66785eed 编写于 作者: Z Zoltan Palfi 提交者: mergify[bot]

fix: returning ProcessDefinition latest version when finding by key (#3056)

* fix: returning ProcessDefinition latest version when finding by key

* fix: remove deprecated annotations, unnecessary codes from test
上级 1752f45b
/*
* Copyright 2018 Alfresco, Inc. and/or its affiliates.
* Copyright 2020 Alfresco, Inc. and/or its affiliates.
*
* 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
* 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,
......@@ -18,8 +18,8 @@ package org.activiti.runtime.api.impl;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import org.activiti.api.model.shared.model.VariableInstance;
import org.activiti.api.process.model.Deployment;
import org.activiti.api.process.model.ProcessDefinition;
......@@ -113,17 +113,8 @@ public class ProcessRuntimeImpl implements ProcessRuntime {
public ProcessDefinition processDefinition(String processDefinitionId) {
org.activiti.engine.repository.ProcessDefinition processDefinition;
// try searching by Key if there is no matching by Id
List<org.activiti.engine.repository.ProcessDefinition> list = repositoryService
.createProcessDefinitionQuery()
.processDefinitionKey(processDefinitionId)
.orderByProcessDefinitionVersion()
.asc()
.list();
if (!list.isEmpty()) {
processDefinition = list.get(0);
} else {
processDefinition = repositoryService.getProcessDefinition(processDefinitionId);
}
processDefinition = findLatestProcessDefinitionByKey(processDefinitionId)
.orElseGet(() -> repositoryService.getProcessDefinition(processDefinitionId));
checkIfDefinitionBelongsToCurrentAppVersion(processDefinition);
......@@ -133,6 +124,16 @@ public class ProcessRuntimeImpl implements ProcessRuntime {
return processDefinitionConverter.from(processDefinition);
}
private Optional<org.activiti.engine.repository.ProcessDefinition> findLatestProcessDefinitionByKey(String processDefinitionKey) {
return repositoryService.createProcessDefinitionQuery()
.processDefinitionKey(processDefinitionKey)
.orderByProcessDefinitionAppVersion()
.desc()
.list()
.stream()
.findFirst();
}
public void checkIfDefinitionBelongsToCurrentAppVersion(org.activiti.engine.repository.ProcessDefinition processDefinition){
if (!selectLatestDeployment().getVersion().equals(processDefinition.getAppVersion())) {
throw new ActivitiForbiddenException("Process definition with the given id:'" + processDefinition.getId() + "' belongs to a different application version.");
......@@ -409,16 +410,11 @@ public class ProcessRuntimeImpl implements ProcessRuntime {
}
}
private ProcessDefinition getProcessDefinitionAndCheckUserHasRights(String processDefinitionId,
String processDefinitionKey) {
ProcessDefinition processDefinition = null;
private ProcessDefinition getProcessDefinitionAndCheckUserHasRights(String processDefinitionId, String processDefinitionKey) {
String checkId = processDefinitionKey != null ?
processDefinitionKey :
(processDefinitionId != null ? processDefinitionId : null);
String checkId = processDefinitionKey != null ? processDefinitionKey : processDefinitionId;
processDefinition = processDefinition(checkId);
ProcessDefinition processDefinition = processDefinition(checkId);
if (processDefinition == null) {
throw new IllegalStateException("At least Process Definition Id or Key needs to be provided to start a process");
......
/* Licensed under the Apache License, Version 2.0 (the "License");
/*
* Copyright 2020 Alfresco, Inc. and/or its affiliates.
*
* 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.
......@@ -322,6 +325,10 @@ public class ProcessDefinitionQueryImpl extends AbstractQuery<ProcessDefinitionQ
return orderBy(ProcessDefinitionQueryProperty.PROCESS_DEFINITION_VERSION);
}
public ProcessDefinitionQuery orderByProcessDefinitionAppVersion() {
return orderBy(ProcessDefinitionQueryProperty.PROCESS_DEFINITION_APP_VERSION);
}
public ProcessDefinitionQuery orderByProcessDefinitionName() {
return orderBy(ProcessDefinitionQueryProperty.PROCESS_DEFINITION_NAME);
}
......
/* Licensed under the Apache License, Version 2.0 (the "License");
/*
* Copyright 2020 Alfresco, Inc. and/or its affiliates.
*
* 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.
......@@ -21,8 +24,6 @@ import org.activiti.engine.repository.ProcessDefinitionQuery;
/**
* Contains the possible properties that can be used in a {@link ProcessDefinitionQuery}.
*
*/
public class ProcessDefinitionQueryProperty implements QueryProperty {
......@@ -34,6 +35,7 @@ public class ProcessDefinitionQueryProperty implements QueryProperty {
public static final ProcessDefinitionQueryProperty PROCESS_DEFINITION_CATEGORY = new ProcessDefinitionQueryProperty("RES.CATEGORY_");
public static final ProcessDefinitionQueryProperty PROCESS_DEFINITION_ID = new ProcessDefinitionQueryProperty("RES.ID_");
public static final ProcessDefinitionQueryProperty PROCESS_DEFINITION_VERSION = new ProcessDefinitionQueryProperty("RES.VERSION_");
public static final ProcessDefinitionQueryProperty PROCESS_DEFINITION_APP_VERSION = new ProcessDefinitionQueryProperty("RES.APP_VERSION_");
public static final ProcessDefinitionQueryProperty PROCESS_DEFINITION_NAME = new ProcessDefinitionQueryProperty("RES.NAME_");
public static final ProcessDefinitionQueryProperty DEPLOYMENT_ID = new ProcessDefinitionQueryProperty("RES.DEPLOYMENT_ID_");
public static final ProcessDefinitionQueryProperty PROCESS_DEFINITION_TENANT_ID = new ProcessDefinitionQueryProperty("RES.TENANT_ID_");
......
/* Licensed under the Apache License, Version 2.0 (the "License");
/*
* Copyright 2020 Alfresco, Inc. and/or its affiliates.
*
* 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.
......@@ -191,6 +194,11 @@ public interface ProcessDefinitionQuery extends Query<ProcessDefinitionQuery, Pr
*/
ProcessDefinitionQuery orderByProcessDefinitionVersion();
/**
* Order by the app version of the process definitions (needs to be followed by {@link #asc()} or {@link #desc()}).
*/
ProcessDefinitionQuery orderByProcessDefinitionAppVersion();
/**
* Order by the name of the process definitions (needs to be followed by {@link #asc()} or {@link #desc()}).
*/
......
package org.activiti.spring.boot;
import java.util.ArrayList;
import java.util.List;
import org.activiti.api.process.model.ProcessDefinition;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.core.common.project.model.ProjectManifest;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.spring.boot.security.util.SecurityUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -16,21 +22,43 @@ import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class ApplicationUpgradeIT {
private static final String PROCESS_DEFINITION_KEY = "SingleTaskProcess";
private static final String PROCESS_NAME = "single-task";
private static final String SINGLE_TASK_PROCESS_DEFINITION_PATH = "processes/SingleTaskProcess.bpmn20.xml";
private static final String DEPLOYMENT_TYPE_NAME = "SpringAutoDeployment";
@Autowired
private RepositoryService repositoryService;
@Test
public void should_UpdateDeploymentVersion_When_ManifestIsPresent() {
@Autowired
private ProcessRuntime processRuntime;
@Autowired
private SecurityUtil securityUtil;
private List<String> deploymentIds;
@Before
public void setUp() {
deploymentIds = new ArrayList<>();
}
@After
public void tearDown() {
deploymentIds.forEach(deploymentId -> repositoryService.deleteDeployment(deploymentId, true));
}
@Test
public void should_updateDeploymentVersion_when_manifestIsPresent() {
ProjectManifest projectManifest = new ProjectManifest();
projectManifest.setVersion("7");
Deployment deployment1 = repositoryService.createDeployment()
.setProjectManifest(projectManifest)
.enableDuplicateFiltering()
.name("deploymentName")
.deploy();
deploymentIds.add(deployment1.getId());
assertThat(deployment1.getVersion()).isEqualTo(1);
assertThat(deployment1.getProjectReleaseVersion()).isEqualTo("7");
......@@ -42,14 +70,41 @@ public class ApplicationUpgradeIT {
.enableDuplicateFiltering()
.name("deploymentName")
.deploy();
deploymentIds.add(deployment2.getId());
assertThat(deployment2.getProjectReleaseVersion()).isEqualTo("17");
assertThat(deployment2.getVersion()).isEqualTo(2);
}
@Test
public void should_getLatestProcessDefinitionByKey_when_multipleVersions() {
ProjectManifest projectManifest = new ProjectManifest();
projectManifest.setVersion("12");
deploySingleTaskProcess(projectManifest);
projectManifest.setVersion("34");
deploySingleTaskProcess(projectManifest);
securityUtil.logInAs("user");
ProcessDefinition result = processRuntime.processDefinition(PROCESS_DEFINITION_KEY);
repositoryService.deleteDeployment(deployment1.getId());
repositoryService.deleteDeployment(deployment2.getId());
assertThat(result).isNotNull();
assertThat(result.getName()).isEqualTo(PROCESS_NAME);
assertThat(result.getId()).contains(PROCESS_DEFINITION_KEY);
assertThat(result.getAppVersion()).isEqualTo("2");
}
private void deploySingleTaskProcess(ProjectManifest projectManifest) {
Deployment deployment = repositoryService.createDeployment()
.setProjectManifest(projectManifest)
.enableDuplicateFiltering()
.tenantId("tenantId")
.name(DEPLOYMENT_TYPE_NAME)
.addClasspathResource(SINGLE_TASK_PROCESS_DEFINITION_PATH)
.deploy();
deploymentIds.add(deployment.getId());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册