forked from eclipse-tractusx/bpdm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sharing state): eclipse-tractusx#163 implement SharingStateContr…
…oller with persistence
- Loading branch information
1 parent
acee403
commit 1ce8460
Showing
8 changed files
with
353 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/SharingState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
|
||
|
35 changes: 35 additions & 0 deletions
35
...gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/repository/SharingStaterRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SharingStateService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
}) | ||
|
||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
bpdm-gate/src/main/resources/db/migration/V0_0_2_0__create_sharingstate_table.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
Oops, something went wrong.