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));
});
}
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;
}
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;
}
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;
}
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;
}
Aggregations