Search in sources :

Example 6 with Rule

use of io.lumeer.api.model.Rule in project engine by Lumeer.

the class MongoCollectionDaoTest method testUpdateCollection.

@Test
public void testUpdateCollection() {
    String id = createCollection(CODE, NAME).getId();
    Collection collection = prepareCollection(CODE2, NAME);
    collection.setRules(Map.of(RULE_NAME, new Rule("rule1", Rule.RuleType.AUTO_LINK, Rule.RuleTiming.UPDATE, new DataDocument(CONFIGURATION_KEY, CONFIGURATION_VALUE))));
    Collection updatedCollection = collectionDao.updateCollection(id, collection, null);
    assertThat(updatedCollection).isNotNull();
    assertThat(updatedCollection.getCode()).isEqualTo(CODE2);
    Collection storedCollection = collectionDao.databaseCollection().find(MongoFilters.idFilter(id)).first();
    assertThat(storedCollection).isNotNull();
    assertThat(updatedCollection).isEqualTo(storedCollection);
    assertThat(updatedCollection.getRules()).hasSize(1).hasEntrySatisfying(RULE_NAME, rule -> {
        assertThat(rule.getType()).isEqualTo(Rule.RuleType.AUTO_LINK);
        assertThat(rule.getConfiguration()).contains(Map.entry(CONFIGURATION_KEY, CONFIGURATION_VALUE));
    });
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) Collection(io.lumeer.api.model.Collection) Rule(io.lumeer.api.model.Rule) Test(org.junit.Test)

Example 7 with Rule

use of io.lumeer.api.model.Rule in project engine by Lumeer.

the class RuleCodec method convertFromDocument.

public static Rule convertFromDocument(final Document bson) {
    final String name = bson.getString(NAME);
    final Integer typeInt = bson.getInteger(TYPE);
    final Rule.RuleType type = typeInt != null ? Rule.RuleType.values()[typeInt] : null;
    final Integer timingInt = bson.getInteger(TIMING);
    final Rule.RuleTiming timing = timingInt != null ? Rule.RuleTiming.values()[timingInt] : null;
    final Document configuration = bson.get(CONFIGURATION, org.bson.Document.class);
    final Rule rule = new Rule(name, type, timing, new DataDocument(configuration));
    final Date createdAt = bson.getDate(CREATED_AT);
    if (createdAt != null) {
        rule.setCreatedAt(ZonedDateTime.ofInstant(createdAt.toInstant(), ZoneOffset.UTC));
    }
    return rule;
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) Rule(io.lumeer.api.model.Rule) Document(org.bson.Document) DataDocument(io.lumeer.engine.api.data.DataDocument) Date(java.util.Date)

Example 8 with Rule

use of io.lumeer.api.model.Rule in project engine by Lumeer.

the class ZapierFacade method createCollectionRule.

public Rule createCollectionRule(final String collectionId, final Rule.RuleTiming timing, final String hookUrl) {
    final Collection collection = collectionFacade.getCollection(collectionId);
    final Optional<Rule> existingRule = collection.getRules().values().stream().filter(rule -> rule.getType() == Rule.RuleType.ZAPIER && rule.getConfiguration().getString(ZapierRule.HOOK_URL).equals(hookUrl)).findFirst();
    if (existingRule.isEmpty()) {
        final String subscribeId = UUID.randomUUID().toString();
        final String userEmail = authenticatedUser.getUserEmail();
        final String ruleName = "Zapier" + (userEmail != null && userEmail.length() > 0 ? " (" + userEmail + ")" : "") + " " + subscribeId;
        final Rule rule = new Rule(ruleName, Rule.RuleType.ZAPIER, timing, new DataDocument(ZapierRule.HOOK_URL, hookUrl).append(ZapierRule.SUBSCRIBE_ID, subscribeId));
        collection.getRules().put(ruleName, rule);
        collectionFacade.updateCollection(collection.getId(), collection);
        return rule;
    }
    return null;
}
Also used : CollectionAttributeFilter(io.lumeer.api.model.CollectionAttributeFilter) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) ConditionType(io.lumeer.api.model.ConditionType) DataDocument(io.lumeer.engine.api.data.DataDocument) Document(io.lumeer.api.model.Document) AuthenticatedUser(io.lumeer.core.auth.AuthenticatedUser) UUID(java.util.UUID) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Objects(java.util.Objects) Inject(javax.inject.Inject) ZapierRule(io.lumeer.api.model.rule.ZapierRule) List(java.util.List) QueryStem(io.lumeer.api.model.QueryStem) RequestScoped(javax.enterprise.context.RequestScoped) Map(java.util.Map) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) Optional(java.util.Optional) Query(io.lumeer.api.model.Query) Collections(java.util.Collections) Collection(io.lumeer.api.model.Collection) ConstraintType(io.lumeer.api.model.ConstraintType) Rule(io.lumeer.api.model.Rule) DataDocument(io.lumeer.engine.api.data.DataDocument) Collection(io.lumeer.api.model.Collection) ZapierRule(io.lumeer.api.model.rule.ZapierRule) Rule(io.lumeer.api.model.Rule)

Example 9 with Rule

use of io.lumeer.api.model.Rule in project engine by Lumeer.

the class FunctionAndRuleCreator method getRule.

private Rule getRule(final String id, final JSONObject o) {
    var name = (String) o.get(Rule.NAME);
    var type = Rule.RuleType.valueOf(o.get(Rule.TYPE).toString());
    var timing = Utils.computeIfNotNull(o.get(Rule.TIMING), t -> Rule.RuleTiming.valueOf(t.toString()));
    var rule = new Rule(name, type, timing, new DataDocument((JSONObject) o.get("configuration")));
    rule.setId(id);
    if (type == Rule.RuleType.BLOCKLY) {
        rule.getConfiguration().put(BlocklyRule.BLOCKLY_JS, cureJs(rule.getConfiguration().getString(BlocklyRule.BLOCKLY_JS)));
        rule.getConfiguration().put(BlocklyRule.BLOCKLY_XML, cureXml(rule.getConfiguration().getString(BlocklyRule.BLOCKLY_XML)));
    }
    if (type == Rule.RuleType.CRON) {
        rule.getConfiguration().put(BlocklyRule.BLOCKLY_JS, cureJs(rule.getConfiguration().getString(BlocklyRule.BLOCKLY_JS)));
        rule.getConfiguration().put(BlocklyRule.BLOCKLY_XML, cureXml(rule.getConfiguration().getString(BlocklyRule.BLOCKLY_XML)));
        final Long startsOn = rule.getConfiguration().getLong(CronRule.CRON_STARTS_ON);
        if (startsOn != null) {
            rule.getConfiguration().put(CronRule.CRON_STARTS_ON, new Date(startsOn));
        }
        final Long endsOn = rule.getConfiguration().getLong(CronRule.CRON_ENDS_ON);
        if (endsOn != null) {
            rule.getConfiguration().put(CronRule.CRON_ENDS_ON, new Date(endsOn));
        }
        final Long lastRun = rule.getConfiguration().getLong(CronRule.CRON_LAST_RUN);
        if (lastRun != null) {
            rule.getConfiguration().put(CronRule.CRON_LAST_RUN, new Date(lastRun));
        }
        processLongToInt(CronRule.CRON_EXECUTIONS_LEFT, rule.getConfiguration());
        processLongToInt(CronRule.CRON_HOUR, rule.getConfiguration());
        processLongToInt(CronRule.CRON_INTERVAL, rule.getConfiguration());
        processLongToInt(CronRule.CRON_OCCURRENCE, rule.getConfiguration());
        processLongToInt(CronRule.CRON_DAYS_OF_WEEK, rule.getConfiguration());
        rule.getConfiguration().put(CronRule.CRON_VIEW_ID, templateParser.getDict().getViewId(rule.getConfiguration().getString(CronRule.CRON_VIEW_ID)));
    }
    if (type == Rule.RuleType.WIZARD) {
        rule.getConfiguration().put(BlocklyRule.BLOCKLY_JS, cureJs(rule.getConfiguration().getString(BlocklyRule.BLOCKLY_JS)));
        rule.getConfiguration().put(BlocklyRule.BLOCKLY_XML, cureXml(rule.getConfiguration().getString(BlocklyRule.BLOCKLY_XML)));
    }
    if (type == Rule.RuleType.AUTO_LINK) {
        rule.getConfiguration().put(AutoLinkRule.AUTO_LINK_LINK_TYPE, templateParser.getDict().getLinkTypeId(rule.getConfiguration().getString(AutoLinkRule.AUTO_LINK_LINK_TYPE)));
        rule.getConfiguration().put(AutoLinkRule.AUTO_LINK_COLLECTION1, templateParser.getDict().getCollectionId(rule.getConfiguration().getString(AutoLinkRule.AUTO_LINK_COLLECTION1)));
        rule.getConfiguration().put(AutoLinkRule.AUTO_LINK_COLLECTION2, templateParser.getDict().getCollectionId(rule.getConfiguration().getString(AutoLinkRule.AUTO_LINK_COLLECTION2)));
    }
    if (type == Rule.RuleType.ZAPIER) {
        rule.getConfiguration().put(ZapierRule.HOOK_URL, templateParser.getDict().getLinkTypeId(rule.getConfiguration().getString(ZapierRule.HOOK_URL)));
        rule.getConfiguration().put(ZapierRule.SUBSCRIBE_ID, templateParser.getDict().getLinkTypeId(rule.getConfiguration().getString(ZapierRule.SUBSCRIBE_ID)));
    }
    return rule;
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) JSONObject(org.json.simple.JSONObject) BlocklyRule(io.lumeer.api.model.rule.BlocklyRule) AutoLinkRule(io.lumeer.api.model.rule.AutoLinkRule) ZapierRule(io.lumeer.api.model.rule.ZapierRule) CronRule(io.lumeer.api.model.rule.CronRule) Rule(io.lumeer.api.model.Rule) Date(java.util.Date)

Example 10 with Rule

use of io.lumeer.api.model.Rule in project engine by Lumeer.

the class CollectionCodec method decode.

@Override
public Collection decode(final BsonReader reader, final DecoderContext decoderContext) {
    Document bson = documentCodec.decode(reader, decoderContext);
    SimpleResource resource = decodeResource(bson, ResourceType.COLLECTION);
    Set<Attribute> attributes;
    List attributeList = bson.get(ATTRIBUTES, List.class);
    if (attributeList != null) {
        attributes = new ArrayList<Document>(attributeList).stream().map(AttributeCodec::convertFromDocument).collect(Collectors.toSet());
    } else {
        attributes = Collections.emptySet();
    }
    Map<String, Rule> rules = new HashMap<>();
    Document rulesMap = bson.get(RULES, Document.class);
    if (rulesMap != null) {
        rulesMap.forEach((k, v) -> {
            final Rule rule = RuleCodec.convertFromDocument(rulesMap.get(k, Document.class));
            rule.setId(k);
            if (StringUtils.isEmpty(rule.getName())) {
                rule.setName(k);
            }
            rules.put(k, rule);
        });
    }
    Integer lastAttributeNum = bson.getInteger(LAST_ATTRIBUTE_NUM);
    Date lastTimeUsed = bson.getDate(LAST_TIME_USED);
    String defaultAttributeId = bson.getString(DEFAULT_ATTRIBUTE_ID);
    String dataDescription = bson.getString(DATA_DESCRIPTION);
    CollectionPurpose purpose;
    try {
        purpose = CollectionPurposeCodec.convertFromDocument(bson.get(PURPOSE, Document.class));
    } catch (ClassCastException e) {
        purpose = new CollectionPurpose(CollectionPurposeType.None, new DataDocument());
    }
    Collection collection = new Collection(resource, attributes, rules, dataDescription, purpose);
    if (lastTimeUsed != null) {
        collection.setLastTimeUsed(ZonedDateTime.ofInstant(lastTimeUsed.toInstant(), ZoneOffset.UTC));
    }
    collection.setDefaultAttributeId(defaultAttributeId);
    collection.setLastAttributeNum(lastAttributeNum);
    collection.setVersion(resource.getVersion());
    return collection;
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) Attribute(io.lumeer.api.model.Attribute) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Document(org.bson.Document) DataDocument(io.lumeer.engine.api.data.DataDocument) Date(java.util.Date) SimpleResource(io.lumeer.api.model.common.SimpleResource) Collection(io.lumeer.api.model.Collection) ArrayList(java.util.ArrayList) List(java.util.List) Rule(io.lumeer.api.model.Rule) CollectionPurpose(io.lumeer.api.model.CollectionPurpose)

Aggregations

Rule (io.lumeer.api.model.Rule)14 DataDocument (io.lumeer.engine.api.data.DataDocument)10 Collection (io.lumeer.api.model.Collection)9 AutoLinkRule (io.lumeer.api.model.rule.AutoLinkRule)8 Document (io.lumeer.api.model.Document)6 LinkType (io.lumeer.api.model.LinkType)5 BlocklyRule (io.lumeer.api.model.rule.BlocklyRule)5 Test (org.junit.Test)5 Attribute (io.lumeer.api.model.Attribute)3 CronRule (io.lumeer.api.model.rule.CronRule)3 Date (java.util.Date)3 CollectionPurpose (io.lumeer.api.model.CollectionPurpose)2 LinkInstance (io.lumeer.api.model.LinkInstance)2 Permissions (io.lumeer.api.model.Permissions)2 ZapierRule (io.lumeer.api.model.rule.ZapierRule)2 AuthenticatedUser (io.lumeer.core.auth.AuthenticatedUser)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Document (org.bson.Document)2