diff --git a/src/main/java/net/gepafin/tendermanagement/constants/GepafinConstant.java b/src/main/java/net/gepafin/tendermanagement/constants/GepafinConstant.java index 885cb2b5..9b37da79 100644 --- a/src/main/java/net/gepafin/tendermanagement/constants/GepafinConstant.java +++ b/src/main/java/net/gepafin/tendermanagement/constants/GepafinConstant.java @@ -503,6 +503,8 @@ public class GepafinConstant { public static final String APPLICATION="application"; public static final String APPLICATION_ID="applicationId"; + public static final String USER_WITH_COMPANY_ID="userWithCompanyId"; + } diff --git a/src/main/java/net/gepafin/tendermanagement/dao/ApplicationDao.java b/src/main/java/net/gepafin/tendermanagement/dao/ApplicationDao.java index df638fa8..75fffc97 100644 --- a/src/main/java/net/gepafin/tendermanagement/dao/ApplicationDao.java +++ b/src/main/java/net/gepafin/tendermanagement/dao/ApplicationDao.java @@ -129,8 +129,6 @@ public class ApplicationDao { @Value("${rinaldo_email}") private String rinaldoEmail; - @Value("${carlo_email}") - private String carloEmail; @Value("${call.id}") private String callId; @@ -195,6 +193,9 @@ public class ApplicationDao { @Autowired private ApplicationEvaluationRepository applicationEvaluationRepository; + @Autowired + private ApplicationViewRepository applicationViewRepository; + public ApplicationResponseBean createApplication(HttpServletRequest request, ApplicationRequestBean applicationRequestBean, Long formId, Long applicationId) { FormEntity formEntity = formService.validateForm(formId); @@ -1632,7 +1633,7 @@ public class ApplicationDao { MatchModeEnum matchMode = filterCriteria.getMatchMode(); if (value != null && matchMode != null) { - Path fieldPath = getFieldPath(root, fieldName); + Path fieldPath = root.get(fieldName); if (fieldPath != null) { Utils.applyStringFilter(fieldPath, criteriaBuilder, predicates, value, matchMode); Utils.applyNumberFilter(fieldPath, criteriaBuilder, predicates, value, matchMode); @@ -1646,21 +1647,21 @@ public class ApplicationDao { - private Path getFieldPath(Root root, String fieldName) { - try { - return switch (fieldName) { - case GepafinConstant.CALL_ID -> root.get(GepafinConstant.CALL).get(GepafinConstant.ID); - case GepafinConstant.CALL_TITLE -> root.get(GepafinConstant.CALL).get(GepafinConstant.NAME); - case GepafinConstant.CALL_END_DATE -> root.get(GepafinConstant.CALL).get(GepafinConstant.END_DATE); - case GepafinConstant.CALL_END_TIME -> root.get(GepafinConstant.CALL).get(GepafinConstant.END_TIME); - case GepafinConstant.MODIFIED_DATE -> root.get(GepafinConstant.CALL).get(GepafinConstant.UPDATED_DATE); - case GepafinConstant.PROTOCOL_NUMBER-> root.get(GepafinConstant.PROTOCOL).get(GepafinConstant.PROTOCOL_NUMBER); - default -> root.get(fieldName); - }; - } catch (IllegalArgumentException e) { - return null; - } - } +// private Path getFieldPath(Root root, String fieldName) { +// try { +// return switch (fieldName) { +//// case GepafinConstant.CALL_ID -> root.get(GepafinConstant.CALL).get(GepafinConstant.ID); +//// case GepafinConstant.CALL_TITLE -> root.get(GepafinConstant.CALL).get(GepafinConstant.NAME); +//// case GepafinConstant.CALL_END_DATE -> root.get(GepafinConstant.CALL).get(GepafinConstant.END_DATE); +//// case GepafinConstant.CALL_END_TIME -> root.get(GepafinConstant.CALL).get(GepafinConstant.END_TIME); +//// case GepafinConstant.MODIFIED_DATE -> root.get(GepafinConstant.CALL).get(GepafinConstant.UPDATED_DATE); +//// case GepafinConstant.PROTOCOL_NUMBER-> root.get(GepafinConstant.PROTOCOL).get(GepafinConstant.PROTOCOL_NUMBER); +// default -> root.get(fieldName); +// }; +// } catch (IllegalArgumentException e) { +// return null; +// } +// } public void checkCallEndDate(CallEntity call) { @@ -1816,4 +1817,199 @@ public class ApplicationDao { } return application; } + public PageableResponseBean> getAllApplicationByPaginationByView(UserEntity userEntity, Long callId, Long companyId, ApplicationPageableRequestBean applicationPageableRequestBean) { + Integer pageNo = null; + Integer pageLimit = null; + + UserWithCompanyEntity userWithCompany= userWithCompanyRepository.findByUserIdAndCompanyIdAndIsDeletedFalse(userEntity.getId(), companyId).orElse(null); + Long userWithCompanyId = userWithCompany!=null?userWithCompany.getId():null; + if (applicationPageableRequestBean.getGlobalFilters() != null) { + pageNo = applicationPageableRequestBean.getGlobalFilters().getPage(); + pageLimit = applicationPageableRequestBean.getGlobalFilters().getLimit(); + } + if (pageLimit == null || pageLimit <= 0) { + pageLimit = GepafinConstant.DEFAULT_PAGE_LIMIT; + } + if (pageNo == null || pageNo <= 0) { + pageNo = GepafinConstant.DEFAULT_PAGE; + } + Specification spec = searchByView(callId,companyId, userWithCompanyId, applicationPageableRequestBean, userEntity); + Page entityPage = applicationViewRepository.findAll(spec, PageRequest.of(pageNo - 1, pageLimit)); + // Prepare the response + + + List applicationResponses = entityPage.getContent().stream() + .map(application -> { + ApplicationResponse response = getApplicationResponseByView(application); + return response; + }) + .collect(Collectors.toList()); + + + PageableResponseBean> pageableResponseBean = new PageableResponseBean<>(); + pageableResponseBean.setBody(applicationResponses); + pageableResponseBean.setCurrentPage(entityPage.getNumber() + 1); // Page numbers typically start from 0, so add 1 for user-friendly indexing + pageableResponseBean.setTotalPages(entityPage.getTotalPages()); + pageableResponseBean.setTotalRecords(entityPage.getTotalElements()); + pageableResponseBean.setPageSize(entityPage.getSize()); + + return pageableResponseBean; + } + + public Specification searchByView(Long callId, Long companyId, Long userWithCompanyId, ApplicationPageableRequestBean applicationPageableRequestBean, UserEntity userEntity) { + return (root, query, criteriaBuilder) -> { + + List predicates = getPredicatesByView(applicationPageableRequestBean, criteriaBuilder, root, callId,companyId, userWithCompanyId, userEntity); + SortBy sortBy = new SortBy(GepafinConstant.CREATED_DATE, true); + + if (applicationPageableRequestBean.getGlobalFilters() != null + && applicationPageableRequestBean.getGlobalFilters().getSortBy() != null && + applicationPageableRequestBean.getGlobalFilters().getSortBy().getColumnName() != null && Boolean.FALSE.equals( + isEmpty(applicationPageableRequestBean.getGlobalFilters().getSortBy().getColumnName()))) { + sortBy.setColumnName(applicationPageableRequestBean.getGlobalFilters().getSortBy().getColumnName()); + sortBy.setSortDesc(true); + if (applicationPageableRequestBean.getGlobalFilters().getSortBy().getSortDesc() != null) { + sortBy.setSortDesc(applicationPageableRequestBean.getGlobalFilters().getSortBy().getSortDesc()); + } + } + + query.orderBy(criteriaBuilder.desc(root.get(sortBy.getColumnName()))); + if (Boolean.FALSE.equals(sortBy.getSortDesc())) { + query.orderBy(criteriaBuilder.asc(root.get(sortBy.getColumnName()))); + } + return query.where(criteriaBuilder.and(predicates.toArray(new Predicate[0]))).getRestriction(); + }; + } + + + private List getPredicatesByView(ApplicationPageableRequestBean applicationPageableRequestBean, + CriteriaBuilder criteriaBuilder, Root root, Long callId,Long companyId, Long userWithCompanyId, UserEntity userEntity) { + + Integer year = null; + String search = null; + Integer daysRange = null; + Map filters = new HashMap<>(); + if (applicationPageableRequestBean.getGlobalFilters() != null) { + year = applicationPageableRequestBean.getGlobalFilters().getYear(); + search = applicationPageableRequestBean.getGlobalFilters().getSearch(); + daysRange = applicationPageableRequestBean.getDaysRange(); + } + if (applicationPageableRequestBean.getFilters() != null) { + filters = applicationPageableRequestBean.getFilters(); + } + List predicates = new ArrayList<>(); + +// Boolean isBeneficiary = validator.checkIsBeneficiary(); + if (Boolean.TRUE.equals(validator.checkIsBeneficiary()) || Boolean.TRUE.equals(validator.checkIsConfidi())) { + predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.USER_ID), userEntity.getId())); + } + if (year != null && year > 0) { + int filterYear = applicationPageableRequestBean.getGlobalFilters().getYear(); + +// Create LocalDateTime boundaries for the start and end of the year + LocalDateTime startOfYear = LocalDateTime.of(filterYear, 1, 1, 0, 0); + LocalDateTime endOfYear = LocalDateTime.of(filterYear, 12, 31, 23, 59, 59); + +// Add the range comparison to filter records within the year + predicates.add(criteriaBuilder.between(root.get(GepafinConstant.CREATED_DATE), startOfYear, endOfYear)); + + } + // Search in `title` and `message` (if search term is provided) + if (search != null && !search.isEmpty()) { + Predicate titlePredicate = criteriaBuilder.like( + criteriaBuilder.upper(root.get(GepafinConstant.COMMENTS)), + "%" + search.toUpperCase() + "%" + ); +// Predicate protocolPredicate = criteriaBuilder.like( +// criteriaBuilder.function( +// "TO_CHAR", +// String.class, +// criteriaBuilder.function("CAST", String.class, root.get(GepafinConstant.PROTOCOL).get(GepafinConstant.PROTOCOL_NUMBER)) +// ), +// "%" + search + "%" +// ); + + Predicate callNamePredicate =criteriaBuilder.like( + criteriaBuilder.upper(root.get(GepafinConstant.CALL_TITLE)), // Adjust field name + "%" + search.toUpperCase() + "%" + ); + +// predicates.add(criteriaBuilder.or(protocolPredicate)); + predicates.add(criteriaBuilder.or(callNamePredicate)); + predicates.add(criteriaBuilder.or(titlePredicate)); + } + + // Filter by `status` (if status list is provided) + if (applicationPageableRequestBean.getStatus() != null && !applicationPageableRequestBean.getStatus().isEmpty()) { + List statusValues = applicationPageableRequestBean.getStatus().stream() + .map(ApplicationStatusTypeEnum::name) // Convert enum to string + .toList(); + predicates.add(root.get(GepafinConstant.STATUS).in(statusValues)); + } + + if (callId != null) { + CallEntity call = callService.validateCall(callId); + predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.CALL_ID), callId)); + } + + // Optional companyId filter + if (userWithCompanyId != null) { + predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.USER_WITH_COMPANY_ID), userWithCompanyId)); + } + if (companyId != null) { + predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.COMPANY_ID), companyId)); + } + + if (daysRange != null && daysRange >= 0) { + LocalDateTime today = LocalDateTime.now(); + LocalDateTime pastDate = today.minusDays(daysRange); + predicates.add(criteriaBuilder.between(root.get(GepafinConstant.CREATED_DATE), pastDate, today)); + } + applyFilters(root, criteriaBuilder, predicates, filters); + + predicates.add(criteriaBuilder.isFalse(root.get(GepafinConstant.IS_DELETED))); + + predicates.add(criteriaBuilder.equal(root.get(GepafinConstant.HUB_ID), userEntity.getHub().getId())); + + + return predicates; + } + + private ApplicationResponse getApplicationResponseByView(ApplicationView applicationView) { + ApplicationResponse responseBean = new ApplicationResponse(); + List flowEdgesList = flowEdgesRepository.findByCallId(applicationView.getCallId()); + Long totalFormSteps = flowFormDao.calculateTotalSteps(flowEdgesList); + Long completedSteps= Long.valueOf(flowFormDao.getCompletedStepsByView(applicationView.getId(), false)); + Integer progress = calculateProgress(totalFormSteps, completedSteps); + responseBean.setId(applicationView.getId()); + responseBean.setProgress(progress); + responseBean.setCallTitle(applicationView.getCallTitle()); + responseBean.setCallEndDate(applicationView.getCallEndDate()); + responseBean.setCallEndTime(applicationView.getCallEndTime()); + responseBean.setModifiedDate(applicationView.getModifiedDate()); + responseBean.setCallId(applicationView.getCallId()); + responseBean.setSubmissionDate(applicationView.getSubmissionDate()); + responseBean.setStatus(applicationView.getStatus()); + responseBean.setEvaluationVersion(EvaluationVersionEnum.valueOf(applicationView.getEvaluationVersion())); + responseBean.setComments(applicationView.getComments()); + responseBean.setCompanyId(applicationView.getCompanyId()); + Optional assignedApplicationsOptional = + assignedApplicationsRepository.findByApplicationIdAndIsDeletedFalse(applicationView.getId()); + if(assignedApplicationsOptional.isPresent()){ + responseBean.setAssignedUserId(assignedApplicationsOptional.get().getUserId()); + UserEntity user = userService.validateUser(assignedApplicationsOptional.get().getUserId()); + String firstName = user.getFirstName() != null ? user.getFirstName() : ""; + String lastName = user.getLastName() != null ? user.getLastName() : ""; + String userName = String.join(" ", firstName, lastName).trim(); + responseBean.setAssignedUserName(userName); + } + CompanyEntity company=companyService.validateCompany(applicationView.getCompanyId()); + responseBean.setCompanyName(company.getCompanyName()); + responseBean.setProtocolNumber(applicationView.getProtocolNumber()); + responseBean.setAmountAccepted(applicationView.getAmountAccepted()); + responseBean.setAmountRequested(applicationView.getAmountRequested()); + responseBean.setDateAccepted(applicationView.getDateAccepted()); + responseBean.setDateRejected(applicationView.getDateRejected()); + return responseBean; + } } diff --git a/src/main/java/net/gepafin/tendermanagement/dao/FlowFormDao.java b/src/main/java/net/gepafin/tendermanagement/dao/FlowFormDao.java index 2287850d..e516f23b 100644 --- a/src/main/java/net/gepafin/tendermanagement/dao/FlowFormDao.java +++ b/src/main/java/net/gepafin/tendermanagement/dao/FlowFormDao.java @@ -368,6 +368,18 @@ public class FlowFormDao { } return getNextForm(currentFormEntity, applicationEntity); } - + public Integer getCompletedStepsByView(Long applicationId, Boolean isSendValidationError) { + Integer completedSteps=0; + List applicationFormList = applicationFormRepository.findByApplicationId(applicationId); + List applicationFormFieldEntities=new ArrayList<>(); + for (ApplicationFormEntity applicationFormEntity:applicationFormList){ + applicationFormFieldEntities=applicationFormFieldRepository.findByApplicationFormId(applicationFormEntity.getId()); + Boolean isCompleted=formDao.validateCompletedSteps(applicationFormFieldEntities, null, applicationFormEntity.getForm(), isSendValidationError); + if(Boolean.TRUE.equals(isCompleted)){ + completedSteps++; + } + } + return completedSteps; + } } diff --git a/src/main/java/net/gepafin/tendermanagement/entities/ApplicationView.java b/src/main/java/net/gepafin/tendermanagement/entities/ApplicationView.java new file mode 100644 index 00000000..ec9c0965 --- /dev/null +++ b/src/main/java/net/gepafin/tendermanagement/entities/ApplicationView.java @@ -0,0 +1,95 @@ +package net.gepafin.tendermanagement.entities; + +import jakarta.persistence.*; +import lombok.Getter; +import lombok.Setter; +import org.hibernate.annotations.Immutable; + +import java.io.Serializable; +import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.time.LocalTime; + +@Entity +@Immutable +@Table(name = "application_view") +@Getter +@Setter +@IdClass(ApplicationViewId.class) +public class ApplicationView implements Serializable { + + @Id + @Column(name = "ID") + private Long id; + + @Column(name = "STATUS") + private String status; + + @Column(name = "IS_DELETED") + private Boolean isDeleted; + + @Column(name = "USER_ID") + private Long userId; + + @Column(name = "SUBMISSION_DATE") + private LocalDateTime submissionDate; + + @Column(name ="COMMENTS") + private String comments; + + @Column(name = "AMOUNT_REQUESTED") + private BigDecimal amountRequested; + + @Column(name = "AMOUNT_ACCEPTED") + private BigDecimal amountAccepted; + + @Column(name = "DATE_ACCEPTED") + private LocalDateTime dateAccepted; + + @Column(name = "DATE_REJECTED") + private LocalDateTime dateRejected; + + @Column(name = "EVALUATION_VERSION") + private String evaluationVersion; + + @Column(name = "CALL_ID") + private Long callId; + + @Column(name = "CALL_TITLE") + private String callTitle; + + @Column(name = "CALL_END_DATE") + private LocalDateTime callEndDate; + + @Column(name = "CALL_END_TIME") + private LocalTime callEndTime; + + @Column(name = "MODIFIED_DATE") + private LocalDateTime modifiedDate; + + @Column(name = "COMPANY_ID") + private Long companyId; + + @Column(name = "USER_WITH_COMPANY_ID") + private Long userWithCompanyId; + + @Column(name = "COMPANY_NAME") + private String companyName; + + @Column(name = "PROTOCOL_NUMBER") + private Long protocolNumber; + + @Column(name = "ASSIGNED_USER_ID") + private Long assigned_user_id; + + @Column(name = "ASSIGNED_USER_NAME") + private String assignedUserName; + + @Column(name = "HUB_ID") + private Long hubId; + + @Column(name = "CREATED_DATE") + private LocalDateTime createdDate; + + +} diff --git a/src/main/java/net/gepafin/tendermanagement/entities/ApplicationViewId.java b/src/main/java/net/gepafin/tendermanagement/entities/ApplicationViewId.java new file mode 100644 index 00000000..f16ca359 --- /dev/null +++ b/src/main/java/net/gepafin/tendermanagement/entities/ApplicationViewId.java @@ -0,0 +1,17 @@ +package net.gepafin.tendermanagement.entities; + +import lombok.Data; + +@Data +public class ApplicationViewId { + + private static final long serialVersionUID = 1L; + private Long id; + + public ApplicationViewId() { + } + + public ApplicationViewId(Long id) { + this.id = id; + } +} diff --git a/src/main/java/net/gepafin/tendermanagement/repositories/ApplicationViewRepository.java b/src/main/java/net/gepafin/tendermanagement/repositories/ApplicationViewRepository.java new file mode 100644 index 00000000..da641685 --- /dev/null +++ b/src/main/java/net/gepafin/tendermanagement/repositories/ApplicationViewRepository.java @@ -0,0 +1,8 @@ +package net.gepafin.tendermanagement.repositories; + +import net.gepafin.tendermanagement.entities.ApplicationView; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; + +public interface ApplicationViewRepository extends JpaRepository , JpaSpecificationExecutor { +} diff --git a/src/main/java/net/gepafin/tendermanagement/service/impl/ApplicationServiceImpl.java b/src/main/java/net/gepafin/tendermanagement/service/impl/ApplicationServiceImpl.java index 8426390e..70ed5794 100644 --- a/src/main/java/net/gepafin/tendermanagement/service/impl/ApplicationServiceImpl.java +++ b/src/main/java/net/gepafin/tendermanagement/service/impl/ApplicationServiceImpl.java @@ -144,7 +144,7 @@ public class ApplicationServiceImpl implements ApplicationService { if (companyId != null) { validator.validateUserWithCompany(request, companyId); } - return applicationDao.getAllApplicationByPagination(userEntity,callId,companyId,applicationPageableRequestBean); + return applicationDao.getAllApplicationByPaginationByView(userEntity,callId,companyId,applicationPageableRequestBean); } @Override diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index fc0e08ce..db8e289f 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -14,7 +14,6 @@ isPecServiceEnabled = false #default_System_Receiver_Email=antonio.manca@bflows.net gepafin_email=rinaldo.bonazzo@bflows.net rinaldo_email=rinaldo.bonazzo@bflows.net -carlo_email=test@test.test default.hub.uuid=p4lk3bcx1RStqTaIVVbXs #Login to Odessa, Appointment Creation, Upload document Configuration diff --git a/src/main/resources/application-local.properties b/src/main/resources/application-local.properties index b67592dc..0e5ea519 100644 --- a/src/main/resources/application-local.properties +++ b/src/main/resources/application-local.properties @@ -13,7 +13,6 @@ isPecServiceEnabled = false #default_System_Receiver_Email=test@test.test gepafin_email=test@test.test rinaldo_email=test@test.test -carlo_email=test@test.test default.hub.uuid=p4lk3bcx1RStqTaIVVbXs appointment.base.url=https://demo.galileonetwork.it/gateway/rest diff --git a/src/main/resources/application-production.properties b/src/main/resources/application-production.properties index 30cb6bee..0774a786 100644 --- a/src/main/resources/application-production.properties +++ b/src/main/resources/application-production.properties @@ -20,7 +20,6 @@ isPecServiceEnabled = true #default_System_Receiver_Email=m.gaudino@gepafin.it,f.marinelli@gepafin.it gepafin_email=bandi@pec.gepafin.it rinaldo_email=rinaldo.bonazzo@bflows.net -carlo_email=carlo.mancosu@bflows.net default.hub.uuid=p4lk3bcx1RStqTaIVVbXs # TEST DEPLOY Configuration diff --git a/src/main/resources/application-testing.properties b/src/main/resources/application-testing.properties index c8e93e85..70239904 100644 --- a/src/main/resources/application-testing.properties +++ b/src/main/resources/application-testing.properties @@ -11,7 +11,6 @@ isPecServiceEnabled = false #default_System_Receiver_Email=test@test.test gepafin_email=test@test.test rinaldo_email=test@test.test -carlo_email=test@test.test default.hub.uuid=p4lk3bcx1RStqTaIVVbXs appointment.base.url=https://demo.galileonetwork.it/gateway/rest diff --git a/src/main/resources/db/changelog/db.changelog-1.0.0.xml b/src/main/resources/db/changelog/db.changelog-1.0.0.xml index 19a44408..a692a560 100644 --- a/src/main/resources/db/changelog/db.changelog-1.0.0.xml +++ b/src/main/resources/db/changelog/db.changelog-1.0.0.xml @@ -2675,6 +2675,10 @@ + + + diff --git a/src/main/resources/db/changelog/dynamic-triggers.xml b/src/main/resources/db/changelog/dynamic-triggers.xml index 9356279d..fbe5b5fe 100644 --- a/src/main/resources/db/changelog/dynamic-triggers.xml +++ b/src/main/resources/db/changelog/dynamic-triggers.xml @@ -13,10 +13,13 @@ BEGIN -- Loop through all tables in the schema that have the 'updated_date' column FOR r IN ( - SELECT table_name - FROM information_schema.columns - WHERE column_name = 'updated_date' - AND table_schema = 'gepafin_schema' + SELECT c.table_name + FROM information_schema.columns c + JOIN information_schema.tables t + ON c.table_name = t.table_name AND c.table_schema = t.table_schema + WHERE c.column_name = 'updated_date' + AND c.table_schema = 'gepafin_schema' + AND t.table_type = 'BASE TABLE' -- Excludes views ) LOOP EXECUTE format( @@ -30,10 +33,13 @@ -- Loop through all tables in the schema that have the 'created_date' column FOR r IN ( - SELECT table_name - FROM information_schema.columns - WHERE column_name = 'created_date' - AND table_schema = 'gepafin_schema' + SELECT c.table_name + FROM information_schema.columns c + JOIN information_schema.tables t + ON c.table_name = t.table_name AND c.table_schema = t.table_schema + WHERE c.column_name = 'created_date' + AND c.table_schema = 'gepafin_schema' + AND t.table_type = 'BASE TABLE' -- Excludes views ) LOOP EXECUTE format( diff --git a/src/main/resources/db/dump/create_application_view.sql b/src/main/resources/db/dump/create_application_view.sql new file mode 100644 index 00000000..e232f895 --- /dev/null +++ b/src/main/resources/db/dump/create_application_view.sql @@ -0,0 +1,81 @@ +CREATE OR REPLACE VIEW application_view AS + +SELECT + a.id, + a.status, + a.submission_date, + a.comments, + a.amount_requested, + a.amount_accepted, + a.date_accepted, + a.date_rejected, + a.is_deleted, + a.hub_id, + a.user_id, + a.evaluation_version AS evaluation_version, + a.updated_date AS modified_date, + a.created_date AS created_date, + + + + -- Call Details + a.call_id AS call_id, + cl.name AS call_title, + cl.end_date AS call_end_date, + cl.end_time AS call_end_time, + + -- Company Details + a.COMPANY_ID AS company_id, + c.company_name AS company_name, + + -- Protocol Details + p.protocol_number AS protocol_number, + + + -- Assigned User Details from ASSIGNED_APPLICATION and GEPPAFIN_USER + COALESCE(aa.user_id, NULL) AS assigned_user_id, + COALESCE( + NULLIF( + TRIM(CONCAT( + COALESCE(u.first_name, ''), ' ', + COALESCE(u.last_name, '') + )), + '' + ), + '' + ) AS assigned_user_name, + +-- User with Company Details (From Application's User) + COALESCE(uwc.id, NULL) AS user_with_company_id + + +FROM gepafin_schema.APPLICATION a + +-- Join Call Entity +LEFT JOIN gepafin_schema.CALL cl + ON a.CALL_ID = cl.id + +-- Join Company Entity (Ensuring it is not deleted) +LEFT JOIN gepafin_schema.COMPANY c + ON a.COMPANY_ID = c.id + +-- Join Protocol Entity +LEFT JOIN gepafin_schema.PROTOCOL p + ON a.PROTOCOL_NUMBER = p.id + +-- Join Assigned Application Entity (Ensuring it is not deleted) +LEFT JOIN gepafin_schema.assigned_applications aa + ON a.id = aa.APPLICATION_ID + AND (aa.IS_DELETED IS FALSE OR aa.IS_DELETED IS NULL) + + -- Join User Entity (Get First & Last Name Combined) +LEFT JOIN gepafin_schema.GEPAFIN_USER u + ON aa.user_id = u.id + + -- Get User With Company ID (From Application's User & Company) + LEFT JOIN gepafin_schema.USER_WITH_COMPANY uwc + ON a.user_id = uwc.user_id + AND a.COMPANY_ID = uwc.company_id + AND uwc.is_deleted = FALSE -- Ensuring the user is active + +WHERE a.IS_DELETED IS FALSE OR a.IS_DELETED IS NULL; \ No newline at end of file