|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 The ORT Server Authors (See <https://github.com/eclipse-apoapsis/ort-server/blob/main/NOTICE>) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + * License-Filename: LICENSE |
| 18 | + */ |
| 19 | + |
| 20 | +package org.eclipse.apoapsis.ortserver.dao.tables |
| 21 | + |
| 22 | +import org.eclipse.apoapsis.ortserver.dao.repositories.userDisplayName.UserDisplayNameDao |
| 23 | +import org.eclipse.apoapsis.ortserver.model.ChangeEvent |
| 24 | +import org.eclipse.apoapsis.ortserver.model.ChangeEventAction |
| 25 | +import org.eclipse.apoapsis.ortserver.model.ChangeEventEntityType |
| 26 | +import org.eclipse.apoapsis.ortserver.model.UserDisplayName |
| 27 | + |
| 28 | +import org.jetbrains.exposed.sql.SortOrder |
| 29 | +import org.jetbrains.exposed.sql.Table |
| 30 | +import org.jetbrains.exposed.sql.and |
| 31 | +import org.jetbrains.exposed.sql.insert |
| 32 | +import org.jetbrains.exposed.sql.kotlin.datetime.timestamp |
| 33 | + |
| 34 | +/* |
| 35 | + * A table to store change log entries representing user-performed change events. |
| 36 | + */ |
| 37 | +object ChangeLogTable : Table("change_log") { |
| 38 | + val entityType = text("entity_type") |
| 39 | + val entityId = text("entity_id") |
| 40 | + val userId = text("user_id") |
| 41 | + val occurredAt = timestamp("occurred_at") |
| 42 | + val action = text("action") |
| 43 | + |
| 44 | + fun insert( |
| 45 | + entityTypeInput: ChangeEventEntityType, |
| 46 | + entityIdInput: String, |
| 47 | + userIdInput: String, |
| 48 | + actionInput: ChangeEventAction |
| 49 | + ) { |
| 50 | + insert { |
| 51 | + it[entityType] = entityTypeInput.name |
| 52 | + it[entityId] = entityIdInput |
| 53 | + it[userId] = userIdInput |
| 54 | + it[action] = actionInput.name |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + fun getAllByEntityTypeAndId( |
| 59 | + entityTypeSearch: ChangeEventEntityType, |
| 60 | + entityIdSearch: String |
| 61 | + ): List<ChangeEvent> { |
| 62 | + return select(columns) |
| 63 | + .where { (entityType eq entityTypeSearch.name) and (entityId eq entityIdSearch) } |
| 64 | + .orderBy(occurredAt, SortOrder.ASC) |
| 65 | + .map { row -> |
| 66 | + ChangeEvent( |
| 67 | + user = UserDisplayNameDao.findById(row[userId])?.mapToModel() |
| 68 | + ?: UserDisplayName(row[userId], "Unknown"), |
| 69 | + occurredAt = row[occurredAt], |
| 70 | + action = ChangeEventAction.valueOf(row[action]) |
| 71 | + ) |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments