Skip to content

Commit

Permalink
story(ccls-2289) cleardown opa assessment log
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilDigitalJustice committed Sep 17, 2024
1 parent cb01b4a commit 2d1b8da
Show file tree
Hide file tree
Showing 8 changed files with 412 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,16 @@ CREATE TABLE XXCCMS_OPA_CHECKPOINT (
CONSTRAINT PK_OPA_CHECKPOINT PRIMARY KEY (RESUME_ID)
);

create sequence XXCCMS_OPA_GENERATED_ID_S;
create sequence XXCCMS_OPA_GENERATED_ID_S;

CREATE TABLE XXCCMS_OPA_ASSESSMENT_LOG (
ID NUMBER(19,0) NOT NULL,
TARGET_ID VARCHAR2(360) NOT NULL,
ASSESSMENT VARCHAR2(36),
OWNER_ID VARCHAR2(36),
ACTION VARCHAR2(36),
TTL VARCHAR2(200),
ASSESSMENT_DATA CLOB,
CREATED TIMESTAMP (6) NOT NULL,
CREATED_BY VARCHAR2(50)
);
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ DROP TABLE XXCCMS_OPA_ATTRIBUTE;
DROP TABLE XXCCMS_OPA_ENTITY;
DROP TABLE XXCCMS_OPA_LISTENTITY;
DROP TABLE XXCCMS_OPA_CHECKPOINT;
DROP TABLE XXCCMS_OPA_SESSION;
DROP TABLE XXCCMS_OPA_SESSION;
DROP TABLE XXCCMS_OPA_ASSESSMENT_LOG;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-- Insert into XXCCMS_OPA_SESSION
INSERT INTO XXCCMS_OPA_SESSION (ID, OWNER_ID, TARGET_ID, ASSESSMENT, STATUS, CREATED, CREATED_BY, MODIFIED, MODIFIED_BY)
VALUES(26, 'owner1', '1234567890', 'assessment1', 'status1', TIMESTAMP '2024-05-13 13:17:06.218000', 'creator1', TIMESTAMP '2024-05-13 13:17:06.218000', 'modifier1');
VALUES (26, 'owner1', '1234567890', 'meansAssessment', 'status1', TIMESTAMP '2024-05-13 13:17:06.218000', 'creator1', TIMESTAMP '2024-05-13 13:17:06.218000', 'modifier1');

-- Insert into XXCCMS_OPA_LISTENTITY
INSERT INTO XXCCMS_OPA_LISTENTITY (ID, FK_OPA_SESSION, ENTITY_TYPE)
Expand All @@ -21,7 +22,110 @@ VALUES (30, 28, 'name1', 1);
INSERT INTO XXCCMS_OPA_RELSHIPTARGET (ID, TARGET_ENTITY_ID)
VALUES (31, 'entityId1');

-- Insert into XXCCMS_OPA_CHECKPOINT
INSERT INTO XXCCMS_OPA_CHECKPOINT (RESUME_ID, USER_NAME, INTERVIEW_DATA)
VALUES (26, 'john.doe', hextoraw('48656c6c6f20776f726c64'));

-- Insert into XXCCMS_OPA_ASSESSMENT_LOG for MEANS assessment
INSERT INTO XXCCMS_OPA_ASSESSMENT_LOG (ID, TARGET_ID, ASSESSMENT, OWNER_ID, ACTION, TTL, ASSESSMENT_DATA, CREATED, CREATED_BY)
VALUES (1, '1234567890', 'MEANS', 'owner1', 'Action_001', 'Time to live 1', 'Sample assessment data 1', SYSTIMESTAMP, 'User_001');

-- Insert into XXCCMS_OPA_SESSION for MERITS
INSERT INTO XXCCMS_OPA_SESSION (ID, OWNER_ID, TARGET_ID, ASSESSMENT, STATUS, CREATED, CREATED_BY, MODIFIED, MODIFIED_BY)
VALUES (32, 'owner2', '1234567891', 'meritsAssessment', 'status2', TIMESTAMP '2024-05-13 13:20:06.218000', 'creator2', TIMESTAMP '2024-05-13 13:20:06.218000', 'modifier2');

-- Insert into XXCCMS_OPA_LISTENTITY for MERITS
INSERT INTO XXCCMS_OPA_LISTENTITY (ID, FK_OPA_SESSION, ENTITY_TYPE)
VALUES (33, 32, 'entityType2');

-- Insert into XXCCMS_OPA_ENTITY for MERITS
INSERT INTO XXCCMS_OPA_ENTITY (ID, FK_OPA_LIST_ENTITY, FK_OPA_SESSION, ENTITY_ID, PREPOPULATED, COMPLETED)
VALUES (34, 33, 32, 'entityId2', 0, 0);

-- Insert into XXCCMS_OPA_ATTRIBUTE for MERITS
INSERT INTO XXCCMS_OPA_ATTRIBUTE (ID, FK_OPA_ENTITY, ATTRIBUTE_ID, ATTRIBUTE_TYPE, ATTRIBUTE_VALUE, INFERENCING_TYPE, PREPOPULATED, ASKED)
VALUES (35, 34, 'OPPONENT_DOB', 'text', null, null, 0, 0);

-- Insert into XXCCMS_OPA_RELATIONSHIP for MERITS
INSERT INTO XXCCMS_OPA_RELATIONSHIP (ID, FK_OPA_ENTITY, NAME, PREPOPULATED)
VALUES (36, 34, 'name2', 1);

-- Insert into XXCCMS_OPA_RELSHIPTARGET for MERITS
INSERT INTO XXCCMS_OPA_RELSHIPTARGET (ID, TARGET_ENTITY_ID)
VALUES (37, 'entityId2');

-- Insert into XXCCMS_OPA_CHECKPOINT for MERITS
INSERT INTO XXCCMS_OPA_CHECKPOINT (RESUME_ID, USER_NAME, INTERVIEW_DATA)
VALUES (32, 'jane.doe', hextoraw('48656c6c6f20776f726c64'));

-- Insert into XXCCMS_OPA_ASSESSMENT_LOG for MERITS assessment
INSERT INTO XXCCMS_OPA_ASSESSMENT_LOG (ID, TARGET_ID, ASSESSMENT, OWNER_ID, ACTION, TTL, ASSESSMENT_DATA, CREATED, CREATED_BY)
VALUES (2, '1234567891', 'MERITS', 'owner2', 'Action_002', 'Time to live 2', 'Sample assessment data 2', SYSTIMESTAMP, 'User_002');


-- Insert into XXCCMS_OPA_SESSION for BILL
INSERT INTO XXCCMS_OPA_SESSION (ID, OWNER_ID, TARGET_ID, ASSESSMENT, STATUS, CREATED, CREATED_BY, MODIFIED, MODIFIED_BY)
VALUES (38, 'owner3', '1234567892', 'billingAssessment', 'status3', TIMESTAMP '2024-05-13 13:23:06.218000', 'creator3', TIMESTAMP '2024-05-13 13:23:06.218000', 'modifier3');

-- Insert into XXCCMS_OPA_LISTENTITY for BILL
INSERT INTO XXCCMS_OPA_LISTENTITY (ID, FK_OPA_SESSION, ENTITY_TYPE)
VALUES (39, 38, 'entityType3');

-- Insert into XXCCMS_OPA_ENTITY for BILL
INSERT INTO XXCCMS_OPA_ENTITY (ID, FK_OPA_LIST_ENTITY, FK_OPA_SESSION, ENTITY_ID, PREPOPULATED, COMPLETED)
VALUES (40, 39, 38, 'entityId3', 0, 0);

-- Insert into XXCCMS_OPA_ATTRIBUTE for BILL
INSERT INTO XXCCMS_OPA_ATTRIBUTE (ID, FK_OPA_ENTITY, ATTRIBUTE_ID, ATTRIBUTE_TYPE, ATTRIBUTE_VALUE, INFERENCING_TYPE, PREPOPULATED, ASKED)
VALUES (41, 40, 'OPPONENT_DOB', 'text', null, null, 0, 0);

-- Insert into XXCCMS_OPA_RELATIONSHIP for BILL
INSERT INTO XXCCMS_OPA_RELATIONSHIP (ID, FK_OPA_ENTITY, NAME, PREPOPULATED)
VALUES (42, 40, 'name3', 1);

-- Insert into XXCCMS_OPA_RELSHIPTARGET for BILL
INSERT INTO XXCCMS_OPA_RELSHIPTARGET (ID, TARGET_ENTITY_ID)
VALUES (43, 'entityId3');

-- Insert into XXCCMS_OPA_CHECKPOINT for BILL
INSERT INTO XXCCMS_OPA_CHECKPOINT (RESUME_ID, USER_NAME, INTERVIEW_DATA)
VALUES (38, 'john.smith', hextoraw('48656c6c6f20776f726c64'));

-- Insert into XXCCMS_OPA_ASSESSMENT_LOG for BILL assessment
INSERT INTO XXCCMS_OPA_ASSESSMENT_LOG (ID, TARGET_ID, ASSESSMENT, OWNER_ID, ACTION, TTL, ASSESSMENT_DATA, CREATED, CREATED_BY)
VALUES (3, '1234567892', 'BILL', 'owner3', 'Action_003', 'Time to live 3', 'Sample assessment data 3', SYSTIMESTAMP, 'User_003');


-- Insert into XXCCMS_OPA_SESSION for POA
INSERT INTO XXCCMS_OPA_SESSION (ID, OWNER_ID, TARGET_ID, ASSESSMENT, STATUS, CREATED, CREATED_BY, MODIFIED, MODIFIED_BY)
VALUES (44, 'owner4', '1234567893', 'poaAssessment', 'status4', TIMESTAMP '2024-05-13 13:25:06.218000', 'creator4', TIMESTAMP '2024-05-13 13:25:06.218000', 'modifier4');

-- Insert into XXCCMS_OPA_LISTENTITY for POA
INSERT INTO XXCCMS_OPA_LISTENTITY (ID, FK_OPA_SESSION, ENTITY_TYPE)
VALUES (45, 44, 'entityType4');

-- Insert into XXCCMS_OPA_ENTITY for POA
INSERT INTO XXCCMS_OPA_ENTITY (ID, FK_OPA_LIST_ENTITY, FK_OPA_SESSION, ENTITY_ID, PREPOPULATED, COMPLETED)
VALUES (46, 45, 44, 'entityId4', 0, 0);

-- Insert into XXCCMS_OPA_ATTRIBUTE for POA
INSERT INTO XXCCMS_OPA_ATTRIBUTE (ID, FK_OPA_ENTITY, ATTRIBUTE_ID, ATTRIBUTE_TYPE, ATTRIBUTE_VALUE, INFERENCING_TYPE, PREPOPULATED, ASKED)
VALUES (47, 46, 'OPPONENT_DOB', 'text', null, null, 0, 0);

-- Insert into XXCCMS_OPA_RELATIONSHIP for POA
INSERT INTO XXCCMS_OPA_RELATIONSHIP (ID, FK_OPA_ENTITY, NAME, PREPOPULATED)
VALUES (48, 46, 'name4', 1);

-- Insert into XXCCMS_OPA_RELSHIPTARGET for POA
INSERT INTO XXCCMS_OPA_RELSHIPTARGET (ID, TARGET_ENTITY_ID)
VALUES (49, 'entityId4');

-- Insert into XXCCMS_OPA_CHECKPOINT for POA
INSERT INTO XXCCMS_OPA_CHECKPOINT (RESUME_ID, USER_NAME, INTERVIEW_DATA)
VALUES (44, 'jane.smith', hextoraw('48656c6c6f20776f726c64'));

-- Insert into XXCCMS_OPA_ASSESSMENT_LOG for POA assessment
INSERT INTO XXCCMS_OPA_ASSESSMENT_LOG (ID, TARGET_ID, ASSESSMENT, OWNER_ID, ACTION, TTL, ASSESSMENT_DATA, CREATED, CREATED_BY)
VALUES (4, '1234567893', 'POA', 'owner4', 'Action_004', 'Time to live 4', 'Sample assessment data 4', SYSTIMESTAMP, 'User_004');


Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package uk.gov.laa.ccms.caab.assessment.constants;

import java.util.Arrays;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
* Enum representing the mapping between session assessment types and log assessment types.
*/
@Getter
@Slf4j
public enum OpaAssessmentLogMap {
MEANS("meansAssessment", "MEANS"),
MEANS_PREPOP("meansAssessment_PREPOP", "MEANS"),
MERITS("meritsAssessment", "MERITS"),
MERITS_PREPOP("meritsAssessment_PREPOP", "MERITS"),
BILLING("billingAssessment", "BILL"),
BILLING_PREPOP("billingAssessment_PREPOP", "BILL"),
POA("poaAssessment", "POA"),
POA_PREPOP("poaAssessment_PREPOP", "POA");


private final String sessionAssessmentType;
private final String logAssessmentType;

OpaAssessmentLogMap(final String sessionAssessmentType, final String logAssessmentType) {
this.sessionAssessmentType = sessionAssessmentType;
this.logAssessmentType = logAssessmentType;
}

/**
* Finds the log assessment type based on the session assessment type.
*
* @param sessionAssessmentType the session assessment type
* @return the corresponding log assessment type, or null if not found
*/
public static String findLogAssessmentTypeBySessionAssessmentType(
final String sessionAssessmentType) {
return Arrays.stream(OpaAssessmentLogMap.values())
.filter(assessmentLogMap -> assessmentLogMap.getSessionAssessmentType()
.equalsIgnoreCase(sessionAssessmentType))
.map(OpaAssessmentLogMap::getLogAssessmentType)
.findFirst()
.orElseGet(() -> {
log.warn("Invalid session assessment type: {}", sessionAssessmentType);
return null;
});
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package uk.gov.laa.ccms.caab.assessment.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Lob;
import jakarta.persistence.Table;
import java.time.LocalDateTime;
import lombok.Getter;
import lombok.Setter;

/**
* Represents the OPA Assessment Log.
*/
@Entity
@Table(name = "XXCCMS_OPA_ASSESSMENT_LOG")
@Getter
@Setter
public class OpaAssessmentLog {

@Id
@Column(name = "ID")
private Long id;

@Column(name = "TARGET_ID")
private String targetId;

@Column(name = "ASSESSMENT")
private String assessment;

@Column(name = "OWNER_ID")
private String ownerId;

@Column(name = "ACTION")
private String action;

@Column(name = "TTL")
private String ttl;

@Column(name = "CREATED")
private LocalDateTime created;

@Column(name = "CREATED_BY")
private String createdBy;

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package uk.gov.laa.ccms.caab.assessment.repository;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
import uk.gov.laa.ccms.caab.assessment.entity.OpaAssessmentLog;
import uk.gov.laa.ccms.caab.assessment.entity.OpaSession;

/**
* Repository interface for managing {@link uk.gov.laa.ccms.caab.assessment.entity.OpaAssessmentLog}
* entities.
*
*/
@Repository
public interface OpaAssessmentLogRepository extends JpaRepository<OpaAssessmentLog, Long> {

/**
* Finds a list of OpaAssessmentLog by the targetId and assessment.
*
* @param targetId the ID of the target
* @param assessment the type of assessment
* @return a list of OpaAssessmentLog matching the targetId and assessment
*/
List<OpaAssessmentLog> findByTargetIdAndAssessment(String targetId, String assessment);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@
import jakarta.persistence.criteria.Predicate;
import jakarta.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.convert.QueryByExamplePredicateBuilder;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import uk.gov.laa.ccms.caab.assessment.constants.OpaAssessmentLogMap;
import uk.gov.laa.ccms.caab.assessment.entity.OpaAssessmentLog;
import uk.gov.laa.ccms.caab.assessment.entity.OpaSession;
import uk.gov.laa.ccms.caab.assessment.exception.ApplicationException;
import uk.gov.laa.ccms.caab.assessment.mapper.AssessmentMapper;
import uk.gov.laa.ccms.caab.assessment.model.AssessmentDetail;
import uk.gov.laa.ccms.caab.assessment.model.AssessmentDetails;
import uk.gov.laa.ccms.caab.assessment.model.PatchAssessmentDetail;
import uk.gov.laa.ccms.caab.assessment.repository.OpaAssessmentLogRepository;
import uk.gov.laa.ccms.caab.assessment.repository.OpaSessionRepository;

/**
Expand All @@ -26,6 +31,7 @@
public class AssessmentService {

private final OpaSessionRepository opaSessionRepository;
private final OpaAssessmentLogRepository opaAssessmentLogRepository;

private final AssessmentMapper assessmentMapper;

Expand Down Expand Up @@ -79,19 +85,63 @@ public Long createAssessment(
}

/**
* Deletes assessments from the repository based on specified criteria and a list of names.
* Deletes OpaSession entities and their associated logs from the repository based on the
* specified assessment criteria and a list of assessment types.
*
* @param criteria the details of the assessment to use as deletion criteria.
* @param names a list of assessment types to further filter the assessments to be deleted.
* @param criteria the assessment details used as the deletion criteria.
* @param names a list of assessment types used to further filter which assessments to delete.
*/
public void deleteAssessments(
final AssessmentDetail criteria,
final List<String> names) {

OpaSession example = assessmentMapper.toOpaSession(criteria);

opaSessionRepository.deleteAll(
opaSessionRepository.findAll(buildQuerySpecification(Example.of(example), names)));
List<OpaSession> sessionsToDelete = opaSessionRepository.findAll(
buildQuerySpecification(Example.of(example), names));

Set<OpaAssessmentLog> logsToDelete = collectLogsToDelete(sessionsToDelete);

deleteLogs(logsToDelete);
if (!sessionsToDelete.isEmpty()) {
opaSessionRepository.deleteAll(sessionsToDelete);
}
}

/**
* Collects assessment logs that are associated with the sessions to be deleted.
*
* @param sessionsToDelete the list of sessions to delete
* @return a set of assessment logs to delete
*/
protected Set<OpaAssessmentLog> collectLogsToDelete(List<OpaSession> sessionsToDelete) {
Set<OpaAssessmentLog> logsToDelete = new HashSet<>();

for (OpaSession session : sessionsToDelete) {
String sessionAssessmentType = session.getAssessment();

String logAssessmentType = OpaAssessmentLogMap
.findLogAssessmentTypeBySessionAssessmentType(sessionAssessmentType);

if (logAssessmentType != null) {
List<OpaAssessmentLog> assessmentLogs = opaAssessmentLogRepository
.findByTargetIdAndAssessment(session.getTargetId(), logAssessmentType);
logsToDelete.addAll(assessmentLogs);
}
}

return logsToDelete;
}

/**
* Deletes the provided assessment logs from the repository.
*
* @param logsToDelete the set of assessment logs to delete
*/
protected void deleteLogs(Set<OpaAssessmentLog> logsToDelete) {
if (!logsToDelete.isEmpty()) {
opaAssessmentLogRepository.deleteAll(logsToDelete);
}
}

/**
Expand Down
Loading

0 comments on commit 2d1b8da

Please sign in to comment.