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 DBColumn[]      dbColumn        = null;
046        private ResourceManager resource = null;
047
048        /**
049         * action="SEND"の時にこのメソッドが呼ばれます。
050         * セッションから取得した宛先テーブルにより宛先マップを作成します。
051         * まだ、action="CHECK"の時に、確認画面から添付ファイルを追加するケースがあるため、
052         * パラメータを再読込を行います。そして、action="SEND"の時に添付ファイルを送信します。
053         *
054         * @param       params  パラメータのマップ
055         * @param       table   DBTableModelオブジェクト
056         *
057         */
058        public void create( final Map<String, String> params, final DBTableModel table ){
059                // 5.6.6.0 (2013/07/05) host指定対応
060                MailPattern mailObj = new MailPattern( params );
061                setHost(mailObj.getHost());
062                setPort(mailObj.getSmtpPort());
063                setAuth(mailObj.getAuth());
064                setAuthUser(mailObj.getAuthUser());
065                setAuthPass(mailObj.getAuthPass());
066                
067                setInitParams( params );
068                setAttachFiles(   params.get( "ATTACH1" )
069                                        , params.get( "ATTACH2" )
070                                , params.get( "ATTACH3" )
071                                , params.get( "ATTACH4" )
072                                , params.get( "ATTACH5" ));
073                Map <String, String[]> dstMap = makeMailDstMap( table );
074                setMailDstMap( dstMap );
075        }
076
077        /**
078         * 画面に各宛先の送信状況を表示するために、送信の宛先マップに基づいてテーブルモデルを作成します。
079         * 作成されたテーブルモデルを指定されるスコープに入れます。
080         *
081         * @og.rev 5.1.9.0 (2010/08/01) keySet() → entrySet() に変更
082         *
083         * @return      宛先マップに基づいたテーブルモデル
084         */
085        public DBTableModel makeDstTable(){
086                Map<String, String[]> mailDst = getMailDstMap();
087                DBTableModel table;
088                int numberOfColumns = names.length;
089
090                table = DBTableModelUtil.newDBTable();
091                table.init( numberOfColumns );
092                setTableDBColumn( table, names );
093
094//              for( String dstId : mailDst.keySet() ) {
095//                      String[] dstInfo = mailDst.get( dstId );
096//                      table.addColumnValues( dstInfo );
097//              }
098
099                // 5.1.9.0 (2010/08/01) keySet() → entrySet() に変更
100                for( Map.Entry<String, String[]> en : mailDst.entrySet() ) {
101                        table.addColumnValues( en.getValue() );
102                }
103
104                return table;
105        }
106
107        /**
108         * リソースマネージャーをセットします。
109         * これは、言語(ロケール)に応じた DBColumn をあらかじめ設定しておく為に
110         * 必要です。
111         * リソースマネージャーが設定されていない、または、所定のキーの DBColumn が
112         * リソースに存在しない場合は、内部で DBColumn オブジェクトを作成します。
113         *
114         * @param  res リソースマネージャー
115         */
116        // 注意:この resource は、実質利用されていません。
117        public void setResourceManager( final ResourceManager res ) {
118                resource = res;
119        }
120
121        /**
122         * DBColumn オブジェクトをテーブルモデルに設定します。
123         *
124         * @param       table   DBTableModelオブジェクト
125         * @param       names   カラム名配列
126         */
127        // 注意:この dbColumn は、実質利用されていません。
128        protected void setTableDBColumn( final DBTableModel table, final String[] names ) {
129//              dbColumn = new DBColumn[names.length] ;
130                for( int i=0; i<names.length; i++ ) {
131                        DBColumn clm = resource.makeDBColumn( names[i] );
132                        table.setDBColumn( i,clm );
133//                      dbColumn[i] = clm;
134                }
135        }
136
137        /**
138         * セッションから取得した宛先テーブルにより宛先マップを作成します。
139         * 宛先テーブルの各レコードに対して、"送信待ち"となっているレコードのみ宛先マップに入れるようにしています。
140         *
141         * @param       table   セッションから取得した宛先テーブル
142         *
143         * @return      宛先マップ
144         */
145        private Map<String, String[]> makeMailDstMap( final DBTableModel table ){
146                Map<String, String[]> dstMap = new TreeMap<String, String[]>();
147                int rowCount = table.getRowCount();
148                int colCount = names.length;
149                for( int i = 0; i < rowCount; i++ ) {
150                        String[] tmpDst = new String[colCount];
151                        if( AbstractMailManager.FGJ_SEND_WAIT.equals( table.getValue( i, IDX_FGJ_MAIL ) ) ) {
152                                for( int j = 0; j < colCount; j++ ) {
153                                        tmpDst[j] = table.getValue( i, j );
154                                }
155                                dstMap.put( tmpDst[IDX_DST_ID], tmpDst );
156                        }
157                }
158
159                return dstMap;
160        }
161}