diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/StatsApi.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/StatsApi.kt index 146e3d9a5..c3fc4aade 100644 --- a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/StatsApi.kt +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/StatsApi.kt @@ -22,10 +22,13 @@ package org.eclipse.tractusx.bpdm.gate.api import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.responses.ApiResponse import io.swagger.v3.oas.annotations.responses.ApiResponses +import org.eclipse.tractusx.bpdm.common.model.StageType +import org.eclipse.tractusx.bpdm.gate.api.model.response.StatsAddressTypesResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.StatsSharingStatesResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.StatsStagesResponse import org.springframework.http.MediaType import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.service.annotation.GetExchange import org.springframework.web.service.annotation.HttpExchange @@ -54,6 +57,14 @@ interface StatsApi { @GetExchange("/stages") fun countPartnersPerStage(): StatsStagesResponse - + @Operation + @ApiResponses( + value = [ + ApiResponse(responseCode = "200") + ] + ) + @GetMapping("/{stage}/address-types") + @GetExchange("/{stage}/address-types") + fun countAddressTypes(@PathVariable("stage") stage: StageType): StatsAddressTypesResponse } \ No newline at end of file diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/StatsAddressTypesResponse.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/StatsAddressTypesResponse.kt new file mode 100644 index 000000000..9253cc63e --- /dev/null +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/response/StatsAddressTypesResponse.kt @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ******************************************************************************/ + +package org.eclipse.tractusx.bpdm.gate.api.model.response + +data class StatsAddressTypesResponse( + val legalAndSiteTotal: Int, + val legalTotal: Int, + val siteTotal: Int, + val additionalTotal: Int +) diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/StatsController.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/StatsController.kt index 39ce15447..f84665baa 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/StatsController.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/StatsController.kt @@ -19,7 +19,9 @@ package org.eclipse.tractusx.bpdm.gate.controller +import org.eclipse.tractusx.bpdm.common.model.StageType import org.eclipse.tractusx.bpdm.gate.api.StatsApi +import org.eclipse.tractusx.bpdm.gate.api.model.response.StatsAddressTypesResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.StatsSharingStatesResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.StatsStagesResponse import org.eclipse.tractusx.bpdm.gate.service.StatsService @@ -36,4 +38,8 @@ class StatsController( override fun countPartnersPerStage(): StatsStagesResponse { return statsService.countBusinessPartnersPerStage() } + + override fun countAddressTypes(stage: StageType): StatsAddressTypesResponse { + return statsService.countAddressTypes(stage) + } } \ No newline at end of file diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/repository/generic/PostalAddressRepository.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/repository/generic/PostalAddressRepository.kt index 180eb2bff..72726d504 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/repository/generic/PostalAddressRepository.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/repository/generic/PostalAddressRepository.kt @@ -19,9 +19,22 @@ package org.eclipse.tractusx.bpdm.gate.repository.generic +import org.eclipse.tractusx.bpdm.common.dto.AddressType +import org.eclipse.tractusx.bpdm.common.model.StageType import org.eclipse.tractusx.bpdm.gate.entity.generic.PostalAddress import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.data.jpa.repository.Query import org.springframework.stereotype.Repository @Repository -interface PostalAddressRepository : JpaRepository +interface PostalAddressRepository : JpaRepository { + + @Query("SELECT a.addressType as type, COUNT(a.addressType) as count FROM BusinessPartner b JOIN b.postalAddress AS a WHERE b.stage = :stage GROUP BY a.addressType") + fun countAddressTypes(stage: StageType): List + + + interface AddressTypeCount { + val type: AddressType? + val count: Int + } +} diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/StatsService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/StatsService.kt index 7f1a6db27..d97156ba9 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/StatsService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/StatsService.kt @@ -19,18 +19,22 @@ package org.eclipse.tractusx.bpdm.gate.service +import org.eclipse.tractusx.bpdm.common.dto.AddressType import org.eclipse.tractusx.bpdm.common.model.StageType import org.eclipse.tractusx.bpdm.gate.api.model.SharingStateType +import org.eclipse.tractusx.bpdm.gate.api.model.response.StatsAddressTypesResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.StatsSharingStatesResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.StatsStagesResponse import org.eclipse.tractusx.bpdm.gate.repository.SharingStateRepository import org.eclipse.tractusx.bpdm.gate.repository.generic.BusinessPartnerRepository +import org.eclipse.tractusx.bpdm.gate.repository.generic.PostalAddressRepository import org.springframework.stereotype.Service @Service class StatsService( private val sharingStateRepository: SharingStateRepository, - private val businessPartnerRepository: BusinessPartnerRepository + private val businessPartnerRepository: BusinessPartnerRepository, + private val postalAddressRepository: PostalAddressRepository ) { fun countSharingStates(): StatsSharingStatesResponse { @@ -54,7 +58,18 @@ class StatsService( inputTotal = countsByType[StageType.Input] ?: 0, outputTotal = countsByType[StageType.Output] ?: 0 ) + } + + fun countAddressTypes(stage: StageType): StatsAddressTypesResponse { + val counts = postalAddressRepository.countAddressTypes(stage) + val countsByType = counts.associate { Pair(it.type, it.count) } + return StatsAddressTypesResponse( + legalAndSiteTotal = countsByType[AddressType.LegalAndSiteMainAddress] ?: 0, + legalTotal = countsByType[AddressType.LegalAddress] ?: 0, + siteTotal = countsByType[AddressType.SiteMainAddress] ?: 0, + additionalTotal = countsByType[AddressType.AdditionalAddress] ?: 0 + ) }