Skip to content

Commit

Permalink
feat(sharing state): eclipse-tractusx#163 implement SharingStateContr…
Browse files Browse the repository at this point in the history
…oller with persistence
  • Loading branch information
rainer-exxcellent committed May 23, 2023
1 parent acee403 commit 1ce8460
Show file tree
Hide file tree
Showing 8 changed files with 353 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,79 +24,28 @@ import org.eclipse.tractusx.bpdm.common.dto.request.PaginationRequest
import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse
import org.eclipse.tractusx.bpdm.gate.api.GateSharingStateApi
import org.eclipse.tractusx.bpdm.gate.api.model.SharingStateDto
import org.eclipse.tractusx.bpdm.gate.api.model.SharingStateType
import org.eclipse.tractusx.bpdm.gate.api.model.response.LsaType
import org.eclipse.tractusx.bpdm.gate.service.AddressService
import org.eclipse.tractusx.bpdm.gate.service.LegalEntityService
import org.eclipse.tractusx.bpdm.gate.service.SiteService
import org.eclipse.tractusx.bpdm.gate.service.SharingStateService
import org.springframework.web.bind.annotation.RestController

@RestController
class SharingStateController(
val legalEntityService: LegalEntityService,
val siteService: SiteService,
val addressService: AddressService
val sharingStateService: SharingStateService
) : GateSharingStateApi {

private val logger = KotlinLogging.logger { }

override fun getSharingStates(paginationRequest: PaginationRequest, lsaType: LsaType?, externalIds: Collection<String>?): PageResponse<SharingStateDto> {
// TODO Replace mock implementation using persistence:
// For now only the BPN is collected from CDQ input. If there is none, no sharing status is returned.
// For now lsaType is required!

lsaType ?: throw IllegalArgumentException("lsaType is required")
externalIds ?: throw IllegalArgumentException("externalIds is required")

val sharingStates = when (lsaType) {
LsaType.LegalEntity ->
legalEntityService.getLegalEntities(externalIds = externalIds, limit = paginationRequest.size, startAfter = null).content
.filter { it.bpn != null }
.map {
SharingStateDto(
lsaType = lsaType,
externalId = it.externalId,
bpn = it.bpn,
sharingStateType = SharingStateType.Success
)
}

LsaType.Site ->
siteService.getSites(externalIds = externalIds, limit = paginationRequest.size, startAfter = null).content
.filter { it.bpn != null }
.map {
SharingStateDto(
lsaType = lsaType,
externalId = it.externalId,
bpn = it.bpn,
sharingStateType = SharingStateType.Success
)
}

LsaType.Address ->
addressService.getAddresses(externalIds = externalIds, limit = paginationRequest.size, startAfter = null).content
.filter { it.bpn != null }
.map {
SharingStateDto(
lsaType = lsaType,
externalId = it.externalId,
bpn = it.bpn,
sharingStateType = SharingStateType.Success
)
}
}

// TODO Not yet implemented
return PageResponse(
totalElements = sharingStates.size.toLong(),
totalPages = if (sharingStates.size > 0) 1 else 0,
page = 0,
contentSize = sharingStates.size,
content = sharingStates
)
return sharingStateService.findSharingStates(paginationRequest, lsaType, externalIds)
}

override fun upsertSharingState(request: SharingStateDto) {
// TODO Not yet implemented
logger.info { "upsertSharingState() called with $request" }
sharingStateService.upsertSharingState(request)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*******************************************************************************
* 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.entity

import jakarta.persistence.*
import org.eclipse.tractusx.bpdm.common.model.BaseEntity
import org.eclipse.tractusx.bpdm.gate.api.exception.BusinessPartnerSharingError
import org.eclipse.tractusx.bpdm.gate.api.model.SharingStateType
import org.eclipse.tractusx.bpdm.gate.api.model.response.LsaType
import java.time.LocalDateTime


@Entity
@Table(name = "sharing_states")
class SharingState(

@Column(name = "external_id", nullable = false)
var externalId: String,

@Enumerated(EnumType.STRING)
@Column(name = "lsa_type", nullable = false)
var lsaType: LsaType,

@Enumerated(EnumType.STRING)
@Column(name = "sharing_state_type", nullable = false)
var sharingStateType: SharingStateType,

@Enumerated(EnumType.STRING)
@Column(name = "sharing_error_code", nullable = true)
var sharingErrorCode: BusinessPartnerSharingError?,

@Column(name = "sharing_error_message", nullable = true)
var sharingErrorMessage: String? = null,

@Column(name = "bpn", nullable = true)
var bpn: String? = null,

@Column(name = "sharing_process_started", nullable = true)
var sharingProcessStarted: LocalDateTime? = null

) : BaseEntity()



Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*******************************************************************************
* 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.repository

import org.eclipse.tractusx.bpdm.gate.api.model.response.LsaType
import org.eclipse.tractusx.bpdm.gate.entity.SharingState
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.repository.CrudRepository
import org.springframework.data.repository.PagingAndSortingRepository

interface SharingStaterRepository : PagingAndSortingRepository<SharingState, Long>, CrudRepository<SharingState, Long> {

fun findByExternalIdAndLsaType(externalId: String, businessPartnerType: LsaType): SharingState?

fun findByExternalIdInAndLsaType(externalIds: Collection<String>, businessPartnerType: LsaType, pageable: Pageable): Page<SharingState>

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*******************************************************************************
* 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.service

import org.eclipse.tractusx.bpdm.common.dto.request.PaginationRequest
import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse
import org.eclipse.tractusx.bpdm.gate.api.model.SharingStateDto
import org.eclipse.tractusx.bpdm.gate.api.model.response.LsaType
import org.eclipse.tractusx.bpdm.gate.entity.SharingState
import org.eclipse.tractusx.bpdm.gate.repository.SharingStaterRepository
import org.springframework.data.domain.PageRequest
import org.springframework.stereotype.Service

@Service
class SharingStateService(private val stateRepository: SharingStaterRepository) {

fun upsertSharingState(request: SharingStateDto) {

val sharingState = this.stateRepository.findByExternalIdAndLsaType(request.externalId, request.lsaType)
if (sharingState == null) {
insertSharingState(request)
} else {
updateSharingState(sharingState, request)
}
}

private fun insertSharingState(dto: SharingStateDto) {

this.stateRepository.save(
SharingState(
externalId = dto.externalId,
lsaType = dto.lsaType,
sharingStateType = dto.sharingStateType,
sharingErrorCode = dto.sharingErrorCode,
sharingErrorMessage = dto.sharingErrorMessage,
bpn = dto.bpn,
sharingProcessStarted = dto.sharingProcessStarted
)
)
}

private fun updateSharingState(entity: SharingState, dto: SharingStateDto) {

entity.sharingStateType = dto.sharingStateType
entity.sharingErrorCode = dto.sharingErrorCode
entity.sharingErrorMessage = dto.sharingErrorMessage
entity.bpn = dto.bpn
entity.sharingProcessStarted = dto.sharingProcessStarted

this.stateRepository.save(entity)
}

fun findSharingStates(paginationRequest: PaginationRequest, lsaType: LsaType, externalIds: Collection<String>): PageResponse<SharingStateDto> {

val pageable = PageRequest.of(paginationRequest.page, paginationRequest.size)
val page = this.stateRepository.findByExternalIdInAndLsaType(externalIds, lsaType, pageable)

return page.toDto(page.content.map {
SharingStateDto(
externalId = it.externalId,
lsaType = it.lsaType,
sharingStateType = it.sharingStateType,
sharingErrorCode = it.sharingErrorCode,
sharingErrorMessage = it.sharingErrorMessage,
bpn = it.bpn,
sharingProcessStarted = it.sharingProcessStarted
)
})

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

CREATE TABLE sharing_states
(
id BIGINT NOT NULL,
uuid UUID NOT NULL,
created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL,
updated_at TIMESTAMP WITHOUT TIME ZONE NOT NULL,
external_id VARCHAR(255) NOT NULL,
lsa_type VARCHAR(255) NOT NULL,
sharing_state_type VARCHAR(255) NOT NULL,
sharing_error_code VARCHAR(255),
sharing_error_message VARCHAR(255),
bpn VARCHAR(255),
sharing_process_started timestamp with time zone,
CONSTRAINT pk_sharing_states PRIMARY KEY (id)
);

ALTER TABLE sharing_states
ADD CONSTRAINT uc_sharing_states_uuid UNIQUE (uuid);
ALTER TABLE sharing_states
ADD CONSTRAINT uc_sharing_states_externalId_lsa_type UNIQUE (external_id, lsa_type);

alter table sharing_states
alter column created_at type timestamp with time zone using created_at at time zone 'UTC';
alter table sharing_states
alter column updated_at type timestamp with time zone using updated_at at time zone 'UTC';
alter table sharing_states
alter column sharing_process_started type timestamp with time zone using sharing_process_started at time zone 'UTC';
Loading

0 comments on commit 1ce8460

Please sign in to comment.