Skip to content

Commit

Permalink
Fix Enum Resource
Browse files Browse the repository at this point in the history
  • Loading branch information
EmanueleValentini1 committed Nov 10, 2023
1 parent 6dd41ab commit 8e63cc8
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package it.gov.pagopa.atmlayer.service.model.dto;

import it.gov.pagopa.atmlayer.service.model.enumeration.NoDeployableResourceType;
import it.gov.pagopa.atmlayer.service.model.enumeration.S3ResourceTypeEnum;
import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotNull;
Expand All @@ -24,7 +25,7 @@ public class ResourceCreationDto {
private String filename;
@FormParam("resourceType")
@NotNull(message = "resource type is required")
private S3ResourceTypeEnum resourceType;
private NoDeployableResourceType resourceType;
@FormParam("path")
@Nullable
@Pattern(regexp = "^(?!/).*(?<!/)$", message = "String must not start or end with '/'")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.gov.pagopa.atmlayer.service.model.dto;

import it.gov.pagopa.atmlayer.service.model.enumeration.DeployableResourceType;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.ws.rs.FormParam;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package it.gov.pagopa.atmlayer.service.model.entity;

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;
Expand Down Expand Up @@ -31,7 +34,8 @@ public class ResourceEntity extends PanacheEntityBase implements Serializable {
@Column(name = "sha256", unique = true)
private String sha256;
@Column(name="resourceType")
S3ResourceTypeEnum s3ResourceTypeEnum;
@Enumerated(EnumType.STRING)
NoDeployableResourceType noDeployableResourceType;
@Column(name="file_name")
String fileName;
@CreationTimestamp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ public enum AppErrorCodeEnum {
NO_CONFIGURATION_FOR_ACQUIRER("ATMLM_4000017","No configuration found for the provided acquirer Id", ID_NOT_FOUND),
METHOD_NOT_ALLOWED("ATMLM_4000018","Cannot invoke method", INVALID_ARGUMENT),
RESOURCE_WITH_SAME_SHA256_ALREADY_EXISTS("ATMLM_4000019","A resource with the same content already exists", CONSTRAINT_VIOLATION),
FILE_NOT_SUPPORTED("ATMLM_4000020", "A resource with same file name and path already exists", NOT_UPLOADABLE),
WORKFLOW_RESOURCE_FILE_WITH_SAME_CONTENT_ALREADY_EXIST("ATMLM_4000021","A Workflow Resource file with the same content already exists", CONSTRAINT_VIOLATION),
WORKFLOW_RESOURCE_FILE_WITH_SAME_CAMUNDA_DEFINITION_KEY_ALREADY_EXISTS("ATMLM_4000022","A Workflow Resource file with the same Camunda definition key already exists", CONSTRAINT_VIOLATION),
WORKFLOW_FILE_DOES_NOT_EXIST("ATMLM_4000023", "The referenced Workflow Resource file does not exist", NOT_EXISTING_REFERENCED_ENTITY),
WORKFLOW_RESOURCE_CANNOT_BE_DELETED_FOR_STATUS("ATMLM_4000024", "The referenced Workflow Resource file can not be deleted in the actual state", NOT_DELETABLE),
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_4000025", "A resource with same file name and path already exists", NOT_UPLOADABLE);
RESOURCE_WITH_SAME_NAME_AND_PATH_ALREADY_SAVED("ATMLM_4000027", "A resource with same file name and path already exists", NOT_UPLOADABLE);



private final String errorCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public enum DeployableResourceType {
BPMN("bpmn","application/bpmn", "bpmn:process", "id"),
DMN("dmn", "application/dmn", "decision", "id"),
FORM("json", "application/json", null, null);
FORM("json", "application/json", "id", null);

String extension;
String mimetype;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ public abstract class ResourceEntityMapper {
public ResourceEntity toEntityCreation(ResourceCreationDto resourceCreationDto) throws NoSuchAlgorithmException, IOException {
ResourceEntity resourceEntity = new ResourceEntity();
resourceEntity.setSha256(BpmnUtils.calculateSha256(resourceCreationDto.getFile()));
resourceEntity.setS3ResourceTypeEnum(resourceCreationDto.getResourceType());
resourceEntity.setNoDeployableResourceType(resourceCreationDto.getResourceType());
resourceEntity.setFileName(resourceCreationDto.getFilename());
return resourceEntity;
}

//TODO: IMPLEMENT toDto(ResourceEntity resourceEntity) METHOD
public abstract ResourceDTO toDTO(ResourceEntity resourceEntity);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package it.gov.pagopa.atmlayer.service.model.model;

import it.gov.pagopa.atmlayer.service.model.enumeration.NoDeployableResourceType;
import it.gov.pagopa.atmlayer.service.model.enumeration.S3ResourceTypeEnum;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand All @@ -12,7 +13,7 @@
public class ResourceDTO {
private UUID resourceId;
private String sha256;
S3ResourceTypeEnum s3ResourceTypeEnum;
NoDeployableResourceType noDeployableResourceType;
private Timestamp createdAt;
private Timestamp lastUpdatedAt;
private String createdBy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.quarkus.hibernate.reactive.panache.common.WithTransaction;
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.unchecked.Unchecked;
import io.vertx.core.Context;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
Expand Down Expand Up @@ -31,6 +32,8 @@
import java.util.Map;
import java.util.Optional;

import static it.gov.pagopa.atmlayer.service.model.utils.EnumConverter.convertEnum;

@ApplicationScoped
@Slf4j
public class ResourceEntityStorageServiceImpl implements ResourceEntityStorageService {
Expand All @@ -51,7 +54,7 @@ public ResourceEntityStorageServiceImpl(ObjectStoreStrategy objectStoreStrategy,

@Override
public Uni<ResourceFile> uploadFile(ResourceEntity resourceEntity, File file, String filename, String relativePath) {
S3ResourceTypeEnum resourceType = resourceEntity.getS3ResourceTypeEnum();
S3ResourceTypeEnum resourceType = convertEnum(resourceEntity.getNoDeployableResourceType());
String path = calculatePath(resourceType);
if (!relativePath.isBlank()) {
path = path.concat("/").concat(relativePath);
Expand All @@ -62,10 +65,10 @@ public Uni<ResourceFile> uploadFile(ResourceEntity resourceEntity, File file, St
String finalPath = path;
return this.resourceFileService.findByStorageKey(storageKey)
.onItem()
.transformToUni(resource -> {
.transformToUni(Unchecked.function(resource -> {

if(resource.isPresent()){
throwErrorMessage(String.format("Cannot upload %s: resource with same file name and path already exists", storageKey), Response.Status.BAD_REQUEST, AppErrorCodeEnum.RESOURCE_WITH_SAME_NAME_AND_PATH_ALREADY_SAVED );
throw new AtmLayerException(String.format("Cannot upload %s: resource with same file name and path already exists", storageKey), Response.Status.BAD_REQUEST, AppErrorCodeEnum.RESOURCE_WITH_SAME_NAME_AND_PATH_ALREADY_SAVED );
}
log.info("Requesting to write file {} in Object Store at path {}", file.getName(), finalPath);
Context context = Vertx.currentContext();
Expand All @@ -74,11 +77,7 @@ public Uni<ResourceFile> uploadFile(ResourceEntity resourceEntity, File file, St
.onItem()
.transformToUni(objectStorePutResponse -> this.writeResourceInfoToDatabase(resourceEntity,
objectStorePutResponse, filename));
});
}

private void throwErrorMessage(String errorMessage, Response.Status status, AppErrorCodeEnum appError){
throw new AtmLayerException(errorMessage, status, appError);
}));
}

@Override
Expand All @@ -96,7 +95,7 @@ public Uni<ResourceFile> writeResourceInfoToDatabase(ResourceEntity resourceEnti
ObjectStorePutResponse putObjectResponse, String filename) {
ResourceFile entity = ResourceFile.builder()
.fileName(filename)
.resourceType(resourceEntity.getS3ResourceTypeEnum())
.resourceType(convertEnum(resourceEntity.getNoDeployableResourceType()))
.resourceEntity(resourceEntity)
.storageKey(putObjectResponse.getStorage_key())
.build();
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ object-store.bucket.name=${MODEL_OBJECT_STORE_BUCKET_NAME:pagopa-dev-atm-layer-s
object-store.bucket.region=${MODEL_OBJECT_STORE_REGION:eu-south-1}
object-store.bpmn.path-template=${MODEL_OBJECT_STORE_BPMN_TEMPLATE_PATH:BPMN/files/UUID/[uuid]/VERSION/[version]}
object-store.workflow-resource.path-template=${MODEL_OBJECT_STORE_WORKFLOW_RESOURCE_TEMPLATE_PATH:WORKFLOW_RESOURCE/[RESOURCE_TYPE]/files/UUID/[uuid]}
object-store.html.path-template=${MODEL_OBJECT_STORE_HTML_TEMPLATE_PATH:/RESOURCE/files/[RESOURCE_TYPE]}
quarkus.rest-client.process-deploy.url=${MODEL_PROCESS_BASE_PATH:http://pagopa-dev-atm-layer-wf-process.pagopa.svc.cluster.local:8080}


Expand Down

0 comments on commit 8e63cc8

Please sign in to comment.