Search in sources :

Example 1 with ComplianceCheck

use of com.hack23.cia.service.api.action.kpi.ComplianceCheck in project cia by Hack23.

the class ComplianceCheckServiceImpl method processService.

@Override
@Secured({ "ROLE_USER", "ROLE_ADMIN", "ROLE_ANONYMOUS" })
public ComplianceCheckResponse processService(final ComplianceCheckRequest serviceRequest) {
    final ComplianceCheckResponse inputValidation = inputValidation(serviceRequest);
    if (inputValidation != null) {
        return inputValidation;
    }
    LOGGER.info("{}", serviceRequest.getClass().getSimpleName());
    final CreateApplicationEventRequest eventRequest = createApplicationEventForService(serviceRequest);
    final UserAccount userAccount = getUserAccountFromSecurityContext();
    if (userAccount != null) {
        eventRequest.setUserId(userAccount.getUserId());
    }
    final ComplianceCheckResponse response;
    final Set<ConstraintViolation<ComplianceCheckRequest>> requestConstraintViolations = validateRequest(serviceRequest);
    if (!requestConstraintViolations.isEmpty()) {
        response = handleInputViolations(eventRequest, requestConstraintViolations, new ComplianceCheckResponse(ServiceResult.FAILURE));
    } else {
        final List<ComplianceCheck> complianceList = rulesEngine.checkRulesCompliance();
        final List<RuleViolation> ruleViolations = new ArrayList<>();
        for (final ComplianceCheck check : complianceList) {
            ruleViolations.addAll(check.getRuleViolations());
        }
        Collections.sort(complianceList, new Comparator<ComplianceCheck>() {

            @Override
            public int compare(final ComplianceCheck o1, final ComplianceCheck o2) {
                return Integer.compare(o2.getRuleViolations().size(), o1.getRuleViolations().size());
            }
        });
        response = new ComplianceCheckResponse(ServiceResult.SUCCESS);
        response.setList(complianceList);
        response.setStatusMap(ruleViolations.stream().collect(Collectors.groupingBy(RuleViolation::getStatus)));
        response.setResourceTypeMap(ruleViolations.stream().collect(Collectors.groupingBy(RuleViolation::getResourceType)));
        eventRequest.setApplicationMessage(response.getResult().toString());
    }
    getCreateApplicationEventService().processService(eventRequest);
    return response;
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) ArrayList(java.util.ArrayList) ComplianceCheckResponse(com.hack23.cia.service.api.action.kpi.ComplianceCheckResponse) CreateApplicationEventRequest(com.hack23.cia.service.api.action.application.CreateApplicationEventRequest) ComplianceCheck(com.hack23.cia.service.api.action.kpi.ComplianceCheck) RuleViolation(com.hack23.cia.service.api.action.kpi.RuleViolation) UserAccount(com.hack23.cia.model.internal.application.user.impl.UserAccount) Secured(org.springframework.security.access.annotation.Secured)

Example 2 with ComplianceCheck

use of com.hack23.cia.service.api.action.kpi.ComplianceCheck in project cia by Hack23.

the class RulesEngineImpl method checkRulesCompliance.

@Override
@Cacheable("checkRulesCompliance")
public List<ComplianceCheck> checkRulesCompliance() {
    final KieSession ksession = rulesContainer.newKieSession();
    final Map<String, ComplianceCheck> complianceChecks = new HashMap<>();
    ksession.addEventListener(new ComplianceCheckAgendaEventListener(complianceChecks));
    insertPoliticians(ksession, dataViewer.getAll(ViewRiksdagenPolitician.class));
    insertParties(ksession, dataViewer.getAll(ViewRiksdagenPartySummary.class));
    ksession.fireAllRules();
    ksession.dispose();
    return new ArrayList<>(complianceChecks.values());
}
Also used : ViewRiksdagenPartySummary(com.hack23.cia.model.internal.application.data.party.impl.ViewRiksdagenPartySummary) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ComplianceCheck(com.hack23.cia.service.api.action.kpi.ComplianceCheck) ViewRiksdagenPolitician(com.hack23.cia.model.internal.application.data.politician.impl.ViewRiksdagenPolitician) Cacheable(org.springframework.cache.annotation.Cacheable)

Example 3 with ComplianceCheck

use of com.hack23.cia.service.api.action.kpi.ComplianceCheck in project cia by Hack23.

the class RuleEngineITest method checkRulesComplianceTest.

/**
 * Rule engine test.
 *
 * @throws Exception
 *             the exception
 */
@Test
public void checkRulesComplianceTest() throws Exception {
    final List<ComplianceCheck> checkRulesCompliance = rulesEngine.checkRulesCompliance();
    assertNotNull(checkRulesCompliance);
    assertFalse(checkRulesCompliance.isEmpty());
    for (final ComplianceCheck complianceCheck : checkRulesCompliance) {
        System.out.println(complianceCheck.getName() + "" + complianceCheck.getRuleViolations());
    }
}
Also used : ComplianceCheck(com.hack23.cia.service.api.action.kpi.ComplianceCheck) Test(org.junit.Test) AbstractServiceFunctionalIntegrationTest(com.hack23.cia.service.impl.AbstractServiceFunctionalIntegrationTest)

Aggregations

ComplianceCheck (com.hack23.cia.service.api.action.kpi.ComplianceCheck)3 ArrayList (java.util.ArrayList)2 ViewRiksdagenPartySummary (com.hack23.cia.model.internal.application.data.party.impl.ViewRiksdagenPartySummary)1 ViewRiksdagenPolitician (com.hack23.cia.model.internal.application.data.politician.impl.ViewRiksdagenPolitician)1 UserAccount (com.hack23.cia.model.internal.application.user.impl.UserAccount)1 CreateApplicationEventRequest (com.hack23.cia.service.api.action.application.CreateApplicationEventRequest)1 ComplianceCheckResponse (com.hack23.cia.service.api.action.kpi.ComplianceCheckResponse)1 RuleViolation (com.hack23.cia.service.api.action.kpi.RuleViolation)1 AbstractServiceFunctionalIntegrationTest (com.hack23.cia.service.impl.AbstractServiceFunctionalIntegrationTest)1 HashMap (java.util.HashMap)1 ConstraintViolation (javax.validation.ConstraintViolation)1 Test (org.junit.Test)1 KieSession (org.kie.api.runtime.KieSession)1 Cacheable (org.springframework.cache.annotation.Cacheable)1 Secured (org.springframework.security.access.annotation.Secured)1