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 java.util.ArrayList;
019import java.util.Arrays;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.opengion.fukurou.db.DBUtil;
025import org.opengion.fukurou.mail.MailTX;
026import org.opengion.fukurou.util.LogWriter;
027import org.opengion.fukurou.util.StringUtil;
028import org.opengion.hayabusa.common.HybsSystem;
029
030/**
031 * パッチによるメール送信の実装クラスです。
032 * 送信デーモンはパラメータテーブル(GE30)を監視して、新規のデータが登録されたら、
033 * そのデータをパラメータとしてメール合成処理メソッドに渡して合成を行って送信します。
034 * 最後に、処理結果を受取って、パラメータテーブルの状況フラグを送信済/送信エラーに更新します。
035 * エラーが発生した場合、エラーテーブルにエラーメッセージを書き込みます。
036 *
037 * @og.group メールモジュール
038 *
039 * @version  4.0
040 * @author   Sen.Li
041 * @since    JDK1.6
042 *
043 * @og.rev 5.9.26.0 (2017/11/02) 子クラスで利用する定数をprivateからprotectedに変更
044 */
045public class MailManager_DB extends AbstractMailManager {
046        // 5.9.26.0 (2017/11/02) 子クラスで利用定数を、privateからprotectedに変更
047        // 5.2.0.0 (2010/09/01) Ver4互換モード対応
048        private static final String H_TXT = HybsSystem.sysBool( "VER4_COMPATIBLE_MODE" ) ? "HEADER" : "H_TXT";
049        private static final String F_TXT = HybsSystem.sysBool( "VER4_COMPATIBLE_MODE" ) ? "FOOTER" : "F_TXT";
050
051        // 5.2.0.0 (2010/09/01) Ver4互換モード対応
052//      private static final String     selGE30 = "SELECT  UNIQ,PTN_ID,FROM_ID,TO_ID,CC_ID,BCC_ID,H_TXT,F_TXT"          // 5.0.3.0 (2009/11/04)
053        protected static final String   selGE30 = "SELECT  UNIQ,PTN_ID,FROM_ID,TO_ID,CC_ID,BCC_ID,"+H_TXT+","+F_TXT
054                                                                                +",PARAM0,PARAM1,PARAM2,PARAM3,PARAM4,PARAM5,PARAM6,PARAM7,PARAM8,PARAM9"
055                                                                                +",ATTACH1,ATTACH2,ATTACH3,ATTACH4,ATTACH5"
056                                                                                + " FROM GE30"
057                                                                                + " WHERE SYSTEM_ID =? AND FGJ='1'"
058                                                                                + " AND (SNDTIME IS NULL OR SNDTIME <= ? )"; // 5.9.18.0 (2017/03/02)
059        protected static final String   insGE36 = "INSERT INTO GE36(PARA_KEY,ERRMSG,DYSET,USRSET,PGUPD,SYSTEM_ID,FGJ)"
060                                                                                + " VALUES(?,?,?,?,?,?,'1')";
061        private static final String     updGE30 = "UPDATE GE30 SET FGJ= ? WHERE UNIQ = ? ";
062        protected static final String   SNED_OK = "2";
063        protected static final String   SNED_NG = "8";
064        protected static final int GE30_UNIQ            = 0 ;
065        private static final int GE30_PTN_ID    = 1 ;
066        private static final int GE30_FROM_ID   = 2 ;
067        private static final int GE30_TO_ID             = 3 ;
068        private static final int GE30_CC_ID             = 4 ;
069        private static final int GE30_BCC_ID    = 5 ;
070        private static final int GE30_H_TXT             = 6 ;           // 5.0.3.0 (2009/11/04) HEADER ⇒ H_TXT
071        private static final int GE30_F_TXT             = 7 ;           // 5.0.3.0 (2009/11/04) FOOTER ⇒ F_TXT
072        private static final int GE30_PARAM0    = 8 ;
073        private static final int GE30_PARAM1    = 9 ;
074        private static final int GE30_PARAM2    = 10 ;
075        private static final int GE30_PARAM3    = 11 ;
076        private static final int GE30_PARAM4    = 12 ;
077        private static final int GE30_PARAM5    = 13 ;
078        private static final int GE30_PARAM6    = 14 ;
079        private static final int GE30_PARAM7    = 15 ;
080        private static final int GE30_PARAM8    = 16 ;
081        private static final int GE30_PARAM9    = 17 ;
082        private static final int GE30_ATTACH1   = 18 ;
083        private static final int GE30_ATTACH2   = 19 ;
084        private static final int GE30_ATTACH3   = 20 ;
085        private static final int GE30_ATTACH4   = 21 ;
086        private static final int GE30_ATTACH5   = 22 ;
087        protected static final int GE36_PARA_KEY        = 0 ;
088        protected static final int GE36_ERRMSG  = 1 ;
089        protected static final int GE36_DYSET           = 2 ;
090        protected static final int GE36_USRSET  = 3 ;
091        protected static final int GE36_PGUPD   = 4 ;
092        protected static final int GE36_SYSTEM_ID       = 5 ;
093        protected final List<String>       errMsgList     = new ArrayList<String>();
094
095        /**
096         * バッチより呼出のメインメソッドです。
097         * パラメータテーブル(GE30)を監視します。
098         * 新規のデータが登録されたら、メール文を合成して送信を行います。
099         * エラーが発生した場合、エラーテーブルにエラーメッセージを書き込みます。
100         *
101         * @og.rev 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対策
102         * @og.rev 5.9.18.0 (2017/03/02) SNDTIME対応
103         *
104         * @param systemId システムID
105         */
106        public void sendDBMail( final String systemId ){
107                // パラメータテーブルよりバッチでセットしたデータを取得します。
108//              String[][] ge30datas = DBUtil.dbExecute( selGE30, new String[]{ systemId }, appInfo );
109//              String[][] ge30datas = DBUtil.dbExecute( selGE30, new String[]{ systemId }, appInfo, DBID );            // 5.5.5.1 (2012/08/07)
110                String[][] ge30datas = DBUtil.dbExecute( selGE30, new String[]{ systemId, HybsSystem.getDate( "yyyyMMddHHmmss" ) }, appInfo, DBID );    // 5.9.18.0 (2017/03/02)
111
112                int ge30Len = ge30datas.length;
113
114                for( int i=0; i < ge30Len; i++ ) {
115                        String fgj = SNED_OK;
116                        try {
117                                Map<String, String> initParam = makeParamMap( systemId, ge30datas[i] );
118                                create( initParam );
119                                send();                                                         // 合成されたメール文書、宛先で送信処理を行います。
120                                errMsgList.addAll( getErrList() );
121                        }
122                        catch( RuntimeException rex ) {
123                                fgj = SNED_NG;
124                                errMsgList.add( "メール送信失敗しました。パラメータキー:" + ge30datas[i][GE30_UNIQ] + " " + rex.getMessage() );
125                        }
126                        finally {
127                                commitParamTable( ge30datas[i][GE30_UNIQ], fgj );
128
129//                              if ( errMsgList.size() > 0 ) {
130                                if ( ! errMsgList.isEmpty() ) {
131                                        writeErrorTable( ge30datas[i][GE30_UNIQ], systemId, errMsgList );
132                                        errMsgList.clear();
133                                }
134                        }
135                }
136        }
137
138        /**
139         * パラメータテーブルに登録したデータをパラメータマップにセットします。
140         *
141         * @og.rev 5.9.26.0 (2017/11/02) 子クラスでの利用対応。privateをprotectedに変更。
142         *
143         * @param       systemId        システムID
144         * @param       ge30Data        パラメータテーブルのデータ配列
145         *
146         * @return      データをセットしたマップ
147         */
148        protected Map<String, String> makeParamMap( final String systemId, final String[] ge30Data ){
149                Map<String,String>   paramMap = null;
150                if( ( ge30Data != null ) && ( ge30Data.length > 0 ) ) {
151                        paramMap = new HashMap<String,String>();
152                        paramMap.put( "SYSTEM_ID", systemId    );
153                        paramMap.put( "PARAKEY", ge30Data[GE30_UNIQ]    );
154                        paramMap.put( "PTN_ID" , ge30Data[GE30_PTN_ID]  );
155                        paramMap.put( "FROM"   , ge30Data[GE30_FROM_ID] );
156                        paramMap.put( "TO"     , ge30Data[GE30_TO_ID]   );
157                        paramMap.put( "CC"     , ge30Data[GE30_CC_ID]   );
158                        paramMap.put( "BCC"    , ge30Data[GE30_BCC_ID]  );
159                        paramMap.put( "H_TXT"  , ge30Data[GE30_H_TXT]   );                      // 5.0.3.0 (2009/11/04) HEADER ⇒ H_TXT
160                        paramMap.put( "F_TXT"  , ge30Data[GE30_F_TXT]   );                      // 5.0.3.0 (2009/11/04) FOOTER ⇒ F_TXT
161                        paramMap.put( "PARAM0" , ge30Data[GE30_PARAM0]  );
162                        paramMap.put( "PARAM1" , ge30Data[GE30_PARAM1]  );
163                        paramMap.put( "PARAM2" , ge30Data[GE30_PARAM2]  );
164                        paramMap.put( "PARAM3" , ge30Data[GE30_PARAM3]  );
165                        paramMap.put( "PARAM4" , ge30Data[GE30_PARAM4]  );
166                        paramMap.put( "PARAM5" , ge30Data[GE30_PARAM5]  );
167                        paramMap.put( "PARAM6" , ge30Data[GE30_PARAM6]  );
168                        paramMap.put( "PARAM7" , ge30Data[GE30_PARAM7]  );
169                        paramMap.put( "PARAM8" , ge30Data[GE30_PARAM8]  );
170                        paramMap.put( "PARAM9" , ge30Data[GE30_PARAM9]  );
171                        paramMap.put( "ATTACH1", ge30Data[GE30_ATTACH1] );
172                        paramMap.put( "ATTACH2", ge30Data[GE30_ATTACH2] );
173                        paramMap.put( "ATTACH3", ge30Data[GE30_ATTACH3] );
174                        paramMap.put( "ATTACH4", ge30Data[GE30_ATTACH4] );
175                        paramMap.put( "ATTACH5", ge30Data[GE30_ATTACH5] );
176                        paramMap.put( "DATE", HybsSystem.getDate("yyyy/MM/dd") );
177                        paramMap.put( "TIME", HybsSystem.getDate("HH:mm:ss") );
178                        paramMap.put( "LOGIN_USERID", "DAEMON" );
179                        paramMap.put( "LOGIN_USERNAME", "DAEMON" );
180                        paramMap.put( "PGID", "DAEMON" );
181                }
182                return paramMap;
183        }
184
185        /**
186         * 送信後、パラメータテーブルの状況フラグを更新します。
187         * 送信エラーなしの場合はフラグを’送信済(2)’、エラーの場合’送信エラー(8)’に更新します。
188         *
189         * @og.rev 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対策
190         * @og.rev 5.9.26.0 (2017/11/02) 子クラスでの利用対応。privateをprotectedに変更。
191         *
192         * @param       uniq    ユニークキー
193         * @param       fgj             状況フラグ[2:送信済/8:エラー]
194         */
195        protected void commitParamTable( final String uniq, final String fgj ){
196                String[] updGE30Args = { fgj, uniq };
197//              DBUtil.dbExecute( updGE30, updGE30Args, appInfo );
198                DBUtil.dbExecute( updGE30, updGE30Args, appInfo, DBID );                // 5.5.5.1 (2012/08/07)
199        }
200
201        /**
202         * エラーテーブルにエラーメッセージを登録します。
203         *
204         * @og.rev 4.4.0.1 (2009/08/08) メール送信追加
205         * @og.rev 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対策
206         *
207         * @param       paraKey         パラメータキー(GE36.PARA_KEY)
208         * @param       systemId        システムID
209         * @param       emList          エラーメッセージリスト
210         *
211         */
212//      private void writeErrorTable( final String paraKey, final String systemId, final List<String> errMsgList ){
213        private void writeErrorTable( final String paraKey, final String systemId, final List<String> emList ){
214                String[] insGE36Args = new String[6];
215                insGE36Args[GE36_PARA_KEY]      = paraKey;
216                insGE36Args[GE36_DYSET]         = HybsSystem.getDate( "yyyyMMddHHmmss" );
217                insGE36Args[GE36_USRSET]        = "DAEMON";
218                insGE36Args[GE36_PGUPD]         = "DAEMON";
219                insGE36Args[GE36_SYSTEM_ID] = systemId;
220                for( int i=0; i< emList.size(); i++ ){
221                        insGE36Args[GE36_ERRMSG] = trim( emList.get( i ), 4000);
222//                      DBUtil.dbExecute( insGE36, insGE36Args,appInfo );
223                        DBUtil.dbExecute( insGE36, insGE36Args, appInfo, DBID );                // 5.5.5.1 (2012/08/07)
224                }
225
226                sendMail( paraKey, systemId, emList ); // 4.4.0.1 (2009/08/08)
227        }
228
229        /**
230         * エラー情報のメール送信を行います。
231         * エラーメールは、システムパラメータ の COMMON_MAIL_SERVER(メールサーバー)と
232         * ERROR_MAIL_FROM_USER(エラーメール発信元)と、ERROR_MAIL_TO_USERS(エラーメール受信者)
233         * がすべて設定されている場合に、送信されます。
234         *
235         * @og.rev 4.4.0.1 (2009/08/08) 追加
236         * @og.rev 5.4.3.2 (2012/01/06) 認証対応
237         *
238         * @param       paraKey         メールキー
239         * @param       systemId        システムID
240         * @param       emList          エラーメッセージリスト
241         */
242//      private void sendMail( final String paraKey, final String systemId, final List<String> errMsgList ) {
243        private void sendMail( final String paraKey, final String systemId, final List<String> emList ) {
244
245                String   host = HybsSystem.sys( "COMMON_MAIL_SERVER" );
246                String   from = HybsSystem.sys( "ERROR_MAIL_FROM_USER" );
247                String   charset                = HybsSystem.sys( "MAIL_DEFAULT_CHARSET" );
248                String   smtpPort               = HybsSystem.sys( "SMTP_PORT" );                                // 5.4.3.2 (2012/01/06)
249                String   auth                   = HybsSystem.sys( "MAIL_SEND_AUTH" );                   // 5.4.3.2 (2012/01/06)
250                String   authPort               = HybsSystem.sys( "MAIL_SEND_AUTH_PORT" );              // 5.8.1.1 (2014/11/14)
251                String   authUser               = HybsSystem.sys( "MAIL_SEND_AUTH_USER" );              // 5.4.3.2 (2012/01/06)
252                String   authPass               = HybsSystem.sys( "MAIL_SEND_AUTH_PASSWORD" );  // 5.4.3.2 (2012/01/06)
253
254                String[] to = StringUtil.csv2Array( HybsSystem.sys( "ERROR_MAIL_TO_USERS" ) );
255                if( host != null && from != null && to.length > 0 ) {
256                        String subject = "SYSTEM_ID=[" + systemId + "] , PARA_KEY=[" + paraKey + "] , "
257                                                   + "DMN_HOST=[" + HybsSystem.HOST_NAME + "]" ;
258                        StringBuilder inErrMsg = new StringBuilder();
259                        inErrMsg.append( emList.size() + "件のエラーがありました。" );
260                        inErrMsg.append( HybsSystem.CR );
261                        for( int i=0; i< emList.size(); i++ ){
262                                inErrMsg.append( i+1 );
263                                inErrMsg.append( "-----" );
264                                inErrMsg.append( HybsSystem.CR );
265                                inErrMsg.append( emList.get( i ) );
266                                inErrMsg.append( HybsSystem.CR );
267                        }
268                        try {
269                                //MailTX tx = new MailTX( host );
270                                MailTX tx = new MailTX( host, charset, smtpPort, auth, authPort, authUser, authPass ); // 5.4.3.2
271                                tx.setFrom( from );
272                                tx.setTo( to );
273                                tx.setSubject( "メールモジュール送信エラー:" + subject );
274                                tx.setMessage( inErrMsg.toString() );
275                                tx.sendmail();
276                        }
277                        catch( Throwable ex ) {
278                                String errMsg = "エラー時メール送信に失敗しました。" + HybsSystem.CR
279                                                        + " SUBJECT:" + subject                                 + HybsSystem.CR
280                                                        + " HOST:" + host                                               + HybsSystem.CR
281                                                        + " FROM:" + from                                               + HybsSystem.CR
282                                                        + " TO:"   + Arrays.toString( to )              + HybsSystem.CR
283                                                        + ex.getMessage();              // 5.1.8.0 (2010/07/01) errMsg 修正
284                                LogWriter.log( errMsg );
285                                LogWriter.log( ex );
286                        }
287                }
288        }
289}