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