Skip to content

Commit

Permalink
[UNIONVMS-4660] added creationDate rules for movements
Browse files Browse the repository at this point in the history
  • Loading branch information
nomikosi authored and Nikos Paraskevopoulos committed Nov 26, 2020
1 parent 6ffe624 commit fcf562e
Show file tree
Hide file tree
Showing 17 changed files with 549 additions and 23 deletions.
2 changes: 1 addition & 1 deletion LIQUIBASE/changelog/db-changelog-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@
<include file="changelog/v3.4/db-changelog-UNIONVMS-incorrect-triggered-rules.xml" />
<include file="changelog/v3.4/db-changelog-UNIONVMS-4477.xml" />
<include file="changelog/v3.4/db-changelog-UNIONVMS-4685.xml" />
<include file="changelog/v3.4/db-changelog-UNIONVMS-4660.xml" />
</databaseChangeLog>

103 changes: 103 additions & 0 deletions LIQUIBASE/changelog/v3.4/db-changelog-UNIONVMS-4660.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.3.xsd">
<changeSet id="UNIONVMS-4660-template-for-MovementRules" author="nomikosi">
<insert tableName="template">
<column name="template_id" value="36" />
<column name="template_name" value="Movement report document" />
<column name="fact_template" value="MOVEMENT_REPORT_DOCUMENT" />
</insert>

<rollback>
<delete tableName="template">
<where>template_id = 36</where>
</delete>
</rollback>
</changeSet>

<changeSet id="UNIONVMS-4660-update-MO-R01-00-0001" author="nomikosi">
<insert tableName="rule">
<column name="template_id" value="36" />
<column name="property_names" value="creationDateTime" />
<column name="note" value="Check presence. Must be present." />
<column name="level" value="L00" />
<column name="error_type" value="ERROR" />
<column name="disabled" value="false" />
<column name="br_id" value="MO-R01-00-0001" />
<column name="rule_id" value="700"/>
</insert>
<insert tableName="context_expression">
<column name="expression" value="creationDateTime == null "/>
<column name="failure_message" value="Message creation date/time missing."/>
<column name="context" value="EU"/>
<column name="rule_id" value="700"/>
<column name="id" value="700"/>
</insert>

<rollback>
<delete tableName="context_expression">
<where>rule_id = 700</where>
</delete>
<delete tableName="rule">
<where>br_id = 'MO-R01-00-0001'</where>
</delete>
</rollback>
</changeSet>

<changeSet id="UNIONVMS-4660-update-MO-R01-00-0002" author="nomikosi">
<insert tableName="rule">
<column name="template_id" value="36" />
<column name="property_names" value="creationDateTime" />
<column name="note" value="Format must be date in UTC according to ISO8601" />
<column name="level" value="L01" />
<column name="error_type" value="ERROR" />
<column name="disabled" value="false" />
<column name="br_id" value="MO-R01-00-0002" />
<column name="rule_id" value="701"/>
</insert>
<insert tableName="context_expression">
<column name="expression" value="!isEmpty(creationDateTimeString) &amp;&amp; !isIsoDateStringValidFormat(creationDateTimeString)"/>
<column name="failure_message" value="Invalid message creation date/time."/>
<column name="context" value="EU"/>
<column name="rule_id" value="701"/>
<column name="id" value="701"/>
</insert>

<rollback>
<delete tableName="context_expression">
<where>rule_id = 701</where>
</delete>
<delete tableName="rule">
<where>br_id = 'MO-R01-00-0002'</where>
</delete>
</rollback>
</changeSet>

<changeSet id="UNIONVMS-4660-update-MO-R01-00-0003" author="nomikosi">
<insert tableName="rule">
<column name="template_id" value="36" />
<column name="property_names" value="creationDateTime" />
<column name="note" value="A threshold to compensate for incorrect clock synchronization of the exchanging systems must be taken into account." />
<column name="level" value="L03" />
<column name="error_type" value="WARNING" />
<column name="disabled" value="false" />
<column name="br_id" value="MO-R01-00-0003" />
<column name="rule_id" value="702"/>
</insert >
<insert tableName="context_expression">
<column name="expression" value="creationDateTime != null &amp;&amp; dateNotInPast(creationDateTime, 10)"/>
<column name="failure_message" value="Message creation date/time not in the past."/>
<column name="context" value="EU"/>
<column name="rule_id" value="702"/>
<column name="id" value="702"/>
</insert >

<rollback>
<delete tableName="context_expression">
<where>rule_id = 702</where>
</delete>
<delete tableName="rule">
<where>br_id = 'MO-R01-00-0003'</where>
</delete>
</rollback>
</changeSet>
</databaseChangeLog>
2 changes: 2 additions & 0 deletions model/src/main/resources/contract/Template.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@
<xsd:enumeration value="SALES_QUERY_PARAMETER"/>
<xsd:enumeration value="SALES_FLUX_SALES_RESPONSE_MESSAGE"/>

<!-- Movements -->
<xsd:enumeration value="MOVEMENT_REPORT_DOCUMENT"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ public enum ContainerType {
FactType.SALES_FISHING_TRIP, FactType.SALES_FLUX_LOCATION, FactType.SALES_FLUX_GEOGRAPHICAL_COORDINATE,
FactType.SALES_STRUCTURED_ADDRESS, FactType.SALES_QUERY, FactType.SALES_FLUX_RESPONSE_DOCUMENT,
FactType.SALES_VALIDATION_RESULT_DOCUMENT, FactType.SALES_VALIDATION_QUALITY_ANALYSIS,FactType.SALES_REPORT_WRAPPER,
FactType.SALES_AUCTION_SALE, FactType.SALES_FLUX_SALES_QUERY_MESSAGE, FactType.SALES_QUERY_PARAMETER, FactType.SALES_FLUX_SALES_RESPONSE_MESSAGE);
FactType.SALES_AUCTION_SALE, FactType.SALES_FLUX_SALES_QUERY_MESSAGE, FactType.SALES_QUERY_PARAMETER, FactType.SALES_FLUX_SALES_RESPONSE_MESSAGE),

MOVEMENTS("movement","ec.europa.eu.movement", FactType.MOVEMENT_REPORT_DOCUMENT
);

private final String packageName;
private final String containerName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ This file is part of the Integrated Fisheries Data Management (IFDM) Suite. The
import eu.europa.ec.fisheries.uvms.rules.service.business.AbstractFact;
import eu.europa.ec.fisheries.uvms.rules.service.business.MessageType;
import eu.europa.ec.fisheries.uvms.rules.service.business.fact.IdType;
import eu.europa.ec.fisheries.uvms.rules.service.business.generator.*;
import eu.europa.ec.fisheries.uvms.rules.service.business.generator.AbstractGenerator;
import eu.europa.ec.fisheries.uvms.rules.service.business.generator.ActivityFaReportFactGenerator;
import eu.europa.ec.fisheries.uvms.rules.service.business.generator.ActivityQueryFactGenerator;
import eu.europa.ec.fisheries.uvms.rules.service.business.generator.ActivityResponseFactGenerator;
import eu.europa.ec.fisheries.uvms.rules.service.business.generator.MovementFactGenerator;
import eu.europa.ec.fisheries.uvms.rules.service.business.generator.SalesQueryFactGenerator;
import eu.europa.ec.fisheries.uvms.rules.service.business.generator.SalesReportFactGenerator;
import eu.europa.ec.fisheries.uvms.rules.service.business.generator.SalesResponseFactGenerator;
import eu.europa.ec.fisheries.uvms.rules.service.business.helper.RuleApplicabilityChecker;
import eu.europa.ec.fisheries.uvms.rules.service.config.BusinessObjectType;
import eu.europa.ec.fisheries.uvms.rules.service.config.ExtraValueType;
Expand All @@ -36,12 +43,19 @@ This file is part of the Integrated Fisheries Data Management (IFDM) Suite. The
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static eu.europa.ec.fisheries.uvms.rules.service.business.MessageType.PULL;
import static eu.europa.ec.fisheries.uvms.rules.service.business.MessageType.PUSH;
import static eu.europa.ec.fisheries.uvms.rules.service.config.ExtraValueType.*;
import static eu.europa.ec.fisheries.uvms.rules.service.config.ExtraValueType.RESPONSE_IDS;
import static eu.europa.ec.fisheries.uvms.rules.service.config.ExtraValueType.SENDER_RECEIVER;
import static eu.europa.ec.fisheries.uvms.rules.service.config.ExtraValueType.XML;

@Stateless
@Slf4j
Expand All @@ -67,6 +81,7 @@ public class RulesEngineBean {
private SalesResponseFactGenerator salesResponseFactGenerator;



private FaResponseFactMapper faResponseFactMapper;

@EJB
Expand Down Expand Up @@ -181,6 +196,22 @@ public Collection<AbstractFact> evaluate(BusinessObjectType businessObjectType,

return validateFacts(facts, initializer.getContainerByType(ContainerType.SALES), globals, extraValues);
}
else if (businessObjectType == BusinessObjectType.RECEIVING_MOVEMENT_MSG){
StopWatch stopWatch = StopWatch.createStarted();
AbstractGenerator generator = new MovementFactGenerator(PUSH);
generator.setBusinessObjectMessage(businessObject);
generator.setExtraValueMap(extraValues);

List<AbstractFact> facts = generator.generateAllFacts();
log.info("Flow Report, Generating the facts took: {} ms", stopWatch.getTime());
stopWatch.reset();
stopWatch.start();

Map<String, Object> globals = new HashMap<>();
globals.put("mdrService", mdrCacheRuleService);
globals.put("appliChecker", appliChecker);
return validateFacts(facts, initializer.getContainerByType(ContainerType.MOVEMENTS), globals, extraValues);
}

log.info(String.format("It took %s to evaluate the message.", stopwatch));
log.debug(String.format("%s fact instances holding in memory.", AbstractFact.getNumOfInstances()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void init() {
List<TemplateRuleMapDto> faTemplatesAndRules = getFaMessageRules(allTemplates);
List<TemplateRuleMapDto> salesTemplatesAndRules = getSalesRules(allTemplates);
List<TemplateRuleMapDto> faQueryTemplatesAndRules = getFaQueryRules(allTemplates);
List<TemplateRuleMapDto> movementTemplatesAndRules = getMovementRules(allTemplates);

log.info("Initializing templates and rules for FA-Report facts. Nr. of Rules : {}", countRuleExpressions(faTemplatesAndRules));
KieContainer faReportContainer = createContainer(faTemplatesAndRules);
Expand All @@ -73,11 +74,16 @@ public void init() {
log.info("Initializing templates and rules for Sales facts. Nr. of Rules : {}", countRuleExpressions(salesTemplatesAndRules));
KieContainer salesContainer = createContainer(salesTemplatesAndRules);

log.info("Initializing templates and rules for Movement facts. Nr. of Rules : {}", countRuleExpressions(movementTemplatesAndRules));
KieContainer movementContainer = createContainer(movementTemplatesAndRules);


containers = new EnumMap<>(ContainerType.class);
containers.put(ContainerType.FA_REPORT, faReportContainer);
containers.put(ContainerType.FA_RESPONSE, faRespContainer);
containers.put(ContainerType.FA_QUERY, faQueryContainer);
containers.put(ContainerType.SALES, salesContainer);
containers.put(ContainerType.MOVEMENTS, movementContainer);

// To make sure that we have deployed all the templates!
if (!allTemplates.isEmpty()) {
Expand Down Expand Up @@ -214,6 +220,18 @@ private List<TemplateRuleMapDto> getFaQueryRules(List<TemplateRuleMapDto> allTem
return faQueryTemplates;
}

private List<TemplateRuleMapDto> getMovementRules(List<TemplateRuleMapDto> allTemplates) {
List<TemplateRuleMapDto> movementTemplates = new ArrayList<>();
List<FactType> factTypesList = ContainerType.MOVEMENTS.getFactTypesList();
for (TemplateRuleMapDto actualTemplate : allTemplates) {
if (factTypesList.contains(actualTemplate.getTemplateType().getType())) {
movementTemplates.add(actualTemplate);
}
}
allTemplates.removeAll(movementTemplates);
return movementTemplates;
}

public KieContainer getContainerByType(ContainerType containerType) {
return containers.get(containerType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This file is part of the Integrated Fisheries Data Management (IFDM) Suite. The
import eu.europa.ec.fisheries.uvms.rules.service.business.CustomRuleDto;
import eu.europa.ec.fisheries.uvms.rules.service.business.MovementFact;
import eu.europa.ec.fisheries.uvms.rules.service.business.RawMovementFact;
import eu.europa.ec.fisheries.uvms.rules.service.business.fact.MovementReportDocumentFact;
import eu.europa.ec.fisheries.uvms.rules.service.exception.RulesServiceException;
import eu.europa.ec.fisheries.uvms.rules.service.mapper.CustomRuleParser;
import org.drools.template.parser.DefaultTemplateContainer;
Expand Down Expand Up @@ -172,7 +173,7 @@ public void evaluate(MovementFact fact) {
}

@Lock(LockType.READ)
public void evaluate(List<MovementFact> factList, boolean justToAvoidErasure) {
public void evaluate(List<MovementFact> factList) {
log.info("Verifying user defined rules");
KieSession ksession = getKieSession();
// TODO : decomment as soon as the "Unexpected global [validationService]" is resolved
Expand All @@ -186,9 +187,9 @@ public void evaluate(List<MovementFact> factList, boolean justToAvoidErasure) {


@Lock(LockType.READ)
public void evaluate(List<RawMovementFact> facts) {
public void evaluateRawList(List<RawMovementFact> facts) {
KieSession ksession = getKieSession();
// ksession.setGlobal(LOGGER_STR, log);
ksession.setGlobal(LOGGER_STR, log);
for (RawMovementFact fact : facts) {
ksession.insert(fact);
}
Expand Down
Loading

0 comments on commit fcf562e

Please sign in to comment.