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 static org.opengion.fukurou.util.StringUtil.*;
019
020import java.util.ArrayList;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024
025import javax.mail.Address;
026import javax.mail.SendFailedException;
027import javax.mail.internet.InternetAddress;
028
029import org.opengion.fukurou.db.ConnectionFactory;
030import org.opengion.fukurou.db.DBFunctionName;
031import org.opengion.fukurou.db.DBUtil;
032import org.opengion.fukurou.db.Transaction;
033import org.opengion.fukurou.db.TransactionReal;
034import org.opengion.fukurou.mail.MailTX;
035import org.opengion.fukurou.util.ApplicationInfo;
036import org.opengion.fukurou.util.StringUtil;
037import org.opengion.hayabusa.common.HybsSystem;
038
039/**
040 * タグ mailSender2 及びバッチによる送信の共通処理部分を実装しています。
041 * 送信タグ mailSender2 もしくは送信デーモンからパラメータを受取ります。
042 * パラメータ中の定型文ID及びシステムIDで定型文マスタよりメールの定型文を取得して、
043 * パラメータ値とマージしてメール文を合成します。同時に、宛先にセットした社員ID、
044 * グループIDと定型文の宛先設定に基づき、社員マスタとグループマスタよりメールアドレス
045 * 情報を取得して送信を行います。
046 * エラーがなければ送信した内容を履歴テーブル、宛先テーブルに書き込みます。
047 * 最後に本処理の呼出元に送信結果、エラーメッセージを返します。
048 *
049 * @og.group メールモジュール
050 *
051 * @version  4.0
052 * @author   Sen.Li
053 * @since    JDK1.6
054 */
055public abstract class AbstractMailManager {
056
057        // 5.2.0.0 (2010/09/01) Ver4互換モード対応
058        private static final String CONTENTS = HybsSystem.sysBool( "VER4_COMPATIBLE_MODE" ) ? "CONTENT" : "CONTENTS";
059
060        // 5.9.26.0 (2017/11/02) DB共通化のため、DBFunctionNameを使用して、シーケンスを取得するように変更
061//      private static final String     selYkno = "SELECT GE32S02.NEXTVAL YKNO FROM DUAL";
062
063        // 5.0.3.0 (2009/11/04) CONTENT ⇒ CONTENTS
064        // 5.2.0.0 (2010/09/01) Ver4互換モード対応
065//      private static final String     insGE32         = "INSERT INTO GE32(YKNO,PARA_KEY,PTN_ID,FROM_ADDR,TITLE,CONTENTS,ATTACH1,ATTACH2,ATTACH3,ATTACH4,ATTACH5,DYSET,USRSET,PGUPD,SYSTEM_ID,FGJ)"
066        private static final String     insGE32         = "INSERT INTO GE32(YKNO,PARA_KEY,PTN_ID,FROM_ADDR,TITLE,"+CONTENTS+",ATTACH1,ATTACH2,ATTACH3,ATTACH4,ATTACH5,DYSET,USRSET,PGUPD,SYSTEM_ID,FGJ)"
067                                                                                        + " VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,'1')";
068        private static final String insGE34             = "INSERT INTO GE34(YKNO,DST_ID,GROUP_ID,DST_NAME,DST_ADDR,DST_KBN,FGJ,DYSET,USRSET,PGUPD)"
069                                                                                        + " VALUES(?,?,?,?,?,?,?,?,?,?)";
070        /** フラグ定数 {@value} */
071        protected static final String FGJ_SEND_WAIT     = "0";
072        /** フラグ定数 {@value} */
073        protected static final String FGJ_SEND_OVER     = "1";
074        /** フラグ定数 {@value} */
075        protected static final String FGJ_ADDR_ERR      = "7";
076        /** フラグ定数 {@value} */
077        protected static final String FGJ_ACNT_ERR      = "8";
078
079        // 内部データのカラム番号(履歴テーブル)
080        private static final int GE32_YKNO              = 0 ;
081        private static final int GE32_PARAKEY   = 1 ;
082        private static final int GE32_PTN_ID    = 2;
083        private static final int GE32_FROM_ADDR = 3;
084        private static final int GE32_TITLE     = 4;
085        private static final int GE32_CONTENTS  = 5;            // 5.0.3.0 (2009/11/04) CONTENT ⇒ CONTENTS
086        private static final int GE32_ATTACH1   = 6;
087        private static final int GE32_ATTACH2   = 7;
088        private static final int GE32_ATTACH3   = 8;
089        private static final int GE32_ATTACH4   = 9;
090        private static final int GE32_ATTACH5   = 10;
091        private static final int GE32_DYSET             = 11;
092        private static final int GE32_USRSET    = 12;
093        private static final int GE32_PGUPD     = 13;
094        private static final int GE32_SYSTEM_ID = 14;
095        // 内部データのカラム番号(履歴テーブル)
096        private static final int GE34_YKNO              = 0 ;
097        private static final int GE34_DST_ID    = 1 ;
098        private static final int GE34_GROUP_ID  = 2 ;
099        private static final int GE34_DST_NAME  = 3 ;
100        private static final int GE34_DST_ADDR  = 4 ;
101        private static final int GE34_DST_KBN   = 5 ;
102        private static final int GE34_FGJ               = 6 ;
103        private static final int GE34_DYSET             = 7 ;
104        private static final int GE34_USRSET    = 8 ;
105        private static final int GE34_PGUPD     = 9 ;
106//      private static String           host            = HybsSystem.sys( "COMMON_MAIL_SERVER" );
107        private static String           charset         = HybsSystem.sys( "MAIL_DEFAULT_CHARSET" );
108//      private static String           smtpPort        = HybsSystem.sys( "SMTP_PORT" );                                // 5.4.3.2 (2012/01/06)
109//      private static String           auth            = HybsSystem.sys( "MAIL_SEND_AUTH" );                   // 5.4.3.2 (2012/01/06)
110//      private static String           authUser        = HybsSystem.sys( "MAIL_SEND_AUTH_USER" );              // 5.4.3.2 (2012/01/06)
111//      private static String           authPass        = HybsSystem.sys( "MAIL_SEND_AUTH_PASSWORD" );  // 5.4.3.2 (2012/01/06)
112        private boolean                         debugFlag   = false;
113        private final List<String>        errAddrList = new ArrayList<String>();
114        private static final int MAX_RETRY              = 3 ;   // メールアドレスエラー発生した場合、メール再送回数
115
116        // 5.6.6.0 (2013/07/05) host等の外部指定に対応
117        private String          host            = HybsSystem.sys( "COMMON_MAIL_SERVER" );               // 5.6.6.0 (2013/07/05)
118        private String          smtpPort        = HybsSystem.sys( "SMTP_PORT" );                                // 5.6.6.0 (2013/07/05)
119        private String          auth            = HybsSystem.sys( "MAIL_SEND_AUTH" );                   // 5.6.6.0 (2013/07/05)
120        private String          authPort        = HybsSystem.sys( "MAIL_SEND_AUTH_PORT" );              // 5.8.1.1 (2014/11/14)
121        private String          authUser        = HybsSystem.sys( "MAIL_SEND_AUTH_USER" );              // 5.6.6.0 (2013/07/05)
122        private String          authPass        = HybsSystem.sys( "MAIL_SEND_AUTH_PASSWORD" );  // 5.6.6.0 (2013/07/05)
123
124        private String  mailTitle, mailContent, fromAddr;
125        private String[] attachFiles;
126        private Map<String, String[]>     mailDstMap              = null;
127        private Map<String,String>                initParamMap    = null;         // パラメータマップ
128        private MailTX                                  mail                    = null;
129
130        protected final String DBID = HybsSystem.sys( "RESOURCE_DBID" );                // 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対応
131
132        /** コネクションにアプリケーション情報を追記するかどうか指定 */
133        private static final boolean USE_DB_APPLICATION_INFO  = HybsSystem.sysBool( "USE_DB_APPLICATION_INFO" ) ;
134
135        /** アプリケーション情報 */
136        public static final ApplicationInfo appInfo;
137        static {
138                if( USE_DB_APPLICATION_INFO ) {
139                        appInfo = new ApplicationInfo();
140                        // ユーザーID,IPアドレス,ホスト名
141                        appInfo.setClientInfo( "MailModuel", HybsSystem.HOST_ADRS, HybsSystem.HOST_NAME );
142                        // 画面ID,操作,プログラムID
143                        appInfo.setModuleInfo( "MailModuel", "MailManager", "MailManager" );
144                }
145                else {
146                        appInfo = null;
147                }
148        }
149
150        /**
151         * 呼出元よりパラメータマップを受取って、メールオブジェクト(mailObj)を作成します。
152         * メールオブジェクトは定型文ID及びシステムIDに基づいて定型文マスタからメールの定型文を取得して、
153         * パラメータ値とマージしてメール文の各項目を合成します。
154         * 宛先については、宛先にセットした社員ID、グループIDと定型文の宛先設定に基づき、社員マスタとグループマスタ
155         * よりメールアドレスを取得して宛先マップを作成します。
156         * まだ、添付ファイルのセット処理も行っています。
157         *
158         * @og.rev 5.6.6.0 (2013/07/05) host等の外部設定対応
159         * @og.rev 5.9.15.1 (2016/12/09) エラーリストのクリア
160         *
161         * @param       params  パラメータのマップ
162         */
163        public void create( final Map<String, String> params ) {
164                initParamMap = params;
165                MailPattern mailObj = new MailPattern( params );
166                fromAddr = mailObj.getFromAddr();
167                setAttachFiles( params.get( "ATTACH1" )
168                                          , params.get( "ATTACH2" )
169                                          , params.get( "ATTACH3" )
170                                          , params.get( "ATTACH4" )
171                                          , params.get( "ATTACH5" ) ); // 添付ファイルのセット
172                mailDstMap = mailObj.getDstMap();
173                mailTitle = mailObj.getTitle();
174                mailContent = mailObj.getContent();
175                errAddrList.clear(); // 5.9.15.1 (2016/12/09)
176                errAddrList.addAll( mailObj.getErrList() );
177
178                // 5.6.6.0 (2013/07/05)
179                host            = nval( mailObj.getHost()               ,host           );
180                smtpPort        = nval( mailObj.getSmtpPort()   ,smtpPort       );
181                auth            = nval( mailObj.getAuth()               ,auth           );
182                authPort        = nval( mailObj.getAuthPort()   ,authPort       );              // 5.8.1.1 (2014/11/14)
183                authUser        = nval( mailObj.getAuthUser()   ,authUser       );
184                authPass        = nval( mailObj.getAuthPass()   ,authPass       );
185        }
186
187        /**
188         * メール送信を行うメソッドです。
189         * createメソッドより合成した内容で送信を行います。アドレスエラー発生時に、
190         * ユーザー設定(addrCheck)により再送/中止します。
191         * メールサーバーに送信して、例外"SendFailedException"をキャッチできたら、
192         * メールアカウントがエラーとなるのを分かります。そして、例外のオブジェクトから
193         * エラーとなっているアカウントを取得して宛先マップから除外して、残りのアドレスに再送できます。
194         * 送信後履歴テーブル(GE32)、宛先テーブル(GE34)に送信結果を書き込みます。
195         *
196         * og.rev 5.4.3.2 (2012/01/06) 送信時認証対応
197         *
198         */
199        public void send() {
200                List<String> invalidAddrBuf       = new ArrayList<String>();
201                // mail = new MailTX( host, charset );
202                mail = new MailTX( host, charset, smtpPort, auth, authPort,authUser, authPass ); // 5.4.3.2 認証対応
203                mail.setFrom( fromAddr );                       // 送信者アドレス
204                mail.setFilename( attachFiles );        // 添付ファイルをセットします。
205                mail.setSubject( mailTitle );           // メールタイトル
206                mail.setMessage( mailContent );         // メール本文
207                mail.setDebug( debugFlag );
208                setMailDst( invalidAddrBuf );           // 宛先をセットします。
209                // メール送信を行います。
210                int retryCount = MAX_RETRY;
211                while( retryCount > 0 ) {
212                        try {
213                                mail.sendmail();
214                        }
215                        catch( RuntimeException rex ) {
216                                Throwable cause = rex.getCause();
217                                if( cause instanceof SendFailedException ) {
218                                        Address[] invAddress = ( (SendFailedException) cause ).getInvalidAddresses();
219                                        if( invAddress != null ) {
220                                                int invCount = invAddress.length;
221                                                for( int i = 0; i < invCount; i++ ) {
222                                                        invalidAddrBuf.add( ( (InternetAddress) invAddress[i] ).getAddress() );
223                                                }
224                                        }
225                                }
226                                else {
227                                        String errMsg = "送信時にエラー発生しました。" + rex.getMessage();
228                                        throw new RuntimeException( errMsg,rex );
229                                }
230                        }
231
232//                      if( invalidAddrBuf.size() > 0 ) {
233//                              StringBuilder errMsgBuf = new StringBuilder();
234//                              for( int i = 0; i < invalidAddrBuf.size(); i++ ) {
235//                                      errMsgBuf.append( "," ).append( invalidAddrBuf.get( i ) );
236//                              }
237//                              String userIds = getUserIds( invalidAddrBuf );
238//                              String errMsg = "アドレスエラー。ユーザーID:" + userIds + " アドレス:" + errMsgBuf.toString().substring( 1 );
239//                              if( "true".equals( initParamMap.get( "ADDR_CHECK" ) ) ){
240//                                      throw new RuntimeException( errMsg );
241//                              }
242//                              else {
243//                                      // メールアカウントチェックしない場合、無効のメールアドレスを除いて再送します。
244//                                      setMailDst( invalidAddrBuf );
245//                                      retryCount--;
246//                                      invalidAddrBuf.clear();
247//                                      errAddrList.add( errMsg );
248//                              }
249//                      }
250//                      else {
251//                              retryCount = -1;
252//                      }
253
254                        if( invalidAddrBuf.isEmpty() ) {
255                                retryCount = -1;
256                        }
257                        else {
258                                StringBuilder errMsgBuf = new StringBuilder();
259                                for( int i = 0; i < invalidAddrBuf.size(); i++ ) {
260                                        errMsgBuf.append( ',' ).append( invalidAddrBuf.get( i ) );
261                                }
262                                String userIds = getUserIds( invalidAddrBuf );
263                                String errMsg = "アドレスエラー。ユーザーID:" + userIds + " アドレス:" + errMsgBuf.toString().substring( 1 );
264                                if( "true".equals( initParamMap.get( "ADDR_CHECK" ) ) ){
265                                        throw new RuntimeException( errMsg );
266                                }
267                                else {
268                                        // メールアカウントチェックしない場合、無効のメールアドレスを除いて再送します。
269                                        setMailDst( invalidAddrBuf );
270                                        retryCount--;
271                                        invalidAddrBuf.clear();
272                                        errAddrList.add( errMsg );
273                                }
274                        }
275                }
276                commitMailDB();         // 送信結果を履歴テーブル、宛先テーブルにセットします。
277        }
278
279        /**
280         * デバッグ情報の表示を行うかどうか[true/false]をセットします。
281         *
282         * @param   debug  [true:出力する/それ以外:しない]
283         */
284        public void setDebug( final boolean debug ) {
285                debugFlag = debug;
286        }
287
288        /**
289         * メール送信者アドレスをセットします。
290         *
291         * @param  from 送信者アドレス
292         */
293        public void setFromAddr( final String from ) {
294                fromAddr = from;
295        }
296
297        /**
298         * メールタイトルをセットします。
299         *
300         * @param  title メールタイトル
301         */
302        public void setTitle( final String title ) {
303                mailTitle = title;
304        }
305
306        /**
307         * メール本文をセットします。
308         *
309         * @param  content メール本文
310         */
311        public void setContent( final String content ) {
312                mailContent = content;
313        }
314
315        /**
316         * メール送信ホストをセットします。
317         * 初期値は、システム定数のCOMMON_MAIL_SERVER を使用します。
318         *
319         * (初期値:システム定数のCOMMON_MAIL_SERVER[={@og.value org.opengion.hayabusa.common.SystemData#COMMON_MAIL_SERVER}])。
320         *
321         * @og.rev 5.6.6.0 (2013/07/05)
322         *
323         * @param  hostName 送信ホスト
324         */
325        public void setHost( final String hostName ) {
326                host = nval( hostName, host );
327        }
328
329        /**
330         * メール送信ポート番号をセットします。
331         * 初期値は、システム定数のSMTP_PORT を使用します。
332         *
333         * (初期値:システム定数のSMTP_PORT[={@og.value org.opengion.hayabusa.common.SystemData#SMTP_PORT}])。
334         *
335         * @og.rev 5.6.6.0 (2013/07/05)
336         *
337         * @param  port SMTPポート
338         */
339        public void setPort( final String port ) {
340                smtpPort = nval( port, smtpPort );
341        }
342
343        /**
344         * メール送信時認証有無をセットします。
345         * 認証を行う場合は「POP_BEFORE_SMTP」と指定して下さい。
346         * 認証時には認証ユーザと認証パスワードを設定する必要があります。
347         * 初期値は、システム定数のMAIL_SEND_AUTH を使用します。
348         *
349         * (初期値:システム定数のMAIL_SEND_AUTH[={@og.value org.opengion.hayabusa.common.SystemData#MAIL_SEND_AUTH}])。
350         *
351         * @og.rev 5.6.6.0 (2013/07/05)
352         *
353         * @param  useAuth 認証方式
354         */
355        public void setAuth( final String useAuth ) {
356                auth = nval( useAuth, auth );
357        }
358
359        /**
360         * メール送信認証ユーザをセットします。
361         * 初期値は、システム定数のMAIL_SEND_AUTH_USER を使用します。
362         *
363         * (初期値:システム定数のMAIL_SEND_AUTH_USER[={@og.value org.opengion.hayabusa.common.SystemData#MAIL_SEND_AUTH_USER}])。
364         *
365         * @og.rev 5.6.6.0 (2013/07/05)
366         *
367         * @param  user 認証ユーザ
368         */
369        public void setAuthUser( final String user ) {
370                authUser = nval( user, authUser );
371        }
372
373        /**
374         * メール送信認証パスワードをセットします。
375         * 初期値は、システム定数のMAIL_SEND_AUTH_PASSWORD を使用します。
376         *
377         * (初期値:システム定数のMAIL_SEND_AUTH_PASSWORD[={@og.value org.opengion.hayabusa.common.SystemData#MAIL_SEND_AUTH_PASSWORD}])。
378         *
379         * @og.rev 5.6.6.0 (2013/07/05)
380         *
381         * @param  pass 認証パスワード
382         */
383        public void setAuthPass( final String pass ) {
384                authPass = nval( pass, authPass );
385        }
386
387        /**
388         * メール送信者アドレスを返します。
389         *
390         * @return      送信者アドレス
391         */
392        public String getFromAddr() {
393                return fromAddr;
394        }
395
396        /**
397         * メールタイトルを返します。
398         *
399         * @return      メールタイトル
400         */
401        public String getTitle() {
402                return mailTitle;
403        }
404
405        /**
406         * メール本文を返します。
407         *
408         * @return      メール本文
409         */
410        public String getContent() {
411                return mailContent;
412        }
413
414        /**
415         * 送信結果を履歴テーブル(GE32)と宛先テーブル(GE34)に登録します。
416         * 登録時に、桁数オーバーにならないように、テーブル定義の桁数を上限として、
417         * 登録前に各項目の桁数整理を行います。
418         *
419         * @og.rev 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対策
420         * @og.rev 5.9.1.3 (2015/10/30) 4000文字カットをやめる
421         */
422        protected void commitMailDB(){
423                // 履歴テーブルの追加
424                String[] insGE32Args = new String[15];
425                String ykno = getYkno();
426                insGE32Args[GE32_YKNO]          = ykno;
427                insGE32Args[GE32_PARAKEY]       = initParamMap.get( "PARAKEY" );
428                insGE32Args[GE32_PTN_ID]        = trim( initParamMap.get( "PTN_ID" ), 20 );
429                insGE32Args[GE32_FROM_ADDR] = trim( fromAddr, 100);
430                insGE32Args[GE32_TITLE]         = trim( mailTitle, 300);
431//              insGE32Args[GE32_CONTENTS]      = trim( mailContent,4000);              // 5.0.3.0 (2009/11/04) CONTENT ⇒ CONTENTS
432                insGE32Args[GE32_CONTENTS]      = mailContent;                                  // 5.9.1.3 (2015/10/30)
433                insGE32Args[GE32_ATTACH1]       = "";
434                insGE32Args[GE32_ATTACH2]       = "";
435                insGE32Args[GE32_ATTACH3]       = "";
436                insGE32Args[GE32_ATTACH4]       = "";
437                insGE32Args[GE32_ATTACH5]       = "";
438                if ( attachFiles != null ) {
439                        int attSize = attachFiles.length;
440                        for( int i = 0; i < attSize; i++ ) {
441                                insGE32Args[6 + i] = trim( attachFiles[i], 256);
442                        }
443                }
444                insGE32Args[GE32_DYSET]  = HybsSystem.getDate( "yyyyMMddHHmmss" );
445                insGE32Args[GE32_USRSET] = initParamMap.get( "LOGIN_USERID" );
446                insGE32Args[GE32_PGUPD] = initParamMap.get( "PGID" );
447                insGE32Args[GE32_SYSTEM_ID] = initParamMap.get( "SYSTEM_ID" );
448//              DBUtil.dbExecute( insGE32, insGE32Args, appInfo );
449                DBUtil.dbExecute( insGE32, insGE32Args, appInfo, DBID );        // 5.5.5.1 (2012/08/07)
450
451                // 宛先テーブル追加
452                String[] insGE34Args = new String[10];
453                insGE34Args[GE34_YKNO]= ykno;
454                for( String dstId : mailDstMap.keySet() ) {
455                        insGE34Args[GE34_DST_ID]        = trim( mailDstMap.get( dstId )[MailPattern.IDX_DST_ID]  , 10 );
456                        insGE34Args[GE34_GROUP_ID]      = trim( mailDstMap.get( dstId )[MailPattern.IDX_GROUP_ID], 20 );
457                        insGE34Args[GE34_DST_NAME]      = trim( mailDstMap.get( dstId )[MailPattern.IDX_DST_NAME], 20 );
458                        insGE34Args[GE34_DST_ADDR]      = trim( mailDstMap.get( dstId )[MailPattern.IDX_DST_ADDR], 100 );
459                        insGE34Args[GE34_DST_KBN]       = mailDstMap.get( dstId )[MailPattern.IDX_DST_KBN];
460                        insGE34Args[GE34_FGJ]           = mailDstMap.get( dstId )[MailPattern.IDX_FGJ];
461                        insGE34Args[GE34_DYSET]         = HybsSystem.getDate( "yyyyMMddHHmmss" );
462                        insGE34Args[GE34_USRSET]        = initParamMap.get( "LOGIN_USERID" );
463                        insGE34Args[GE34_PGUPD]         = initParamMap.get( "PGID" );
464//                      DBUtil.dbExecute( insGE34, insGE34Args, appInfo );
465                        DBUtil.dbExecute( insGE34, insGE34Args, appInfo, DBID );                // 5.5.5.1 (2012/08/07)
466                }
467        }
468
469        /**
470         * パラメータマップをセットします。
471         *
472         * @param       params  パラメータのマップ
473         */
474        protected void setInitParams( final Map<String, String> params ) {
475                initParamMap = params;
476        }
477
478        /**
479         * 添付ファイル配列をセットします。
480         *
481         * @param       attach1 添付ファイル名1
482         * @param       attach2 添付ファイル名2
483         * @param       attach3 添付ファイル名3
484         * @param       attach4 添付ファイル名4
485         * @param       attach5 添付ファイル名5
486         */
487        protected void setAttachFiles( final String attach1
488                                                                , final String attach2
489                                                                , final String attach3
490                                                                , final String attach4
491                                                                , final String attach5 ) {
492                List<String> fileList = new ArrayList<String>();
493                if( attach1 != null && attach1.length() != 0 ) { fileList.add( attach1 ); }
494                if( attach2 != null && attach2.length() != 0 ) { fileList.add( attach2 ); }
495                if( attach3 != null && attach3.length() != 0 ) { fileList.add( attach3 ); }
496                if( attach4 != null && attach4.length() != 0 ) { fileList.add( attach4 ); }
497                if( attach5 != null && attach5.length() != 0 ) { fileList.add( attach5 ); }
498                attachFiles = fileList.toArray( new String[fileList.size()] );
499        }
500
501        /**
502         * メール宛先マップをセットします。
503         *
504         * @param   mailDst     メール宛先マップ
505         */
506        protected void setMailDstMap( final Map<String, String[]> mailDst ) {
507                mailDstMap = mailDst;
508        }
509
510        /**
511         * メール宛先マップをセットします。
512         *
513         * @return      メール宛先マップ
514         */
515        protected Map<String, String[]> getMailDstMap() {
516                return mailDstMap;
517        }
518
519        /**
520         * 指定の長さ以内の文字列を返します。
521         *
522         * @og.rev 5.9.1.3 (2015/10/30) 文字数ではなくByte数に変更
523         *
524         * @param       src             オリジナルの文字列
525         * @param       maxLen  指定の長さ
526         *
527         * @return      指定の長さに短縮された文字列
528         */
529        protected String trim( final String src, final int maxLen ) {
530                String rtn = src;
531                if( src != null && src.length() > maxLen ) {
532//                      rtn = src.substring( 0, maxLen );
533                        rtn = StringUtil.cut( src, maxLen );
534                }
535                return rtn;
536        }
537
538        /**
539         * アドレスチェックのエラーリストを返します。
540         *
541         * @return      エラーリスト
542         */
543        protected List<String> getErrList(){
544                return errAddrList;
545        }
546
547        /**
548         * 宛先マップを元に、送信オブジェクトに宛先をセットします。
549         * セットする際に、アカウントエラーとなっているアドレスを除外します。
550         * 宛先が存在しない場合、例外を投げます。
551         *
552         * @og.rev 4.3.7.5 (2009/07/08) 送信先名称が設定されていない場合は、アドレスを<>で囲わない
553         *
554         * @param invalidAddr 宛先のリスト
555         */
556        private void setMailDst( final List<String> invalidAddr ){
557                ArrayList<String> toList = new ArrayList<String>();
558                ArrayList<String> ccList = new ArrayList<String>();
559                ArrayList<String> bccList = new ArrayList<String>();
560
561                Map<Integer, ArrayList<String>> tempMap = new HashMap<Integer, ArrayList<String>>();
562                tempMap.put( Integer.valueOf( MailPattern.KBN_TO ),  toList );
563                tempMap.put( Integer.valueOf( MailPattern.KBN_CC ),  ccList );
564                tempMap.put( Integer.valueOf( MailPattern.KBN_BCC ), bccList );
565
566                for( String dstId : mailDstMap.keySet() ) {
567                        String[] dstInfo = mailDstMap.get( dstId );
568                        Integer kbn = Integer.valueOf( dstInfo[MailPattern.IDX_DST_KBN] );
569                        if( !invalidAddr.contains( dstInfo[MailPattern.IDX_DST_ADDR] )
570                                        && !FGJ_ADDR_ERR.equals( dstInfo[MailPattern.IDX_FGJ] )){
571                                dstInfo[MailPattern.IDX_FGJ] = FGJ_SEND_OVER;
572
573                                // 4.3.7.5 (2009/07/08)
574                                String name = dstInfo[MailPattern.IDX_DST_NAME];
575                                if( name != null && name.length() > 0 ) {
576                                        tempMap.get( kbn ).add( dstInfo[MailPattern.IDX_DST_NAME] +  "<"+ dstInfo[MailPattern.IDX_DST_ADDR] + ">" );
577                                }
578                                else {
579                                        tempMap.get( kbn ).add( dstInfo[MailPattern.IDX_DST_ADDR] );
580                                }
581                        }
582                        else {
583                                if( FGJ_SEND_OVER.equals( dstInfo[MailPattern.IDX_FGJ] ) ) {
584                                        dstInfo[MailPattern.IDX_FGJ] = FGJ_ACNT_ERR;
585                                }
586                        }
587                }
588
589                mail.clearTo();         // 宛先(TO)をクリア
590                mail.clearCc();         // 宛先(CC)をクリア
591                mail.clearBcc();        // 宛先(BCC)をクリア
592                boolean haveValidAddr = false ;
593//              if( toList.size() > 0 ) {    // toのセット
594                if( ! toList.isEmpty() ) {      // toのセット
595                        haveValidAddr = true;
596                        String[] to = toList.toArray( new String[toList.size()] );
597                        mail.setTo( to );
598                }
599//              if( ccList.size() > 0 ) {    // ccのセット
600                if( ! ccList.isEmpty() ) {      // ccのセット
601                        haveValidAddr = true;
602                        String[] cc = ccList.toArray( new String[ccList.size()] );
603                        mail.setCc( cc );
604                }
605//              if( bccList.size() > 0 ) {   // bccのセット
606                if( ! bccList.isEmpty() ) {     // bccのセット
607                        haveValidAddr = true;
608                        String[] bcc = bccList.toArray( new String[bccList.size()] );
609                        mail.setBcc( bcc );
610                }
611                if( !haveValidAddr ){           // 宛先全部無効の場合、例外を投げます。
612                        String errMsg = "宛先のメールアドレスが有効ではありません。"
613                                                + "TO , CC , BCC のいづれにもアドレスが設定されていません。"; // 5.1.8.0 (2010/07/01) errMsg 修正
614                        throw new RuntimeException( errMsg );
615                }
616        }
617
618        /**
619         * 要求NOを採番します。
620         * この要求NOで履歴テーブル(GE32)と宛先テーブル(GE30)の関連付けを持たせます。
621         *
622         * @og.rev 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対策
623         * @og.rev 5.9.26.0 (2017/11/02) DB共通化対応
624         *
625         * @return      要求NO
626         */
627        private String getYkno() {
628// 2017/10/27 DELETE
629//              String[][] tmp = DBUtil.dbExecute( selYkno, new String[0], appInfo );
630//              String[][] tmp = DBUtil.dbExecute( selYkno, new String[0], appInfo, DBID );             // 5.5.5.1 (2012/08/07)
631//              if( tmp == null || tmp.length == 0 ) {
632//                      String errMsg = "要求NO採番エラー"
633//                                              + " SQL=" + selYkno ;           // 5.1.8.0 (2010/07/01) errMsg 修正
634//                      throw new RuntimeException( errMsg );
635//              }
636//              return tmp[0][0];
637
638                // 5.9.26.0 (2017/11/02) DBFnctionNameを利用して、シーケンスを取得
639                String selYkno = "";
640                Transaction tran = new TransactionReal( appInfo );
641                try{
642                        selYkno = String.valueOf(DBFunctionName.getDBName(ConnectionFactory.getDBName(DBID)).getSequence("GE32S02", tran));
643                }finally{
644                        tran.close();
645                }
646
647                return selYkno;
648        }
649
650        /**
651         * メールアドレスのリストよりユーザーIDを逆引きします。
652         *
653         * @param       addressList     メールアドレスのリスト
654         *
655         * @return      ユーザーID
656         */
657        private String getUserIds( final List<String> addressList ){
658                StringBuilder idBuf = new StringBuilder();
659                Map<String,String>  addressMap = new HashMap<String, String>();
660                for( String userId : mailDstMap.keySet() ) {
661                        String[] dstInfo = mailDstMap.get( userId );
662                        addressMap.put( dstInfo[MailPattern.IDX_DST_ADDR], userId );
663                }
664                for(int i=0; i < addressList.size(); i++){
665                        idBuf.append( ',' ).append( addressMap.get( addressList.get( i ) ) );
666                }
667                String rtn = "";
668                if ( idBuf.length() > 0 ) {
669                        rtn = idBuf.toString().substring( 1 );
670                }
671                return rtn;
672        }
673}