001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.hayabusa.mail;
017
018import java.util.Map;
019import java.util.TreeMap;
020
021import org.opengion.hayabusa.db.DBColumn;
022import org.opengion.hayabusa.db.DBTableModelUtil;
023import org.opengion.hayabusa.resource.ResourceManager;
024import org.opengion.hayabusa.db.DBTableModel;
025
026/**
027 * タグ mailSender2 による送信を行う際に利用するメール送信マネージャの処理クラスです。
028 * タグ mailSender2 よりパラメータマップを受取って、メール文の合成、送信を行います。
029 * バッチ送信する場合と共通する部分はスーパークラス AbstractMailManager に実装していますが、
030 * タグ独自ロジックの部分は本クラスより実装を行っています。
031 * 独自ロジックはセッションから取得した宛先テーブルにより宛先マップを作成、セッションから取得したメール
032 * 文により送信を行うロジックとあります。
033 *
034 * @og.group メールモジュール
035 *
036 * @version  4.0
037 * @author   Sen.Li
038 * @since    JDK1.6
039 */
040public class MailManager_DIRECT extends AbstractMailManager {
041
042        private static final String[] names     = { "DST_ID", "GROUP_ID", "GROUP_NAME", "DST_NAME", "DST_ADDR", "DST_KBN", "FGJ_MAIL" };
043        private static final int IDX_DST_ID     = 0;
044        private static final int IDX_FGJ_MAIL   = 6;
045        private ResourceManager resource = null;
046
047        /**
048         * action="SEND"の時にこのメソッドが呼ばれます。
049         * セッションから取得した宛先テーブルにより宛先マップを作成します。
050         * まだ、action="CHECK"の時に、確認画面から添付ファイルを追加するケースがあるため、
051         * パラメータを再読込を行います。そして、action="SEND"の時に添付ファイルを送信します。
052         *
053         * @param       params  パラメータのマップ
054         * @param       table   DBTableModelオブジェクト
055         *
056         */
057        public void create( final Map<String, String> params, final DBTableModel table ){
058                // 5.6.6.0 (2013/07/05) host指定対応
059                MailPattern mailObj = new MailPattern( params );
060                setHost(mailObj.getHost());
061                setPort(mailObj.getSmtpPort());
062                setAuth(mailObj.getAuth());
063                setAuthUser(mailObj.getAuthUser());
064                setAuthPass(mailObj.getAuthPass());
065                
066                setInitParams( params );
067                setAttachFiles(   params.get( "ATTACH1" )
068                                        , params.get( "ATTACH2" )
069                                , params.get( "ATTACH3" )
070                                , params.get( "ATTACH4" )
071                                , params.get( "ATTACH5" ));
072                Map <String, String[]> dstMap = makeMailDstMap( table );
073                setMailDstMap( dstMap );
074        }
075
076        /**
077         * 画面に各宛先の送信状況を表示するために、送信の宛先マップに基づいてテーブルモデルを作成します。
078         * 作成されたテーブルモデルを指定されるスコープに入れます。
079         *
080         * @og.rev 5.1.9.0 (2010/08/01) keySet() → entrySet() に変更
081         *
082         * @return      宛先マップに基づいたテーブルモデル
083         */
084        public DBTableModel makeDstTable(){
085                Map<String, String[]> mailDst = getMailDstMap();
086                DBTableModel table;
087                int numberOfColumns = names.length;
088
089                table = DBTableModelUtil.newDBTable();
090                table.init( numberOfColumns );
091                setTableDBColumn( table, names );
092
093                // 5.1.9.0 (2010/08/01) keySet() → entrySet() に変更
094                for( Map.Entry<String, String[]> en : mailDst.entrySet() ) {
095                        table.addColumnValues( en.getValue() );
096                }
097
098                return table;
099        }
100
101        /**
102         * リソースマネージャーをセットします。
103         * これは、言語(ロケール)に応じた DBColumn をあらかじめ設定しておく為に
104         * 必要です。
105         * リソースマネージャーが設定されていない、または、所定のキーの DBColumn が
106         * リソースに存在しない場合は、内部で DBColumn オブジェクトを作成します。
107         *
108         * @param  res リソースマネージャー
109         */
110        // 注意:この resource は、実質利用されていません。
111        public void setResourceManager( final ResourceManager res ) {
112                resource = res;
113        }
114
115        /**
116         * DBColumn オブジェクトをテーブルモデルに設定します。
117         *
118         * @param       table   DBTableModelオブジェクト
119         * @param       names   カラム名配列
120         */
121        // 注意:この dbColumn は、実質利用されていません。
122        protected void setTableDBColumn( final DBTableModel table, final String[] names ) {
123                for( int i=0; i<names.length; i++ ) {
124                        DBColumn clm = resource.makeDBColumn( names[i] );
125                        table.setDBColumn( i,clm );
126                }
127        }
128
129        /**
130         * セッションから取得した宛先テーブルにより宛先マップを作成します。
131         * 宛先テーブルの各レコードに対して、"送信待ち"となっているレコードのみ宛先マップに入れるようにしています。
132         *
133         * @param       table   セッションから取得した宛先テーブル
134         *
135         * @return      宛先マップ
136         */
137        private Map<String, String[]> makeMailDstMap( final DBTableModel table ){
138                Map<String, String[]> dstMap = new TreeMap<String, String[]>();
139                int rowCount = table.getRowCount();
140                int colCount = names.length;
141                for( int i = 0; i < rowCount; i++ ) {
142                        String[] tmpDst = new String[colCount];
143                        if( AbstractMailManager.FGJ_SEND_WAIT.equals( table.getValue( i, IDX_FGJ_MAIL ) ) ) {
144                                for( int j = 0; j < colCount; j++ ) {
145                                        tmpDst[j] = table.getValue( i, j );
146                                }
147                                dstMap.put( tmpDst[IDX_DST_ID], tmpDst );
148                        }
149                }
150
151                return dstMap;
152        }
153}