Commit 0198af00 authored by Anastasios Poimenidis's avatar Anastasios Poimenidis
Browse files

feat: implement logic for GeographicSiteDTO and GeographicSiteResponse

parent 221093e8
Loading
Loading
Loading
Loading
+0 −7
Original line number Diff line number Diff line
@@ -5,7 +5,6 @@ import org.apache.commons.collections4.CollectionUtils;
import org.etsi.osl.hypo.api.tmf.agreement.schema.Agreement;
import org.etsi.osl.hypo.api.tmf.agreement.schema.AgreementSpecification;
import org.etsi.osl.hypo.api.tmf.alarm.schema.Alarm;
import org.etsi.osl.hypo.api.tmf.geographic.site.schema.GeographicSite;
import org.etsi.osl.hypo.api.tmf.party.partnership.management.schema.Partnership;
import org.etsi.osl.hypo.api.tmf.party.partnership.management.schema.PartnershipSpecification;
import org.etsi.osl.hypo.api.tmf.party.role.management.schema.PartyRole;
@@ -71,12 +70,6 @@ public interface UpdateMapper {
    @BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
    PartyRole updatePartyRole(PartyRole newPartyRole, @MappingTarget PartyRole oldPartyRole);

    @Mapping(target = "entityId", ignore = true)
    @Mapping(target = "relatedParties", ignore = true)
    @BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
    GeographicSite updateGeographicSite(
            GeographicSite newGeographicSite, @MappingTarget GeographicSite oldGeographicSite);

    @Mapping(target = "entityId", ignore = true)
    @Mapping(target = "roleSpecification.entityId", ignore = true)
    @BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
+30 −0
Original line number Diff line number Diff line
package org.etsi.osl.hypo.api.tmf.geographic.site.api.mapper;

import org.etsi.osl.hypo.api.tmf.geographic.site.model.GeographicSiteDTO;
import org.etsi.osl.hypo.api.tmf.geographic.site.model.GeographicSiteResponse;
import org.etsi.osl.hypo.api.tmf.geographic.site.schema.GeographicSite;
import org.mapstruct.BeanMapping;
import org.mapstruct.InjectionStrategy;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.NullValuePropertyMappingStrategy;
import org.mapstruct.SubclassExhaustiveStrategy;

@Mapper(
        componentModel = "cdi",
        injectionStrategy = InjectionStrategy.CONSTRUCTOR,
        subclassExhaustiveStrategy = SubclassExhaustiveStrategy.RUNTIME_EXCEPTION)
public interface GeographicSiteMapper {
    GeographicSite geographicSiteDTOtoEntity(GeographicSiteDTO geographicSiteDTO);

    GeographicSiteResponse geographicSiteEntityToResponse(GeographicSite geographicSite);

    GeographicSiteDTO geographicSiteResponseToDTO(GeographicSiteResponse expectedResponse);

    @Mapping(target = "entityId", ignore = true)
    @Mapping(target = "relatedParty", ignore = true)
    @BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
    GeographicSite updateGeographicSite(
            GeographicSite newGeographicSite, @MappingTarget GeographicSite oldGeographicSite);
}
+32 −30
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@ import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;

import io.quarkus.security.Authenticated;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
@@ -16,6 +15,7 @@ import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
import java.util.List;
import java.util.Map;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
@@ -26,8 +26,9 @@ import org.eclipse.microprofile.openapi.annotations.tags.Tag;
import org.etsi.osl.hypo.api.tmf.common.api.services.UtilService;
import org.etsi.osl.hypo.api.tmf.common.model.ErrorMessage;
import org.etsi.osl.hypo.api.tmf.geographic.site.api.services.GeographicSiteService;
import org.etsi.osl.hypo.api.tmf.geographic.site.model.GeographicSiteDTO;
import org.etsi.osl.hypo.api.tmf.geographic.site.model.GeographicSiteResponse;
import org.etsi.osl.hypo.api.tmf.geographic.site.schema.ApplicationProperties;
import org.etsi.osl.hypo.api.tmf.geographic.site.schema.GeographicSite;
import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.RestPath;
import org.jboss.resteasy.reactive.RestQuery;
@@ -62,7 +63,7 @@ public class GeographicSiteResourceImpl {
            content =
                    @Content(
                            mediaType = APPLICATION_JSON,
                            schema = @Schema(implementation = GeographicSite.class)))
                            schema = @Schema(implementation = GeographicSiteResponse.class)))
    @APIResponse(responseCode = "400", description = "Bad Request")
    @APIResponse(
            responseCode = "500",
@@ -71,11 +72,15 @@ public class GeographicSiteResourceImpl {
                    @Content(
                            mediaType = APPLICATION_JSON,
                            schema = @Schema(implementation = ErrorMessage.class)))
    public Response create(@Valid GeographicSite geographicSite) {
        logger.info("Request : " + geographicSite.toString());
        GeographicSite newGeographicSite =
                geographicSiteService.createGeographicSite(geographicSite, uriInfo);
        logger.info("Response : " + newGeographicSite.toString());
    public Response create(GeographicSiteDTO geographicSiteDTO) {
        logger.debugf("POST Geographic Site Request :%s", geographicSiteDTO);
        logger.infof("POST Geographic Site Request with code :%s ", geographicSiteDTO.getCode());

        GeographicSiteResponse newGeographicSite =
                geographicSiteService.createGeographicSite(geographicSiteDTO, uriInfo);
        logger.debugf("POST Geographic Site Response : %s", newGeographicSite.toString());
        logger.infof("POST Geographic Site Success. Response with id %s: ", newGeographicSite.getId());

        return Response.ok(newGeographicSite).status(201).build();
    }

@@ -83,7 +88,6 @@ public class GeographicSiteResourceImpl {
    @PermitAll
    @Path(ApplicationProperties.TMF_URL_VERSION + "/" + GEOGRAPHIC_SITE)
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @Operation(
            summary = "List or find all GeographicSite objects",
            description = "This operation list or find GeographicSite entities.")
@@ -93,17 +97,21 @@ public class GeographicSiteResourceImpl {
            content =
                    @Content(
                            mediaType = APPLICATION_JSON,
                            schema = @Schema(implementation = GeographicSite.class, type = SchemaType.ARRAY)))
                            schema =
                                    @Schema(implementation = GeographicSiteResponse.class, type = SchemaType.ARRAY)))
    public Response getAll(@RestQuery String fields, @Context UriInfo uriInfo) {
        logger.info("GET all Request Received");
        Map<String, String> criteriaParams = UtilService.getRequestedCriteriaForFiltering(uriInfo);
        return Response.ok(geographicSiteService.getByCriteria(fields, criteriaParams)).build();
        List<GeographicSiteResponse> geographicSiteList =
                geographicSiteService.getByCriteria(fields, criteriaParams);
        logger.info("GET all Response Success");
        return Response.ok(geographicSiteList).build();
    }

    @GET
    @PermitAll
    @Path(ApplicationProperties.TMF_URL_VERSION + "/" + GEOGRAPHIC_SITE + "/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @Operation(
            summary = "Retrieves a GeographicSite by ID",
            description =
@@ -114,7 +122,7 @@ public class GeographicSiteResourceImpl {
            content =
                    @Content(
                            mediaType = APPLICATION_JSON,
                            schema = @Schema(implementation = GeographicSite.class)))
                            schema = @Schema(implementation = GeographicSiteResponse.class)))
    @APIResponse(
            responseCode = "404",
            description = "Not Found",
@@ -140,7 +148,7 @@ public class GeographicSiteResourceImpl {
            content =
                    @Content(
                            mediaType = APPLICATION_JSON,
                            schema = @Schema(implementation = GeographicSite.class)))
                            schema = @Schema(implementation = GeographicSiteResponse.class)))
    @APIResponse(
            responseCode = "400",
            description = "Bad Request",
@@ -162,26 +170,20 @@ public class GeographicSiteResourceImpl {
                    @Content(
                            mediaType = APPLICATION_JSON,
                            schema = @Schema(implementation = ErrorMessage.class)))
    public Response update(@Valid GeographicSite geographicSite, @RestPath String id) {
        logger.info("Update Geographic Site with id : " + id);
        logger.info("Request : " + geographicSite.toString());
        return Response.ok(geographicSiteService.updateGeographicSite(id, geographicSite)).build();
    public Response update(GeographicSiteDTO geographicSiteDTO, @RestPath String id) {
        logger.infof("PATCH Geographic Site Request received with id :%s", id);
        logger.debugf("Request :%s", geographicSiteDTO.toString());
        logger.infof("PATCH Geographic Site Success received with id :%s", id);
        return Response.ok(geographicSiteService.updateGeographicSite(id, geographicSiteDTO)).build();
    }

    @DELETE
    @Path(ApplicationProperties.TMF_URL_VERSION + "/" + GEOGRAPHIC_SITE + "/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @Operation(
            summary = "Deletes a GeographicSite by ID",
            description = "This operation deletes a GeographicSite entity.")
    @APIResponse(
            responseCode = "200",
            description = "Deleted",
            content =
                    @Content(
                            mediaType = APPLICATION_JSON,
                            schema = @Schema(implementation = GeographicSite.class)))
    @APIResponse(responseCode = "204", description = "Deleted")
    @APIResponse(
            responseCode = "404",
            description = "Not Found",
@@ -190,9 +192,9 @@ public class GeographicSiteResourceImpl {
                            mediaType = APPLICATION_JSON,
                            schema = @Schema(implementation = ErrorMessage.class)))
    public Response delete(@RestPath String id) {
        logger.info("Delete Geographic Site with id : " + id);
        GeographicSite deletedGeographicSite = geographicSiteService.deleteGeographicSite(id);
        logger.info("Successful delete Geographic Site with id : " + id);
        return Response.ok(deletedGeographicSite).build();
        logger.infof("DELETE Geographic Site Request with id : %s", id);
        geographicSiteService.deleteGeographicSite(id);
        logger.infof("DELETE Geographic Site Success with id : %s", id);
        return Response.noContent().build();
    }
}
+31 −40
Original line number Diff line number Diff line
@@ -9,33 +9,34 @@ import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils;
import org.etsi.osl.hypo.api.tmf.common.api.mapper.UpdateMapper;
import org.etsi.osl.hypo.api.tmf.common.api.repository.RelatedPartyRepository;
import org.etsi.osl.hypo.api.tmf.common.api.services.UtilService;
import org.etsi.osl.hypo.api.tmf.common.exception.DuplicateKeyException;
import org.etsi.osl.hypo.api.tmf.common.exception.ServiceNotFoundException;
import org.etsi.osl.hypo.api.tmf.common.schema.RelatedPartyEntity;
import org.etsi.osl.hypo.api.tmf.geographic.site.api.mapper.GeographicSiteMapper;
import org.etsi.osl.hypo.api.tmf.geographic.site.api.repository.GeographicSiteRepository;
import org.etsi.osl.hypo.api.tmf.geographic.site.model.GeographicSiteDTO;
import org.etsi.osl.hypo.api.tmf.geographic.site.model.GeographicSiteResponse;
import org.etsi.osl.hypo.api.tmf.geographic.site.schema.GeographicSite;
import org.jboss.logging.Logger;

@ApplicationScoped
public class GeographicSiteService {
    private static final Logger logger = Logger.getLogger(GeographicSiteService.class);

    private final GeographicSiteRepository geographicSiteRepository;
    private final RelatedPartyRepository relatedPartyRepository;

    private final UpdateMapper updateMapper;

    private final GeographicSiteMapper geographicSiteMapper;
    private static final String GEOGRAPHIC_SITE_WITH_ID = "Geographic Site with id ";
    private static final String NOT_FOUND = " not found.";

    public GeographicSiteService(
            GeographicSiteRepository geographicSiteRepository,
            RelatedPartyRepository relatedPartyRepository,
            UpdateMapper updateMapper) {
            GeographicSiteMapper geographicSiteMapper) {
        this.geographicSiteRepository = geographicSiteRepository;
        this.relatedPartyRepository = relatedPartyRepository;
        this.updateMapper = updateMapper;
        this.geographicSiteMapper = geographicSiteMapper;
    }

    @Transactional
@@ -44,7 +45,8 @@ public class GeographicSiteService {
    }

    @Transactional
    public GeographicSite getByIdAndCriteria(String id, String fields, Map<String, String> params) {
    public GeographicSiteResponse getByIdAndCriteria(
            String id, String fields, Map<String, String> params) {
        List<GeographicSite> geographicSites =
                geographicSiteRepository.findWithCriteriaQuery(id, params, null);
        if (geographicSites.isEmpty()) {
@@ -53,14 +55,16 @@ public class GeographicSiteService {
            throw new ServiceNotFoundException(notFoundMessage);
        }

        return UtilService.filterFields(geographicSites.get(0), fields);
        GeographicSite geographicSite = UtilService.filterFields(geographicSites.get(0), fields);
        return geographicSiteMapper.geographicSiteEntityToResponse(geographicSite);
    }

    @Transactional
    public List<GeographicSite> getByCriteria(String fields, Map<String, String> params) {
    public List<GeographicSiteResponse> getByCriteria(String fields, Map<String, String> params) {
        List<GeographicSite> geographicSites =
                geographicSiteRepository.findWithCriteriaQuery(null, params, null);
        return UtilService.getFilteredList(geographicSites, fields);
        List<GeographicSite> filteredList = UtilService.getFilteredList(geographicSites, fields);
        return filteredList.stream().map(geographicSiteMapper::geographicSiteEntityToResponse).toList();
    }

    @Transactional
@@ -73,18 +77,19 @@ public class GeographicSiteService {
    }

    @Transactional
    public GeographicSite createGeographicSite(GeographicSite geographicSite, UriInfo uriInfo) {
        checkIfIdExists(geographicSite.getId());
        findRelatedPartyAndAddItToResource(geographicSite);
        if (uriInfo != null) {
    public GeographicSiteResponse createGeographicSite(
            GeographicSiteDTO geographicSiteDTO, UriInfo uriInfo) {
        GeographicSite geographicSite =
                geographicSiteMapper.geographicSiteDTOtoEntity(geographicSiteDTO);
        geographicSite.setUri(uriInfo.getAbsolutePath());
        }
        findRelatedPartyAndAddItToResource(geographicSite);
        geographicSiteRepository.persist(geographicSite);
        return geographicSite;
        logger.infof("Successfully created Geographic site with id:%s", geographicSite.getId());
        return geographicSiteMapper.geographicSiteEntityToResponse(geographicSite);
    }

    private void findRelatedPartyAndAddItToResource(GeographicSite geographicSite) {
        Set<RelatedPartyEntity> relatedParties = geographicSite.getRelatedParties();
        Set<RelatedPartyEntity> relatedParties = geographicSite.getRelatedParty();
        if (CollectionUtils.isNotEmpty(relatedParties)) {

            Set<String> partyIds =
@@ -93,41 +98,27 @@ public class GeographicSiteService {
            // find and relate parties
            Set<RelatedPartyEntity> relatedPartyEntities = relatedPartyRepository.findByIdsSet(partyIds);
            if (CollectionUtils.isNotEmpty(relatedPartyEntities)) {
                geographicSite.setRelatedParties(relatedPartyEntities);
                geographicSite.setRelatedParty(relatedPartyEntities);
            }
        }
    }

    @Transactional
    public GeographicSite deleteGeographicSite(String id) {
    public void deleteGeographicSite(String id) {
        GeographicSite geographicSite = getById(id);

        geographicSite
                .getRelatedParties()
                .forEach(
                        relatedPartyEntity -> {
                            relatedPartyEntity.getGeographicSites().remove(geographicSite);
                            relatedPartyRepository.persist(relatedPartyEntity);
                        });

        geographicSite.getRelatedParties().clear();
        geographicSiteRepository.delete(geographicSite);
        return geographicSite;
    }

    @Transactional
    public GeographicSite updateGeographicSite(String id, GeographicSite newGeographicSite) {
    public GeographicSiteResponse updateGeographicSite(
            String id, GeographicSiteDTO geographicSiteDTO) {
        GeographicSite newGeographicSite =
                geographicSiteMapper.geographicSiteDTOtoEntity(geographicSiteDTO);
        GeographicSite oldGeographicSite = getById(id);
        GeographicSite geographicSite =
                updateMapper.updateGeographicSite(newGeographicSite, oldGeographicSite);
                geographicSiteMapper.updateGeographicSite(newGeographicSite, oldGeographicSite);
        geographicSiteRepository.persist(geographicSite);
        return geographicSite;
    }

    private void checkIfIdExists(String id) {
        if (idExists(id)) {
            throw new DuplicateKeyException(GEOGRAPHIC_SITE_WITH_ID + id + " already exists");
        }
        return geographicSiteMapper.geographicSiteEntityToResponse(geographicSite);
    }

    public boolean idExists(String id) {
+60 −49

File changed.

Preview size limit exceeded, changes collapsed.

Loading