Skip to content

Commit

Permalink
feat: add operation update for data import (#29)
Browse files Browse the repository at this point in the history
* feat: update data import

* feat: create data import update request

* feat: fix sonar issues
  • Loading branch information
andrejpetras authored Mar 6, 2024
1 parent 80dbcf0 commit f6ca445
Show file tree
Hide file tree
Showing 29 changed files with 884 additions and 247 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class ApplicationSearchCriteria {
private String appId;

private String name;

private Integer pageNumber;
private Integer pageSize;
private Integer pageNumber = 0;
private Integer pageSize = 100;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
public class AssignmentSearchCriteria {

private String[] appId;
private Integer pageNumber;
private Integer pageSize;
private Integer pageNumber = 0;
private Integer pageSize = 100;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.tkit.onecx.permission.domain.criteria;

import java.util.List;

import lombok.Getter;
import lombok.Setter;

Expand All @@ -10,7 +8,6 @@
public class PermissionSearchCriteria {

private String appId;
private List<String> productNames;
private Integer pageNumber;
private Integer pageSize;
private Integer pageNumber = 0;
private Integer pageSize = 100;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public class RoleSearchCriteria {

private String name;
private String description;
private Integer pageNumber;
private Integer pageSize;
private Integer pageNumber = 0;
private Integer pageSize = 100;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.persistence.NoResultException;
Expand All @@ -30,7 +31,6 @@ public PageResult<Application> findByCriteria(ApplicationSearchCriteria criteria
List<Predicate> predicates = new ArrayList<>();
addSearchStringPredicate(predicates, cb, root.get(Application_.appId), criteria.getAppId());
addSearchStringPredicate(predicates, cb, root.get(Application_.name), criteria.getName());

if (!predicates.isEmpty()) {
cq.where(predicates.toArray(new Predicate[] {}));
}
Expand All @@ -57,8 +57,22 @@ public Application loadByAppId(String productName, String appId) {
}
}

public List<Application> findByProductNames(Set<String> productNames) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(Application.class);
var root = cq.from(Application.class);
cq.where(root.get(Permission_.PRODUCT_NAME).in(productNames));
return this.getEntityManager().createQuery(cq).getResultList();
} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_FIND_APPLICATIONS_BY_PRODUCT_NAMES, ex);
}
}

public enum ErrorKeys {

ERROR_FIND_APPLICATIONS_BY_PRODUCT_NAMES,

ERROR_FIND_APPLICATIONS_BY_CRITERIA,
ERROR_LOAD_BY_APP_ID;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.persistence.NoResultException;
import jakarta.persistence.criteria.Predicate;
import jakarta.transaction.Transactional;

import org.tkit.onecx.permission.domain.criteria.AssignmentSearchCriteria;
import org.tkit.onecx.permission.domain.models.Assignment;
import org.tkit.onecx.permission.domain.models.Assignment_;
import org.tkit.onecx.permission.domain.models.Permission_;
import org.tkit.onecx.permission.domain.models.*;
import org.tkit.quarkus.jpa.daos.AbstractDAO;
import org.tkit.quarkus.jpa.daos.Page;
import org.tkit.quarkus.jpa.daos.PageResult;
Expand Down Expand Up @@ -46,13 +45,16 @@ public PageResult<Assignment> findByCriteria(AssignmentSearchCriteria criteria)
var cq = cb.createQuery(Assignment.class);
var root = cq.from(Assignment.class);

List<Predicate> predicates = new ArrayList<>();
if (criteria.getAppId() != null) {
List<String> filteredAppIds = Arrays.stream(criteria.getAppId()).filter(s -> !s.isBlank()).toList();
if (!filteredAppIds.isEmpty()) {
cq.where(root.get(Assignment_.permission).get(Permission_.APP_ID).in(filteredAppIds));
predicates.add(root.get(Assignment_.permission).get(Permission_.APP_ID).in(filteredAppIds));
}
}

if (!predicates.isEmpty()) {
cq.where(predicates.toArray(new Predicate[] {}));
}
cq.orderBy(cb.asc(root.get(AbstractTraceableEntity_.creationDate)));

return createPageQuery(cq, Page.of(criteria.getPageNumber(), criteria.getPageSize())).getPageResult();
Expand Down Expand Up @@ -84,8 +86,30 @@ public void deleteByCriteria(String roleId, List<String> productNames, String pe
this.getEntityManager().createQuery(dq).executeUpdate();
}

public List<PermissionAction> findPermissionActionForProducts(Set<String> productNames) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(PermissionAction.class);
var root = cq.from(Assignment.class);

cq.select(cb.construct(PermissionAction.class,
root.get(Assignment_.ROLE).get(Role_.NAME),
root.get(Assignment_.permission).get(Permission_.PRODUCT_NAME),
root.get(Assignment_.permission).get(Permission_.APP_ID),
root.get(Assignment_.permission).get(Permission_.RESOURCE),
root.get(Assignment_.permission).get(Permission_.ACTION)));
cq.where(root.get(Assignment_.permission).get(Permission_.PRODUCT_NAME).in(productNames));

return this.getEntityManager().createQuery(cq).getResultList();
} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_FIND_PERMISSION_ACTION_FOR_PRODUCTS, ex);
}
}

public enum ErrorKeys {

ERROR_FIND_PERMISSION_ACTION_FOR_PRODUCTS,

FIND_ENTITY_BY_ID_FAILED,
ERROR_FIND_ASSIGNMENT_BY_CRITERIA;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.tkit.quarkus.jpa.utils.QueryCriteriaUtil.addSearchStringPredicate;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import jakarta.enterprise.context.ApplicationScoped;
Expand Down Expand Up @@ -30,9 +31,6 @@ public PageResult<Permission> findByCriteria(PermissionSearchCriteria criteria)

List<Predicate> predicates = new ArrayList<>();
addSearchStringPredicate(predicates, cb, root.get(Permission_.appId), criteria.getAppId());
if (criteria.getProductNames() != null) {
predicates.add(root.get(Permission_.PRODUCT_NAME).in(criteria.getProductNames()));
}
if (!predicates.isEmpty()) {
cq.where(predicates.toArray(new Predicate[] {}));
}
Expand All @@ -43,27 +41,29 @@ public PageResult<Permission> findByCriteria(PermissionSearchCriteria criteria)
}
}

public List<Permission> loadByAppId(String appId) {
public List<Permission> findByProductAndAppId(String productName, String appId) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(Permission.class);
var root = cq.from(Permission.class);
cq.where(cb.equal(root.get(Permission_.APP_ID), appId));
cq.where(cb.and(
cb.equal(root.get(Permission_.PRODUCT_NAME), productName),
cb.equal(root.get(Permission_.APP_ID), appId)));
return this.getEntityManager().createQuery(cq).getResultList();
} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_LOAD_BY_APP_ID, ex);
throw new DAOException(ErrorKeys.ERROR_FIND_BY_PRODUCT_AND_APP_ID, ex);
}
}

public List<Permission> loadByProductNames(List<String> productNames) {
public List<Permission> findByProductNames(Collection<String> productNames) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(Permission.class);
var root = cq.from(Permission.class);
cq.where(root.get(Permission_.PRODUCT_NAME).in(productNames));
return this.getEntityManager().createQuery(cq).getResultList();
} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_LOAD_BY_PRODUCT_NAMES, ex);
throw new DAOException(ErrorKeys.ERROR_FIND_BY_PRODUCT_NAMES, ex);
}
}

Expand Down Expand Up @@ -91,8 +91,8 @@ public List<Permission> findPermissionForUser(String appId, List<String> roles)
public enum ErrorKeys {

ERROR_FIND_PERMISSION_FOR_USER,
ERROR_LOAD_BY_APP_ID,
ERROR_FIND_BY_PRODUCT_AND_APP_ID,
ERROR_FIND_PERMISSION_BY_CRITERIA,
ERROR_LOAD_BY_PRODUCT_NAMES;
ERROR_FIND_BY_PRODUCT_NAMES;
}
}
13 changes: 13 additions & 0 deletions src/main/java/org/tkit/onecx/permission/domain/daos/RoleDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.persistence.NoResultException;
Expand Down Expand Up @@ -59,6 +60,18 @@ public PageResult<Role> findByCriteria(RoleSearchCriteria criteria) {
}
}

public List<Role> findByNames(Set<String> names) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(Role.class);
var root = cq.from(Role.class);
cq.where(root.get(Role_.name).in(names));
return this.getEntityManager().createQuery(cq).getResultList();
} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_FIND_ROLE_BY_CRITERIA, ex);
}
}

public enum ErrorKeys {

FIND_ENTITY_BY_ID_FAILED,
Expand Down
Loading

0 comments on commit f6ca445

Please sign in to comment.