All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.flowcentraltech.flowcentral.notification.data.NotifMessage Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2021-2024 FlowCentral Technologies Limited.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.flowcentraltech.flowcentral.notification.data;

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

import com.flowcentraltech.flowcentral.common.data.Attachment;
import com.flowcentraltech.flowcentral.common.data.Recipient;
import com.flowcentraltech.flowcentral.configuration.constants.ImportanceType;
import com.flowcentraltech.flowcentral.configuration.constants.NotifMessageFormat;
import com.flowcentraltech.flowcentral.configuration.constants.NotifRecipientType;
import com.flowcentraltech.flowcentral.configuration.constants.NotifType;
import com.tcdng.unify.common.util.StringToken;
import com.tcdng.unify.core.constant.FileAttachmentType;
import com.tcdng.unify.core.util.DataUtils;

/**
 * Notification message object.
 * 
 * @author FlowCentral Technologies Limited
 * @since 1.0
 */
public class NotifMessage {

    private ImportanceType importance;

    private NotifType notifType;

    private NotifMessageFormat format;

    private String template;

    private String from;

    private List subjectTokenList;

    private List templateTokenList;

    private Map params;

    private List recipients;

    private List attachments;
    
    private int sendDelayInMinutes;

    private NotifMessage(ImportanceType importance, NotifType notifType, NotifMessageFormat format, String from,
            String template, List subjectTokenList, List templateTokenList,
            Map params, List recipients, List attachments,
            int sendDelayInMinutes) {
        this.importance = importance;
        this.notifType = notifType;
        this.format = format;
        this.from = from;
        this.template = template;
        this.subjectTokenList = subjectTokenList;
        this.templateTokenList = templateTokenList;
        this.params = params;
        this.recipients = recipients;
        this.attachments = attachments;
        this.sendDelayInMinutes = sendDelayInMinutes;
    }

    public String getTemplate() {
        return template;
    }

    public ImportanceType getImportance() {
        return importance;
    }

    public NotifType getNotifType() {
        return notifType;
    }

    public NotifMessageFormat getFormat() {
        return format;
    }

    public String getFrom() {
        return from;
    }

    public List getSubjectTokenList() {
        return subjectTokenList;
    }

    public List getTemplateTokenList() {
        return templateTokenList;
    }

    public Map getParams() {
        return params;
    }

    public List getRecipients() {
        return recipients;
    }

    public List getAttachments() {
        return attachments;
    }

    public boolean isUseTemplate() {
        return template != null;
    }

    public NotifMessage setSendDelayInMinutes(int sendDelayInMinutes) {
        this.sendDelayInMinutes = sendDelayInMinutes;
        return this;
    }

    public int getSendDelayInMinutes() {
        return sendDelayInMinutes;
    }

    public boolean isWithSendDelayInMinutes() {
        return sendDelayInMinutes > 0;
    }

    public static Builder newBuilder(String template) {
        return new Builder(template);
    }

    public static Builder newBuilder(List subjectTokenList, List templateTokenList) {
        return new Builder(subjectTokenList, templateTokenList);
    }

    public static class Builder {

        private ImportanceType importance;

        private NotifType notifType;

        private NotifMessageFormat format;

        private String template;

        private String from;

        private List subjectTokenList;

        private List templateTokenList;

        private Map params;

        private List recipients;

        private List attachments;
        
        private int sendDelayInMinutes;

        private Builder(String template) {
            this.importance = ImportanceType.LOW;
            this.notifType = NotifType.EMAIL;
            this.format = NotifMessageFormat.HTML;
            this.template = template;
            this.params = new HashMap();
            this.recipients = new ArrayList();
            this.attachments = new ArrayList();
        }

        public Builder(List subjectTokenList, List templateTokenList) {
            if (DataUtils.isBlank(subjectTokenList) || DataUtils.isBlank(templateTokenList)) {
                throw new IllegalArgumentException("Token lists can not be empty.");
            }

            this.importance = ImportanceType.LOW;
            this.notifType = NotifType.EMAIL;
            this.format = NotifMessageFormat.HTML;
            this.subjectTokenList = subjectTokenList;
            this.templateTokenList = templateTokenList;
            this.params = new HashMap();
            this.recipients = new ArrayList();
            this.attachments = new ArrayList();
        }

        public Builder addTORecipient(String name, String contact) {
            recipients.add(new Recipient(NotifRecipientType.TO, name, contact));
            return this;
        }

        public Builder addCCRecipient(String name, String contact) {
            recipients.add(new Recipient(NotifRecipientType.CC, name, contact));
            return this;
        }

        public Builder addBCCRecipient(String name, String contact) {
            recipients.add(new Recipient(NotifRecipientType.BCC, name, contact));
            return this;
        }

        public Builder addRecipient(Recipient recipient) {
            recipients.add(recipient);
            return this;
        }

        public Builder from(String from) {
            this.from = from;
            return this;
        }

        public Builder notifType(NotifType notifType) {
            this.notifType = notifType;
            return this;
        }

        public Builder format(NotifMessageFormat format) {
            this.format = format;
            return this;
        }

        public Builder importance(ImportanceType importance) {
            this.importance = importance != null ? importance : ImportanceType.LOW;
            return this;
        }

        public Builder sendDelayInMinutes(int sendDelayInMinutes) {
            this.sendDelayInMinutes = sendDelayInMinutes;
            return this;
        }

        public Builder addParam(String name, Object val) {
            params.put(name, val);
            return this;
        }

        public Builder addParams(Map params) {
            params.putAll(params);
            return this;
        }

        public Builder addAttachment(FileAttachmentType type, String name, String title, String fileName, byte[] data) {
            attachments.add(Attachment.newBuilder(type).name(name).title(title).fileName(fileName).data(data).build());
            return this;
        }

        public Builder addAttachment(FileAttachmentType type, String name, String title, byte[] data) {
            attachments.add(Attachment.newBuilder(type).name(name).title(title).data(data).build());
            return this;
        }

        public Builder addAttachment(Attachment attachment) {
            attachments.add(attachment);
            return this;
        }

        public boolean isWithRecipients() {
            return !DataUtils.isBlank(recipients);
        }

        public NotifMessage build() {
            if (DataUtils.isBlank(recipients)) {
                throw new IllegalArgumentException("At least one recipient is required.");
            }

            return new NotifMessage(importance, notifType, format, from, template, subjectTokenList, templateTokenList,
                    DataUtils.unmodifiableMap(params), DataUtils.unmodifiableList(recipients),
                    DataUtils.unmodifiableList(attachments), sendDelayInMinutes);
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy