Skip to content

Commit

Permalink
issue #1161: moves api to tabService
Browse files Browse the repository at this point in the history
  • Loading branch information
mrk-vi committed Jan 14, 2025
1 parent 3b70990 commit 0db51d4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package io.openk9.datasource.graphql;

import java.util.List;
import java.util.Set;

import jakarta.enterprise.context.ApplicationScoped;
Expand All @@ -28,7 +27,6 @@
import io.openk9.common.util.SortBy;
import io.openk9.datasource.graphql.dto.TokenTabWithDocTypeFieldDTO;
import io.openk9.datasource.model.DocTypeField;
import io.openk9.datasource.model.Tab;
import io.openk9.datasource.model.TokenTab;
import io.openk9.datasource.model.dto.TokenTabDTO;
import io.openk9.datasource.service.TokenTabService;
Expand Down Expand Up @@ -118,11 +116,6 @@ public Uni<Connection<TokenTab>> getTotalTokenTabs(
after, before, first, last, searchText, sortByList);
}

@Query
public Uni<List<Tab>> getUnboundTabByTokenTab(long tokenTabId) {
return tokenTabService.getUnboundTabByTokenTab(tokenTabId);
}

@Mutation
public Uni<TokenTab> removeExtraParam(@Id int id, String key) {
return tokenTabService.removeExtraParam(id, key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@

package io.openk9.datasource.service;

import java.util.List;
import java.util.Set;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Join;
import jakarta.persistence.criteria.JoinType;
import jakarta.persistence.criteria.Root;
import jakarta.persistence.criteria.Subquery;

import io.openk9.common.graphql.util.relay.Connection;
import io.openk9.common.util.SortBy;
import io.openk9.datasource.graphql.dto.TabWithTokenTabsDTO;
Expand All @@ -25,6 +37,7 @@
import io.openk9.datasource.model.Tab;
import io.openk9.datasource.model.Tab_;
import io.openk9.datasource.model.TokenTab;
import io.openk9.datasource.model.TokenTab_;
import io.openk9.datasource.model.dto.TabDTO;
import io.openk9.datasource.model.dto.TranslationDTO;
import io.openk9.datasource.model.dto.TranslationKeyDTO;
Expand All @@ -33,19 +46,12 @@
import io.openk9.datasource.resource.util.Pageable;
import io.openk9.datasource.service.util.BaseK9EntityService;
import io.openk9.datasource.service.util.Tuple2;

import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.groups.UniJoin;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import org.hibernate.FlushMode;
import org.hibernate.reactive.mutiny.Mutiny;

import java.util.List;
import java.util.Set;

@ApplicationScoped
public class TabService extends BaseK9EntityService<Tab, TabDTO> {
TabService(TabMapper mapper) {
Expand Down Expand Up @@ -162,16 +168,31 @@ public Uni<Tab> update(long tabId, TabDTO tabDTO) {
}

public Uni<List<Tab>> findUnboundTabsByTokenTab(long tokenTabId) {

return sessionFactory.withTransaction(s -> {
String queryString = "SELECT tab.* FROM tab " +
"WHERE tab.id not in (" +
"SELECT tab_token_tab.tab_id FROM tab_token_tab " +
"WHERE tab_token_tab.token_tab_id = (:tokenTabId))";
CriteriaBuilder cb = sessionFactory.getCriteriaBuilder();

return s.createNativeQuery(queryString, Tab.class)
.setParameter("tokenTabId", tokenTabId)
.getResultList();
CriteriaQuery<Tab> criteriaQuery = cb.createQuery(Tab.class);
Root<Tab> rootTab = criteriaQuery.from(Tab.class);

criteriaQuery.select(rootTab);

Subquery<Long> idsToExcludeQuery = criteriaQuery.subquery(Long.class);
Root<Tab> rootTabToExclude = idsToExcludeQuery.from(Tab.class);

Join<Tab, TokenTab> tokenTabJoinToExclude =
rootTabToExclude.join(Tab_.tokenTabs, JoinType.INNER);

idsToExcludeQuery
.select(rootTabToExclude.get(Tab_.id))
.where(cb.equal(tokenTabJoinToExclude.get(TokenTab_.id), tokenTabId));

criteriaQuery.where(
cb.not(rootTab.get(Tab_.id).in(idsToExcludeQuery)));

return s.createQuery(criteriaQuery).getResultList();
});

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,16 @@

package io.openk9.datasource.service;

import java.util.List;
import java.util.Set;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Join;
import jakarta.persistence.criteria.JoinType;
import jakarta.persistence.criteria.Root;
import jakarta.persistence.criteria.Subquery;

import io.openk9.common.graphql.util.relay.Connection;
import io.openk9.common.util.SortBy;
import io.openk9.datasource.graphql.dto.TokenTabWithDocTypeFieldDTO;
import io.openk9.datasource.mapper.TokenTabMapper;
import io.openk9.datasource.model.DocTypeField;
import io.openk9.datasource.model.Tab;
import io.openk9.datasource.model.Tab_;
import io.openk9.datasource.model.TokenTab;
import io.openk9.datasource.model.TokenTab_;
import io.openk9.datasource.model.dto.TokenTabDTO;
Expand Down Expand Up @@ -154,34 +145,6 @@ public String[] getSearchFields() {
return new String[] {TokenTab_.NAME, TokenTab_.TOKEN_TYPE};
}

public Uni<List<Tab>> getUnboundTabByTokenTab(long tokenTabId) {

return sessionFactory.withTransaction(s -> {
CriteriaBuilder cb = sessionFactory.getCriteriaBuilder();

CriteriaQuery<Tab> criteriaQuery = cb.createQuery(Tab.class);
Root<Tab> rootTab = criteriaQuery.from(Tab.class);

criteriaQuery.select(rootTab);

Subquery<Long> idsToExcludeQuery = criteriaQuery.subquery(Long.class);
Root<Tab> rootTabToExclude = idsToExcludeQuery.from(Tab.class);

Join<Tab, TokenTab> tokenTabJoinToExclude =
rootTabToExclude.join(Tab_.tokenTabs, JoinType.INNER);

idsToExcludeQuery
.select(rootTabToExclude.get(Tab_.id))
.where(cb.equal(tokenTabJoinToExclude.get(TokenTab_.id), tokenTabId));

criteriaQuery.where(
cb.not(rootTab.get(Tab_.id).in(idsToExcludeQuery)));

return s.createQuery(criteriaQuery).getResultList();
});

}

@Override
public Uni<TokenTab> patch(long id, TokenTabDTO dto) {
if (dto instanceof TokenTabWithDocTypeFieldDTO withDocTypeFieldDTO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class UnboundTabTest {
TabService tabService;

@Test
void should_get_unbound_tab_by_tokeTab() {
void should_get_unbound_tab_by_tokenTab() {

var boundTab = tabService.create(TabDTO.builder()
.name("UnboundTabTest_Bound_tab")
Expand All @@ -61,7 +61,7 @@ void should_get_unbound_tab_by_tokeTab() {
tabService.addTokenTabToTab(boundTab.getId(), tokenTab.getId())
.await().indefinitely();

var tabs = tokenTabService.getUnboundTabByTokenTab(tokenTab.getId())
var tabs = tabService.findUnboundTabsByTokenTab(tokenTab.getId())
.await().indefinitely();

Assertions.assertTrue(tabs.contains(unboundTab));
Expand Down

0 comments on commit 0db51d4

Please sign in to comment.