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 org.opengion.fukurou.system.OgRuntimeException ;         // 6.4.2.0 (2016/01/29)
019import java.util.concurrent.ConcurrentMap;                                                      // 6.4.3.3 (2016/03/04)
020import java.util.concurrent.ConcurrentSkipListMap;                                      // 6.4.3.3 (2016/03/04)
021
022import org.opengion.hayabusa.db.DBColumn;
023import org.opengion.hayabusa.db.DBTableModelUtil;
024import org.opengion.hayabusa.resource.ResourceManager;
025import org.opengion.hayabusa.db.DBTableModel;
026
027/**
028 * タグ mailSender2 による送信を行う際に利用するメール送信マネージャの処理クラスです。
029 * タグ mailSender2 よりパラメータマップを受取って、メール文の合成、送信を行います。
030 * バッチ送信する場合と共通する部分はスーパークラス DefaultMailManager に実装していますが、
031 * タグ独自ロジックの部分は本クラスより実装を行っています。
032 * 独自ロジックはセッションから取得した宛先テーブルにより宛先マップを作成、セッションから取得したメール
033 * 文により送信を行うロジックとあります。
034 *
035 * @og.group メールモジュール
036 *
037 * @version  4.0
038 * @author   Sen.Li
039 * @since    JDK1.6
040 */
041public class MailManager_DIRECT extends DefaultMailManager {
042
043        // 6.4.1.1 (2016/01/16) names → NAMES refactoring
044        private static final String[] NAMES     = { "DST_ID", "GROUP_ID", "GROUP_NAME", "DST_NAME", "DST_ADDR", "DST_KBN", "FGJ_MAIL" };
045        private static final int IDX_DST_ID     = 0;
046        private static final int IDX_FGJ_MAIL   = 6;
047        private ResourceManager resource                ;
048
049        /**
050         * デフォルトコンストラクター
051         *
052         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
053         */
054        public MailManager_DIRECT() { super(); }                // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
055
056        /**
057         * action="SEND"の時にこのメソッドが呼ばれます。
058         * セッションから取得した宛先テーブルにより宛先マップを作成します。
059         * まだ、action="CHECK"の時に、確認画面から添付ファイルを追加するケースがあるため、
060         * パラメータを再読込を行います。そして、action="SEND"の時に添付ファイルを送信します。
061         *
062         * @og.rev 6.0.3.0 (2014/11/13) Ver6用キーワード変更
063         * @og.rev 6.4.3.3 (2016/03/04) ConcurrentHashMap を受け取ることを明確にするため、I/FをConcurrentMapに変更します。
064         *
065         * @param       params  パラメータのマップ
066         * @param       table   DBTableModelオブジェクト
067         *
068         */
069        public void create( final ConcurrentMap<String, String> params, final DBTableModel table ){
070                // 5.6.6.0 (2013/07/05) host指定対応
071                final MailPattern mailObj = new MailPattern( params );
072                setHost(mailObj.getHost());
073                setPort(mailObj.getSmtpPort());
074                setAuthType(mailObj.getAuthType());             // 6.0.3.0 (2014/11/13) Ver6用キーワード変更
075                setAuthUser(mailObj.getAuthUser());
076                setAuthPass(mailObj.getAuthPass());
077
078                setInitParams( params );
079                setAttachFiles(   params.get( "ATTACH1" )
080                                        , params.get( "ATTACH2" )
081                                , params.get( "ATTACH3" )
082                                , params.get( "ATTACH4" )
083                                , params.get( "ATTACH5" ));
084                final ConcurrentMap <String, String[]> dstMap = makeMailDstMap( table );                        // 6.4.3.3 (2016/03/04)
085                setMailDstMap( dstMap );
086        }
087
088        /**
089         * 画面に各宛先の送信状況を表示するために、送信の宛先マップに基づいてテーブルモデルを作成します。
090         * 作成されたテーブルモデルを指定されるスコープに入れます。
091         *
092         * @og.rev 5.1.9.0 (2010/08/01) keySet() → entrySet() に変更
093         * @og.rev 6.4.3.3 (2016/03/04) ConcurrentHashMap を受け取ることを明確にするため、I/FをConcurrentMapに変更します。
094         * @og.rev 6.4.3.4 (2016/03/11) forループを、forEach メソッドに置き換えます。
095         *
096         * @return      宛先マップに基づいたテーブルモデル
097         */
098        public DBTableModel makeDstTable(){
099                final ConcurrentMap<String, String[]> mailDst = getMailDstMap();
100                DBTableModel table;
101                final int numberOfColumns = NAMES.length;
102
103                table = DBTableModelUtil.newDBTable();
104                table.init( numberOfColumns );
105                setTableDBColumn( table, NAMES );
106
107                // 5.1.9.0 (2010/08/01) keySet() → entrySet() に変更
108                // 6.4.3.4 (2016/03/11) forループを、forEach メソッドに置き換えます。
109                mailDst.values().forEach( v -> table.addColumnValues( v ) );
110
111                return table;
112        }
113
114        /**
115         * リソースマネージャーをセットします。
116         * これは、言語(ロケール)に応じた DBColumn をあらかじめ設定しておく為に
117         * 必要です。
118         * リソースマネージャーが設定されていない、または、所定のキーの DBColumn が
119         * リソースに存在しない場合は、内部で DBColumn オブジェクトを作成します。
120         *
121         * @param  res リソースマネージャー
122         */
123        // 注意:この resource は、実質利用されていません。
124        public void setResourceManager( final ResourceManager res ) {
125                resource = res;
126        }
127
128        /**
129         * DBColumn オブジェクトをテーブルモデルに設定します。
130         *
131         * @og.rev 6.3.9.0 (2015/11/06) コンストラクタで初期化されていないフィールドを null チェックなしで利用している(findbugs)
132         *
133         * @param       table   DBTableModelオブジェクト
134         * @param       names   カラム名配列(可変長引数)
135         */
136        // 注意:この dbColumn は、実質利用されていません。
137        protected void setTableDBColumn( final DBTableModel table, final String... names ) {
138                // 6.3.9.0 (2015/11/06) コンストラクタで初期化されていないフィールドを null チェックなしで利用している(findbugs)
139                if( resource == null ) {
140                        final String errMsg = "#setResourceManager(ResourceManager)を先に実行しておいてください。" ;
141                        throw new OgRuntimeException( errMsg );
142                }
143
144                for( int i=0; i<names.length; i++ ) {
145                        final DBColumn clm = resource.makeDBColumn( names[i] );
146                        table.setDBColumn( i,clm );
147                }
148        }
149
150        /**
151         * セッションから取得した宛先テーブルにより宛先マップを作成します。
152         * 宛先テーブルの各レコードに対して、"送信待ち"となっているレコードのみ宛先マップに入れるようにしています。
153         *
154         * @og.rev 6.4.3.3 (2016/03/04) ConcurrentHashMap を受け取ることを明確にするため、I/FをConcurrentMapに変更します。
155         *
156         * @param       table   セッションから取得した宛先テーブル
157         *
158         * @return      宛先マップ
159         */
160        private ConcurrentMap<String, String[]> makeMailDstMap( final DBTableModel table ){
161                final ConcurrentMap<String, String[]> dstMap = new ConcurrentSkipListMap<>();           // 6.4.3.3 (2016/03/04)
162                final int rowCount = table.getRowCount();
163                final int colCount = NAMES.length;
164                for( int i=0; i<rowCount; i++ ) {
165                        String[] tmpDst = new String[colCount];
166                        if( DefaultMailManager.FGJ_SEND_WAIT.equals( table.getValue( i, IDX_FGJ_MAIL ) ) ) {
167                                for( int j=0; j<colCount; j++ ) {
168                                        tmpDst[j] = table.getValue( i, j );
169                                }
170                                if( tmpDst[IDX_DST_ID] != null ) {                                                                                      // 6.4.3.3 (2016/03/04) not null 制限
171                                        dstMap.put( tmpDst[IDX_DST_ID], tmpDst );
172                                }
173                        }
174                }
175
176                return dstMap;
177        }
178}