Skip to content

Commit

Permalink
[MODLISTS-75] Source version author information from list rather than…
Browse files Browse the repository at this point in the history
… session (#51)

* [MODLISTS-75] Source version author information from list rather than session

* Add updatedBy mappings to created list

* Add liquibase changelog

* fix liquibase script

* nit fix

Co-authored-by: mweaver-ebsco <[email protected]>

---------

Co-authored-by: mweaver-ebsco <[email protected]>
  • Loading branch information
ncovercash and mweaver-ebsco authored Dec 21, 2023
1 parent 591e1dc commit 02caa9d
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 28 deletions.
31 changes: 15 additions & 16 deletions src/main/java/org/folio/list/domain/ListVersion.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package org.folio.list.domain;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.folio.list.rest.UsersClient.User;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.UUID;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Entity
Expand Down Expand Up @@ -66,27 +67,25 @@ public class ListVersion {
@NotNull
private Boolean isPrivate;


@Column(name = "version")
@NotNull
private int version;

@Column(name = "user_friendly_query")
private String userFriendlyQuery;

public void setDataFromListEntity(ListEntity listEntity, User user) {
public void setDataFromListEntity(ListEntity listEntity) {
listId = listEntity.getId();
name = listEntity.getName();
fqlQuery = listEntity.getFqlQuery();
description = listEntity.getDescription();
userFriendlyQuery = listEntity.getUserFriendlyQuery();
fields = listEntity.getFields();
updatedBy = user.id();
updatedByUsername = user.getFullName().orElse(user.id().toString());
updatedDate = OffsetDateTime.now();
updatedBy = listEntity.getUpdatedBy();
updatedByUsername = listEntity.getUpdatedByUsername();
updatedDate = listEntity.getUpdatedDate();
version = listEntity.getVersion();
isActive = listEntity.getIsActive();
isPrivate = listEntity.getIsPrivate();
}

}
3 changes: 3 additions & 0 deletions src/main/java/org/folio/list/mapper/ListEntityMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public interface ListEntityMapper {
@Mapping(target = "createdDate", expression = "java(java.time.OffsetDateTime.now())")
@Mapping(target = "createdBy", expression = "java(createdBy.id())")
@Mapping(target = "createdByUsername", expression = "java(createdBy.getFullName().orElse(createdBy.id().toString()))")
@Mapping(target = "updatedDate", expression = "java(java.time.OffsetDateTime.now())")
@Mapping(target = "updatedBy", expression = "java(createdBy.id())")
@Mapping(target = "updatedByUsername", expression = "java(createdBy.getFullName().orElse(createdBy.id().toString()))")
@Mapping(target = "isCanned", constant = "false")
@Mapping(target = "version", constant = "1")
ListEntity toListEntity(ListRequestDTO request, UsersClient.User createdBy);
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/folio/list/services/ListService.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ public Optional<ListDTO> updateList(UUID id, ListUpdateRequestDTO request) {
Optional<ListEntity> listEntity = listRepository.findById(id);
listEntity.ifPresent(list -> {
ListVersion previousVersions = new ListVersion();
previousVersions.setDataFromListEntity(listEntity.get(), getCurrentUser());
listVersionRepository.save(previousVersions);
previousVersions.setDataFromListEntity(list);

EntityType entityType = getEntityType(list.getEntityTypeId());
validationService.validateUpdate(list, request, entityType);
if (!request.getIsActive()) {
Expand All @@ -153,6 +153,8 @@ public Optional<ListDTO> updateList(UUID id, ListUpdateRequestDTO request) {
timer.start(TimedStage.TOTAL);
importListContentsFromAsyncQuery(list, getCurrentUser(), request.getQueryId(), timer);
}

listVersionRepository.save(previousVersions);
listRepository.save(list);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@

<include file="sql/create-list-versions-table.sql" relativeToChangelogFile="true"/>
<include file="yml/update-list-versions-fkey.yaml" relativeToChangelogFile="true"/>
<include file="yml/alter-list-details-updated-metadata-add-not-null-constraint.yaml" relativeToChangelogFile="true"/>
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
databaseChangeLog:
- changeSet:
id: alter-list-details-updated-metadata-add-not-null-constraint
author: [email protected]
changes:
# backfill default values
- update:
tableName: list_details
columns:
- column:
name: updated_by
valueComputed: created_by
- column:
name: updated_by_username
valueComputed: created_by_username
- column:
name: updated_date
valueComputed: created_date
where: updated_by IS NULL

# add new constraints
- addNotNullConstraint:
columnName: updated_by
tableName: list_details
- addNotNullConstraint:
columnName: updated_by_username
tableName: list_details
- addNotNullConstraint:
columnName: updated_date
tableName: list_details
19 changes: 9 additions & 10 deletions src/test/java/org/folio/list/domain/ListVersionTest.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
package org.folio.list.domain;

import org.folio.list.rest.UsersClient;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.folio.list.utils.TestDataFixture;
import org.junit.jupiter.api.Test;

import java.util.Optional;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;

class ListVersionTest {

@Test
void shouldSetDataFromListEntity() {
ListEntity listEntity = TestDataFixture.getListEntityWithSuccessRefresh();
UsersClient.User user = new UsersClient.User(UUID.randomUUID(), Optional.of(new UsersClient.Personal("Test", "User")));
ListVersion listVersion = TestDataFixture.getListVersion();
listVersion.setDataFromListEntity(listEntity, user);
listVersion.setDataFromListEntity(listEntity);
assertEquals(listVersion.getListId(), listEntity.getId());
assertEquals(listVersion.getName(), listEntity.getName());
assertEquals(listVersion.getFqlQuery(), listEntity.getFqlQuery());
assertEquals(listVersion.getIsActive(), listEntity.getIsActive());
assertEquals(listVersion.getUpdatedBy(), user.id());
assertEquals(listVersion.getUpdatedByUsername(), user.getFullName().get());
assertEquals(listVersion.getUpdatedBy(), listEntity.getUpdatedBy());
assertEquals(
listVersion.getUpdatedByUsername(),
listEntity.getUpdatedByUsername()
);
}
}

0 comments on commit 02caa9d

Please sign in to comment.