All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.unlaxer.jaddress.dao.mysql.Load Maven / Gradle / Ivy

package org.unlaxer.jaddress.dao.mysql;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.unlaxer.jaddress.GradleStructure;
import org.unlaxer.jaddress.ProjectContext;
import org.unlaxer.jaddress.SubProjects;
import org.unlaxer.jaddress.entity.standard.郵便番号;
import org.unlaxer.jaddress.parser.AddressContext;
import org.unlaxer.jaddress.parser.AddressParser;
import org.unlaxer.jaddress.parser.AddressParserImpl;
import org.unlaxer.jaddress.parser.DataAccessContext;
import org.unlaxer.jaddress.parser.DataAccessContextFactory;
import org.unlaxer.jaddress.parser.DataAccessContextImpl;
import org.unlaxer.jaddress.parser.ParsingContext;
import org.unlaxer.util.collection.ID;

public class Load {
	static Logger logger = LoggerFactory.getLogger(Load.class);

	public static void main(String[] args) {
		DataAccessContext dataAccessContext = DataAccessContextFactory.get();
		AddressParser addressParser = new AddressParserImpl();
		MySqlUtil mySqlUtil = new MySqlUtil();
		
		Path path = ProjectContext.getPath(SubProjects.main, GradleStructure.testResources, "testAddress.tsv");
		try (BufferedReader br = Files.newBufferedReader(path)) {
			String text;
			int i = 0;
			while ((text = br.readLine()) != null) {
				i++;

				List list = Arrays.asList(text.split("\t"));
				if (list.size() < 2) {
					logger.warn(String.format("bad data:[%d]:%s", i, text));
					continue;
				}

				List list2 = list.subList(1, list.size());
				AddressContext addressContext = new AddressContext(ID.generate(), new 郵便番号(list.get(0)), list2);
				ParsingContext parsingContext = new ParsingContext(dataAccessContext, addressContext);
				try {
					AddressContext parse = addressParser.parse(parsingContext);
					mySqlUtil.insertOrUpdate(parse);

				} catch (Exception e) {
					logger.error("load:" + i, e);
					throw e;
				}

			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}finally {
//			LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
//			loggerContext.stop();

			System.exit(0);
		}
//		logger.error("load:");
		
	}


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy