Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat : using blaze MultiSelect View #363

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import org.gradle.plugins.ide.eclipse.model.SourceFolder
buildscript {
repositories {
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}
dependencies {
classpath "org.owasp:dependency-check-gradle:${owasp_plugin_version}"
Expand Down Expand Up @@ -39,6 +40,7 @@ configurations {

repositories {
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}

dependencies {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ spotless_version=6.25.0

jacoco_min_coverage_required=0.84

blaze_persistence_version=1.6.12-SNAPSHOT
spring_boot_version=3.3.1
blaze_persistence_version=1.6.11
spring_dependency_management_version=1.1.5
springdoc_openapi_version=2.6.0

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.learning.mfscreener.repository;

import com.learning.mfscreener.models.entityviews.UserCASDetailsEntityView;

public interface CustomUserCASDetailsEntityRepository {

UserCASDetailsEntityView findByInvestorEmailAndName(String email, String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import org.springframework.stereotype.Repository;

@Repository
public interface UserCASDetailsEntityRepository extends JpaRepository<UserCASDetailsEntity, Long> {
public interface UserCASDetailsEntityRepository
extends JpaRepository<UserCASDetailsEntity, Long>, CustomUserCASDetailsEntityRepository {
rajadilipkolli marked this conversation as resolved.
Show resolved Hide resolved

@Query(
nativeQuery = true,
Expand Down Expand Up @@ -58,11 +59,4 @@ SELECT SUM(balance) AS balanceUnits,
""")
List<PortfolioDetailsProjection> getPortfolioDetails(
@Param("pan") String panNumber, @Param("asOfDate") LocalDate asOfDate);

@Query(
"""
select u from UserCASDetailsEntity u join fetch u.folioEntities join fetch u.investorInfoEntity as i
where i.email = :email and i.name = :name
""")
UserCASDetailsEntity findByInvestorEmailAndName(@Param("email") String email, @Param("name") String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.learning.mfscreener.repository.impl;

import com.blazebit.persistence.CriteriaBuilderFactory;
import com.blazebit.persistence.view.EntityViewManager;
import com.blazebit.persistence.view.EntityViewSetting;
import com.learning.mfscreener.entities.UserCASDetailsEntity;
import com.learning.mfscreener.models.entityviews.UserCASDetailsEntityView;
import com.learning.mfscreener.repository.CustomUserCASDetailsEntityRepository;
import jakarta.persistence.EntityManager;

public class CustomUserCASDetailsEntityRepositoryImpl implements CustomUserCASDetailsEntityRepository {

private final EntityManager entityManager;

private final CriteriaBuilderFactory criteriaBuilderFactory;

private final EntityViewManager entityViewManager;

public CustomUserCASDetailsEntityRepositoryImpl(
EntityManager entityManager,
CriteriaBuilderFactory criteriaBuilderFactory,
EntityViewManager entityViewManager) {
this.entityManager = entityManager;
this.criteriaBuilderFactory = criteriaBuilderFactory;
this.entityViewManager = entityViewManager;
}

@Override
public UserCASDetailsEntityView findByInvestorEmailAndName(String email, String name) {
return entityViewManager
.applySetting(
EntityViewSetting.create(UserCASDetailsEntityView.class),
criteriaBuilderFactory.create(entityManager, UserCASDetailsEntity.class))
.where("investorInfoEntity.email")
.eq(email)
.where("investorInfoEntity.name")
.eq(name) // Adding condition for the name
.getSingleResult();
}
}
30 changes: 17 additions & 13 deletions src/main/java/com/learning/mfscreener/service/PortfolioService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.learning.mfscreener.entities.UserTransactionDetailsEntity;
import com.learning.mfscreener.mapper.CasDetailsMapper;
import com.learning.mfscreener.models.PortfolioDetailsDTO;
import com.learning.mfscreener.models.entityviews.UserCASDetailsEntityView;
import com.learning.mfscreener.models.portfolio.CasDTO;
import com.learning.mfscreener.models.portfolio.UserFolioDTO;
import com.learning.mfscreener.models.portfolio.UserSchemeDTO;
Expand Down Expand Up @@ -133,45 +134,46 @@ UploadResponseHolder findDelta(String email, String name, CasDTO casDTO) {
long userTransactionCount = portfolioServiceHelper.countTransactionsByUserFolioDTOList(inputUserFolioDTOList);
List<UserTransactionDetailsEntity> userTransactionDetailsEntityList =
this.userTransactionDetailsService.findAllTransactionsByEmailAndName(email, name);
UserCASDetailsEntity userCASDetailsEntity = userCASDetailsService.findByInvestorEmailAndName(email, name);
UserCASDetailsEntityView userCASDetailsEntityView =
Copy link
Contributor

@coderabbitai coderabbitai bot Jun 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updates to use UserCASDetailsEntityView are consistent with PR objectives. Ensure completion of TODO items.

Would you like assistance in completing the TODO items mentioned in the code?

Also applies to: 146-146, 153-153, 164-164, 176-176, 338-338

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coderabbitai, please help me with fixing TODO as per above comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rajadilipkolli, I can help you address the TODO items in your code. Here are the suggested changes:

1. Adding Folios to UserCASDetailsEntityView

You have a TODO comment where you need to add folio entities to UserCASDetailsEntityView. Here's how you can do it:

// In the processNewFolios method
userFolioDTOList.forEach(userFolioDTO -> {
    String folio = userFolioDTO.folio();
    if (!existingFolioNumbers.contains(folio)) {
        LOGGER.info("New folio: {} created that is not present in the database", folio);
        userCASDetailsEntityView.addFolioEntity(
            casDetailsMapper.mapUserFolioDTOToUserFolioDetailsEntity(userFolioDTO));
        folioCounter.incrementAndGet();
        int newTransactions = userFolioDTO.schemes().stream()
                .map(UserSchemeDTO::transactions)
                .flatMap(List::stream)
                .toList()
                .size();
        transactionsCounter.addAndGet(newTransactions);
    }
});

2. Bulk Insert for Transactions

For the second TODO, you mentioned converting to bulk insert. This typically involves collecting all entities and using a method that supports batch processing to minimize database hits. Here's a conceptual change:

// Collect all transactions to be inserted
List<UserTransactionDetailsEntity> transactionsToInsert = new ArrayList<>();
userSchemaTransactionMap.forEach((isinFromRequest, requestTransactions) -> {
    List<UserTransactionDetailsEntity> dbTransactions =
            userSchemaTransactionMapFromDB.getOrDefault(isinFromRequest, List.of());
    if (requestTransactions.size() != dbTransactions.size()) {
        requestTransactions.forEach(userTransactionDTO -> {
            LocalDate newTransactionDate = userTransactionDTO.date();
            if (!transactionDateListDB.contains(newTransactionDate)) {
                UserTransactionDetailsEntity userTransactionDetailsEntity =
                        casDetailsMapper.transactionDTOToTransactionEntity(userTransactionDTO);
                transactionsToInsert.add(userTransactionDetailsEntity);
            }
        });
    }
});

// Use a method to bulk insert
userTransactionDetailsService.bulkInsert(transactionsToInsert);

Make sure to implement or update the bulkInsert method in your UserTransactionDetailsService to handle the batch insert operation efficiently.

These changes should help you complete the TODO items and improve the efficiency of your application. Let me know if you need further assistance!

userCASDetailsService.findByInvestorEmailAndName(email, name);

if (userTransactionCount == userTransactionDetailsEntityList.size()) {
LOGGER.info("No new transactions are added");
return null;
}

return processFoliosAndTransactions(
email, name, casDTO, userCASDetailsEntity, userTransactionCount, userTransactionDetailsEntityList);
email, name, casDTO, userCASDetailsEntityView, userTransactionCount, userTransactionDetailsEntityList);
}

UploadResponseHolder processFoliosAndTransactions(
String email,
String name,
CasDTO casDTO,
UserCASDetailsEntity userCASDetailsEntity,
UserCASDetailsEntityView userCASDetailsEntityView,
long userTransactionDTOListCount,
List<UserTransactionDetailsEntity> userTransactionDetailsEntityList) {
AtomicInteger folioCounter = new AtomicInteger();
AtomicInteger transactionsCounter = new AtomicInteger();

processNewFolios(casDTO.folios(), userCASDetailsEntity, folioCounter, transactionsCounter);
processNewFolios(casDTO.folios(), userCASDetailsEntityView, folioCounter, transactionsCounter);
updateSchemesAndTransactions(
email,
name,
casDTO,
userCASDetailsEntity,
userCASDetailsEntityView,
userTransactionDTOListCount,
userTransactionDetailsEntityList,
transactionsCounter);

return new UploadResponseHolder(userCASDetailsEntity, folioCounter.get(), transactionsCounter.get());
return new UploadResponseHolder(null, folioCounter.get(), transactionsCounter.get());
}

void updateSchemesAndTransactions(
String email,
String name,
CasDTO casDTO,
UserCASDetailsEntity userCASDetailsEntity,
UserCASDetailsEntityView userCASDetailsEntityView,
long userTransactionDTOListCount,
List<UserTransactionDetailsEntity> userTransactionDetailsEntityList,
AtomicInteger transactionsCounter) {
Expand All @@ -196,7 +198,8 @@ void updateSchemesAndTransactions(
existingFolioSchemesMap,
existingUserFolioDetailsEntityList,
transactionsCounter);
userCASDetailsEntity.setFolioEntities(existingUserFolioDetailsEntityList);
// TODO
// userCASDetailsEntityView.setFolioEntities(existingUserFolioDetailsEntityList);

// Check if all new transactions are added as part of adding schemes
if (userTransactionDTOListCount == (userTransactionDetailsEntityList.size() + transactionsCounter.get())) {
Expand Down Expand Up @@ -332,20 +335,21 @@ Map<String, List<UserSchemeDTO>> groupSchemesByFolio(List<UserFolioDTO> userFoli

void processNewFolios(
List<UserFolioDTO> userFolioDTOList,
UserCASDetailsEntity userCASDetailsEntity,
UserCASDetailsEntityView userCASDetailsEntityView,
AtomicInteger folioCounter,
AtomicInteger transactionsCounter) {
// Logic to process new folios
List<String> existingFolioNumbers = userCASDetailsEntity.getFolioEntities().stream()
.map(UserFolioDetailsEntity::getFolio)
List<String> existingFolioNumbers = userCASDetailsEntityView.getFolioEntities().stream()
.map(t -> t.getFolio())
.toList();

userFolioDTOList.forEach(userFolioDTO -> {
String folio = userFolioDTO.folio();
if (!existingFolioNumbers.contains(folio)) {
LOGGER.info("New folio: {} created that is not present in the database", folio);
userCASDetailsEntity.addFolioEntity(
casDetailsMapper.mapUserFolioDTOToUserFolioDetailsEntity(userFolioDTO));
// TODO
// userCASDetailsEntityView.addFolioEntity(
// casDetailsMapper.mapUserFolioDTOToUserFolioDetailsEntity(userFolioDTO));
folioCounter.incrementAndGet();
int newTransactions = userFolioDTO.schemes().stream()
.map(UserSchemeDTO::transactions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.learning.mfscreener.config.logging.Loggable;
import com.learning.mfscreener.entities.UserCASDetailsEntity;
import com.learning.mfscreener.models.entityviews.UserCASDetailsEntityView;
import com.learning.mfscreener.models.projection.PortfolioDetailsProjection;
import com.learning.mfscreener.repository.UserCASDetailsEntityRepository;
import java.time.LocalDate;
Expand All @@ -25,7 +26,7 @@ public UserCASDetailsEntity saveEntity(UserCASDetailsEntity casDetailsEntity) {
return userCASDetailsEntityRepository.save(casDetailsEntity);
}

public UserCASDetailsEntity findByInvestorEmailAndName(String email, String name) {
public UserCASDetailsEntityView findByInvestorEmailAndName(String email, String name) {
return userCASDetailsEntityRepository.findByInvestorEmailAndName(email, name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

import static org.assertj.core.api.Assertions.assertThat;

import com.learning.mfscreener.config.BlazePersistenceConfiguration;
import jakarta.persistence.EntityManager;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;

@DataJpaTest(
properties = {
"spring.jpa.hibernate.ddl-auto=validate",
"spring.test.database.replace=none",
"spring.datasource.url=jdbc:tc:postgresql:16-alpine:///db"
})
@Import(BlazePersistenceConfiguration.class)
class SchemaValidationPostgresTest {

@Autowired
Expand Down
Loading