diff --git a/insert_db_excel.iml b/insert_db_excel.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/insert_db_excel.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/lib/cubrid-jdbc-11.0.1.0286.jar b/lib/cubrid-jdbc-11.0.1.0286.jar new file mode 100644 index 0000000..ae43a61 Binary files /dev/null and b/lib/cubrid-jdbc-11.0.1.0286.jar differ diff --git a/lib/slf4j-api-2.0.7.jar b/lib/slf4j-api-2.0.7.jar new file mode 100644 index 0000000..be5447c Binary files /dev/null and b/lib/slf4j-api-2.0.7.jar differ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..d85cd4e --- /dev/null +++ b/pom.xml @@ -0,0 +1,176 @@ + + + 4.0.0 + + org.example + insertdb + 1.0-SNAPSHOT + + + + org.springframework.boot + spring-boot-starter-parent + 2.7.8 + + + + + + maven-public + http://nexus.iten.co.kr:9999/repository/maven-public/ + + true + + + false + + + + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.3.0 + + + org.slf4j + slf4j-api + + + + + + org.springframework.boot + spring-boot-devtools + true + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + cubrid + cubrid-jdbc + 11.0.1.0286 + system + ${basedir}/lib/cubrid-jdbc-11.0.1.0286.jar + + + org.apache.commons + commons-dbcp2 + 2.10.0 + + + + + org.apache.poi + poi + 5.2.3 + + + org.apache.poi + poi-ooxml + 5.2.3 + + + org.apache.httpcomponents + httpclient + 4.5.3 + + + + com.googlecode.json-simple + json-simple + 1.1.1 + + + org.slf4j + slf4j-api + + + org.junit.platform + junit-platform-commons + + + + + + + + UTF-8 + UTF-8 + ${project.basedir}/lib + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.3.0 + + + make-assembly + package + + single + + + + + InsertDb + + + + jar-with-dependencies + + UTF-8 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 11 + 11 + UTF-8 + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + + + + + + + diff --git a/src/main/java/InsertDb.java b/src/main/java/InsertDb.java new file mode 100644 index 0000000..bc8c8f7 --- /dev/null +++ b/src/main/java/InsertDb.java @@ -0,0 +1,304 @@ +import org.apache.commons.dbcp2.BasicDataSource; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; +import org.apache.poi.util.IOUtils; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.*; +import java.nio.charset.StandardCharsets; +import java.nio.file.*; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +public class InsertDb { + + final static private Logger logger = LoggerFactory.getLogger(InsertDb.class); + private static final long MIN_PROCESSING_INTERVAL = 1000; + private static final Map lastProcessedTimes = new HashMap<>(); + private static final Map isFileProcessing = new HashMap<>(); + + public static void main( String[] args ) { + if(args.length < 1) { + System.out.println("Usage: java InsertDb config.properties"); + return; + } + + System.out.println("start"); + + String configFilePath = args[0]; + Properties properties = loadProperties(configFilePath); + + if(properties.isEmpty()) { + System.out.println("Failed to load properties. Exiting..."); + return; + } + + String driverClassName = properties.getProperty("db.driverClassName"); + String dbUrl = properties.getProperty("db.url"); + String dbUser = properties.getProperty("db.username"); + String dbPassword = properties.getProperty("db.password"); + String folderPath = properties.getProperty("folderPath"); + String insertQuery = properties.getProperty("insertQuery"); + String ResposeUrl = properties.getProperty("destUrl"); + + try { + Connection connection = DatabaseConnectionPool.getConnection(driverClassName, dbUrl, dbUser, dbPassword); + connection.createStatement().execute("SET NAMES 'utf8'"); + + WatchService watchService = FileSystems.getDefault().newWatchService(); + Path path = new File(folderPath).toPath(); + path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY); + + System.out.println("Waiting for new files in the folder..."); + + while(true) { + WatchKey key; + try { + key = watchService.take(); + } catch(InterruptedException e) { + System.err.println("Interrupted while waiting for file events."); + return; + } + + for(WatchEvent event : key.pollEvents()) { + Path filePath = path.resolve((Path) event.context()); + if(event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) { + String fileName = filePath.toString().toLowerCase(); + if((fileName.endsWith(".json") || fileName.endsWith(".txt")) && + !isFileProcessing.getOrDefault(filePath.toString(), false) && + shouldProcessFile(filePath.toString())) { + System.out.println("File modification detected json or txt: " + filePath); + + isFileProcessing.put(filePath.toString(), true); + + waitForFileMove(filePath); + insertDataFromFileToDatabase(connection, filePath.toFile(), insertQuery); + + isFileProcessing.put(filePath.toString(), false); + lastProcessedTimes.put(filePath.toString(), System.currentTimeMillis()); + } else if(fileName.endsWith(".xlsx") && + !isFileProcessing.getOrDefault(filePath.toString(), false) && + shouldProcessFile(filePath.toString())) { + System.out.println("File modification detected xlsx: " + filePath); + waitForFileMove(filePath); + + requestDataFromFileToWeb(filePath.toFile(), ResposeUrl); + + isFileProcessing.put(filePath.toString(), false); + lastProcessedTimes.put(filePath.toString(), System.currentTimeMillis()); + } + } + } + + boolean valid = key.reset(); + if(!valid) { + System.err.println("WatchKey no longer valid, exiting."); + return; + } + } + } catch(Exception e) { + e.printStackTrace(); + } + } + + private static Properties loadProperties( String filePath ) { + Properties properties = new Properties(); + try(InputStream input = new FileInputStream(filePath)) { + if(input == null) { + System.out.println("Sorry, unable to find " + filePath); + return properties; + } + properties.load(input); + } catch(IOException e) { + e.printStackTrace(); + } + return properties; + } + + private static boolean shouldProcessFile( String filePath ) { + long currentTime = System.currentTimeMillis(); + long lastProcessedTime = lastProcessedTimes.getOrDefault(filePath, 0L); + + return (currentTime - lastProcessedTime) >= MIN_PROCESSING_INTERVAL; + } + + private static void waitForFileMove( Path filePath ) { + boolean isFileMoved = false; + while(!isFileMoved) { + try { + if(Files.exists(filePath)) { + isFileMoved = true; + System.out.println("File moved: " + filePath); + } else { + Thread.sleep(1000); + } + } catch(Exception e) { + e.printStackTrace(); + } + } + } + + private static void insertDataFromFileToDatabase( Connection connection, File file, String insertQuery ) { + try { + BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)); + + String line; + StringBuilder fileContent = new StringBuilder(); + + while((line = br.readLine()) != null) { + fileContent.append(line).append("\n"); + } + + String fileName = file.getName(); + String filePath = file.getAbsolutePath(); + + PreparedStatement preparedStatement = connection.prepareStatement(insertQuery); + preparedStatement.setString(1, fileName); + preparedStatement.setString(2, filePath); + preparedStatement.setString(3, fileContent.toString()); + + preparedStatement.executeUpdate(); + + br.close(); + preparedStatement.close(); + } catch(Exception e) { + System.err.println("Error inserting data to the database: " + e.getMessage()); + } + } + + private static void requestDataFromFileToWeb( File file , String ResposeUrl) { + try { + String filePath = file.getPath(); + + FileInputStream fis = new FileInputStream(filePath); + IOUtils.setByteArrayMaxOverride(Integer.MAX_VALUE); + XSSFWorkbook workbook = new XSSFWorkbook(fis); + + //로그 보기 + logging(workbook); + + fis.close(); + String fileName = file.getName(); + //데이터 셋 + JSONObject jsonObject = getJsonObject(workbook, fileName, filePath); + + String apiUrl = ResposeUrl; + + HttpClient httpClient = HttpClientBuilder.create().build(); + HttpPost httpPost = new HttpPost(apiUrl); + httpPost.setEntity(new StringEntity(jsonObject.toString(), "UTF-8")); + httpPost.addHeader("Content-type", "application/json"); + httpPost.addHeader("Accept", "application/json"); + + HttpResponse response = httpClient.execute(httpPost); + + String result = ""; + String statusCode = Integer.toString(response.getStatusLine().getStatusCode()); + + if(statusCode.equals("200")) { + result = EntityUtils.toString(response.getEntity()); + + result = new String(result.getBytes(StandardCharsets.UTF_8)); + JSONParser parser = new JSONParser(); + Object obj = parser.parse(result); + JSONObject object = (JSONObject) obj; + + System.out.println("result ::: " + result); + } else { + System.out.println("fail !!!!!!!!!!!!!!!!!!"); + } + + } catch(IOException e) { + e.getMessage(); + } catch(ParseException e) { + throw new RuntimeException(e); + } catch(NullPointerException e) { + e.getMessage(); + } + } + + private static void logging( XSSFWorkbook workbook ) { + //첫번째 시트 + System.out.println("================================="); + System.out.println("user_1: {}"+ isNullToString(workbook.getSheetAt(0).getRow(1).getCell(2))); + System.out.println("address_1: {}"+ isNullToString(workbook.getSheetAt(0).getRow(2).getCell(2))); + System.out.println("phone_1: {}"+ isNullToString(workbook.getSheetAt(0).getRow(3).getCell(2))); + System.out.println("mobile_1: {}"+ isNullToString(workbook.getSheetAt(0).getRow(3).getCell(4))); + System.out.println("mail_1: {}"+ isNullToString(workbook.getSheetAt(0).getRow(4).getCell(2))); + System.out.println("================================="); + System.out.println("user_2: {}"+ isNullToString(workbook.getSheetAt(0).getRow(5).getCell(2))); + System.out.println("address_2: {}"+ isNullToString(workbook.getSheetAt(0).getRow(6).getCell(2))); + System.out.println("phone_2: {}"+ isNullToString(workbook.getSheetAt(0).getRow(7).getCell(2))); + System.out.println("mobile_2: {}"+ isNullToString(workbook.getSheetAt(0).getRow(7).getCell(4))); + System.out.println("mail_2: {}"+ isNullToString(workbook.getSheetAt(0).getRow(8).getCell(2))); + System.out.println("================================="); + + //두번째 시트 + System.out.println("================================="); + System.out.println("purpose: {}"+ isNullToString(workbook.getSheetAt(1).getRow(1).getCell(0))); + System.out.println("reason: {}"+ isNullToString(workbook.getSheetAt(1).getRow(3).getCell(0))); + System.out.println("attachment: {}"+ isNullToString(workbook.getSheetAt(1).getRow(5).getCell(0))); + System.out.println("================================="); + } + + private static JSONObject getJsonObject( XSSFWorkbook workbook, String fileName , String filePath ) { + JSONObject jsonObject = new JSONObject(); + + jsonObject.put("fileName", fileName); + jsonObject.put("filePath", filePath); + + jsonObject.put("user_1", isNullToString(workbook.getSheetAt(0).getRow(1).getCell(2).toString())); + jsonObject.put("address_1", isNullToString(workbook.getSheetAt(0).getRow(2).getCell(2).toString())); + jsonObject.put("phone_1", isNullToString(workbook.getSheetAt(0).getRow(3).getCell(2).toString())); + jsonObject.put("mobile_1", isNullToString(workbook.getSheetAt(0).getRow(3).getCell(4).toString())); + jsonObject.put("mail_1", isNullToString(workbook.getSheetAt(0).getRow(4).getCell(2).toString())); + jsonObject.put("user_2", isNullToString(workbook.getSheetAt(0).getRow(5).getCell(2).toString())); + jsonObject.put("address_2", isNullToString(workbook.getSheetAt(0).getRow(6).getCell(2).toString())); + jsonObject.put("phone_2", isNullToString(workbook.getSheetAt(0).getRow(7).getCell(2).toString())); + jsonObject.put("mobile_2", isNullToString(workbook.getSheetAt(0).getRow(7).getCell(4).toString())); + jsonObject.put("mail_2", isNullToString(workbook.getSheetAt(0).getRow(8).getCell(2).toString())); + jsonObject.put("purpose", isNullToString(workbook.getSheetAt(1).getRow(1).getCell(0).toString())); + jsonObject.put("reason", isNullToString(workbook.getSheetAt(1).getRow(3).getCell(0).toString())); + jsonObject.put("attachment", isNullToString(workbook.getSheetAt(1).getRow(5).getCell(0).toString())); + return jsonObject; + } + + private static String isNullToString( Object object ) { + String string = ""; + + if(object != null) { + string = object.toString().trim(); + } + + return string; + } +} + +class DatabaseConnectionPool { + private static BasicDataSource dataSource; + + public static Connection getConnection( String driverClassName, String dbUrl, String dbUser, String dbPassword ) throws SQLException { + if(dataSource == null) { + dataSource = new BasicDataSource(); + dataSource.setDriverClassName(driverClassName); + dataSource.setUrl(dbUrl); + dataSource.setUsername(dbUser); + dataSource.setPassword(dbPassword); + + } + return dataSource.getConnection(); + } +} \ No newline at end of file diff --git a/src/main/resources/META-INF/MANIFEST.MF b/src/main/resources/META-INF/MANIFEST.MF new file mode 100644 index 0000000..b9ef791 --- /dev/null +++ b/src/main/resources/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: InsertDb + diff --git a/src/main/resources/config.properties b/src/main/resources/config.properties new file mode 100644 index 0000000..57c181a --- /dev/null +++ b/src/main/resources/config.properties @@ -0,0 +1,12 @@ +#folderPath=C:/ePapyrus/insertdb +folderPath=E:/properties/testDir +#db.driverClassName=com.mysql.cj.jdbc.Driver +db.driverClassName=cubrid.jdbc.driver.CUBRIDDriver +#db.url=jdbc:mysql://localhost:3306/insertdb?useUnicode=true&characterEncoding=UTF-8 +db.url=jdbc:cubrid:119.193.218.98:8097:kcc_adr_advc:::?althosts=119.193.215.98:8097&ictime=300&charset=utf-8 +#db.username=root +db.username=kccadradvcUr +#db.password=epapyrus12#$ +db.password=kccadradvc!@#$ +insertQuery=INSERT INTO textdata (filename, filepath, filecontents) VALUES (?, ?, ?) +destUrl=http://119.193.215.98:8081/kccadr/textsence/textSenseResponse.do \ No newline at end of file diff --git a/target/classes/DatabaseConnectionPool.class b/target/classes/DatabaseConnectionPool.class new file mode 100644 index 0000000..fc354ee Binary files /dev/null and b/target/classes/DatabaseConnectionPool.class differ diff --git a/target/classes/InsertDb.class b/target/classes/InsertDb.class new file mode 100644 index 0000000..73899a1 Binary files /dev/null and b/target/classes/InsertDb.class differ diff --git a/target/classes/META-INF/MANIFEST.MF b/target/classes/META-INF/MANIFEST.MF new file mode 100644 index 0000000..b9ef791 --- /dev/null +++ b/target/classes/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: InsertDb + diff --git a/target/classes/config.properties b/target/classes/config.properties new file mode 100644 index 0000000..57c181a --- /dev/null +++ b/target/classes/config.properties @@ -0,0 +1,12 @@ +#folderPath=C:/ePapyrus/insertdb +folderPath=E:/properties/testDir +#db.driverClassName=com.mysql.cj.jdbc.Driver +db.driverClassName=cubrid.jdbc.driver.CUBRIDDriver +#db.url=jdbc:mysql://localhost:3306/insertdb?useUnicode=true&characterEncoding=UTF-8 +db.url=jdbc:cubrid:119.193.218.98:8097:kcc_adr_advc:::?althosts=119.193.215.98:8097&ictime=300&charset=utf-8 +#db.username=root +db.username=kccadradvcUr +#db.password=epapyrus12#$ +db.password=kccadradvc!@#$ +insertQuery=INSERT INTO textdata (filename, filepath, filecontents) VALUES (?, ?, ?) +destUrl=http://119.193.215.98:8081/kccadr/textsence/textSenseResponse.do \ No newline at end of file