Skip to content

Commit

Permalink
Merge changes from the mod-consortia
Browse files Browse the repository at this point in the history
  • Loading branch information
OleksiiKuzminov committed May 7, 2024
1 parent 9804ac6 commit 71a5a38
Show file tree
Hide file tree
Showing 40 changed files with 678 additions and 350 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@
<applications-poc-tools.version>1.4.0</applications-poc-tools.version>

<!-- test dependencies -->
<wiremock-standalone.version>2.27.2</wiremock-standalone.version>
<testcontainers.version>1.19.7</testcontainers.version>
<wiremock-standalone.version>3.0.1</wiremock-standalone.version>
<testcontainers.version>1.19.3</testcontainers.version>
<snakeyaml.version>2.2</snakeyaml.version>
<testcontainers-bom.version>1.19.7</testcontainers-bom.version>
<maven-checkstyle-plugin.version>3.2.0</maven-checkstyle-plugin.version>
<testcontainers-bom.version>1.19.3</testcontainers-bom.version>
<maven-checkstyle-plugin.version>3.3.1</maven-checkstyle-plugin.version>
</properties>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.folio.spring.config.FeignClientConfiguration;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

Expand All @@ -12,4 +13,7 @@ public interface UserTenantsClient {

@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
void postUserTenant(@RequestBody UserTenant userTenant);

@DeleteMapping
void deleteUserTenants();
}
2 changes: 1 addition & 1 deletion src/main/java/org/folio/consortia/config/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ObjectMapper objectMapper() {
objectMapper.findAndRegisterModules()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return objectMapper;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public Tenant convert(TenantDetailsEntity source) {
tenant.setCode(source.getCode());
tenant.setName(source.getName());
tenant.setIsCentral(source.getIsCentral());
tenant.setIsDeleted(source.getIsDeleted());
return tenant;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public TenantDetails convert(TenantDetailsEntity source) {
.code(source.getCode())
.name(source.getName())
.isCentral(source.getIsCentral())
.isDeleted(source.getIsDeleted())
.setupStatus(source.getSetupStatus());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public Tenant convert(TenantEntity source) {
tenant.setCode(source.getCode());
tenant.setName(source.getName());
tenant.setIsCentral(source.getIsCentral());
tenant.setIsDeleted(source.getIsDeleted());
return tenant;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ public abstract class AbstractTenantEntity extends AuditableEntity {
private String name;
private UUID consortiumId;
private Boolean isCentral;
private Boolean isDeleted;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof AbstractTenantEntity that)) return false;
return Objects.equals(id, that.id) && Objects.equals(code, that.code) && Objects.equals(name, that.name) && Objects.equals(consortiumId, that.consortiumId) && Objects.equals(isCentral, that.isCentral);
return Objects.equals(id, that.id) && Objects.equals(code, that.code) &&
Objects.equals(name, that.name) && Objects.equals(consortiumId, that.consortiumId) &&
Objects.equals(isCentral, that.isCentral) && Objects.equals(isDeleted, that.isDeleted);
}

@Override
public int hashCode() {
return Objects.hash(id, code, name, consortiumId, isCentral);
return Objects.hash(id, code, name, consortiumId, isCentral, isDeleted);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
Expand All @@ -10,11 +11,15 @@
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface SharingInstanceRepository extends JpaRepository<SharingInstanceEntity, UUID>, JpaSpecificationExecutor<SharingInstanceEntity> {

@Query("SELECT si FROM SharingInstanceEntity si WHERE si.instanceId = ?1 AND si.sourceTenantId= ?2 AND si.targetTenantId= ?3")
Optional<SharingInstanceEntity> findByInstanceAndTenantIds(UUID instanceIdentifier, String sourceTenantId, String targetTenantId);

interface Specifications {
static Specification<SharingInstanceEntity> constructSpecification(UUID instanceIdentifier, String sourceTenantId,
String targetTenantId, Status status) {
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/org/folio/consortia/repository/TenantRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@
@Repository
public interface TenantRepository extends JpaRepository<TenantEntity, String> {

@Query("SELECT t FROM TenantEntity t WHERE t.consortiumId = ?1 and t.isDeleted = FALSE")
Page<TenantEntity> findByConsortiumId(UUID consortiumId, Pageable pageable);

@Query("SELECT t FROM TenantEntity t WHERE t.consortiumId = ?1 and t.isDeleted = FALSE")
List<TenantEntity> findByConsortiumId(UUID consortiumId);

@Query("SELECT t FROM TenantEntity t where t.isCentral = true")
@Query("SELECT t FROM TenantEntity t WHERE t.isCentral = true")
Optional<TenantEntity> findCentralTenant();

boolean existsByIsCentralTrue();
boolean existsByCode(String code);
boolean existsByName(String name);
@Query("SELECT CASE WHEN COUNT(t) > 0 THEN TRUE ELSE FALSE END FROM TenantEntity t " +
"WHERE t.code = ?1 AND t.id != ?2")
boolean existsByCodeForOtherTenant(String name, String tenantId);

@Query("SELECT CASE WHEN COUNT(t) > 0 THEN TRUE ELSE FALSE END FROM TenantEntity t " +
"WHERE t.name = ?1 AND t.id != ?2")
boolean existsByNameForOtherTenant(String name, String tenantId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@

@Repository
public interface UserTenantRepository extends JpaRepository<UserTenantEntity, UUID> {

@Query("SELECT ut FROM UserTenantEntity ut WHERE ut.tenant.isDeleted= FALSE")
Page<UserTenantEntity> getAll(Pageable pageable);

@Query("SELECT ut FROM UserTenantEntity ut WHERE ut.userId= ?1 AND ut.tenant.isDeleted= FALSE")
Page<UserTenantEntity> findByUserId(UUID userId, Pageable pageable);

@Query("SELECT ut FROM UserTenantEntity ut WHERE ut.username= ?1 AND ut.tenant.id= ?2")
@Query("SELECT ut FROM UserTenantEntity ut WHERE ut.userId= ?1")
Page<UserTenantEntity> findAnyByUserId(UUID userId, Pageable pageable);

@Query("SELECT ut FROM UserTenantEntity ut WHERE ut.username= ?1 AND ut.tenant.id= ?2 AND ut.tenant.isDeleted= FALSE")
Optional<UserTenantEntity> findByUsernameAndTenantId(String username, String tenantId);

@Query("SELECT ut FROM UserTenantEntity ut WHERE ut.userId= ?1 AND ut.tenant.id= ?2")
Optional<UserTenantEntity> findByUserIdAndTenantId(UUID userId, String tenantId);

boolean existsByTenantId(String tenantId);

@Query("SELECT ut FROM UserTenantEntity ut WHERE ut.userId= ?1 AND ut.isPrimary= true")
Optional<UserTenantEntity> findByUserIdAndIsPrimaryTrue(UUID userId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public interface ConsortiaConfigurationService {
*/
ConsortiaConfiguration createConfiguration(String centralTenantId);

/**
* Check if there is any central tenant
* @return boolean value based one whether central tenant configuration exists or not
*/
boolean isCentralTenantConfigurationExists();
}
4 changes: 4 additions & 0 deletions src/main/java/org/folio/consortia/service/TenantService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public interface TenantService {

/**
* Inserts single tenant based on consortiumId.
* Method checks whether requesting tenant is soft deleted or new tenant.
* For re-adding soft deleted tenant,
* tenant is_deleted flag will be changed to false and dummy user will be created in mod_users.user-tenants table
* For new tenant, all necessary actions will be done.
*
* @param consortiumId the consortiumId
* @param tenantDto the tenantDto
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package org.folio.consortia.service.impl;

import org.folio.consortia.exception.ResourceAlreadyExistException;
import org.folio.consortia.exception.ResourceNotFoundException;
import org.folio.consortia.repository.ConsortiaConfigurationRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.folio.consortia.domain.dto.ConsortiaConfiguration;
import org.folio.consortia.domain.entity.ConsortiaConfigurationEntity;
import org.folio.consortia.exception.ResourceAlreadyExistException;
import org.folio.consortia.exception.ResourceNotFoundException;
import org.folio.consortia.repository.ConsortiaConfigurationRepository;
import org.folio.consortia.service.ConsortiaConfigurationService;
import org.folio.consortia.utils.TenantContextUtils;
import org.folio.spring.FolioExecutionContext;
import org.springframework.core.convert.ConversionService;
import org.springframework.stereotype.Service;

import java.util.List;

@Log4j2
@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -54,6 +53,10 @@ private ConsortiaConfigurationEntity getConfiguration(String requestTenantId) {
return configList.get(0);
}

public boolean isCentralTenantConfigurationExists() {
return configurationRepository.count() > 0;
}

private void checkAnyConsortiaConfigurationNotExistsOrThrow() {
if (configurationRepository.count() > 0) {
throw new ResourceAlreadyExistException(CONSORTIA_CONFIGURATION_EXIST_MSG_TEMPLATE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package org.folio.consortia.service.impl;

import org.folio.consortia.exception.ResourceAlreadyExistException;
import org.folio.consortia.exception.ResourceNotFoundException;
import org.folio.consortia.repository.ConsortiumRepository;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.folio.consortia.domain.dto.Consortium;
import org.folio.consortia.domain.dto.ConsortiumCollection;
import org.folio.consortia.domain.entity.ConsortiumEntity;
import org.folio.consortia.exception.ResourceAlreadyExistException;
import org.folio.consortia.exception.ResourceNotFoundException;
import org.folio.consortia.repository.ConsortiumRepository;
import org.folio.consortia.service.ConsortiumService;
import org.folio.consortia.utils.HelperUtils;
import org.folio.spring.data.OffsetRequest;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
@Log4j2
@RequiredArgsConstructor
Expand Down Expand Up @@ -57,7 +56,7 @@ public Consortium update(UUID consortiumId, Consortium consortiumDto) {
public ConsortiumCollection getAll() {
var result = new ConsortiumCollection();

Page<ConsortiumEntity> consortiaPage = repository.findAll(PageRequest.of(0, 1));
Page<ConsortiumEntity> consortiaPage = repository.findAll(OffsetRequest.of(0, 1));
result.setConsortia(consortiaPage.stream().map(o -> converter.convert(o, Consortium.class)).toList());
result.setTotalRecords((int) consortiaPage.getTotalElements());
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
import org.folio.consortia.utils.TenantContextUtils;
import org.folio.spring.FolioExecutionContext;
import org.folio.spring.FolioModuleMetadata;
import org.folio.spring.data.OffsetRequest;
import org.folio.spring.scope.FolioExecutionContextSetter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.task.TaskExecutor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -95,7 +95,7 @@ public PublicationDetailsResponse getPublicationDetails(UUID consortiumId, UUID
var publicationStatusEntity = publicationStatusRepository.findById(publicationId)
.orElseThrow(() -> new ResourceNotFoundException(PUBLICATION_ID_FIELD, String.valueOf(publicationId)));

var ptrEntities = publicationTenantRequestRepository.findByPcStateId(publicationId, PageRequest.of(0, Integer.MAX_VALUE));
var ptrEntities = publicationTenantRequestRepository.findByPcStateId(publicationId, OffsetRequest.of(0, Integer.MAX_VALUE));
log.info("getPublicationDetails:: Found {} of {} expected tenant request records", ptrEntities.getTotalElements(), publicationStatusEntity.getTotalRecords());

var errorList = buildErrorListFromPublicationTenantRequestEntities(ptrEntities);
Expand Down Expand Up @@ -312,7 +312,7 @@ public PublicationResultCollection getPublicationResults(UUID consortiumId, UUID
var publicationStatusEntity = publicationStatusRepository.findById(publicationId)
.orElseThrow(() -> new ResourceNotFoundException(PUBLICATION_ID_FIELD, String.valueOf(publicationId)));

var ptrEntities = publicationTenantRequestRepository.findByPcStateId(publicationId, PageRequest.of(0, Integer.MAX_VALUE));
var ptrEntities = publicationTenantRequestRepository.findByPcStateId(publicationId, OffsetRequest.of(0, Integer.MAX_VALUE));
log.info("getPublicationResults:: Found {} of {} expected tenant request records for publication {}", ptrEntities.getTotalElements(), publicationStatusEntity.getTotalRecords(), publicationId);

var resultList = ptrEntities.stream()
Expand Down
Loading

0 comments on commit 71a5a38

Please sign in to comment.