diff --git a/students/2756638003/srp/pom.xml b/students/2756638003/srp/pom.xml new file mode 100644 index 0000000000..5024466d17 --- /dev/null +++ b/students/2756638003/srp/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ds-assignment + 0.0.1-SNAPSHOT + jar + + ds-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/2756638003/srp/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..6f743552bb --- /dev/null +++ b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,131 @@ +package com.coderising.ood.srp; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import com.coderising.ood.srp.conf.Configuration; +import com.coderising.ood.srp.conf.ConfigurationKeys; +import com.coderising.ood.srp.conf.EmailStatus; +import com.coderising.ood.srp.domain.Product; +import com.coderising.ood.srp.domain.Subscriber; +import com.coderising.ood.srp.util.ConfigUtil; +import com.coderising.ood.srp.util.Email; +import com.coderising.ood.srp.util.MailUtil; +import com.coderising.ood.srp.util.SubscriberUtil; + +public class PromotionMail { + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + + private static Configuration config; + + public static void main(String[] args) throws Exception { + File file = new File("XX/product_promotion.txt"); + boolean emailDebug = false; + new PromotionMail(file, emailDebug); + } + + /** + * 从配置文件中读取商品信息 + */ + private List loadFile(File file) throws IOException { + Map conf = ConfigUtil.readTextFile(file); + List productList = new ArrayList(16); + Set> entrySet = conf.entrySet(); + for (Entry entry : entrySet) { + productList.add(new Product(entry.getKey(), entry.getValue())); + } + return productList; + } + + /** + * 根据商品查询订阅的用户信息 + */ + private List querySubscribersFormFile(List productList) + throws IOException { + StringBuilder query = new StringBuilder( + "Select name from Subscriber where product_id in( "); + for (int i = 0, len = productList.size(); i < len - 1; i++) { + query.append(productList.get(i).getProductID() + " ,"); + } + query.append(productList.get(productList.size() - 1).getProductID()); + query.append(") and send_mail = ?"); + return SubscriberUtil.loadSubscriberList(query.toString(), + EmailStatus.READY); + } + + /** + * 根据订阅者信息生成邮件 + */ + private Email generatorEmail(Subscriber subscriber, String host) { + String subject = "您关注的产品降价了"; + String message = "尊敬的 " + subscriber.getName() + ", 您关注的产品 " + + subscriber.getProduct().getProductDesc() + " 降价了,欢迎购买!"; + return new Email(subscriber.getEmail(), fromAddress, subject, message, + host); + } + + public PromotionMail(File file, boolean mailDebug) throws Exception { + config = new Configuration(); + setSMTPHost(); + setAltSMTPHost(); + setFromAddress(); + // 从配置文件中读取商品信息 + List productList = loadFile(file); + // 根据商品查询订阅的用户信息 + List subscriberList = querySubscribersFormFile(productList); + // 发送邮件 + sendEMails(mailDebug, subscriberList); + } + + /** + * 发送邮件 + */ + protected void sendEMails(boolean debug, List subscriberList) + throws IOException { + System.out.println("开始发送邮件"); + if (subscriberList != null && subscriberList.size() > 0) { + Iterator iter = subscriberList.iterator(); + Subscriber subscriber = null; + + while (iter.hasNext()) { + subscriber = iter.next(); + try { + MailUtil.sendEmail(generatorEmail(subscriber, smtpHost), + debug); + } catch (Exception e) { + try { + MailUtil.sendEmail( + generatorEmail(subscriber, altSmtpHost), debug); + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + + e2.getMessage()); + } + } + } + } else { + System.out.println("没有邮件发送"); + } + } + + protected void setSMTPHost() { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + protected void setAltSMTPHost() { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + } + + protected void setFromAddress() { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + +} diff --git a/students/2756638003/srp/src/main/java/com/coderising/ood/srp/conf/Configuration.java b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/conf/Configuration.java new file mode 100644 index 0000000000..444e4cf912 --- /dev/null +++ b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/conf/Configuration.java @@ -0,0 +1,29 @@ +package com.coderising.ood.srp.conf; + +import java.util.HashMap; +import java.util.Map; + +/** + * 邮件服务器配置文件 + */ +public class Configuration { + + static Map configurations = new HashMap<>(); + + static { + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * + * @param key + * @return + */ + public String getProperty(String key) { + return configurations.get(key); + } + +} diff --git a/students/2756638003/srp/src/main/java/com/coderising/ood/srp/conf/ConfigurationKeys.java b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/conf/ConfigurationKeys.java new file mode 100644 index 0000000000..36009abc3d --- /dev/null +++ b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/conf/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp.conf; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/2756638003/srp/src/main/java/com/coderising/ood/srp/conf/EmailStatus.java b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/conf/EmailStatus.java new file mode 100644 index 0000000000..3245a8d468 --- /dev/null +++ b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/conf/EmailStatus.java @@ -0,0 +1,31 @@ +package com.coderising.ood.srp.conf; + +/** + * 邮件发送状态常量 + */ +public enum EmailStatus { + /** + * 未发送 + */ + READY(0), + /** + * 已发送 + */ + SEND(1), + /** + * 已查阅 + */ + RECEIVED(2), + /** + * 被退回 + */ + REJECTED(3); + + @SuppressWarnings("unused") + private int value; + + private EmailStatus(int value) { + this.value = value; + } + +} diff --git a/students/2756638003/srp/src/main/java/com/coderising/ood/srp/conf/product_promotion.txt b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/conf/product_promotion.txt new file mode 100644 index 0000000000..a98917f829 --- /dev/null +++ b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/conf/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo R15 +P4955 Vivo X20 \ No newline at end of file diff --git a/students/2756638003/srp/src/main/java/com/coderising/ood/srp/domain/Product.java b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/domain/Product.java new file mode 100644 index 0000000000..52656e0389 --- /dev/null +++ b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/domain/Product.java @@ -0,0 +1,43 @@ +package com.coderising.ood.srp.domain; + +/** + * 商品类 + */ +public class Product { + + private String productID; + private String productDesc; + + public Product(String productID, String productDesc) { + super(); + this.productID = productID; + this.productDesc = productDesc; + } + + public Product() { + super(); + } + + public String getProductID() { + return productID; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } + + @Override + public String toString() { + return "Product [productID=" + productID + ", productDesc=" + + productDesc + "]"; + } + +} diff --git a/students/2756638003/srp/src/main/java/com/coderising/ood/srp/domain/Subscriber.java b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/domain/Subscriber.java new file mode 100644 index 0000000000..cdef11498f --- /dev/null +++ b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/domain/Subscriber.java @@ -0,0 +1,74 @@ +package com.coderising.ood.srp.domain; + +/** + * 订阅者 + */ +public class Subscriber { + + private String subscriberId; + private String name; + private String email; + private Product product; + private Integer sendStatus; + + public Subscriber(String subscriberId, String name, String email, + Product product) { + super(); + this.subscriberId = subscriberId; + this.name = name; + this.email = email; + this.product = product; + } + + public Subscriber() { + super(); + } + + public String getSubscriberId() { + return subscriberId; + } + + public void setSubscriberId(String subscriberId) { + this.subscriberId = subscriberId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public Product getProduct() { + return product; + } + + public void setProduct(Product product) { + this.product = product; + } + + public Integer getSendStatus() { + return sendStatus; + } + + public void setSendStatus(Integer sendStatus) { + this.sendStatus = sendStatus; + } + + @Override + public String toString() { + return "Subscriber [subscriberId=" + subscriberId + ", name=" + name + + ", email=" + email + ", product=" + product + ", sendStatus=" + + sendStatus + "]"; + } + +} diff --git a/students/2756638003/srp/src/main/java/com/coderising/ood/srp/util/ConfigUtil.java b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/util/ConfigUtil.java new file mode 100644 index 0000000000..4494739dd6 --- /dev/null +++ b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/util/ConfigUtil.java @@ -0,0 +1,33 @@ +package com.coderising.ood.srp.util; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +/** + * 配置文件读写工具类 + * + */ +public class ConfigUtil { + + public static Map readTextFile(File file) + throws IOException { + Map conf = new HashMap(); + try (BufferedReader br = new BufferedReader(new FileReader(file));) { + String temp = null; + while ((temp = br.readLine()) != null) { + int indexOf = temp.indexOf(' '); + if (indexOf > -1) { + conf.put(temp.substring(0, indexOf), + temp.substring(indexOf)); + } + } + } catch (IOException e) { + throw new IOException(e.getMessage()); + } + return conf; + } +} diff --git a/students/2756638003/srp/src/main/java/com/coderising/ood/srp/util/DBUtil.java b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/util/DBUtil.java new file mode 100644 index 0000000000..a4facaff0b --- /dev/null +++ b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/util/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp.util; + +import java.util.ArrayList; +import java.util.List; + +import com.coderising.ood.srp.domain.Product; +import com.coderising.ood.srp.domain.Subscriber; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * + * @param sql + * @return + */ + public static List query(String sql, Object... args) { + List list = new ArrayList(16); + for (int i = 1; i <= 3; i++) { + list.add(new Subscriber(String.valueOf(i), "User" + i, "user-" + i + + "@bb.com", new Product("P8756", " iPhone8"))); + } + return list; + } +} diff --git a/students/2756638003/srp/src/main/java/com/coderising/ood/srp/util/Email.java b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/util/Email.java new file mode 100644 index 0000000000..3422aebb2a --- /dev/null +++ b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/util/Email.java @@ -0,0 +1,75 @@ +package com.coderising.ood.srp.util; + +/** + * 邮件类 + */ +public class Email { + + private String toAddress; + private String fromAddress; + private String subject; + private String message; + private String smtpHost; + + public Email(String toAddress, String fromAddress, String subject, + String message, String smtpHost) { + super(); + this.toAddress = toAddress; + this.fromAddress = fromAddress; + this.subject = subject; + this.message = message; + this.smtpHost = smtpHost; + } + + public Email() { + super(); + } + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getSmtpHost() { + return smtpHost; + } + + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + + @Override + public String toString() { + return "Email [toAddress=" + toAddress + ", fromAddress=" + fromAddress + + ", subject=" + subject + ", message=" + message + + ", smtpHost=" + smtpHost + "]"; + } + +} diff --git a/students/2756638003/srp/src/main/java/com/coderising/ood/srp/util/MailUtil.java b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/util/MailUtil.java new file mode 100644 index 0000000000..7994870bef --- /dev/null +++ b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/util/MailUtil.java @@ -0,0 +1,17 @@ +package com.coderising.ood.srp.util; + +/** + * 邮件发送工具类 + */ +public class MailUtil { + public static void sendEmail(Email email, boolean debug) { + // 假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(email.getFromAddress()).append("\n"); + buffer.append("To:").append(email.getToAddress()).append("\n"); + buffer.append("Subject:").append(email.getSubject()).append("\n"); + buffer.append("Content:").append(email.getMessage()).append("\n"); + System.out.println(buffer.toString()); + } + +} diff --git a/students/2756638003/srp/src/main/java/com/coderising/ood/srp/util/SubscriberUtil.java b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/util/SubscriberUtil.java new file mode 100644 index 0000000000..2410a45950 --- /dev/null +++ b/students/2756638003/srp/src/main/java/com/coderising/ood/srp/util/SubscriberUtil.java @@ -0,0 +1,17 @@ +package com.coderising.ood.srp.util; + +import java.util.List; + +import com.coderising.ood.srp.domain.Subscriber; + +/** + * 订阅者查询工具类 + */ +public class SubscriberUtil { + + public static List loadSubscriberList(String sql, + Object... args) { + return DBUtil.query(sql); + } + +}