Search in sources :

Example 16 with Constraint

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"));
}
Also used : Constraint(io.lumeer.api.model.Constraint) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 17 with Constraint

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);
}
Also used : Constraint(io.lumeer.api.model.Constraint) ConstraintType(io.lumeer.api.model.ConstraintType)

Example 18 with Constraint

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);
}
Also used : Position(com.mongodb.client.model.geojson.Position) DataUtils(io.lumeer.core.util.DataUtils) CoordinatesParser(io.lumeer.core.util.coordinates.CoordinatesParser) AttributeFilter(io.lumeer.api.model.AttributeFilter) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) BiFunction(java.util.function.BiFunction) Point(com.mongodb.client.model.geojson.Point) StringUtils(org.apache.commons.lang3.StringUtils) NumberFormat(java.text.NumberFormat) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) Resource(io.lumeer.api.model.common.Resource) Locale(java.util.Locale) Map(java.util.Map) ConditionValue(io.lumeer.api.model.ConditionValue) Query(io.lumeer.api.model.Query) ZoneOffset(java.time.ZoneOffset) ParseException(java.text.ParseException) DateTimeException(java.time.DateTimeException) Decimal128(org.bson.types.Decimal128) DataDocument(io.lumeer.engine.api.data.DataDocument) Iterator(java.util.Iterator) DecimalFormat(java.text.DecimalFormat) Set(java.util.Set) Constraint(io.lumeer.api.model.Constraint) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) LinkType(io.lumeer.api.model.LinkType) LatLng(io.lumeer.core.util.coordinates.LatLng) DefaultConfigurationProducer(io.lumeer.core.facade.configuration.DefaultConfigurationProducer) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) ResourceUtils(io.lumeer.api.util.ResourceUtils) DateTimeFormatter(java.time.format.DateTimeFormatter) Optional(java.util.Optional) NamedCoordinateReferenceSystem(com.mongodb.client.model.geojson.NamedCoordinateReferenceSystem) Attribute(io.lumeer.api.model.Attribute) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) Collection(io.lumeer.api.model.Collection) ConstraintType(io.lumeer.api.model.ConstraintType) ConditionValue(io.lumeer.api.model.ConditionValue)

Example 19 with Constraint

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"));
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) Attribute(io.lumeer.api.model.Attribute) Constraint(io.lumeer.api.model.Constraint) HashMap(java.util.HashMap) DataDocument(io.lumeer.engine.api.data.DataDocument) Document(io.lumeer.api.model.Document) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collection(io.lumeer.api.model.Collection) Test(org.junit.Test)

Example 20 with Constraint

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")));
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) Attribute(io.lumeer.api.model.Attribute) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Constraint(io.lumeer.api.model.Constraint) HashMap(java.util.HashMap) Collection(io.lumeer.api.model.Collection) DataDocument(io.lumeer.engine.api.data.DataDocument) Document(io.lumeer.api.model.Document) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

Constraint (io.lumeer.api.model.Constraint)25 Attribute (io.lumeer.api.model.Attribute)17 Test (org.junit.Test)17 DataDocument (io.lumeer.engine.api.data.DataDocument)15 Document (io.lumeer.api.model.Document)13 Query (io.lumeer.api.model.Query)9 Collection (io.lumeer.api.model.Collection)8 HashMap (java.util.HashMap)5 BigDecimal (java.math.BigDecimal)4 ConstraintType (io.lumeer.api.model.ConstraintType)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Point (com.mongodb.client.model.geojson.Point)2 Position (com.mongodb.client.model.geojson.Position)2 CollectionPurpose (io.lumeer.api.model.CollectionPurpose)2 LinkType (io.lumeer.api.model.LinkType)2 NumberFormat (java.text.NumberFormat)2 ZonedDateTime (java.time.ZonedDateTime)2 Date (java.util.Date)2 List (java.util.List)2 Map (java.util.Map)2