DEMO Springboot

## 1. `com.example.demo`
```ruby:DemoApplication.java
package com.example.demo;

import com.example.demo.service.DataImportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class DemoApplication {

    private static final Logger logger = LoggerFactory.getLogger(DemoApplication.class);

    public static void main(String[] args) {

    	logger.info("DemoApplication開始");

    	try (ConfigurableApplicationContext context =
        	SpringApplication.run(DemoApplication.class, args)) {
    		DataImportService m = context.getBean(DataImportService.class);
    		m.importCollectingData();
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	logger.info("DemoApplication終了");
    }
}
```
## 2. `com.example.demo.service`
```ruby:DataImportService.java
package com.example.demo.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DataImportService {

    private static final Logger logger = LoggerFactory.getLogger(DataImportService.class);

    private final CaSanpoSfaService caSanpoSfaService;
    private final BatchCompletionService batchCompletionService;

    @Autowired
    public DataImportService(
                    CaSanpoSfaService caSanpoSfaService,
                    BatchCompletionService batchCompletionService
    ) {
        logger.info("DataImportServiceスタート");
            this.caSanpoSfaService = caSanpoSfaService;
            this.batchCompletionService = batchCompletionService;
        logger.info("DataImportServiceエンド");
    }

    /**
     * 各種データインポートサービスを実行する。
     */
    public void importCollectingData() {
        logger.info("importCollectingDataスタート");
        if (
                caSanpoSfaService.importCaData()
        ) {
            batchCompletionService.completeSaaskeImport();
        }

        logger.info("importCollectingDataエンド");
    }

}
```
```ruby:CaSanpoSfaService.java
package com.example.demo.service;

import com.example.demo.domain.repository.ca.CaSanpoSfaCsvRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.file.Path;
import java.nio.file.Paths;

@Service
public class CaSanpoSfaService {
    private static final Logger logger = LoggerFactory.getLogger(CaSanpoSfaService.class);
    private final CaSanpoSfaCsvRepository caSanpoSfaCsvRepository;
    @Autowired
    public CaSanpoSfaService(
            CaSanpoSfaCsvRepository caSanpoSfaCsvRepository
    ) {
        logger.info("CaSanpoSfaService START");
        this.caSanpoSfaCsvRepository = caSanpoSfaCsvRepository;
        logger.info("CaSanpoSfaService END");
    }

    /**
     * 産保保険基盤DBにあるCA一時テーブルのデータを消してデータを読み込む
     */
    public boolean importCaData() {
        logger.info("SAASKE カスタムアプリ 産保SFA　のデータ取り込み処理 START");
        boolean complete = false;
        for (int count = 0; count < 3; count++) {
            try {
                Path path = Paths.get("C:\\temp\\CaSanpoSfa.csv");
                caSanpoSfaCsvRepository.importCsv(path);
                complete = true;
                logger.info("SAASKE カスタムアプリ 産保SFA　のデータ取り込み処理 END");
                break;
            } catch(Exception e) {
                if (count < 2) {
                    logger.warn("SAASKE カスタムアプリ 産保SFA　のデータ取り込み処理に失敗しました。リトライします。", e);
                } else {
                    logger.warn("SAASKE カスタムアプリ 産保SFA　のデータ取り込み処理に失敗しました。", e);
                }
            }
        }
        return complete;
    }
}
```
```ruby:BatchCompletionService.java
package com.example.demo.service;

import java.sql.Timestamp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//import com.example.demo.repository.batch.BatchCompletionRepository;

@Service
public class BatchCompletionService {
    private static final Logger logger = LoggerFactory.getLogger(BatchCompletionService.class);
//    private final BatchCompletionRepository saaskeBatchCompletionRepository;

    @Autowired
    public BatchCompletionService(
//                       BatchCompletionRepository saaskeBatchCompletionRepository
    ) {
        logger.info("BatchCompletionServiceスタート");
//               this.saaskeBatchCompletionRepository = saaskeBatchCompletionRepository;
        logger.info("BatchCompletionServiceエンド");
    }

    /**
     * サスケバッチ完了テーブルにレコードを登録
     * 既に存在する場合は既存レコードを更新
     */
    public void completeSaaskeImport() {
        logger.info("completeSaaskeImportスタート");
        Timestamp now = new Timestamp(System.currentTimeMillis());
//        List<BatchCompleteionModel> model = saaskeBatchCompletionRepository.select();
        logger.info("completeSaaskeImportエンド");
    }
}
```
## 3. com.example.demo.infrastructure.repository.ca
```ruby:CaSanpoSfaCsvRepositoryImpl.java
package com.example.demo.infrastructure.repository.ca;

import com.example.demo.domain.exception.CsvException;
import com.example.demo.domain.model.ca.CaSanpoSfaModel;
import com.example.demo.domain.repository.ca.CaSanpoSfaCsvRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Objects;

@Repository
public class CaSanpoSfaCsvRepositoryImpl implements CaSanpoSfaCsvRepository {
    //private final CaSanpoSfaRepository caSanpoSfaRepository;

    @Autowired
    //public CaSanpoSfaCsvRepositoryImpl(CaSanpoSfaCsvRepository caSanpoSfaCsvRepository) {
    public CaSanpoSfaCsvRepositoryImpl() {
    }

    @Override
    public void importCsv(Path fileName) throws CsvException {
        Logger logger = LoggerFactory.getLogger(CaSanpoSfaCsvRepositoryImpl.class);
        try (CSVParser parser = CSVFormat.DEFAULT.parse(
                new BufferedReader(new InputStreamReader(new FileInputStream(fileName.toString()), StandardCharsets.UTF_8)))) {
            for (CSVRecord record : parser) {
                if (record.getRecordNumber() == 1) {
                    continue;
                }
                int index = 0;
/*
                CaSanpoSfaModel model = CaSanpoSfaModel.builder()
                        .recordNo(chkIntNullValue(record.get(index++)))
                        .createdAt(chkTimestampNullValue(record.get(index++)))
                        .createdBy(chkStrNullValue(record.get(index++)))
                        .build();
*/
            logger.info("recordNo=" + record.get(index++));
            logger.info("createdAt" + record.get(index++));
            logger.info("createdBy" + record.get(index++));
            }
        } catch(Exception e) {
            String errMsg = "CSVファイルの読み込みに失敗しました。(カスタムアプリ_産保SFA)";
            logger.error(errMsg);
            throw new CsvException(errMsg, e);
        }
    }
}
```
## 4. com.example.demo.domain.repository.ca
```ruby:CaSanpoSfaCsvRepository.java
package com.example.demo.domain.repository.ca;

import com.example.demo.domain.exception.CsvException;

import java.nio.file.Path;

public interface CaSanpoSfaCsvRepository {
    void importCsv(Path fileName) throws CsvException;

}
```
## 5. com.example.demo.domain.model.ca
```ruby:CaSanpoSfaModel.java
package com.example.demo.domain.model.ca;


import java.sql.Timestamp;
import java.util.Optional;

@lombok.Value
@lombok.Builder
public class CaSanpoSfaModel {
    private final Integer recordNo;
    private final Timestamp createdAt;
    private final String createdBy;

    public Optional<Integer> getRecordNo() {
        return Optional.ofNullable(recordNo);
    }
    public Optional<Timestamp> getCreatedAt() {
        return Optional.ofNullable(createdAt);
    }
    public Optional<String> getCreatedBy() {
        return Optional.ofNullable(createdBy);
    }
}
```
## 5. com.example.demo.domain.exception
```ruby:CsvException.java
package com.example.demo.domain.exception;

public class CsvException extends Exception {
    public CsvException(String message) {
        super(message);
    }

    public CsvException(String message, Exception e) {
        super(message, e);
    }
}
```
## 6. com.example.demo.common
```ruby:CreateTableModel.java
package com.example.demo.common;

public class CreateTableModel {
}
```
```ruby:CodeGenerator.java
package com.example.demo.common;

import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Objects;

public class CodeGenerator {
/*
    public statis String generatorCsvModel(String prefix, List<CreateTableModel> list) {
        StringBuffer code = new StringBuffer();
        if (list == null) {
            return code.toString();
        }
        String javaClassName = "";
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                javaClassName = list.get(i).getJavaClassName();
                code.append(list.get(i).getJavaClassName());
                code.append("Model.builder()\n");
            }
            code.append("            .");
            code.append(list.get(i).getJavaMemberName());
            code.append("(");
            switch (list.get(i).getJavaType()) {
                case "String":
                    code.append("chkStrNullValue(");
                    break;
                case "Integer":
                    code.append("chkIntNullValue(");
                    break;
                case "Date":
                    code.append("chkDateNullValue(");
                    break;
                case "Timestamp":
                    code.append("chkTimestampNullValue(");
                    break;
            }
            code.append("record.get");
            code.append("(");
            code.append(i - 1);
            code.append(")))\n");
       }
        code.append("            .build();\n");
        return code.toString();
    }
*/
}