Skip to content

Commit

Permalink
Merge pull request #67 from opensrp/revert-messageservice-deletion
Browse files Browse the repository at this point in the history
revert deletion of message service
  • Loading branch information
ndegwamartin authored Dec 11, 2021
2 parents 9f50719 + 210209e commit 7138656
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions src/main/java/org/opensrp/connector/rapidpro/MessageService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package org.opensrp.connector.rapidpro;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONException;
import org.opensrp.domain.Camp;
import org.smartregister.domain.Client;
import org.smartregister.domain.Event;
import org.opensrp.service.ClientService;
import org.opensrp.service.RapidProServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MessageService {

private static Logger logger = LogManager.getLogger(MessageService.class.toString());

private RapidProServiceImpl rapidproService;

private ClientService clientService;

public MessageService() {

}

@Autowired
public MessageService(RapidProServiceImpl rapidproService, ClientService clientService) {

this.rapidproService = rapidproService;
this.clientService = clientService;
}

public void sentMessageToClient(MessageFactory messageFactory, List<Event> events, Camp camp) throws JSONException {

if (events != null) {
for (Event event : events) {
/* Map<String, String> data = action.data();
logger.info("sentMessageToClient actiondata:" + data.toString());*/
if (event.getEntityType().equalsIgnoreCase(ClientType.child.name())) {
Client child = clientService.find(event.getBaseEntityId());
if (child != null) {
logger.info("sending message to child childBaseEntityId:" + child.getBaseEntityId());
Map<String, List<String>> relationships = child.getRelationships();
String motherId = relationships.get("mother").get(0);
Client mother = clientService.find(motherId);
logger.info("sending message to mother moterBaseEntityId:" + mother.getBaseEntityId());
generateDataAndsendMessageToRapidpro(mother, ClientType.child, messageFactory, camp);
}
} else if (event.getEntityType().equalsIgnoreCase(ClientType.mother.name())) {
Client mother = clientService.find(event.getBaseEntityId());
if (mother != null) {
logger.info("sending message to mother moterBaseEntityId:" + mother.getBaseEntityId());
generateDataAndsendMessageToRapidpro(mother, ClientType.mother, messageFactory, camp);
}

}
}
} else {
logger.info("No vaccine data Found Today");
}
}

private void generateDataAndsendMessageToRapidpro(Client client, ClientType clientType, MessageFactory messageFactory,
Camp camp) {

Map<String, Object> attributes = new HashMap<>();
attributes = client.getAttributes();
List<String> urns;
urns = new ArrayList<String>();
if (attributes.containsKey("phoneNumber")) {
logger.info("sending mesage to mobileno:" + addExtensionToMobile((String) attributes.get("phoneNumber")));
urns.add("tel:" + addExtensionToMobile((String) attributes.get("phoneNumber")));
List<String> contacts;
contacts = new ArrayList<String>();
List<String> groups = new ArrayList<String>();
rapidproService.sendMessage(urns, contacts, groups,
messageFactory.getClientType(clientType).message(client, camp, null), "");
}
}

private String addExtensionToMobile(String mobile) {
String phoneNumber;
if (mobile.length() == 10) {
phoneNumber = new StringBuilder().append("+880").append(mobile).toString();
} else if (mobile.length() > 10) {
phoneNumber = new StringBuilder().append("+880").append(mobile.substring(mobile.length() - 10)).toString();
} else {
throw new IllegalArgumentException("invalid mobile no!!");
}
return phoneNumber;

}

}

0 comments on commit 7138656

Please sign in to comment.