Skip to content

Commit b4029d0

Browse files
committed
Split WorkflowRepository to allow a native query to fetch GitDetails.
Update tests and remove code warnings checking for null.
1 parent f843d4d commit b4029d0

File tree

5 files changed

+111
-25
lines changed

5 files changed

+111
-25
lines changed

src/main/java/org/commonwl/view/workflow/QueuedWorkflowRepositoryImpl.java

+23-13
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
33
import io.hypersistence.utils.hibernate.type.json.JsonType;
44
import jakarta.persistence.EntityManager;
55
import jakarta.persistence.PersistenceContext;
6+
import jakarta.persistence.PersistenceContextType;
67
import org.commonwl.view.git.GitDetails;
78
import org.hibernate.query.Query;
9+
import org.springframework.transaction.annotation.Transactional;
810

911
public class QueuedWorkflowRepositoryImpl implements QueuedWorkflowRepositoryCustom {
1012

11-
// Tested this query directly, and it works! Problem is elsewhere!
1213
private static final String QUERY_FIND_BY_RETRIEVED_FROM =
1314
"SELECT q.* FROM queued_workflow q WHERE q.temp_representation -> 'retrievedFrom' = :retrievedFrom";
1415

1516
private static final String QUERY_DELETE_BY_RETRIEVED_FROM =
1617
"DELETE FROM queued_workflow q WHERE q.temp_representation -> 'retrievedFrom' = :retrievedFrom";
1718

18-
@PersistenceContext EntityManager entityManager;
19+
@PersistenceContext(type = PersistenceContextType.EXTENDED)
20+
EntityManager entityManager;
1921

2022
@Override
2123
public QueuedWorkflow findByRetrievedFrom(GitDetails retrievedFrom) {
@@ -27,22 +29,30 @@ public QueuedWorkflow findByRetrievedFrom(GitDetails retrievedFrom) {
2729
// out of ideas, so started testing everything found online):
2830
// Use `new JsonType(GitDetails.class)`. That finally solved it.
2931
// Ref: https://github.com/common-workflow-language/cwlviewer/pull/568
30-
return (QueuedWorkflow)
32+
final Query<?> query =
3133
entityManager
3234
.createNativeQuery(QUERY_FIND_BY_RETRIEVED_FROM, QueuedWorkflow.class)
33-
.unwrap(Query.class)
34-
.setParameter("retrievedFrom", retrievedFrom, new JsonType(GitDetails.class))
35-
.stream()
36-
.findFirst()
37-
.orElse(null);
35+
.unwrap(Query.class);
36+
37+
if (query == null) {
38+
return null;
39+
}
40+
41+
query.setParameter("retrievedFrom", retrievedFrom, new JsonType(GitDetails.class));
42+
return (QueuedWorkflow) query.uniqueResult();
3843
}
3944

45+
@Transactional
4046
@Override
4147
public void deleteByTempRepresentation_RetrievedFrom(GitDetails retrievedFrom) {
42-
entityManager
43-
.createNativeQuery(QUERY_DELETE_BY_RETRIEVED_FROM, QueuedWorkflow.class)
44-
.unwrap(Query.class)
45-
.setParameter("retrievedFrom", retrievedFrom, new JsonType(GitDetails.class))
46-
.executeUpdate();
48+
final Query<?> query =
49+
entityManager
50+
.createNativeQuery(QUERY_DELETE_BY_RETRIEVED_FROM, QueuedWorkflow.class)
51+
.unwrap(Query.class);
52+
53+
if (query != null) {
54+
query.setParameter("retrievedFrom", retrievedFrom, new JsonType(GitDetails.class));
55+
query.executeUpdate();
56+
}
4757
}
4858
}

src/main/java/org/commonwl/view/workflow/WorkflowRepository.java

+3-11
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020
package org.commonwl.view.workflow;
2121

2222
import java.util.List;
23-
import org.commonwl.view.git.GitDetails;
2423
import org.springframework.data.domain.Page;
2524
import org.springframework.data.domain.Pageable;
25+
import org.springframework.data.jpa.repository.JpaRepository;
2626
import org.springframework.data.jpa.repository.Query;
27-
import org.springframework.data.repository.CrudRepository;
2827
import org.springframework.stereotype.Repository;
2928

3029
/**
@@ -34,15 +33,8 @@
3433
* href="https://docs.spring.io/spring-data/jpa/docs/current/reference/html/">...</a>
3534
*/
3635
@Repository
37-
public interface WorkflowRepository extends CrudRepository<Workflow, String> {
38-
39-
/**
40-
* Finds a workflow model in the database based on where it was retrieved from
41-
*
42-
* @param retrievedFrom Details of where the workflow is from
43-
* @return The workflow model
44-
*/
45-
Workflow findByRetrievedFrom(GitDetails retrievedFrom);
36+
public interface WorkflowRepository
37+
extends JpaRepository<Workflow, String>, WorkflowRepositoryCustom {
4638

4739
/**
4840
* Finds a workflow model in the database based on a commit ID and path
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.commonwl.view.workflow;
20+
21+
import org.commonwl.view.git.GitDetails;
22+
23+
public interface WorkflowRepositoryCustom {
24+
25+
/**
26+
* Finds a workflow model in the database based on where it was retrieved from
27+
*
28+
* @param retrievedFrom Details of where the workflow is from
29+
* @return The workflow model
30+
*/
31+
Workflow findByRetrievedFrom(GitDetails retrievedFrom);
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.commonwl.view.workflow;
21+
22+
import io.hypersistence.utils.hibernate.type.json.JsonType;
23+
import jakarta.persistence.EntityManager;
24+
import jakarta.persistence.PersistenceContext;
25+
import jakarta.persistence.PersistenceContextType;
26+
import org.commonwl.view.git.GitDetails;
27+
import org.hibernate.query.Query;
28+
29+
public class WorkflowRepositoryImpl implements WorkflowRepositoryCustom {
30+
31+
private static final String QUERY_FIND_BY_RETRIEVED_FROM =
32+
"SELECT w.* FROM workflow w WHERE w.retrieved_from = :retrievedFrom";
33+
34+
@PersistenceContext(type = PersistenceContextType.EXTENDED)
35+
EntityManager entityManager;
36+
37+
@Override
38+
public Workflow findByRetrievedFrom(GitDetails retrievedFrom) {
39+
final Query<?> query =
40+
entityManager
41+
.createNativeQuery(QUERY_FIND_BY_RETRIEVED_FROM, Workflow.class)
42+
.unwrap(Query.class);
43+
44+
if (query == null) {
45+
return null;
46+
}
47+
48+
query.setParameter("retrievedFrom", retrievedFrom, new JsonType(GitDetails.class));
49+
return (Workflow) query.uniqueResult();
50+
}
51+
}

src/test/java/org/commonwl/view/workflow/QueuedWorkflowRepositoryTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@
1212
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
1313
import org.springframework.test.context.ContextConfiguration;
1414
import org.springframework.test.context.TestPropertySource;
15+
import org.springframework.transaction.annotation.Propagation;
1516
import org.springframework.transaction.annotation.Transactional;
1617

1718
@TestPropertySource(locations = "classpath:it-application.properties")
1819
@DataJpaTest(showSql = true)
20+
@Transactional(propagation = Propagation.NOT_SUPPORTED)
1921
@ContextConfiguration(initializers = PostgreSQLContextInitializer.class)
2022
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
2123
public class QueuedWorkflowRepositoryTest {
2224

2325
@Autowired QueuedWorkflowRepository repository;
2426

25-
@Transactional
2627
@Test
2728
public void deleteQueuedWorkflowByRetrievedFromTest() {
2829

0 commit comments

Comments
 (0)