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 */
016 package org.opengion.hayabusa.report;
017
018 import org.opengion.hayabusa.common.HybsSystem;
019 import org.opengion.fukurou.util.StringUtil;
020
021 import org.opengion.fukurou.mail.MailReceiveListener ;
022 import org.opengion.fukurou.mail.MailTX ;
023 import org.opengion.fukurou.mail.MailMessage ;
024 import org.opengion.fukurou.mail.MailAttachFiles ;
025
026 import java.util.Date;
027
028 /**
029 * MailReceiveListener の実è£?‚¯ラスですã?
030 * MailRX クラスにセãƒ?ƒˆすることで、メール?‘件ごとに receive( MailMessage ) メソãƒ?ƒ‰ã�?
031 * 呼び出されますã?
032 * メール?‘件に、添付ファイルがè¤?•°登録されてã�?‚‹場合ã?処ç�?‚’行ってã�?�¾すã?
033 * 添付ファイルごとに要求番号を採番して、要求番号.xls としてセーブし、帳票要求テーブルに
034 * 書き込みますã?
035 *
036 * @og.rev 3.8.0.0 (2005/06/07) 新規追åŠ?
037 * @og.group 帳票シスãƒ?ƒ
038 *
039 * @version 4.0
040 * @author Kazuhiko Hasegawa
041 * @since JDK5.0,
042 */
043 public class ExcelInsertReceiveListener implements MailReceiveListener {
044
045 // EXCEL取込時に使用するãƒ?ƒ³ポラリフォルãƒ?��。ファイル名ã?、要求番号.xls
046 private final String EXCELIN_URL =
047 HybsSystem.url2dir( StringUtil.nval(
048 HybsSystem.sys( "EXCEL_IN_FILE_URL" ) ,
049 HybsSystem.sys( "FILE_URL" ) + "EXCELIN/" ) ) ;
050
051 private GE50Access ge50 = null;
052
053 /**
054 * メール受信処ç�?�§ã€?¼‘メール受信ごとに呼び出されますã?
055 * 処ç�?µ�果をã?boolean で返しますã?
056 *
057 * @param message MailMessageオブジェクãƒ?
058 *
059 * @return 処ç�?µ�果(正常:true / 異常:false)
060 */
061 public boolean receive( final MailMessage message ) {
062 System.out.println();
063 System.out.println( "Receive " + new Date() );
064
065 String errMsg ;
066 boolean okFlag = false;
067
068 // 毎回 オブジェクトを構築しますã?登録日付が初期化されますã?
069 ge50 = new GE50Access( "CXXXXX","M_RECEIVE","ExcelInsert" );
070
071 try {
072 String from = message.getHeader( "From" );
073 String subject = message.getSubject();
074 String content = message.getContent();
075 String msgid = message.getMessageID();
076 String comments = from + " " + subject + " " + msgid ;
077
078 String systemId = getContentParam( content,"SYSTEM_ID" );
079 if( systemId == null ) {
080 errMsg = "メール本æ–?�« SYSTEM_ID=[xx] のキーワードが存在しませんã€? ;
081 errorReport( message,errMsg ) ;
082 return okFlag;
083 }
084
085 System.out.println( " From:" + from );
086 String joken = getContentParam( content,"JOKEN" );
087 if( joken == null ) { joken = "EXCELIN"; }
088
089 ge50.setSystemId( systemId );
090 ge50.setJoken ( joken );
091 ge50.setComments( comments );
092
093 MailAttachFiles attFiles = new MailAttachFiles( message.getMessage() );
094 String[] files = attFiles.getNames();
095 if( files == null || files.length == 0 ) {
096 errMsg = "メールに EXCEL 添付ファイルが存在しませんã€? ;
097 errorReport( message,errMsg ) ;
098 return okFlag;
099 }
100
101 // 添付ファイルの数ã�?�‘処ç�?�—ますã?
102 for( int i=0; i<files.length; i++ ) {
103 String ykno = ge50.makeYkno(); // 新たな要求番号がå?部にもセãƒ?ƒˆされるã?
104 String file = ykno + ".xls" ;
105 String attFile = files[i];
106
107 // LISTID を取得しますã? EXCELファイル名に(帳票ID)をå?れますã?
108 int st = attFile.indexOf( '(' );
109 int ed = attFile.indexOf( ')',st );
110 if( st < 0 || ed < 0 ) {
111 errMsg = "EXCEL 添付ファイルに(帳票ID)が存在しませんã€?" + attFile + "]" ;
112 errorReport( message,errMsg ) ;
113 return okFlag;
114 }
115
116 String listId = attFile.substring( st+1,ed );
117 if( listId.length() == 0 ) {
118 errMsg = "EXCEL 添付ファイルの(帳票ID)をå?りå?せませんã€?" + attFile + "]" ;
119 errorReport( message,errMsg ) ;
120 return okFlag;
121 }
122
123 String fileDir = EXCELIN_URL + systemId + "/" + listId + "/" ;
124
125 attFiles.saveFileName( fileDir,file,i );
126
127 ge50.setListId( listId );
128 ge50.setOutDir( fileDir );
129 ge50.setOutFile( files[i] );
130
131 ge50.insertGE50( GE50Access.FG_SET );
132
133 System.out.println( attFile + " -> " + file );
134 }
135 System.out.println( " End." );
136 okFlag = true;
137 }
138 catch( Throwable ex ) {
139 errMsg = StringUtil.stringStackTrace( ex );
140 errorReport( message,errMsg ) ;
141 okFlag = false;
142 }
143 return okFlag;
144 }
145
146 /**
147 * メール処ç�?¸にエラーが発生したå?合ã?処ç�?‚’行いますã?
148 * 処ç�??ã€?¼“種類ありますã?
149 * ?‘.エラーメールをã?EXCELIN_URL/ERROR_MAIL フォルãƒ?�«、メãƒ?‚»ージID.txt で保存しますã?
150 * ?’.COMMON_MAIL_SERVER と ERROR_MAIL_TO_USERS が設定されてã�?‚‹場合にã€?
151 * そã?あて先に、返信メールをé?信しますã?
152 * ?“.GE50(帳票要求テーブル)と、GE51(帳票エラーãƒ??ブル)にエラー状況を書き込みますã?
153 *
154 * @param message MailMessageオブジェクãƒ?
155 * @param errorMessage エラーメãƒ?‚»ージ
156 */
157 private void errorReport( final MailMessage message,final String errorMessage ) {
158 // エラーメールã‚?æ‰?®šã?ãƒ?‚£レクトリに出力しますã?
159 message.saveSimpleMessage( EXCELIN_URL + "/ERROR_MAIL/" );
160
161 String subject = "EXCEL取込メール受信中にエラーが発生しましたã€?;
162 String org_from = message.getHeader( "From" ) ;
163 String ord_subject = message.getSubject();
164 String content = message.getContent();
165 String errMsg = subject + HybsSystem.CR
166 + " Subject=" + ord_subject + " From=" + org_from + HybsSystem.CR
167 + " MessageID=" + message.getMessageID() + HybsSystem.CR
168 + " Save ErrorMail=" + EXCELIN_URL + "/ERROR_MAIL/" + HybsSystem.CR
169 + errorMessage + HybsSystem.CR
170 + "==========================================================="
171 + HybsSystem.CR
172 + content ;
173
174 if( ge50 != null ) {
175 String systemId = ge50.getSystemId() ;
176 if( systemId == null ) { ge50.setSystemId( "ERR" ); }
177
178 // エラー時ã?要求番号は、新たに採番しなおしますã?
179 ge50.makeYkno();
180
181 ge50.insertGE50( GE50Access.FG_ERR2 );
182 ge50.insertErrorGE56( errMsg );
183 }
184
185 // host と user のメール送信先が記述されてã�?‚‹場合ã?、メールでも転送するã?
186 String host = message.getHost();
187 String from = message.getUser();
188
189 if( host != null && from != null ) {
190 String[] to = new String[] { org_from };
191 String[] cc = StringUtil.csv2Array( HybsSystem.sys( "ERROR_MAIL_TO_USERS" ) );
192
193 MailTX tx = new MailTX( host );
194 // tx.setHost( host );
195 tx.setFrom( from );
196 tx.setTo( to );
197 if( cc.length > 0 ) { tx.setCc( cc ); }
198 tx.setSubject( subject );
199 tx.setMessage( errMsg );
200 tx.sendmail();
201 }
202 }
203
204 /**
205 * メãƒ?‚»ージ本æ–?‚ˆりã?æŒ?®šã?キーに関連付けられてã�?‚‹æƒ??をå?りå?しますã?
206 * これは、指定ã?æ–?—å?=[設定å?] とã�?�†キーワードからã?設定å?を取りå?しますã?
207 * æŒ?®šã?æ–?—å?と=のと間に、スペã?スをå?れなã�?�§くださいã€?
208 * 設定å?のåˆ?‚Š出しã?ã€?æŒ?®šã?æ–?—å?=[" と "]" の間ã?æ–?—å?をå?りå?しますã?
209 * å†?ƒ¨にスペã?スがå?ってã�?�¦もã?問題ありません。たã�?�—、] がå?ってã�?‚‹場合ã?ã€?
210 * 正常にåˆ?‚Š出すことは出来ませんしã?エスケープ文字も用意してã�?�¾せんã€?
211 *
212 * @param content コンãƒ?ƒ³ãƒ??æ–?—å?
213 * @param key æƒ??をå?りå?す時のキー
214 *
215 * @return 設定å? (設定å?が見つからなã�??å�?null
216 */
217 private String getContentParam( final String content,final String key ) {
218 if( content == null || key == null ) { return null; }
219
220 String newKey = key + "=[" ;
221
222 // キーの存在チェãƒ?‚¯と場æ‰?‚’求めるã?
223 int keyAd = content.indexOf( newKey );
224 if( keyAd >= 0 ) {
225 // [設定å?] の終äº??æ‰?‚’求めるã?(見つけた位置?‹文字å?数)
226 int st = keyAd + newKey.length() ;
227 int ed = content.indexOf( ']' , st );
228 if( ed >= 0 ) {
229 return content.substring( st,ed );
230 }
231 }
232 return null;
233 }
234 }