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