use of io.lumeer.api.model.Constraint in project engine by Lumeer.
the class ConstraintManagerTest method testPercentageConstraint.
@Test
public void testPercentageConstraint() {
final Constraint percentageConstraint = new Constraint(ConstraintType.Percentage, null);
final ConstraintManager cm = new ConstraintManager();
cm.setLocale(l);
Object encoded = cm.encode("12%", percentageConstraint);
assertThat(encoded).isEqualTo(new BigDecimal("0.12"));
encoded = cm.encode("12 %", percentageConstraint);
assertThat(encoded).isEqualTo(new BigDecimal("0.12"));
encoded = cm.encode("1,2e1 %", percentageConstraint);
assertThat(encoded).isEqualTo(new BigDecimal("0.12"));
encoded = cm.encode("12e-2", percentageConstraint);
assertThat(encoded).isEqualTo(new BigDecimal("0.12"));
}
use of io.lumeer.api.model.Constraint in project engine by Lumeer.
the class ConstraintCodec method convertFromDocument.
public static Constraint convertFromDocument(final Document document) {
if (document == null) {
return null;
}
ConstraintType type = ConstraintType.valueOf(document.getString(TYPE));
Object config = document.get(CONFIG);
return new Constraint(type, config);
}
use of io.lumeer.api.model.Constraint in project engine by Lumeer.
the class ConstraintManager method processAttributeFilter.
private void processAttributeFilter(final Constraint constraint, final AttributeFilter filter, final BiFunction<Object, Constraint, Object> processor) {
var conditionValues = filter.getConditionValues().stream().map(f -> {
if (f.getValue() != null) {
return new ConditionValue(processor.apply(f.getValue(), constraint));
}
return f;
}).collect(Collectors.toList());
filter.setConditionValues(conditionValues);
}
use of io.lumeer.api.model.Constraint in project engine by Lumeer.
the class CollectionFacadeIT method testDateAttributeConversion.
@Test
public void testDateAttributeConversion() {
Collection collection = collectionFacade.createCollection(prepareCollection(CODE3));
collectionFacade.createCollectionAttributes(collection.getId(), Arrays.asList(new Attribute("a1", "Task", null, null, null, null, 0, null), new Attribute("a2", ATTRIBUTE_STATE, null, null, null, null, 0, null)));
var dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy H:mm:ss");
var d1 = ZonedDateTime.of(LocalDateTime.of(2019, 11, 23, 8, 23, 10), ZoneId.systemDefault());
var d2 = ZonedDateTime.of(LocalDateTime.of(2019, 2, 28, 23, 34, 12), ZoneId.systemDefault());
var d3 = ZonedDateTime.of(LocalDateTime.of(1943, 3, 24, 6, 55, 19), ZoneId.systemDefault());
var d4 = ZonedDateTime.of(LocalDateTime.of(1929, 4, 21, 9, 37, 1), ZoneId.systemDefault());
var values = Arrays.asList(dtf.format(d1), dtf.format(d2), dtf.format(d3), dtf.format(d4));
var i = new AtomicInteger(1);
values.forEach(value -> {
documentFacade.createDocument(collection.getId(), new Document(new DataDocument("a1", "Task-" + i.getAndIncrement()).append("a2", value)));
});
var attributes = collectionFacade.getCollection(collection.getId()).getAttributes();
var attr = attributes.stream().filter(attribute -> attribute.getName().equals(ATTRIBUTE_STATE)).findFirst().get();
attr.setConstraint(new Constraint(ConstraintType.DateTime, new org.bson.Document("format", "DD/MM/YYYY H:mm:ss")));
collectionFacade.updateCollectionAttribute(collection.getId(), attr.getId(), attr);
var documents = documentDao.getDocumentsByCollection(collection.getId());
Map<String, Object> res = new HashMap<>();
documents.forEach(document -> {
// we must skip DocumentFacade because with that, Date gets converted to String for compatibility with UI
DataDocument data = dataDao.getData(collection.getId(), document.getId());
res.put(data.getString("a1"), data.getObject("a2"));
});
assertThat(res).contains(Map.entry("Task-1", Date.from(d1.toInstant())), Map.entry("Task-2", Date.from(d2.toInstant())), Map.entry("Task-3", Date.from(d3.toInstant())), Map.entry("Task-4", Date.from(d4.toInstant())));
// now back to no constraint
var attr2 = attributes.stream().filter(attribute -> attribute.getName().equals(ATTRIBUTE_STATE)).findFirst().get();
attr2.setConstraint(new Constraint(ConstraintType.None, null));
collectionFacade.updateCollectionAttribute(collection.getId(), attr2.getId(), attr2);
documents = documentDao.getDocumentsByCollection(collection.getId());
Map<String, String> res2 = new HashMap<>();
documents.forEach(document -> {
var doc = documentFacade.getDocument(collection.getId(), document.getId());
res2.put(doc.getData().getString("a1"), doc.getData().getString("a2"));
});
assertThat(res2).contains(Map.entry("Task-1", "23/11/2019 8:23:10"), Map.entry("Task-2", "28/02/2019 23:34:12"), Map.entry("Task-3", "24/03/1943 6:55:19"), Map.entry("Task-4", "21/04/1929 9:37:01"));
}
use of io.lumeer.api.model.Constraint in project engine by Lumeer.
the class CollectionFacadeIT method testPercentageAttributeConversion.
@Test
public void testPercentageAttributeConversion() {
Collection collection = collectionFacade.createCollection(prepareCollection(CODE3));
collectionFacade.createCollectionAttributes(collection.getId(), Arrays.asList(new Attribute("a1", "Task", null, null, null, null, 0, null), new Attribute("a2", ATTRIBUTE_STATE, null, null, null, null, 0, null)));
var values = Arrays.asList("10", "12%", "0.12", "0.12 %");
var i = new AtomicInteger(1);
values.forEach(value -> {
documentFacade.createDocument(collection.getId(), new Document(new DataDocument("a1", "Task-" + i.getAndIncrement()).append("a2", value)));
});
var attributes = collectionFacade.getCollection(collection.getId()).getAttributes();
var attr = attributes.stream().filter(attribute -> attribute.getName().equals(ATTRIBUTE_STATE)).findFirst().get();
attr.setConstraint(new Constraint(ConstraintType.Percentage, null));
collectionFacade.updateCollectionAttribute(collection.getId(), attr.getId(), attr);
var documents = documentDao.getDocumentsByCollection(collection.getId());
Map<String, Object> res = new HashMap<>();
documents.forEach(document -> {
// we must skip DocumentFacade because with that, BigDecimal gets converted to String for compatibility with UI
DataDocument data = dataDao.getData(collection.getId(), document.getId());
res.put(data.getString("a1"), data.getObject("a2"));
});
assertThat(res).contains(Map.entry("Task-1", 10L), Map.entry("Task-2", new BigDecimal("0.12")), Map.entry("Task-3", new BigDecimal("0.12")), Map.entry("Task-4", new BigDecimal("0.0012")));
// now back to no constraint
var attr2 = attributes.stream().filter(attribute -> attribute.getName().equals(ATTRIBUTE_STATE)).findFirst().get();
attr2.setConstraint(new Constraint(ConstraintType.None, null));
collectionFacade.updateCollectionAttribute(collection.getId(), attr2.getId(), attr2);
documents = documentDao.getDocumentsByCollection(collection.getId());
Map<String, Object> res2 = new HashMap<>();
documents.forEach(document -> {
// we must skip DocumentFacade because with that,, BigDecimal gets converted to String for compatibility with UI
DataDocument data = dataDao.getData(collection.getId(), document.getId());
res2.put(data.getString("a1"), data.getObject("a2"));
});
assertThat(res2).contains(Map.entry("Task-1", 10L), Map.entry("Task-2", new BigDecimal("0.12")), Map.entry("Task-3", new BigDecimal("0.12")), Map.entry("Task-4", new BigDecimal("0.0012")));
}
Aggregations