package com.pgf.mqspring; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.amazonaws.ClientConfiguration; import com.amazonaws.Protocol; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.S3Object; import com.pgf.mqspring.model.TelegramInfoModel; public class TelegramInfo { private static HashMap> TelegramInfoList = new HashMap>(); public static void initTelegramInfo(String s3BucketName) { Logger logger = LogManager.getLogger(); // クライアント設定 ClientConfiguration clientConfig = new ClientConfiguration(); clientConfig.setProtocol(Protocol.HTTPS); // プロトコル clientConfig.setConnectionTimeout(30000); // 接続タイムアウト(ms) //S3クライアントの生成 // AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient(); //S3クライアントの生成 AWSCredentials credentials = new BasicAWSCredentials("AKIAQS4UNQ7ILVHOOEPL", "PJjpDPIIkrzeI5cEcD3qH/uAXSA3dDNnJ8+UjzuP"); AmazonS3 s3Client = AmazonS3ClientBuilder .standard() .withCredentials(new AWSStaticCredentialsProvider(credentials)) .withRegion(Regions.AP_NORTHEAST_1) .build(); S3Object s3Object = s3Client.getObject(s3BucketName, "document/ibm_telegram_info.csv"); InputStream inputStream = null; BufferedReader buf = null; try { inputStream = s3Object.getObjectContent(); buf = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); String line; String preDispCode = ""; int preRepeatNum = 0; int groupNum = 0; List telInfoByDispCodeList = new ArrayList(); List repeatGroupList = new ArrayList(); // CSVを1行ずつ読込 while ((line = buf.readLine()) != null) { String[] data = line.split(",", 0); //ヘッダ抜き if ("DISP_CODE".equals(data[0])) { continue; } if (!"".equals(preDispCode) && !preDispCode.equals(data[0])) { TelegramInfoList.put(preDispCode, telInfoByDispCodeList); telInfoByDispCodeList = new ArrayList(); } TelegramInfoModel model = new TelegramInfoModel(); model.setDispCode(data[0]); model.setFieldNo(data[1]); model.setItemName(data[2]); model.setItemLength(data[3]); model.setCharDiv(data[4]); model.setRepeatNum(data[5]); model.setGroupNum(data[6]); model.setInsDate(data[7]); //繰り返す項目対応(ループ回数分) groupNum = Integer.parseInt(data[6]); if (groupNum != 0) { repeatGroupList.add(model); } if (groupNum == 0 && repeatGroupList.size() != 0) { for (int m = 0; m < preRepeatNum - 1; m++) { telInfoByDispCodeList.addAll(repeatGroupList); } //初期化 repeatGroupList = new ArrayList(); } telInfoByDispCodeList.add(model); preDispCode = data[0]; preRepeatNum = Integer.parseInt(data[5]); } //最後 TelegramInfoList.put(preDispCode, telInfoByDispCodeList); } catch (Exception e) { e.printStackTrace(); logger.error("[CDA] [SYS7065E]:" + MqSpringMessageConfig.SYS7065E); } } public static void initTelegramInfo() { getTelInfo(); } static private void getTelInfo() { Logger logger = LogManager.getLogger(); // クライアント設定 ClientConfiguration clientConfig = new ClientConfiguration(); clientConfig.setProtocol(Protocol.HTTPS); // プロトコル clientConfig.setConnectionTimeout(30000); // 接続タイムアウト(ms) //S3クライアントの生成 // AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient(); //S3クライアントの生成 AWSCredentials credentials = new BasicAWSCredentials("AKIAQS4UNQ7ILVHOOEPL", "PJjpDPIIkrzeI5cEcD3qH/uAXSA3dDNnJ8+UjzuP"); AmazonS3 s3Client = AmazonS3ClientBuilder .standard() .withCredentials(new AWSStaticCredentialsProvider(credentials)) .withRegion(Regions.AP_NORTHEAST_1) .build(); S3Object s3Object = s3Client.getObject("mqrest-dev", "document/ibm_telegram_info.csv"); InputStream inputStream = null; BufferedReader buf = null; try { inputStream = s3Object.getObjectContent(); buf = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); String line; String preDispCode = ""; int preRepeatNum = 0; int groupNum = 0; List telInfoByDispCodeList = new ArrayList(); List repeatGroupList = new ArrayList(); // CSVを1行ずつ読込 while ((line = buf.readLine()) != null) { String[] data = line.split(",", 0); //ヘッダ抜き if ("DISP_CODE".equals(data[0])) { continue; } if (!"".equals(preDispCode) && !preDispCode.equals(data[0])) { TelegramInfoList.put(preDispCode, telInfoByDispCodeList); telInfoByDispCodeList = new ArrayList(); } TelegramInfoModel model = new TelegramInfoModel(); model.setDispCode(data[0]); model.setFieldNo(data[1]); model.setItemName(data[2]); model.setItemLength(data[3]); model.setCharDiv(data[4]); model.setRepeatNum(data[5]); model.setGroupNum(data[6]); model.setInsDate(data[7]); //繰り返す項目対応(ループ回数分) groupNum = Integer.parseInt(data[6]); if (groupNum != 0) { repeatGroupList.add(model); } if (groupNum == 0 && repeatGroupList.size() != 0) { for (int m = 0; m < preRepeatNum - 1; m++) { telInfoByDispCodeList.addAll(repeatGroupList); } //初期化 repeatGroupList = new ArrayList(); } telInfoByDispCodeList.add(model); preDispCode = data[0]; preRepeatNum = Integer.parseInt(data[5]); } //最後 TelegramInfoList.put(preDispCode, telInfoByDispCodeList); } catch (Exception e) { e.printStackTrace(); logger.error("[CDA] [SYS7065E]:" + MqSpringMessageConfig.SYS7065E); } } public static HashMap> getTelegramInfoList() { return TelegramInfoList; } }