郵件傳送程式,用到了模版模式,幫我看看有啥問題。

sayschi發表於2006-06-20
我做了一個郵件傳送的Java程式,基本情況如下:
需要發的信件有 發貨通知,訂單確認信,訂單取消信等等,我建立一個儲存jsp模版的資料表,欄位主要有ID(主鍵),TITLE(郵件主題),BODY(郵件內容),BODY設定為CLOB型別,其實是各種信件的jsp頁面,上面有若干標籤,如:<OrderNO>,<MemberName>(每種模版標籤都不一樣),傳送之前選出郵件資料,並逐條把這些標籤替換掉,然後呼叫郵件傳送類SendMail.send(LetterTemplate letter)來迴圈傳送。

LetterTemplate.java(郵件模版抽象類)


public abstract class LetterTemplate {
    
    /** 郵件主題 **/
    private String title = null;
    
    /** 郵件內容 **/
    private String template = null;
    
    /** 收件人地址 **/
    private String email = null;
    
    /** 標籤對應的值(子類來引用這個變數), 格式為key:<OrderNO>, value:050500001 **/
    protected Map map = null;

    public LetterTemplate() {
        map = new HashMap(); 
    }

    /**
     * @return String
     * @roseuid 44220CAB00B5
     */
    public String getTitle() {
        return this.title;
    }

    /**
     * @return String
     * @roseuid 44220CC701C4
     */
    public String getTemplate() {
        return this.template;
    }

    /**
     * @return String
     * @roseuid 44220CD80092
     */
    public String getEmail() {
        return this.email;
    }
    
    /**
     * @param email The email to set.
     */
    public void setEmail(String email) {
        this.email = email;
    }
    /**
     * @param template The template to set.
     */
    public void setTemplate(String template) {
        this.template = template;
    }
    /**
     * @param title The title to set.
     */
    public void setTitle(String title) {
        this.title = title;
    }
    
    /**
     * 從資料庫中選出資料,用來替換標籤變數
     * @return String
     * @roseuid 44220CD80092
     */
    public abstract void setValue(Connection con, int orderID) throws SQLException;
    
    /**
     * 替換郵件主題
     * @return String
     * @roseuid 44220CD80092
     */
    public abstract String replaceTitle();
    
    /**
     * 替換郵件內容
     * @return String
     * @roseuid 44220CD80092
     */
    public abstract String replaceBody();
    
    
    /**
     * 變數替換
     * @return String
     * @roseuid 44220CD80092
     */
    final public void buildTemplate(Connection con, int orderID) throws SQLException {
        setValue(con, orderID);
        this.title = replaceTitle();
        this.template = replaceBody();
    }
    
    /**
     * 標籤替換公用方法
     * @param replaceStr
     * @param label
     * @param map
     * @return
     */
    protected String replace(String replaceStr, String[] label) {
        
        for (int i = 0; i < label.length; i++) {
            if (replaceStr.indexOf(label[i]) != -1) {
                String mapValue = (String) map.get(label[i]);
                if (mapValue == null || mapValue.equals("null")) {
                    mapValue = "";
                }
                
                replaceStr = replaceStr.replaceAll(label[i], mapValue);
            }
        }
        return replaceStr;
    }

}

<p class="indent">


OrderSendOfLetter.java(訂單發貨通知,還有幾個類省略,和這個相似)

public class OrderSendofLetter extends LetterTemplate {
    
    /** 本模版需要替換的變數(頁面上定義的標籤) * */
    private String[] label = new String[] { 
            "<MemberName>", 
            "<DeliveryType>",
            "<PaymentMethod>",
            "<PaymentRemark>", 
            "<OrderNO>", 
            "<MemberEmail>",
            "<MemberPostcode>", 
            "<MemberAddress>",
            "<MemberTelephone>", 
            "<ProductList>" 
            };

    public OrderSendofLetter() {
        
    }

    /**
     * 替換title標籤
     */
    public String replaceTitle() {
        return replace(getTitle(), label);
    }
    
    /**
     * 替換body標籤
     */
    public String replaceBody() {
        return replace(getTemplate(), label);
    }
   
    /**
     * 設定標籤對應的值(每個子模版的集中業務)
     */
    public void setValue(Connection con, int orderID) throws SQLException{
                String sql = "select... "; //選出替換標籤的資料
                ......
        try {
                        
                if (rs.next()) {
                name = rs.getString("name");
                deliveryType= rs.getInt("delivey_type");
                .....
                map.put("<MemberName>", name);
                map.put("<DeliveryType>", deliveryType);
                map.put("<PaymentMethod>", paymentMethod);
                map.put("<PaymentRemark>", paymentRemark);
                map.put("<OrderNO>", so_number);
                map.put("<MemberEmail>", email);
                map.put("<MemberPostcode>", post_code);
                map.put("<MemberAddress>", member_address);
                map.put("<MemberTelephone>", member_phone);
                ......
            }
        } finally {
            if (rs != null)
                try {
                    rs.close();
                } catch (Exception e) {
                }
            if (pstmt != null)
                try {
                    pstmt.close();
                } catch (Exception e) {
                }

        }

        
            
        } 

    }
    
}

<p class="indent">


MailGen.java(部分客戶端程式碼)

public void dealConfirmMail() throws Exception {
LetterTemplate template = null;//郵件模版
SendMail sm = new SendMail();//發郵件工具類
String sql = "select ...";需要發的郵件都選出來
......
try {
......
rs = pstmt.executeQuery();

while (rs.next()) {
     type = rs.getInt("mail_type");//郵件型別
     orderID = rs.getInt("order_id");//訂單ID
     email = getEmail("email");//收件人地址
 //靜態工廠方法產生不同型別的郵件,如果郵件型別不正確丟擲LetterTypeException異常
      template = UserLetterFactory.getInstance(type);

      //設定郵件郵件主題,郵件內容,收件人
      LetterTemplateBean temp = (LetterTemplateBean) letterMap
                                    .get(String.valueOf(type));
      template.setTitle(temp.getTitle());
      template.setTemplate(temp.getBody());
      email = getEmail(conn, orderID);

      //呼叫子類的setValue(),replace()方法
      template.buildTemplate(conn, orderID);
      sm.send(template);//傳送郵件
      ....//do other thingings
}
} finally {
            if (rs != null)
                try {
                    rs.close();
                } catch (Exception e) {
                }
            if (pstmt != null)
                try {
                    pstmt.close();
                } catch (Exception e) {
                }
            if (conn != null)
                try {
                    conn.close();
                } catch (Exception e) {
                }

        }

<p class="indent">

終於貼完了,請banq大俠幫我看看程式有沒什麼問題,如何改進,謝謝。

相關文章