Skip to content
This repository has been archived by the owner on Jan 21, 2021. It is now read-only.

Commit

Permalink
MK: Resolved #124 . Added test case for this.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maimoona Kausar committed Aug 1, 2016
1 parent fb1dae0 commit 139a49c
Show file tree
Hide file tree
Showing 4 changed files with 382 additions and 5 deletions.
129 changes: 126 additions & 3 deletions opensrp-core/src/main/java/org/opensrp/scheduler/Alert.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public Alert(String providerId, String entityId, String beneficiaryType, AlertTy
this.details = details;
}

public void markAlertAsClosed(String reasonForClose) {
public Alert markAlertAsClosed(String reasonForClose) {
if(alertStatus.equalsIgnoreCase(AlertStatus.closed.name())
|| alertStatus.equalsIgnoreCase(AlertStatus.complete.name())){
throw new IllegalStateException("Alert was found "+alertStatus);
Expand All @@ -91,9 +91,11 @@ public void markAlertAsClosed(String reasonForClose) {
this.alertStatus = AlertStatus.closed.name();
this.dateClosed = new DateTime().toLocalDate().toString();
this.isActive = false;

return this;
}

public void markAlertAsComplete(String completionDate) {
public Alert markAlertAsComplete(String completionDate) {
if(alertStatus.equalsIgnoreCase(AlertStatus.closed.name())
|| alertStatus.equalsIgnoreCase(AlertStatus.complete.name())){
throw new IllegalStateException("Alert was found "+alertStatus);
Expand All @@ -103,6 +105,8 @@ public void markAlertAsComplete(String completionDate) {
this.alertStatus = AlertStatus.complete.name();
this.dateClosed = new DateTime().toLocalDate().toString();
this.isActive = false;

return this;
}

public String providerId() {
Expand Down Expand Up @@ -167,6 +171,9 @@ public long timestamp() {
}

public Map<String, String> details() {
if(details == null){
details = new HashMap<>();
}
return details;
}

Expand Down Expand Up @@ -198,7 +205,123 @@ String getTriggerType() {
String getAlertType() {
return alertType;
}
@Override
public String getTriggerName() {
return triggerName;
}

public void setTriggerName(String triggerName) {
this.triggerName = triggerName;
}

public String getTriggerCode() {
return triggerCode;
}

public void setTriggerCode(String triggerCode) {
this.triggerCode = triggerCode;
}

public String getStartDate() {
return startDate;
}

public void setStartDate(String startDate) {
this.startDate = startDate;
}

public String getExpiryDate() {
return expiryDate;
}

public void setExpiryDate(String expiryDate) {
this.expiryDate = expiryDate;
}

public String getAlertStatus() {
return alertStatus;
}

public void setAlertStatus(String alertStatus) {
this.alertStatus = alertStatus;
}

public String getClosingPeriod() {
return closingPeriod;
}

public void setClosingPeriod(String closingPeriod) {
this.closingPeriod = closingPeriod;
}

public String getDateClosed() {
return dateClosed;
}

public void setDateClosed(String dateClosed) {
this.dateClosed = dateClosed;
}

public String getDateComplete() {
return dateComplete;
}

public void setDateComplete(String dateComplete) {
this.dateComplete = dateComplete;
}

public String getReasonClosed() {
return reasonClosed;
}

public void setReasonClosed(String reasonClosed) {
this.reasonClosed = reasonClosed;
}

public Boolean getIsActive() {
return isActive;
}

public void setIsActive(Boolean isActive) {
this.isActive = isActive;
}

public long getTimestamp() {
return timestamp;
}

public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}

public Map<String, String> getDetails() {
return details;
}

public void setDetails(Map<String, String> details) {
this.details = details;
}

public void setProviderId(String providerId) {
this.providerId = providerId;
}

public void setEntityId(String entityId) {
this.entityId = entityId;
}

public void setBeneficiaryType(String beneficiaryType) {
this.beneficiaryType = beneficiaryType;
}

public void setAlertType(String alertType) {
this.alertType = alertType;
}

public void setTriggerType(String triggerType) {
this.triggerType = triggerType;
}

@Override
public boolean equals(Object o) {
return EqualsBuilder.reflectionEquals(this, o, "timeStamp", "revision");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package org.opensrp.scheduler.repository;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.ektorp.ComplexKey;
import org.ektorp.CouchDbConnector;
import org.ektorp.support.GenerateView;
import org.ektorp.support.View;
import org.joda.time.DateTime;
import org.motechproject.dao.MotechBaseRepository;
import org.opensrp.common.AllConstants;
import org.opensrp.dto.AlertStatus;
import org.opensrp.scheduler.Action;
import org.opensrp.scheduler.Alert;
import org.opensrp.scheduler.Alert.AlertType;
import org.opensrp.scheduler.Alert.TriggerType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -73,7 +80,7 @@ public List<Alert> findActiveAlertByEntityIdTriggerName(String entityId, String
@View(name = "alert_by_entityId_active",
map = "function(doc) { " +
"if(doc.type === 'Alert' && doc.isActive) {" +
"emit([doc.entityId], null)} " +
"emit(doc.entityId, null)} " +
"}")
public List<Alert> findActiveAlertByEntityId(String entityId) {
return db.queryView(createQuery("alert_by_entityId_active").key(entityId).includeDocs(true), Alert.class);
Expand Down Expand Up @@ -138,6 +145,36 @@ public void markAlertAsCompleteFor(String providerId, String entityId, String tr
db.executeBulk(existingAlerts);
}

public void addOrUpdateScheduleNotificationAlert(String beneficiaryType, String entityId, String providerId,
String triggerName, String triggerCode, AlertStatus alertStatus, DateTime startDate, DateTime expiryDate) {
List<Alert> existingAlerts = findActiveAlertByProviderEntityIdTriggerName(providerId, entityId, triggerName);
if (existingAlerts.size() > 1) {
logger.warn(MessageFormat.format("Found more than one active alerts for the combination of "
+ "providerId: {0}, entityId: {1} and triggerName: {2}", providerId, entityId, triggerName));
}

if(existingAlerts.size() == 0){
add(new Alert(providerId, entityId, beneficiaryType, AlertType.notification, TriggerType.schedule, triggerName, triggerCode, startDate, expiryDate, alertStatus, null));
}
else {
Alert al = existingAlerts.get(0);
// if visit code is same then update otherwise add another record
if(StringUtils.isNotBlank(al.triggerCode()) && StringUtils.isNotBlank(triggerCode)
&& al.triggerCode().equalsIgnoreCase(triggerCode)){
al.setAlertStatus(alertStatus.name());
al.setStartDate(startDate.toString());
al.setExpiryDate(expiryDate.toString());
al.details().put(alertStatus.name()+":start", startDate.toString());
al.details().put(alertStatus.name()+":end", expiryDate.toString());

update(al);
}
else {
add(new Alert(providerId, entityId, beneficiaryType, AlertType.notification, TriggerType.schedule, triggerName, triggerCode, startDate, expiryDate, alertStatus, null));
}
}
}

public void markAlertAsCompleteFor(String entityId, String triggerName, String completionDate) {
List<Alert> existingAlerts = findActiveAlertByEntityIdTriggerName(entityId, triggerName);
if (existingAlerts.size() > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public List<Alert> findAlertByEntityIdScheduleAndTimeStamp(String entityId, Stri

public void alertForBeneficiary(String beneficiaryType, String caseID, String anmIdentifier, String scheduleName, String visitCode, AlertStatus alertStatus, DateTime startDate, DateTime expiryDate) {
allActions.addOrUpdateAlert(new Action(caseID, anmIdentifier, ActionData.createAlert(beneficiaryType, scheduleName, visitCode, alertStatus, startDate, expiryDate)));
allAlerts.add(new Alert(anmIdentifier, caseID, beneficiaryType, AlertType.notification, TriggerType.schedule, scheduleName, visitCode, startDate, expiryDate, alertStatus, null));
allAlerts.addOrUpdateScheduleNotificationAlert(beneficiaryType, caseID, anmIdentifier, scheduleName, visitCode, alertStatus, startDate, expiryDate);
}

public void markAllAlertsAsInactive(String entityId) {
Expand Down
Loading

0 comments on commit 139a49c

Please sign in to comment.