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