Java

This tutorial provides you with a step-by-step walkthrough of how to build a ‘Doctor’s Appointment App’ to send appointment reminders via SMS using Live Link 365.

This is not a production-ready application. Please take your time to enhance it for production so that it meets your specific business requirements.

The main panel on the right highlights the right piece of code for each step.

Steps

Code

keyboard_arrow_down
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package org.sinch;

import java.util.Date;

// _Step_1
class Doctor {

    private int doctorID;
    private String doctorName;
    private Specialization specialization;

    public int getDoctorID() {
        return doctorID;
    }

    public void setDoctorID(int doctorID) {
        this.doctorID = doctorID;
    }

    public String getDoctorName() {
        return doctorName;
    }

    public void setDoctorName(String doctorName) {
        this.doctorName = doctorName;
    }

    public Specialization getSpecialization() {
        return specialization;
    }

    public void setSpecialization(Specialization specialization) {
        this.specialization = specialization;
    }
}

// _Step_2
class Patient {
    private int patientID;
    private String patientName;

    public int getPatientID() {
        return patientID;
    }

    public void setPatientID(int patientID) {
        this.patientID = patientID;
    }

    public String getPatientName() {
        return patientName;
    }

    public void setPatientName(String patientName) {
        this.patientName = patientName;
    }
}

// _Step_3
class Specialization {
    private int specializationID;
    private String specializationName;

    public Specialization(int specializationID, String specializationName) {
        super();
        this.specializationID = specializationID;
        this.specializationName = specializationName;
    }

    public int getSpecializationID() {
        return specializationID;
    }

    public void setSpecializationID(int specializationID) {
        this.specializationID = specializationID;
    }

    public String getSpecializationName() {
        return specializationName;
    }

    public void setSpecializationName(String specializationName) {
        this.specializationName = specializationName;
    }
}

// _Step_4
class Appointment {
    private Doctor doctor;
    private Patient patient;
    private Date date;
    private Specialization specialization;

    public Doctor getDoctor() {
        return doctor;
    }

    public void setDoctor(Doctor doctor) {
        this.doctor = doctor;
    }

    public Patient getPatient() {
        return patient;
    }

    public void setPatient(Patient patient) {
        this.patient = patient;
    }

    public Specialization getSpecialization() {
        return specialization;
    }

    public void setSpecialization(Specialization specialization) {
        this.specialization = specialization;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Appointment(Doctor doctor, Patient patient, Date date) {
        this.doctor = doctor;
        this.specialization = this.doctor.getSpecialization();
        this.patient = patient;
        this.date = date;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# _Step_5
baseUrl=https://livelink.sapdigitalinterconnect.com/api
# Insert your generated app key here
appKey=appKey
# Insert your generated app secret here
appSecret=appSecret

patient_name=John Doe
patient_number=number
doctor_name=Dr. Donald
specialization=Diabetologist
date=20170411
time=0400
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package org.sinch;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import org.json.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

// _Step_6
class RemindAppointment
{
    static Properties config;
    public static Properties loadProperties(String filePath) throws IOException {
        Properties prop = new Properties();
        InputStream input = null;
        ClassLoader loader = Thread.currentThread().getContextClassLoader();

        try {
            input = loader.getResourceAsStream(filePath);
            // load a properties file
            prop.load(input);
        } catch (IOException ex) {
            ex.printStackTrace();
            throw ex;
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return prop;
    }

    public static void main(String[] args) throws IOException, ParseException
    {
        Doctor doctor = new Doctor();
        Patient patient = new Patient();
        Date bookingDate = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
        String date = "";
        String time = "";
        int pid = 0;
        int did = 0;
        int sid = 0;

        config = loadProperties("config.properties");

        patient.setPatientID(pid++);
        patient.setPatientName(config.getProperty("patient_name"));
        doctor.setDoctorID(did++);
        doctor.setDoctorName(config.getProperty("doctor_name"));
        doctor.setSpecialization(new Specialization(sid++, config.getProperty("specialization")));
        date = config.getProperty("date");
        time = config.getProperty("time");
        bookingDate = formatter.parse(date + time);

        Appointment appointment = new Appointment(doctor, patient, bookingDate);
        String doctorName = appointment.getDoctor().getDoctorName();
        String patientName = appointment.getPatient().getPatientName();
        String specializationName = appointment.getDoctor().getSpecialization().getSpecializationName();
        Date appointmentDate = appointment.getDate();

        // _Step_9
        String appointmentMessage = "Mr./Mrs " + patientName + ", you have an appointment with " + doctorName + " (" + specializationName + ")" + " scheduled on " + appointmentDate;
        String appKey = config.getProperty("appKey"); //Replace with an App Key generated on the App Key area of the portal (Integrate - App Keys)
        String appSecret = config.getProperty("appSecret"); //Replace with an App Secret generated on the App Key area of the portal (Integrate - App Keys)
        String destinationNumber = config.getProperty("patient_number"); //substitute with your destination number. Phone numbers have to be in E.164 format.
        String baseUrl = config.getProperty("baseUrl"); //substitute with your destination number.
        MessageHandler handler = new MessageHandler(baseUrl);
        handler.sendSMS(appointmentMessage, destinationNumber, handler.getAccessToken(appKey, appSecret));
    }

    static String readFile(String path, Charset encoding)
    throws IOException
    {
        byte[] encoded = Files.readAllBytes(Paths.get(path));
        return new String(encoded, encoding);
    }
}
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
package org.sinch;

import org.json.*;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Base64;

public class MessageHandler
{
    private String baseURL;

    public MessageHandler(String baseUrl) {
        this.baseURL = baseUrl;
    }

    // _Step_7
    public String getAccessToken(String appKey, String appSecret) throws MalformedURLException, IOException
    {
        String token_url = this.baseURL + "/oauth/token";
        //Encode your App Key and App Secret in Base64
        String credentials = appKey + ":" + appSecret;
        String credentialsEncoded = Base64.getEncoder().encodeToString(credentials.getBytes());
        //      System.out.println(credentialsEncoded);
        String param = "grant_type=client_credentials";
        String authTokenJsonString = this.postForAccessToken(token_url, param, credentialsEncoded);
        JSONObject object = new JSONObject(authTokenJsonString);
        //At this point, you will have retrieved your json string, which should be parsed.
        //There are many third parties available for this.
        //From the json string, extract the "access_token" attribute and return it in this function.
        //return "access_token"; //Return the "access_token" field from the json string.
        return object.getString("access_token");
    }

    public String postForAccessToken(String path, String data, String authenticationToken) throws MalformedURLException, IOException
    {
        URL url = new URL(path);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Authorization", "Basic " + authenticationToken);
        connection.setDoOutput(true);
        DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
        writer.writeBytes(data);
        writer.flush();
        writer.close();
        InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream());
        BufferedReader reader = new BufferedReader(inputStreamReader);
        StringBuilder response = new StringBuilder();
        String inputLine;
        while ((inputLine = reader.readLine()) != null) response.append(inputLine);
        reader.close();
        return response.toString();
    }

    // _Step_8
    public void sendSMS(String message, String destination, String authenticationToken)
    {
        //Make sure to escape all double quotes inside the json, only the outer quotes for each string should be left unescaped.
//        String postBody = "{" + " \"message\": \"" + message + "\"," //This is the string that will be shown to the user.
//                          + " \"destination\": \"" + destination + "\"," //Place here the destination number(s). Phone numbers have to be in E.164 format.
//                          + "}";
        JSONObject postBody = new JSONObject()
                .put("message", message)
                .put("destination", destination);
        //
        String url = this.baseURL + "/v2/sms";
        try
        {
            this.post(url, postBody.toString(), authenticationToken);
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //This is a method that does an http post, given the path/url, data (a json string).
    public String post(String path, String data, String authenticationToken) throws MalformedURLException, IOException
    {
        URL url = new URL(path);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        if (authenticationToken != null && !authenticationToken.isEmpty())
        {
            connection.setRequestProperty("Authorization", "Bearer " + authenticationToken);
        }
        System.out.println(authenticationToken);
        connection.setDoOutput(true);
        DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
        writer.writeBytes(data);
        writer.flush();
        writer.close();
        InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream());
        BufferedReader reader = new BufferedReader(inputStreamReader);
        StringBuilder response = new StringBuilder();
        String inputLine;
        while ((inputLine = reader.readLine()) != null) response.append(inputLine);
        reader.close();
        return response.toString();
    }
}