<commit_before>package org.labkey.wnprc_ehr.notification;

import org.apache.commons.lang3.StringUtils;
import org.labkey.api.data.Container;
import org.labkey.api.data.Results;
import org.labkey.api.data.SimpleFilter;
import org.labkey.api.ldk.notification.NotificationService;
import org.labkey.api.module.Module;
import org.labkey.api.module.ModuleLoader;
import org.labkey.api.query.FieldKey;
import org.labkey.api.query.QueryHelper;
import org.labkey.api.security.User;
import org.labkey.api.security.UserManager;
import org.labkey.api.security.UserPrincipal;
import org.labkey.api.util.MailHelper;
import org.labkey.wnprc_ehr.WNPRC_EHRModule;

import javax.mail.Address;
import javax.mail.Message;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import static org.labkey.api.search.SearchService._log;
import static org.labkey.ehr.pipeline.GeneticCalculationsJob.getContainer;

public class ViralLoadQueueNotification extends AbstractEHRNotification
{
    public Integer rowId;
    public User currentUser;
    public String animalId;
    public String hostName;
    public String requestorEmail = "";
    public String notifyEmails = "";
    public Container container;
    public final String openResearchPortal = "https://openresearch.labkey.com/study/ZEST/Private/dataset.view?datasetId=5080";

    public ViralLoadQueueNotification(Module owner)
    {
        super(owner);
    }

    public ViralLoadQueueNotification(Module owner, Integer rowid, User currentuser, Container c, String hostname) throws SQLException
    {
        super(owner);
        rowId = rowid;
        currentUser = currentuser;
        hostName = hostname;
        container = c;
        this.setUp();
    }

    public void setUp() throws SQLException
    {
        SimpleFilter filter = new SimpleFilter(FieldKey.fromString("Key"), rowId);
        QueryHelper viralLoadQuery = new QueryHelper(container, currentUser, "lists", "vl_sample_queue");

        // Define columns to get
        List<FieldKey> columns = new ArrayList<>();
        columns.add(FieldKey.fromString("Key"));
        columns.add(FieldKey.fromString("CreatedBy"));
        columns.add(FieldKey.fromString("Status"));
        columns.add(FieldKey.fromString("Id"));
        columns.add(FieldKey.fromString("emails"));

        Integer userid = null;
        String animalid = null;
        String notifyEmails = null;

        // Execute the query
        try (Results rs = viralLoadQuery.select(columns, filter)){
            if (rs.next()){
                userid = rs.getInt(FieldKey.fromString("CreatedBy"));
                animalid = rs.getString(FieldKey.fromString("Id"));
                notifyEmails = rs.getString(FieldKey.fromString("emails"));
            }
        }
        if (userid != null){
            User u = UserManager.getUser(userid);
            if (u != null){
                u.getEmail();
                this.requestorEmail = u.getEmail();
                this.animalId = animalid;
            }
        }
        if (notifyEmails != null){
            this.notifyEmails = notifyEmails;
        }

    }

    @Override
    public String getCategory()
    {
        return ModuleLoader.getInstance().getModule(WNPRC_EHRModule.class).getName();
    }

    public String getName()
    {
        return "Viral Load Queue Notification";
    }

    public String getScheduleDescription()
    {
        return "As soon as Viral Load request has been completed";
    }

    public String getDescription()
    {
        return "This notification gets sent every time there is a viral load queue completed";
    }

    @Override
    public String getEmailSubject(Container c)
    {
        return "[EHR Services] Viral load request has been completed as of " + _dateTimeFormat.format(new Date());
    }

    @Override
    public String getMessageBodyHTML(Container c, User u)
    {
        final StringBuilder msg = new StringBuilder();
        Date now = new Date();
        msg.append("<p>There was a viral load sample completed on " +
                AbstractEHRNotification._dateFormat.format(now) +
                " at " +
                AbstractEHRNotification._timeFormat.format(now) +
                ".</p>");

        msg.append("<p>Click <a href=\"" +
                hostName +
                "/list/WNPRC/WNPRC_Units/Research_Services/Virology_Services/viral_load_sample_tracker/details.view?listId=3&pk=" +
                rowId +
                "\">here</a> to review the request.</p>");

        msg.append("<p>View the animal virology results " +
                "<a href=\"" +
                openResearchPortal +
                "&Dataset.ParticipantId~eq=" +
                animalId +
                " >here</a>.</p>");


        return msg.toString();<commit_msg>Further customize email body and add modified by email<commit_after>package org.labkey.wnprc_ehr.notification;

import org.apache.commons.lang3.StringUtils;
import org.labkey.api.data.Container;
import org.labkey.api.data.Results;
import org.labkey.api.data.SimpleFilter;
import org.labkey.api.ldk.notification.NotificationService;
import org.labkey.api.module.Module;
import org.labkey.api.module.ModuleLoader;
import org.labkey.api.query.FieldKey;
import org.labkey.api.query.QueryHelper;
import org.labkey.api.security.User;
import org.labkey.api.security.UserManager;
import org.labkey.api.security.UserPrincipal;
import org.labkey.api.util.MailHelper;
import org.labkey.wnprc_ehr.WNPRC_EHRModule;

import javax.mail.Address;
import javax.mail.Message;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import static org.labkey.api.search.SearchService._log;
import static org.labkey.ehr.pipeline.GeneticCalculationsJob.getContainer;

public class ViralLoadQueueNotification extends AbstractEHRNotification
{
    public Integer rowId;
    public User currentUser;
    public String animalId;
    public String hostName;
    public String requestorEmail = "";
    public String notifyEmails = "";
    public Container container;
    public String fullName = "";
    public String modifiedByEmail = "";
    public final String openResearchPortal = "https://openresearch.labkey.com/study/ZEST/Private/dataset.view?datasetId=5080";

    public ViralLoadQueueNotification(Module owner)
    {
        super(owner);
    }

    public ViralLoadQueueNotification(Module owner, Integer rowid, User currentuser, Container c, String hostname) throws SQLException
    {
        super(owner);
        rowId = rowid;
        currentUser = currentuser;
        hostName = hostname;
        container = c;
        this.setUp();
    }

    public void setUp() throws SQLException
    {
        SimpleFilter filter = new SimpleFilter(FieldKey.fromString("Key"), rowId);
        QueryHelper viralLoadQuery = new QueryHelper(container, currentUser, "lists", "vl_sample_queue");

        // Define columns to get
        List<FieldKey> columns = new ArrayList<>();
        columns.add(FieldKey.fromString("Key"));
        columns.add(FieldKey.fromString("CreatedBy"));
        columns.add(FieldKey.fromString("Status"));
        columns.add(FieldKey.fromString("Id"));
        columns.add(FieldKey.fromString("emails"));
        columns.add(FieldKey.fromString("ModifiedBy"));

        Integer userid = null;
        String animalid = null;
        String notifyEmails = null;
        Integer modifiedBy = null;

        // Execute the query
        try (Results rs = viralLoadQuery.select(columns, filter))
        {
            if (rs.next()){
                userid = rs.getInt(FieldKey.fromString("CreatedBy"));
                animalid = rs.getString(FieldKey.fromString("Id"));
                notifyEmails = rs.getString(FieldKey.fromString("emails"));
                modifiedBy = rs.getInt(FieldKey.fromString("ModifiedBy"));
            }
        }
        if (userid != null)
        {
            User u = UserManager.getUser(userid);
            if (u != null)
            {
                this.fullName = u.getFullName();
                this.requestorEmail = u.getEmail();
                this.animalId = animalid;
            }
        }
        if (notifyEmails != null)
        {
            this.notifyEmails = notifyEmails;
        }
        if (modifiedBy != null)
        {
         User mod = UserManager.getUser(modifiedBy);
         if (mod != null)
         {
             this.modifiedByEmail = mod.getEmail();
         }
        }

    }

    @Override
    public String getCategory()
    {
        return ModuleLoader.getInstance().getModule(WNPRC_EHRModule.class).getName();
    }

    public String getName()
    {
        return "Viral Load Queue Notification";
    }

    public String getScheduleDescription()
    {
        return "As soon as Viral Load request has been completed";
    }

    public String getDescription()
    {
        return "This notification gets sent every time there is a viral load queue completed";
    }

    @Override
    public String getEmailSubject(Container c)
    {
        return "[EHR Server] Viral load results completed on " + _dateTimeFormat.format(new Date());
    }

    @Override
    public String getMessageBodyHTML(Container c, User u)
    {
        final StringBuilder msg = new StringBuilder();
        Date now = new Date();
        msg.append("<p>Hello " +
                fullName +
                ",</p>");
        msg.append("<p>Good news - Virology Services has completed viral load testing on a sample(s) you submitted. " +
                "The results can be found in the Zika portal, and using the following " +
                "<a href=\"" +
                openResearchPortal +
                "&Dataset.ParticipantId~eq=" +
                animalId +
                "\">link</a>. ");
        msg.append("Please feel free to contact " +
                "<a href=\"mailto:" +
                modifiedByEmail +
                "\">" +
                modifiedByEmail +
                "</a> " +
                "if you have any questions or concerns. We look forward to serving you again in the future.</p>");


        return msg.toString();
{'fraction_non_alphanumeric': 0.10523749146591242, 'fraction_numerical': 0.0010728567248610163, 'mean_word_length': 2.451363177381353, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '421947', 'n_tokens_mistral': 2882, 'n_tokens_neox': 2783, 'n_words': 649}