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