Search in sources :

Example 6 with CollectionPurpose

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

the class StateChangeDetector method detectChanges.

@Override
public void detectChanges(final DocumentEvent documentEvent, final Collection collection) {
    final CollectionPurpose purpose = collection.getPurpose();
    final String stateAttr = purpose.getStateAttributeId();
    if (StringUtils.isNotEmpty(stateAttr) && isAttributeChanged(documentEvent, stateAttr)) {
        final boolean doneState = isDoneState(documentEvent, collection);
        if (!(documentEvent instanceof CreateDocument)) {
            // delete previous due date and assignee events on the document
            if (documentEvent instanceof RemoveDocument || doneState) {
                delayedActionDao.deleteScheduledActions(getResourcePath(documentEvent), Set.of(NotificationType.DUE_DATE_SOON, NotificationType.PAST_DUE_DATE, NotificationType.TASK_ASSIGNED, NotificationType.TASK_REOPENED, NotificationType.STATE_UPDATE, NotificationType.DUE_DATE_CHANGED));
            }
        }
        if (documentEvent instanceof UpdateDocument) {
            // switched back to non-final state, reschedule due dates and assignees
            if (wasDoneState(documentEvent, collection) && !doneState) {
                // create new due date events on the document
                final ZonedDateTime dueDate = getDueDate(documentEvent, collection);
                if (dueDate != null) {
                    delayedActionDao.scheduleActions(getDelayedActions(documentEvent, collection, NotificationType.PAST_DUE_DATE, dueDate));
                    if (dueDate.minus(DUE_DATE_SOON_DAYS, ChronoUnit.DAYS).isAfter(ZonedDateTime.now())) {
                        delayedActionDao.scheduleActions(getDelayedActions(documentEvent, collection, NotificationType.DUE_DATE_SOON, dueDate.minus(DUE_DATE_SOON_DAYS, ChronoUnit.DAYS)));
                    }
                }
                delayedActionDao.scheduleActions(getDelayedActions(documentEvent, collection, NotificationType.TASK_REOPENED, nowPlus()));
            }
        }
        if (!(documentEvent instanceof RemoveDocument)) {
            // create new due date events on the document
            delayedActionDao.scheduleActions(getDelayedActions(documentEvent, collection, NotificationType.STATE_UPDATE, nowPlus()));
        }
    }
}
Also used : RemoveDocument(io.lumeer.engine.api.event.RemoveDocument) UpdateDocument(io.lumeer.engine.api.event.UpdateDocument) ZonedDateTime(java.time.ZonedDateTime) CreateDocument(io.lumeer.engine.api.event.CreateDocument) CollectionPurpose(io.lumeer.api.model.CollectionPurpose)

Example 7 with CollectionPurpose

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

the class AssigneeChangeDetector method detectChanges.

@Override
public void detectChanges(final DocumentEvent documentEvent, final Collection collection) {
    final CollectionPurpose purpose = collection.getPurpose();
    final String assigneeAttr = purpose.getAssigneeAttributeId();
    final boolean doneState = isDoneState(documentEvent, collection);
    if (StringUtils.isNotEmpty(assigneeAttr) && isAttributeChanged(documentEvent, assigneeAttr)) {
        if (!(documentEvent instanceof CreateDocument)) {
            // delete previous due date and assignee events on the document
            delayedActionDao.deleteScheduledActions(getResourcePath(documentEvent), Set.of(NotificationType.DUE_DATE_SOON, NotificationType.PAST_DUE_DATE, NotificationType.TASK_ASSIGNED, NotificationType.TASK_REOPENED, NotificationType.DUE_DATE_CHANGED));
            if (!(documentEvent instanceof RemoveDocument) && !doneState) {
                final ZonedDateTime dueDate = getDueDate(documentEvent, collection);
                if (dueDate != null) {
                    delayedActionDao.scheduleActions(getDelayedActions(documentEvent, collection, NotificationType.PAST_DUE_DATE, dueDate));
                    if (dueDate.minus(DUE_DATE_SOON_DAYS, ChronoUnit.DAYS).isAfter(ZonedDateTime.now())) {
                        delayedActionDao.scheduleActions(getDelayedActions(documentEvent, collection, NotificationType.DUE_DATE_SOON, dueDate.minus(DUE_DATE_SOON_DAYS, ChronoUnit.DAYS)));
                    }
                }
            }
        }
        if (documentEvent instanceof UpdateDocument) {
            delayedActionDao.scheduleActions(getDelayedActions(documentEvent, collection, NotificationType.TASK_UNASSIGNED, nowPlus(), getRemovedAssignees(documentEvent, collection)));
        }
        if (!(documentEvent instanceof RemoveDocument) && !doneState) {
            // create new due date events on the document
            delayedActionDao.scheduleActions(getDelayedActions(documentEvent, collection, NotificationType.TASK_ASSIGNED, nowPlus(), getAddedAssignees(documentEvent, collection)));
        }
    }
}
Also used : RemoveDocument(io.lumeer.engine.api.event.RemoveDocument) ZonedDateTime(java.time.ZonedDateTime) UpdateDocument(io.lumeer.engine.api.event.UpdateDocument) CreateDocument(io.lumeer.engine.api.event.CreateDocument) CollectionPurpose(io.lumeer.api.model.CollectionPurpose)

Example 8 with CollectionPurpose

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

the class TaskUpdateChangeDetector method detectChanges.

@Override
public void detectChanges(final DocumentEvent documentEvent, final Collection collection) {
    final CollectionPurpose purpose = collection.getPurpose();
    final boolean doneState = isDoneState(documentEvent, collection);
    if (!(documentEvent instanceof CreateDocument) && !(documentEvent instanceof RemoveDocument)) {
        // delete previous due date and assignee events on the document
        delayedActionDao.deleteScheduledActions(getResourcePath(documentEvent), Set.of(NotificationType.TASK_UPDATED));
        if (!doneState) {
            final Set<Assignee> observers = getObservers(documentEvent, collection);
            delayedActionDao.scheduleActions(getDelayedActions(documentEvent, collection, NotificationType.TASK_UPDATED, nowPlus(), observers));
            // assignee != initiator, initiator != observer, no change in due date, state, nor assignee => send task update
            final String assigneeAttr = purpose.getAssigneeAttributeId();
            final String dueDateAttr = purpose.getDueDateAttributeId();
            final String stateAttr = purpose.getStateAttributeId();
            if (!isAttributeChanged(documentEvent, assigneeAttr) && !isAttributeChanged(documentEvent, dueDateAttr) && !isAttributeChanged(documentEvent, stateAttr)) {
                if (observers != null) {
                    // prevent double notification when assignee is also an observer
                    final Set<Assignee> assignees = new HashSet<>(getAssignees(documentEvent, collection));
                    observers.forEach(assignee -> {
                        assignees.remove(new Assignee(assignee.getEmail(), true));
                        assignees.remove(new Assignee(assignee.getEmail(), false));
                    });
                    delayedActionDao.scheduleActions(getDelayedActions(documentEvent, collection, NotificationType.TASK_UPDATED, nowPlus(), assignees));
                } else {
                    delayedActionDao.scheduleActions(getDelayedActions(documentEvent, collection, NotificationType.TASK_UPDATED, nowPlus()));
                }
            }
        }
    }
    if (documentEvent instanceof RemoveDocument) {
        // create new due date events on the document
        if (!doneState) {
            delayedActionDao.scheduleActions(getDelayedActions(documentEvent, collection, NotificationType.TASK_REMOVED, nowPlus()));
            delayedActionDao.scheduleActions(getDelayedActions(documentEvent, collection, NotificationType.TASK_REMOVED, nowPlus(), getObservers(documentEvent, collection)));
        }
    }
}
Also used : RemoveDocument(io.lumeer.engine.api.event.RemoveDocument) CreateDocument(io.lumeer.engine.api.event.CreateDocument) CollectionPurpose(io.lumeer.api.model.CollectionPurpose) HashSet(java.util.HashSet)

Example 9 with CollectionPurpose

use of io.lumeer.api.model.CollectionPurpose 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)

Example 10 with CollectionPurpose

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

the class CollectionPurposeCodec method convertFromDocument.

public static CollectionPurpose convertFromDocument(Document bson) {
    if (bson == null) {
        return new CollectionPurpose(CollectionPurposeType.None, new DataDocument());
    }
    String typeString = bson.getString(TYPE);
    CollectionPurposeType purposeType = typeString != null ? CollectionPurposeType.valueOf(typeString) : CollectionPurposeType.None;
    Document metaData = bson.get(META_DATA, Document.class);
    return new CollectionPurpose(purposeType, new DataDocument(metaData == null ? new Document() : metaData));
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) Document(org.bson.Document) DataDocument(io.lumeer.engine.api.data.DataDocument) CollectionPurpose(io.lumeer.api.model.CollectionPurpose) CollectionPurposeType(io.lumeer.api.model.CollectionPurposeType)

Aggregations

CollectionPurpose (io.lumeer.api.model.CollectionPurpose)12 Collection (io.lumeer.api.model.Collection)6 DataDocument (io.lumeer.engine.api.data.DataDocument)6 Attribute (io.lumeer.api.model.Attribute)4 CreateDocument (io.lumeer.engine.api.event.CreateDocument)4 RemoveDocument (io.lumeer.engine.api.event.RemoveDocument)4 Permissions (io.lumeer.api.model.Permissions)3 ZonedDateTime (java.time.ZonedDateTime)3 CollectionPurposeType (io.lumeer.api.model.CollectionPurposeType)2 Constraint (io.lumeer.api.model.Constraint)2 Organization (io.lumeer.api.model.Organization)2 Permission (io.lumeer.api.model.Permission)2 Project (io.lumeer.api.model.Project)2 User (io.lumeer.api.model.User)2 AuthenticatedUser (io.lumeer.core.auth.AuthenticatedUser)2 UpdateDocument (io.lumeer.engine.api.event.UpdateDocument)2 Document (org.bson.Document)2 Before (org.junit.Before)2 Document (io.lumeer.api.model.Document)1 Group (io.lumeer.api.model.Group)1