1

在下面的代码中,我将 setOriginatingNumber 重置为 null,因为对于每个非 null 值,

if (config.getOriginPhoneNumber() != null) {
                PhoneNumber originphoneNumber = new PhoneNumber();
                config.setOriginatingNumber(formatPhoneNumber(originphoneNumber));
}

它将重置为空白电话号码。这可能导致多个问题 这种情况仅在用户编辑现有电话号码值并更改为字母数字时存在。

如何确保对于 config.getOriginPhoneNumber() 的每个非空值,代码确实设置为空白电话号码

public SmsChannelConfig applyTo(
            final SmsChannelConfigRepository repository,
            final UserContext context, final ModuleRepository moduleRepository) {
        final SmsChannelConfig config;
        if (source == null) {
            config = new SmsChannelConfig(context);
        } else {
            config = source;
        }
        super.applyTo(repository, config);
        if (!Strings.isNullOrEmpty(getOriginatorType())
                && "Number".equals(getOriginatorType())) {
            PhoneNumber originphoneNumber = new PhoneNumber();
            originphoneNumber.setNumber(getPhoneNumber());
            originphoneNumber.setCountryCode(getCountryCode());
            config.setOriginPhoneNumber(originphoneNumber);
            config.setOriginatingNumber(formatPhoneNumber(originphoneNumber));
            config.setOriginText(null);
        } else {
            config.setOriginText(getOriginator());
            if (config.getOriginPhoneNumber() != null) {
                PhoneNumber originphoneNumber = new PhoneNumber();
                config.setOriginatingNumber(formatPhoneNumber(originphoneNumber));
            }
        }
        config.setOperator(getOperator());
        if (!Strings.isNullOrEmpty(getOperator())) {
            List<ExternalResource> resources = SmsChannelConfigUtil
                    .getSmsProviders(moduleRepository, context);
            for (ExternalResource resource : resources) {
                if (getOperator().equals(resource.getId().getResourceName())) {
                    config.setOperatorName(resource.toString());
                }
            }
        }
        config.setDescription(getDescription());
        config.setName(getName());
        if (repository.query().all().isEmpty()) {
            repository.setDefault(context.getOrganization(), config);
        }
        return config;
    }

仅当 getOriginatorType() 不等于“Number”时将 originphoneNumber 值重置为 null,这样它就不会产生多个问题

4

2 回答 2

2

代替:

if (config.getOriginPhoneNumber() != null) {
                PhoneNumber originphoneNumber = new PhoneNumber();
                config.setOriginatingNumber(formatPhoneNumber(originphoneNumber));
}

和:

if (config.getOriginPhoneNumber().matches("^.*[\\w+].*$") {
                PhoneNumber originphoneNumber = new PhoneNumber();
                config.setOriginatingNumber(formatPhoneNumber(originphoneNumber));
}

检查电话号码是否为字母数字。如果您想要仅测试字符,则应替换[\\w+][a-zA-Z]+

于 2013-10-23T06:55:37.247 回答
1

更改if块:

if (!Strings.isNullOrEmpty(getOriginatorType())
                && "Number".equals(getOriginatorType())) {

对此:

if (!Strings.isNullOrEmpty(getOriginatorType()){
    if("Number".equals(getOriginatorType())){
            PhoneNumber originphoneNumber = new PhoneNumber();
            originphoneNumber.setNumber(getPhoneNumber());
            originphoneNumber.setCountryCode(getCountryCode());
            config.setOriginPhoneNumber(originphoneNumber);
            config.setOriginatingNumber(formatPhoneNumber(originphoneNumber));
            config.setOriginText(null);
    } else {
            // RESET originphoneNumber value to null
    }
}
于 2013-10-23T07:01:01.533 回答