Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add workspace operator import #79

Merged
merged 5 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/docker/Dockerfile.jvm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ghcr.io/onecx/docker-quarkus-jvm:0.5.0
FROM ghcr.io/onecx/docker-quarkus-jvm:0.6.0

COPY --chown=185 target/quarkus-app/lib/ /deployments/lib/
COPY --chown=185 target/quarkus-app/*.jar /deployments/
Expand Down
2 changes: 1 addition & 1 deletion src/main/docker/Dockerfile.native
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
FROM ghcr.io/onecx/docker-quarkus-native:0.3.0
FROM ghcr.io/onecx/docker-quarkus-native:0.4.0

COPY --chown=1001:root target/*-runner /work/application
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.tkit.onecx.workspace.domain.daos;

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

import jakarta.enterprise.context.ApplicationScoped;
Expand Down Expand Up @@ -92,6 +93,18 @@ public void deleteAllByWorkspaceId(String id) {
}
}

@Transactional
public void deleteAllByWorkspaceIds(Collection<String> ids) {
try {
var cq = this.deleteQuery();
var root = cq.from(Assignment.class);
cq.where(root.get(Assignment_.MENU_ITEM).get(MenuItem_.WORKSPACE_ID).in(ids));
getEntityManager().createQuery(cq).executeUpdate();
} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_DELETE_ITEMS_BY_WORKSPACE_ID, ex);
}
}

@Transactional
public void deleteAllByMenuId(List<Object> ids) {
try {
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/org/tkit/onecx/workspace/domain/daos/ImageDAO.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.tkit.onecx.workspace.domain.daos;

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

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.persistence.NoResultException;
import jakarta.transaction.Transactional;
Expand Down Expand Up @@ -63,8 +66,40 @@ public void deleteQueryByRefIdAndRefType(String refId, RefTypeDTO refType) throw
}
}

public List<Image> findByRefIds(Collection<String> refIds) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(Image.class);
var root = cq.from(Image.class);
if (refIds != null && !refIds.isEmpty()) {
cq.where(root.get(Image_.REF_ID).in(refIds));
}
return this.getEntityManager().createQuery(cq).getResultList();

} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_FIND_REF_IDS, ex);
}
}

@Transactional(value = Transactional.TxType.REQUIRED, rollbackOn = DAOException.class)
public void deleteQueryByRefIds(Collection<String> refIds) throws DAOException {
try {
var cq = deleteQuery();
var root = cq.from(Image.class);
cq.where(root.get(Image_.REF_ID).in(refIds));
getEntityManager().createQuery(cq).executeUpdate();
getEntityManager().flush();
} catch (Exception e) {
throw handleConstraint(e, ErrorKeys.FAILED_TO_DELETE_BY_REF_IDS_QUERY);
}
}

public enum ErrorKeys {

FAILED_TO_DELETE_BY_REF_IDS_QUERY,

ERROR_FIND_REF_IDS,

FAILED_TO_DELETE_BY_REF_ID_QUERY,

FIND_ENTITY_BY_REF_ID_REF_TYPE_FAILED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.tkit.onecx.workspace.domain.models.MenuItem.*;

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

import jakarta.enterprise.context.ApplicationScoped;
Expand Down Expand Up @@ -62,6 +63,23 @@ public int deleteAllMenuItemsByWorkspaceId(String id) {
}
}

/**
* This method delete all menu items by workspace id.
*
* @param ids - workspace ids
*/
@Transactional
public void deleteAllMenuItemsByWorkspaceIds(Collection<String> ids) {
try {
var cq = this.deleteQuery();
var root = cq.from(MenuItem.class);
cq.where(root.get(MenuItem_.WORKSPACE_ID).in(ids));
getEntityManager().createQuery(cq).executeUpdate();
} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_DELETE_ALL_MENU_ITEMS_BY_WORKSPACE_IDS, ex);
}
}

public MenuItem loadMenuItemByWorkspaceAndKey(String workspaceName, String itemKey) {

try {
Expand Down Expand Up @@ -231,7 +249,7 @@ public enum ErrorKeys {
ERROR_LOAD_MENU_BY_ID_AND_KEY,
FIND_ENTITY_BY_ID_FAILED,
ERROR_DELETE_ALL_MENU_ITEMS_BY_WORKSPACE_ID,

ERROR_DELETE_ALL_MENU_ITEMS_BY_WORKSPACE_IDS,
ERROR_LOAD_ALL_MENU_ITEMS_BY_CRITERIA,

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

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

Expand Down Expand Up @@ -173,8 +174,21 @@ public List<String> filterWorkspaceNames(Set<String> workspaceNames) {
}
}

public List<Workspace> findByNames(Collection<String> workspaceNames) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(Workspace.class);
var root = cq.from(Workspace.class);
cq.where(root.get(NAME).in(workspaceNames));
return this.getEntityManager().createQuery(cq).getResultList();
} catch (Exception ex) {
throw handleConstraint(ex, ErrorKeys.ERROR_FIND_WORKSPACE_BY_NAMES);
}
}

public enum ErrorKeys {

ERROR_FIND_WORKSPACE_BY_NAMES,
ERROR_FILTER_WORKSPACE_NAMES,
ERROR_LOAD_WORKSPACE_PRODUCTS,
ERROR_LOAD_WORKSPACE_PRODUCTS_SLOTS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ default List<Assignment> createAssignments(List<Role> roles, List<MenuItem> menu
@Mapping(target = "modificationCount", ignore = true)
@Mapping(target = "persisted", ignore = true)
@Mapping(target = "tenantId", ignore = true)
@Mapping(target = "operator", ignore = true)
Workspace createWorkspace(TemplateWorkspaceDI dto);

@AfterMapping
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/tkit/onecx/workspace/domain/models/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.persistence.*;

import org.hibernate.annotations.TenantId;
import org.tkit.quarkus.jpa.models.TraceableEntity;

import lombok.Getter;
Expand Down Expand Up @@ -31,4 +32,13 @@ public class Image extends TraceableEntity {
@Column(name = "DATA")
private byte[] imageData;

/**
* Flag to identify created by an operator
*/
@Column(name = "OPERATOR")
private Boolean operator;

@TenantId
@Column(name = "TENANT_ID")
private String tenantId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,9 @@ public class Workspace extends TraceableEntity {
@OneToMany(mappedBy = "workspace", fetch = LAZY, cascade = { CascadeType.REMOVE })
private List<Slot> slots;

/**
* Flag to identify created by an operator
*/
@Column(name = "OPERATOR")
private Boolean operator;
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package org.tkit.onecx.workspace.domain.services;

import java.util.List;
import java.util.stream.Collectors;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;

import org.tkit.onecx.workspace.domain.daos.*;
import org.tkit.onecx.workspace.domain.models.Image;
import org.tkit.onecx.workspace.domain.models.Product;
import org.tkit.onecx.workspace.domain.models.Slot;
import org.tkit.onecx.workspace.domain.models.Workspace;
import org.tkit.onecx.workspace.domain.template.models.WorkspaceCreateTemplate;

import com.fasterxml.jackson.databind.ObjectMapper;

@ApplicationScoped
public class WorkspaceService {

Expand Down Expand Up @@ -37,9 +41,6 @@ public class WorkspaceService {
@Inject
AssignmentDAO assignmentDAO;

@Inject
ObjectMapper mapper;

@Transactional
public void deleteWorkspace(String id) {
var workspace = workspaceDAO.findById(id);
Expand Down Expand Up @@ -71,4 +72,39 @@ public Workspace createWorkspace(Workspace workspace, WorkspaceCreateTemplate te

return workspace;
}

@Transactional
public void importWorkspace(List<Workspace> createWorkspaces, List<Image> createImages, List<Slot> createSlots,
List<Product> createProducts) {
imageDAO.create(createImages);
workspaceDAO.create(createWorkspaces);
productDAO.create(createProducts);
slotDAO.create(createSlots);
}

@Transactional
public void importOperator(List<Workspace> workspaces, List<Image> images, List<Slot> slots,
List<Product> products) {
if (workspaces.isEmpty()) {
return;
}
var names = workspaces.stream().map(Workspace::getName).collect(Collectors.toSet());

// delete existing data
var tmp = workspaceDAO.findByNames(names);
if (!tmp.isEmpty()) {
var workspaceIds = tmp.stream().map(Workspace::getId).collect(Collectors.toSet());
assignmentDAO.deleteAllByWorkspaceIds(workspaceIds);
menuItemDAO.deleteAllMenuItemsByWorkspaceIds(workspaceIds);
imageDAO.deleteQueryByRefIds(names);
workspaceDAO.delete(tmp);
}

// create new data
workspaceDAO.create(workspaces);
imageDAO.create(images);
productDAO.create(products);
slotDAO.create(slots);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.tkit.onecx.workspace.domain.daos.*;
import org.tkit.onecx.workspace.domain.models.*;
import org.tkit.onecx.workspace.domain.services.MenuService;
import org.tkit.onecx.workspace.domain.services.WorkspaceService;
import org.tkit.onecx.workspace.rs.exim.v1.mappers.ExportImportExceptionMapperV1;
import org.tkit.onecx.workspace.rs.exim.v1.mappers.ExportImportMapperV1;
import org.tkit.quarkus.jpa.exceptions.ConstraintException;
Expand Down Expand Up @@ -50,13 +51,10 @@ class ExportImportRestControllerV1 implements WorkspaceExportImportApi {
AssignmentDAO assignmentDAO;

@Inject
RoleDAO roleDAO;
ImageDAO imageDAO;

@Inject
ProductDAO productDAO;

@Inject
SlotDAO slotDAO;
WorkspaceService service;

@Override
public Response exportMenuByWorkspaceName(String name) {
Expand Down Expand Up @@ -88,7 +86,9 @@ public Response exportWorkspacesByNames(ExportWorkspacesRequestDTOV1 request) {
if (data.isEmpty()) {
return Response.status(Response.Status.NOT_FOUND).build();
}
return Response.ok(mapper.create(data)).build();

var images = imageDAO.findByRefIds(request.getNames());
return Response.ok(mapper.create(data, images)).build();
}

@Override
Expand Down Expand Up @@ -122,17 +122,53 @@ public Response importMenu(String name, MenuSnapshotDTOV1 menuSnapshotDTOV1) {
}

@Override
@Transactional
public Response importOperatorWorkspaces(WorkspaceSnapshotDTOV1 workspaceSnapshotDTOV1) {

if (workspaceSnapshotDTOV1.getWorkspaces() == null) {
return Response.ok().build();
}

List<Workspace> workspaces = new ArrayList<>();
List<Image> images = new ArrayList<>();
List<Slot> slots = new ArrayList<>();
List<Product> products = new ArrayList<>();
workspaceSnapshotDTOV1.getWorkspaces().forEach((name, dto) -> {
var workspace = mapper.create(dto);
workspace.setName(name);
workspace.setOperator(true);
workspaces.add(workspace);

var items = mapper.createImages(name, dto.getImages());
items.forEach(x -> x.setOperator(true));
images.addAll(items);
if (!dto.getProducts().isEmpty()) {
products.addAll(mapper.create(dto.getProducts(), workspace));
}
if (!dto.getSlots().isEmpty()) {
slots.addAll(mapper.createSlots(dto.getSlots(), workspace));
}
});

// delete and create data
service.importOperator(workspaces, images, slots, products);

return Response.ok().build();
}

@Override
public Response importWorkspaces(WorkspaceSnapshotDTOV1 request) {
var workspaceNames = request.getWorkspaces().keySet();

var criteria = new WorkspaceSearchCriteria();
criteria.setNames(workspaceNames);
var workspaces = dao.findBySearchCriteria(criteria);

var map = workspaces.getStream().collect(Collectors.toMap(Workspace::getName, workspace -> workspace));

Map<String, ImportResponseStatusDTOV1> items = new HashMap<>();
List<Workspace> createWorkspaces = new ArrayList<>();
List<Slot> createSlots = new ArrayList<>();
List<Product> createProducts = new ArrayList<>();
List<Image> createImages = new ArrayList<>();

request.getWorkspaces().forEach((name, dto) -> {
try {
Expand All @@ -141,14 +177,15 @@ public Response importWorkspaces(WorkspaceSnapshotDTOV1 request) {
if (workspace == null) {
workspace = mapper.create(dto);
workspace.setName(name);
dao.create(workspace);
createWorkspaces.add(workspace);
createImages.addAll(mapper.createImages(name, dto.getImages()));
if (!dto.getProducts().isEmpty()) {
var products = mapper.create(dto.getProducts(), workspace);
productDAO.create(products);
createProducts.addAll(products);
}
if (!dto.getSlots().isEmpty()) {
var slots = mapper.createSlots(dto.getSlots(), workspace);
slotDAO.create(slots);
createSlots.addAll(slots);
}
items.put(name, ImportResponseStatusDTOV1.CREATED);
} else {
Expand All @@ -159,6 +196,8 @@ public Response importWorkspaces(WorkspaceSnapshotDTOV1 request) {
}
});

service.importWorkspace(createWorkspaces, createImages, createSlots, createProducts);

return Response.ok(mapper.create(request, items)).build();
}

Expand Down
Loading
Loading