在Java

时间:2018-08-21 07:47:52

标签: java

我有一种情况,必须检查数据库中是否已经存在模板。如果存在,我必须根据现有模板返回一条消息。

我有几个模板:电子邮件,信函,短信。如果它们全部都存在,我必须返回“所有模板已经存在”。

如果仅存在电子邮件模板,那么我必须返回,仅电子邮件模板存在,对于Letter和Sms模板也是如此。

代码:

for (EventVO eventVO: eventModuleList) {
    List <EmailTemplateMaster> emailTemplateList = communicationDAO
        .checkEmailTemplateExist(eventVO.getEventCode());
    if (CollectionUtils.isNotEmpty(emailTemplateList)) {
        emailTemplateExist = true;
    }
    List <LetterTemplateMaster> letterTemplateList = communicationDAO
        .checkLetterTemplateExist(eventVO.getEventCode());
    if (CollectionUtils.isNotEmpty(letterTemplateList)) {
        letterTemplateExist = true;
    }
    List <SmsTemplateMaster> smsTemplateList = communicationDAO
        .checkSmsTemplateExist(eventVO.getEventCode());
    if (CollectionUtils.isNotEmpty(smsTemplateList)) {
        smsTemplateExist = true;
    }
    if (emailTemplateExist && letterTemplateExist && smsTemplateExist) {
        templateExist = CommunicationConstants.ALL_TEMPLATE_EXIST;
    }
    if (emailTemplateExist || !letterTemplateExist && !smsTemplateExist) {
        templateExist = CommunicationConstants.EMAIL_TEMPLATE_EXIST;
    }
    if (!emailTemplateExist && letterTemplateExist && !smsTemplateExist) {
        templateExist = CommunicationConstants.LETTER_TEMPLATE_EXIST;
    }
    if (!emailTemplateExist && !letterTemplateExist && smsTemplateExist) {
        templateExist = CommunicationConstants.SMS_TEMPLATE_EXIST;
    }
}

我能知道检查退出模板的布尔值是否存在的最简单方法。

基于退出,我必须发送相应的消息。

public static final String ALL_TEMPLATE_EXIST = "Email, Letter and Sms Template already exist for the selected event."; 
public static final String EMAIL_TEMPLATE_EXIST = "Email Template already exist for the selected event.";
public static final String SMS_TEMPLATE_EXIST = "Sms Template already exist for the selected event.";
public static final String LETTER_TEMPLATE_EXIST = "Email Letter Template already exist for the selected event.";
public static final String EMAIL_SMS_TEMPLATE_EXIST = "Email and Sms Template already exist for the selected event.";
public static final String EMAIL_LETTER_TEMPLATE_EXIST = "Email and Letter Template already exist for the selected event.";
public static final String SMS_LETTER_TEMPLATE_EXIST = "Sms and Letter Template already exist for the selected event.";

5 个答案:

答案 0 :(得分:2)

您可以“准备”布尔值与CommunicationConstants类中的消息之间的映射:

public class CommunicationConstants
{
    private static final Map<List<Boolean>, String> CONSTANTS = new HashMap<>();
    static
    {
        CONSTANTS.put(Arrays.asList(true, true, true), "Email, Letter and Sms Template already exist for the selected event.");
        CONSTANTS.put(Arrays.asList(true, true, false), "Email and Sms Template already exist for the selected event.");
        CONSTANTS.put(Arrays.asList(true, false, true), "Email and Letter Template already exist for the selected event.");
        CONSTANTS.put(Arrays.asList(false, true, true), "Sms and Letter Template already exist for the selected event.");
        CONSTANTS.put(Arrays.asList(true, false, false), "Email Template already exist for the selected event.");
        CONSTANTS.put(Arrays.asList(false, true, false), "Sms Template already exist for the selected event.");
        CONSTANTS.put(Arrays.asList(false, false, true), "Letter Template already exist for the selected event.");
    }

    public static String getMessage(boolean emailExists, boolean smsExists, boolean letterExists)
    {
        return CONSTANTS.get(Arrays.asList(emailExists, smsExists, letterExists));
    }
} 

然后检索它的代码将是单行代码:

System.out.println(CommunicationConstants.getMessage(true, false, true));

答案 1 :(得分:0)

如果可以更改数据库模型,请进行以下更改:

1-将所有布尔值更改为具有以下允许值的VARCHAR()模板类型=> ALL_TEMPLATE_EXIST,EMAIL_TEMPLATE_EXIST,LETTER_TEMPLATE_EXIST,SMS_TEMPLATE_EXIST

2-从数据库中读取值后,仅使用一个行代码来设置您的值:

templateExist = CommunicationConstants.valueOf(thePropertyValueYouReadFromDataBase);

编辑

通过此更改,决定哪个模板已经存在将取决于插入模板的业务代码,仅读取部分实际读取的内容的值

答案 2 :(得分:0)

我会这样做。我发现它更清晰,但显然每个人的思想都不同。

if (emailTemplateExist) {
    templateExist = letterTemplateExist && smsTemplateExist ? CommunicationConstants.ALL_TEMPLATE_EXIST: CommunicationConstants.EMAIL_TEMPLATE_EXIST;
}
else{
    if (letterTemplateExist && !smsTemplateExist)
        templateExist = CommunicationConstants.LETTER_TEMPLATE_EXIST;
    else if (!letterTemplateExist && smsTemplateExist)
        templateExist = CommunicationConstants.SMS_TEMPLATE_EXIST;
}

// There is a possiblity that you will end up with a null value in templateExist so you should initialize it with some default.

答案 3 :(得分:0)

我认为您遗漏了几种情况……但是,可以通过嵌套if语句来做到这一点。请注意,我在enum中添加了一个值,该值表示数据库中不存在模板的情况。使用这种方法,您将需要电子邮件和短信电子邮件和信件以及字母和短信的其他enum值。如果您确定这些情况永远不会发生,那么这种方法涵盖了太多。

public static void main(String[] args) {

    boolean emailTemplateExist = true;
    boolean letterTemplateExist = true;
    boolean smsTemplateExist = true;

    CommunicationConstants templateExist = CommunicationConstants.NO_TEMPLATE_EXIST;

    if (emailTemplateExist) {
        if (letterTemplateExist) {
            if (smsTemplateExist) {
                // email exists, letter exists, sms exists ==> 3/3
                templateExist = CommunicationConstants.ALL_TEMPLATE_EXIST;
            } else {
                // email exists, letter exists, sms does not exist ==> 2/3
                // what to do here?

                /*
                 *  what to do in this case? The enum has no value for that...
                 */
            }
        } else {
            if (smsTemplateExist) {
                // email exists, letter does not exist, sms exists ==> 2/3

                /*
                 *  what to do in this case? The enum has no value for that...
                 */
            } else {
                // email exists, letter does not exist, sms does not exist ==> 1/3

            }
        }
    } else {
        if (letterTemplateExist) {
            if (smsTemplateExist) {
                // email does not exist, letter exists, sms exists ==> 2/3

                /*
                 *  what to do in this case? The enum has no value for that...
                 */
            } else {
                // email does not exist, letter exists, sms does not exist ==> 1/3
                templateExist = CommunicationConstants.LETTER_TEMPLATE_EXIST;
            }
        } else {
            if (smsTemplateExist) {
                // email does not exist, letter does not exist, sms exists ==> 1/3
                templateExist = CommunicationConstants.SMS_TEMPLATE_EXIST;
            } else {
                // email does not exist, letter does not exist, sms does not exist ==> 0/3

                /* 
                 * by initializing the return value with something like "NO_TEMPLATE_EXIST",
                 * you can omit this else block entirely
                 */
                templateExist = CommunicationConstants.NO_TEMPLATE_EXIST;
            }
        }
    }

    System.out.println(templateExist.toString());

}

enum CommunicationConstants {
    ALL_TEMPLATE_EXIST,
    EMAIL_TEMPLATE_EXIST,
    LETTER_TEMPLATE_EXIST,
    SMS_TEMPLATE_EXIST,
    NO_TEMPLATE_EXIST
}

只需处理初始值(当前均为true)。

答案 4 :(得分:0)

以下代码使用单个模板生成所有组合:

firstChild

测试输出:

public class Main {

    enum TemplateType {
        Email, Sms, Letter
    }

    private static final String MSG_EXISTS = "%s Template%s already exist%s for the selected event.";
    private static int testCounter;

    public static void main(String[] args) {
        // Generates all combinations
        for (testCounter = 0; testCounter < 8; testCounter++) {
            System.out.println(testCounter + ". " + evaluate());
        }
    }

    private static String evaluate() {

        List<String> templateList = Arrays.stream(TemplateType.values())
                .filter(Main::checkExistence)
                .map(TemplateType::name)
                .collect(Collectors.toList());

        if (templateList.isEmpty()) {
            return null;
        }

        int size = templateList.size();
        String arg1 = prettyPrintListJoiner(templateList);

        return String.format(MSG_EXISTS, arg1, size == 1 ? "" : "s", size == 1 ? "s" : "");
    }

    private static String prettyPrintListJoiner(List<String> templateList) {

        int size = templateList.size();
        if (size == 1) {
            return templateList.get(0);
        }

        StringJoiner joiner = new StringJoiner(", ");
        if (size > 2) {
            joiner.add(String.join(", ", templateList.subList(0, size - 2)));
        }
        return joiner.add(String.join(" and ", templateList.subList(size - 2, size))).toString();
    }

    // To generate all possibilities with help of testCounter
    private static boolean checkExistence(TemplateType type) {
        switch (type) {
            case Email:
                return ((testCounter >> 2) & 1) == 1;
            case Sms:
                return ((testCounter >> 1) & 1) == 1;
            case Letter:
                return ((testCounter) & 1) == 1;
        }
        throw new IllegalArgumentException();
    }
}