Skip to content

Commit

Permalink
Workflow resources and resources implementation (#23)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucia Mazzocchi <[email protected]>
Co-authored-by: Elis <[email protected]>
Co-authored-by: Escarsel <[email protected]>
Co-authored-by: Cocuzzoli, Andrea <[email protected]>
  • Loading branch information
5 people authored Nov 14, 2023
1 parent 0d694ee commit 79ecbf1
Show file tree
Hide file tree
Showing 36 changed files with 651 additions and 446 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<mockito.core.version>5.4.0</mockito.core.version>
<google.guava.version>32.1.3-jre</google.guava.version>
<easy.random.version>5.0.0</easy.random.version>
<commons-io.version>2.15.0</commons-io.version>
</properties>
<dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -143,6 +144,11 @@
<version>${mapstruct.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>

<!-- TEST -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import jakarta.validation.constraints.Pattern;
import jakarta.ws.rs.DefaultValue;
import jakarta.ws.rs.FormParam;
import java.io.File;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.File;

@Data
@NoArgsConstructor
public class ResourceCreationDto {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package it.gov.pagopa.atmlayer.service.model.dto;

import it.gov.pagopa.atmlayer.service.model.enumeration.NoDeployableResourceType;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.ws.rs.DefaultValue;
import jakarta.ws.rs.FormParam;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.File;

@Data
@NoArgsConstructor
public class ResourceUpdateDto {

@FormParam("file")
@NotNull(message = "resource file is required")
private File file;
@FormParam("filename")
@NotNull(message = "filename is required")
@Pattern(regexp = "^[a-zA-Z0-9_-]+\\.[a-zA-Z]+$", message = "it must be of form ${regexp}")
private String filename;
@FormParam("resourceType")
@NotNull(message = "resource type is required")
private NoDeployableResourceType resourceType;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

import io.quarkus.hibernate.reactive.panache.PanacheEntityBase;
import it.gov.pagopa.atmlayer.service.model.enumeration.NoDeployableResourceType;
import it.gov.pagopa.atmlayer.service.model.enumeration.S3ResourceTypeEnum;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.PrePersist;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand All @@ -28,7 +29,6 @@
@Table(name = "resource_entity")
public class ResourceEntity extends PanacheEntityBase implements Serializable {
@Column(name = "resource_id", nullable = false, updatable = false)
@GeneratedValue(strategy = GenerationType.UUID)
@Id
private UUID resourceId;
@Column(name = "sha256", unique = true)
Expand All @@ -48,4 +48,14 @@ public class ResourceEntity extends PanacheEntityBase implements Serializable {
private String createdBy;
@Column(name = "last_updated_by")
private String lastUpdatedBy;
@Transient
String storageKey;

@PrePersist
public void generateUUID() {
if (getResourceId() == null) {
setResourceId(UUID.randomUUID());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ public enum AppErrorCodeEnum {
WORKFLOW_RESOURCE_CANNOT_BE_UPDATED_FOR_STATUS("ATMLM_4000025", "The referenced Workflow Resource file can not be updated in the actual state", NOT_UPDATABLE),
WORKFLOW_RESOURCE_FILE_CANNOT_BE_DEPLOYED("ATMLM_4000026", "The referenced Workflow Resource file can not be deployed", NOT_DEPLOYABLE_STATUS),
RESOURCE_WITH_SAME_NAME_AND_PATH_ALREADY_SAVED("ATMLM_4000027", "A resource with same file name and path already exists", NOT_UPLOADABLE),
RESOURCE_FILE_DOES_NOT_EXIST("ATMLM_4000028", "The referenced Resource file does not exist", NOT_EXISTING_REFERENCED_ENTITY);


INEXISTENT_RESOURCE_CANNOT_BE_UPDATED("ATMLM_4000028","The referenced resource does not exist: cannot be updated",NOT_UPDATABLE),
RESOURCE_WITH_DIFFERENT_STORAGE_KEY_CANNOT_BE_UPDATED("ATMLM_4000029","The referenced resource has a different storage key: cannot be updated",NOT_UPDATABLE),
RESOURCE_DOES_NOT_EXIST("ATMLM_4000030", "The referenced Resource does not exist", NOT_EXISTING_REFERENCED_ENTITY),
WORKFLOW_RESOURCE_CANNOT_BE_UPDATED("ATMLM_4000031", "The referenced Workflow Resource file can not be updated", NOT_UPDATABLE),
RESOURCE_FILE_DOES_NOT_EXIST("ATMLM_4000032", "The referenced Resource file does not exist", NOT_EXISTING_REFERENCED_ENTITY);

private final String errorCode;
private final String errorMessage;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import it.gov.pagopa.atmlayer.service.model.enumeration.StatusEnum;
import it.gov.pagopa.atmlayer.service.model.model.BpmnDTO;
import it.gov.pagopa.atmlayer.service.model.utils.BpmnUtils;
import it.gov.pagopa.atmlayer.service.model.utils.FileUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

Expand All @@ -26,7 +27,7 @@ public BpmnVersion toEntityCreation(BpmnCreationDto bpmnCreationDto) throws NoSu
BpmnVersion bpmnVersion = new BpmnVersion();
bpmnVersion.setFunctionType(bpmnCreationDto.getFunctionType());
bpmnVersion.setStatus(StatusEnum.CREATED);
bpmnVersion.setSha256(BpmnUtils.calculateSha256(bpmnCreationDto.getFile()));
bpmnVersion.setSha256(FileUtils.calculateSha256(bpmnCreationDto.getFile()));
bpmnVersion.setDeployedFileName(bpmnCreationDto.getFilename().concat(".").concat(S3ResourceTypeEnum.BPMN.getExtension()));
bpmnVersion.setEnabled(true);
return bpmnVersion;
Expand All @@ -46,7 +47,7 @@ public BpmnVersion toEntityUpgrade(BpmnUpgradeDto bpmnUpgradeDto, Long version,
BpmnVersion bpmnVersion = new BpmnVersion();
bpmnVersion.setFunctionType(bpmnUpgradeDto.getFunctionType());
bpmnVersion.setStatus(StatusEnum.CREATED);
bpmnVersion.setSha256(BpmnUtils.calculateSha256(bpmnUpgradeDto.getFile()));
bpmnVersion.setSha256(FileUtils.calculateSha256(bpmnUpgradeDto.getFile()));
bpmnVersion.setDeployedFileName(bpmnUpgradeDto.getFilename().concat(".").concat(S3ResourceTypeEnum.BPMN.getExtension()));
bpmnVersion.setEnabled(true);
bpmnVersion.setModelVersion(version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,36 @@
import it.gov.pagopa.atmlayer.service.model.dto.ResourceCreationDto;
import it.gov.pagopa.atmlayer.service.model.entity.ResourceEntity;
import it.gov.pagopa.atmlayer.service.model.model.ResourceDTO;
import it.gov.pagopa.atmlayer.service.model.utils.BpmnUtils;
import it.gov.pagopa.atmlayer.service.model.service.ResourceEntityStorageService;
import it.gov.pagopa.atmlayer.service.model.utils.FileUtils;
import jakarta.inject.Inject;
import org.mapstruct.Mapper;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.stream.Collectors;
import org.mapstruct.Mapper;

@Mapper(componentModel = "cdi")
public abstract class ResourceEntityMapper {

public ResourceEntity toEntityCreation(ResourceCreationDto resourceCreationDto)
throws NoSuchAlgorithmException, IOException {
ResourceEntity resourceEntity = new ResourceEntity();
resourceEntity.setSha256(BpmnUtils.calculateSha256(resourceCreationDto.getFile()));
resourceEntity.setNoDeployableResourceType(resourceCreationDto.getResourceType());
resourceEntity.setFileName(resourceCreationDto.getFilename());
return resourceEntity;
}
@Inject
ResourceEntityStorageService resourceEntityStorageService;

public ResourceEntity toEntityCreation(ResourceCreationDto resourceCreationDto) throws NoSuchAlgorithmException, IOException {
ResourceEntity resourceEntity = new ResourceEntity();
resourceEntity.setSha256(FileUtils.calculateSha256(resourceCreationDto.getFile()));
resourceEntity.setNoDeployableResourceType(resourceCreationDto.getResourceType());
resourceEntity.setFileName(resourceCreationDto.getFilename());
resourceEntity.setStorageKey(resourceEntityStorageService.calculateStorageKey(
resourceCreationDto.getResourceType(),resourceCreationDto.getPath(),resourceCreationDto.getFilename()
));
return resourceEntity;
}

public abstract ResourceDTO toDTO(ResourceEntity resourceEntity);
public abstract ResourceDTO toDTO(ResourceEntity resourceEntity);

public List<ResourceDTO> toDTOList(List<ResourceEntity> list) {
return list.stream().map(this::toDTO).collect(Collectors.toList());
}
public List<ResourceDTO> toDTOList(List<ResourceEntity> list) {
return list.stream().map(this::toDTO).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import it.gov.pagopa.atmlayer.service.model.entity.WorkflowResource;
import it.gov.pagopa.atmlayer.service.model.enumeration.StatusEnum;
import it.gov.pagopa.atmlayer.service.model.model.WorkflowResourceDTO;
import it.gov.pagopa.atmlayer.service.model.utils.BpmnUtils;
import it.gov.pagopa.atmlayer.service.model.utils.FileUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

Expand All @@ -23,7 +23,7 @@ public abstract class WorkflowResourceMapper {
public WorkflowResource toEntityCreation(WorkflowResourceCreationDto workflowCreationDto) throws NoSuchAlgorithmException, IOException {
WorkflowResource workflowResource = new WorkflowResource();
workflowResource.setStatus(StatusEnum.CREATED);
workflowResource.setSha256(BpmnUtils.calculateSha256(workflowCreationDto.getFile()));
workflowResource.setSha256(FileUtils.calculateSha256(workflowCreationDto.getFile()));
workflowResource.setDeployedFileName(workflowCreationDto.getFilename().concat(".").concat(workflowCreationDto.getResourceType().toString()));
workflowResource.setResourceType(workflowCreationDto.getResourceType());
return workflowResource;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package it.gov.pagopa.atmlayer.service.model.model;

import it.gov.pagopa.atmlayer.service.model.enumeration.NoDeployableResourceType;
import java.sql.Timestamp;
import java.util.UUID;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.sql.Timestamp;
import java.util.UUID;

@Data
@NoArgsConstructor
public class ResourceDTO {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ public Uni<ResourceFile> findByStorageKey(String key){
"select resource from ResourceFile resource where resource.storageKey = :key",
Parameters.with("key", key)).firstResult();
}

public Uni<ResourceFile> findByResourceId(UUID resourceId){
return find(
"select resource from ResourceFile resource where resource.resourceEntity.resourceId = :id",
Parameters.with("id", resourceId)).firstResult();
}
}
Original file line number Diff line number Diff line change
@@ -1,46 +1,76 @@
package it.gov.pagopa.atmlayer.service.model.resource;

import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.RESOURCE_FILE_DOES_NOT_EXIST;

import io.smallrye.common.annotation.NonBlocking;
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.unchecked.Unchecked;
import it.gov.pagopa.atmlayer.service.model.dto.ResourceCreationDto;
import it.gov.pagopa.atmlayer.service.model.entity.ResourceEntity;
import it.gov.pagopa.atmlayer.service.model.exception.AtmLayerException;
import it.gov.pagopa.atmlayer.service.model.mapper.ResourceEntityMapper;
import it.gov.pagopa.atmlayer.service.model.mapper.ResourceFileMapper;
import it.gov.pagopa.atmlayer.service.model.model.ResourceDTO;
import it.gov.pagopa.atmlayer.service.model.model.ResourceFileDTO;
import it.gov.pagopa.atmlayer.service.model.service.ResourceEntityService;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.validation.Valid;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.FormParam;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;

import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;

import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.RESOURCE_FILE_DOES_NOT_EXIST;
@ApplicationScoped
@Path("/resources")
@Tag(name = "RESOURCES", description = "RESOURCES operations")
@Slf4j
public class ResourceEntityResource {
@Inject
ResourceEntityMapper resourceEntityMapper;
@Inject
ResourceFileMapper resourceFileMapper;
@Inject
ResourceEntityService resourceEntityService;

@Inject
ResourceEntityMapper resourceEntityMapper;
@Inject
ResourceEntityService resourceEntityService;
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@NonBlocking
public Uni<ResourceDTO> createResource(
@RequestBody(required = true) @Valid ResourceCreationDto resourceCreationDto)
throws NoSuchAlgorithmException, IOException {
ResourceEntity resourceEntity = resourceEntityMapper.toEntityCreation(resourceCreationDto);
return resourceEntityService.createResource(resourceEntity, resourceCreationDto.getFile(),
resourceCreationDto.getFilename(), resourceCreationDto.getPath())
.onItem()
.transformToUni(resource -> Uni.createFrom().item(resourceEntityMapper.toDTO(resource)));
}

@PUT
@Path("/{uuid}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Uni<ResourceFileDTO> updateResource(@RequestBody(required = true) @FormParam("file") File file,
@PathParam("uuid") UUID uuid) {
return resourceEntityService.updateResource(uuid, file)
.onItem()
.transformToUni(updateResourceFile -> Uni.createFrom().item(resourceFileMapper.toDTO(updateResourceFile)));
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public Uni<List<ResourceDTO>> getAll() {
Expand All @@ -67,19 +97,4 @@ public Uni<ResourceDTO> getById(@PathParam("uuid") UUID uuid){
return resourceEntityMapper.toDTO(x.get());
}));
}


@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@NonBlocking
public Uni<ResourceDTO> createResource(
@RequestBody(required = true) @Valid ResourceCreationDto resourceCreationDto)
throws NoSuchAlgorithmException, IOException {
ResourceEntity resourceEntity = resourceEntityMapper.toEntityCreation(resourceCreationDto);
return resourceEntityService.createResource(resourceEntity, resourceCreationDto.getFile(),
resourceCreationDto.getFilename(), resourceCreationDto.getPath())
.onItem()
.transformToUni(resource -> Uni.createFrom().item(resourceEntityMapper.toDTO(resource)));
}
}
Loading

0 comments on commit 79ecbf1

Please sign in to comment.