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
018
019import java.util.HashMap;
020import java.util.Map;
021import java.util.TreeMap;
022
023import org.opengion.fukurou.db.DBUtil;
024import org.opengion.fukurou.db.Transaction;
025import org.opengion.fukurou.db.TransactionReal;
026import org.opengion.fukurou.db.ConnectionFactory;
027import org.opengion.fukurou.db.DBFunctionName;
028import org.opengion.fukurou.util.ApplicationInfo;
029import org.opengion.fukurou.util.StringUtil;
030import org.opengion.hayabusa.common.HybsSystem;
031
032/**
033 * メールモジュール関係の機能の一部を他から使用するためのクラスです。
034 * 
035 * ※MailSenderTagからGE32,34へ履歴を出力する機能を追加する際に、モジュール系の動作を本パッケージに集約しておくために作成。
036 *   必要としている箇所のみ実装。
037 *   
038 * @og.rev 5.9.2.3 (2015/11/27) 新規作成
039 *
040 * @og.group メールモジュール
041 *
042 * @version  4.0
043 * @author   Takahashi Masakazu
044 * @since    JDK1.6
045 */
046public class MailModuleUtil {
047
048        //  Ver4互換モード対応
049        private static final String CONTENTS = HybsSystem.sysBool( "VER4_COMPATIBLE_MODE" ) ? "CONTENT" : "CONTENTS";
050
051//      private static final String     selYkno = "SELECT GE32S02.NEXTVAL YKNO FROM DUAL";
052        private static final String     selYkno         = "GE32S02";
053        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)"
054                                                                                        + " VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,'1')";
055        private static final String insGE34             = "INSERT INTO GE34(YKNO,DST_ID,GROUP_ID,DST_NAME,DST_ADDR,DST_KBN,FGJ,DYSET,USRSET,PGUPD)"
056                                                                                        + " VALUES(?,?,?,?,?,?,?,?,?,?)";
057
058        // 内部データのカラム番号(履歴テーブル)
059        private static final int GE32_YKNO              = 0 ;
060        private static final int GE32_PARAKEY   = 1 ;
061        private static final int GE32_PTN_ID    = 2;
062        private static final int GE32_FROM_ADDR = 3;
063        private static final int GE32_TITLE     = 4;
064        private static final int GE32_CONTENTS  = 5;            
065        private static final int GE32_ATTACH1   = 6;
066        private static final int GE32_ATTACH2   = 7;
067        private static final int GE32_ATTACH3   = 8;
068        private static final int GE32_ATTACH4   = 9;
069        private static final int GE32_ATTACH5   = 10;
070        private static final int GE32_DYSET             = 11;
071        private static final int GE32_USRSET    = 12;
072        private static final int GE32_PGUPD     = 13;
073        private static final int GE32_SYSTEM_ID = 14;
074        // 内部データのカラム番号(履歴テーブル)
075        private static final int GE34_YKNO              = 0 ;
076        private static final int GE34_DST_ID    = 1 ;
077        private static final int GE34_GROUP_ID  = 2 ;
078        private static final int GE34_DST_NAME  = 3 ;
079        private static final int GE34_DST_ADDR  = 4 ;
080        private static final int GE34_DST_KBN   = 5 ;
081        private static final int GE34_FGJ               = 6 ;
082        private static final int GE34_DYSET             = 7 ;
083        private static final int GE34_USRSET    = 8 ;
084        private static final int GE34_PGUPD     = 9 ;
085        
086        // アドレスマップ
087        private static final int IDX_DST_ADDR   = 0;
088        private static final int IDX_DST_KBN    = 1;
089        
090        /** メール送信区分 {@value} */
091        private static final int KBN_TO                 = 0 ;   // メール送信区分(TO)
092        /** メール送信区分 {@value} */
093        private static final int KBN_CC                 = 1 ;   // メール送信区分(CC)
094        /** メール送信区分 {@value} */
095        private static final int KBN_BCC                        = 2 ;   // メール送信区分(BCC)
096
097        private Map<String, String[]>     mailDstMap              = null;
098        private Map<String,String>                initParamMap    = null;         // パラメータマップ
099
100        protected final String DBID = HybsSystem.sys( "RESOURCE_DBID" );
101        protected final DBFunctionName dbName = DBFunctionName.getDBName( ConnectionFactory.getDBName( DBID ) ); // 5.9.31.1 (2018/04/13)
102
103        /** コネクションにアプリケーション情報を追記するかどうか指定 */
104        private static final boolean USE_DB_APPLICATION_INFO  = HybsSystem.sysBool( "USE_DB_APPLICATION_INFO" ) ;
105
106        /** アプリケーション情報 */
107        public static final ApplicationInfo appInfo;
108        static {
109                if( USE_DB_APPLICATION_INFO ) {
110                        appInfo = new ApplicationInfo();
111                        // ユーザーID,IPアドレス,ホスト名
112                        appInfo.setClientInfo( "MailModuel", HybsSystem.HOST_ADRS, HybsSystem.HOST_NAME );
113                        // 画面ID,操作,プログラムID
114                        appInfo.setModuleInfo( "MailModuel", "MailManager", "MailManager" );
115                }
116                else {
117                        appInfo = null;
118                }
119        }
120
121
122        /**
123         * 履歴テーブル(GE32)と宛先テーブル(GE34)に登録します。
124         * 登録時に、桁数オーバーにならないように、テーブル定義の桁数を上限として、
125         * 登録前に各項目の桁数整理を行います。
126         * 
127         * @og.rev 5.9.3.0 (2015/12/04) 添付ファイル対応
128         *
129         */
130        public void commitMailDB(){
131                // 履歴テーブルの追加
132                String[] insGE32Args = new String[15];
133                String ykno = getYkno();
134                String[] attachFiles = StringUtil.csv2Array( initParamMap.get( "FILES" )); // 5.9.3.0
135                
136                insGE32Args[GE32_YKNO]          = ykno;
137                insGE32Args[GE32_PARAKEY]       = initParamMap.get( "PARAKEY" );
138                insGE32Args[GE32_PTN_ID]        = trim( initParamMap.get( "PTN_ID" ), 20 );
139                insGE32Args[GE32_FROM_ADDR] = trim( initParamMap.get( "FROM" ), 100);
140                insGE32Args[GE32_TITLE]         = trim( initParamMap.get( "TITLE" ), 300);
141                insGE32Args[GE32_CONTENTS]      = initParamMap.get( "CONTENT" );        
142//              insGE32Args[GE32_ATTACH1]       = "";
143//              insGE32Args[GE32_ATTACH2]       = "";
144//              insGE32Args[GE32_ATTACH3]       = "";
145//              insGE32Args[GE32_ATTACH4]       = "";
146//              insGE32Args[GE32_ATTACH5]       = "";
147                // 5.9.3.0
148                if ( attachFiles != null ) { 
149                        int attSize = attachFiles.length;
150                        for( int i = 0; i < attSize; i++ ) {
151                                insGE32Args[6 + i] = trim( attachFiles[i], 256);
152                        }
153                }
154                
155                insGE32Args[GE32_DYSET]  = HybsSystem.getDate( "yyyyMMddHHmmss" );
156                insGE32Args[GE32_USRSET] = initParamMap.get( "LOGIN_USERID" );
157                insGE32Args[GE32_PGUPD] = initParamMap.get( "PGID" );
158                insGE32Args[GE32_SYSTEM_ID] = initParamMap.get( "SYSTEM_ID" );
159                DBUtil.dbExecute( insGE32, insGE32Args, appInfo, DBID );
160
161                // 宛先テーブル追加
162                String[] insGE34Args = new String[10];
163                insGE34Args[GE34_YKNO]= ykno;
164                for( String dstId : mailDstMap.keySet() ) {
165                        insGE34Args[GE34_DST_ID]        = trim( mailDstMap.get( dstId )[IDX_DST_ADDR], 10 );
166                        insGE34Args[GE34_GROUP_ID]      = "";
167                        insGE34Args[GE34_DST_NAME]      = "";
168                        insGE34Args[GE34_DST_ADDR]      = trim( mailDstMap.get( dstId )[IDX_DST_ADDR], 100 );
169                        insGE34Args[GE34_DST_KBN]       = mailDstMap.get( dstId )[IDX_DST_KBN];
170                        insGE34Args[GE34_FGJ]           = "1";
171                        insGE34Args[GE34_DYSET]         = HybsSystem.getDate( "yyyyMMddHHmmss" );
172                        insGE34Args[GE34_USRSET]        = initParamMap.get( "LOGIN_USERID" );
173                        insGE34Args[GE34_PGUPD]         = initParamMap.get( "PGID" );
174                        DBUtil.dbExecute( insGE34, insGE34Args, appInfo, DBID );
175                }
176        }
177
178        /**
179         * パラメータマップをセットします。
180         *
181         * @param       params  パラメータのマップ
182         */
183//      public void setInitParams( final Map<String, String> params ) {
184//              initParamMap = params;
185//      }
186        
187        /**
188         * パラメータからマップをセットします。
189         * 
190         * @og.rev 5.9.3.0 (2015/11/30) files追加
191         * 
192         * @param systemId       システムID
193         * @param from          FROMアドレス
194         * @param tos           TOアドレス(カンマ区切り)
195         * @param ccs           CCアドレス(カンマ区切り)
196         * @param bccs          BCCアドレス(カンマ区切り)
197         * @param content       本文
198         * @param title         タイトル
199         * @param userid        登録ユーザ
200         * @param pgid          登録PG
201         * @param files         添付ファイル
202         *
203         */
204        public void setInitParams( final String systemId, final String from, final String[] tos, final String[] ccs
205                                                                ,final String[] bccs, final String content, final String title, final String userid, final String pgid
206                                                                ,final String[] files) {
207                Map<String,String>   paramMap = new HashMap<String,String>();;
208                
209                paramMap.put( "SYSTEM_ID", systemId    );
210                paramMap.put( "FROM"   , from );
211                paramMap.put( "TO"     , StringUtil.array2csv( tos )   );
212                paramMap.put( "CC"     , StringUtil.array2csv( ccs )   );
213                paramMap.put( "BCC"    , StringUtil.array2csv( bccs )  );
214                paramMap.put( "CONTENT"    , content  );
215                paramMap.put( "TITLE"    , title  );
216                paramMap.put( "DATE", HybsSystem.getDate("yyyy/MM/dd") );
217                paramMap.put( "TIME", HybsSystem.getDate("HH:mm:ss") );
218                paramMap.put( "LOGIN_USERID", userid );
219                paramMap.put( "PGID", pgid );
220                paramMap.put( "FILES", StringUtil.array2csv( files ) ); // 5.9.3.0 (2015/12/04)
221                
222                getDstMap(tos, ccs, bccs  );
223                
224                initParamMap = paramMap;
225        }
226
227        /**
228         * 指定の長さ以内の文字列を返します。
229         *
230         * @param       src             オリジナルの文字列
231         * @param       maxLen  指定の長さ
232         *
233         * @return      指定の長さに短縮された文字列
234         */
235        private String trim( final String src, final int maxLen ) {
236                String rtn = src;
237                if( src != null && src.length() > maxLen ) {
238//                      rtn = src.substring( 0, maxLen );
239                        rtn = StringUtil.cut( src, maxLen );
240                }
241                return rtn;
242        }
243
244        /**
245         * 要求NOを採番します。
246         * この要求NOで履歴テーブル(GE32)と宛先テーブル(GE30)の関連付けを持たせます。
247         * 
248         * @og.rev 5.9.31.1 (2018/04/13) シーケンスの取り方変更
249         *
250         * @return      要求NO
251         */
252        private String getYkno() {
253//              String[][] tmp = DBUtil.dbExecute( selYkno, new String[0], appInfo, DBID );
254//              if( tmp == null || tmp.length == 0 ) {
255//                      String errMsg = "要求NO採番エラー"
256//                                              + " SQL=" + selYkno ;
257//                      throw new RuntimeException( errMsg );
258//              }
259                Transaction tran = new TransactionReal( appInfo );
260                int rtn_ykno = dbName.getSequence(selYkno,tran,DBID);
261                return Integer.toString( rtn_ykno );
262        }
263        
264        /**
265         * 送信先のアドレスをセットします。
266         *
267         * @param toId
268         * @param ccId
269         * @param bccId
270         */
271        private void getDstMap( final String[] toId, final String[] ccId, final String[] bccId  ){
272
273                // 送信先(TO、CC、BCC)のマップを作成します。
274                mailDstMap =  (TreeMap<String, String[]>)getDstAddrMap( bccId, KBN_BCC );
275                mailDstMap.putAll( (TreeMap<String, String[]>) getDstAddrMap( ccId, KBN_CC ) );
276                mailDstMap.putAll( (TreeMap<String, String[]>) getDstAddrMap( toId, KBN_TO ) );
277        }
278        
279        /**
280         * 送信先のアドレス・マップを作成します。
281         *
282         * @param       dstBuf  送信先配列
283         * @param       kbn             送信区分[0:TO/1:CC/2:BCC]
284         *
285         * @return      送信先のアドレス・マップ
286         */
287        private Map<String, String[]> getDstAddrMap( final String[] dstBuf, final int kbn ){
288                Map<String,String[]> dstMap= new TreeMap<String,String[]>();
289                 // IDX_DST_ADDR ,IDX_DST_KBN
290                 String[] dstInit = { "", Integer.toString( kbn ) };
291
292                 int len = dstBuf.length;
293                 for( int i=0; i < len; i++ ){
294                        String[] indMember = dstInit.clone();
295                        indMember[IDX_DST_ADDR] = dstBuf[i];                    //メールアドレス
296
297                        dstMap.put( dstBuf[i], indMember );
298                }
299                 return dstMap;
300        }
301
302}