Java tutorial
/** * service-eisstockitems, a webservice for Eisstock material components * * Copyright (C) 2016 Klaus Meisl <info@klaus-meisl.de> * * This file is part of service-eisstockitems. * * service-eisstockitems is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * service-eisstockitems is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with service-eisstockitems. If not, see <http://www.gnu.org/licenses/>. * * @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+> */ package de.meisl.eisstockitems; import java.io.File; import java.io.IOException; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Locale; import org.apache.commons.io.FileUtils; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; import org.springframework.test.context.transaction.TransactionalTestExecutionListener; import de.meisl.eisstockitems.domain.Category; import de.meisl.eisstockitems.domain.Item; import de.meisl.eisstockitems.domain.Producer; import de.meisl.eisstockitems.domain.YearCharacter; import de.meisl.eisstockitems.repository.CategoryRepository; import de.meisl.eisstockitems.repository.ItemRepository; import de.meisl.eisstockitems.repository.ProducerRepository; import de.meisl.eisstockitems.repository.YearCharacterRepository; @ActiveProfiles("setup") @RunWith(SpringRunner.class) @SpringBootTest @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class }) public class ImportTest { private static final Logger log = LoggerFactory.getLogger(ImportTest.class); @Autowired private ProducerRepository producerRepository; @Autowired private CategoryRepository categoryRepository; @Autowired private ItemRepository itemRepository; @Autowired private YearCharacterRepository yearCharRepository; @Test public void importAll() throws ParseException, IOException { importStoecke(); importSchuelerstoecke(); importStiele(); importWinterlaufsohlen(); importGrundplatten(); importSlsGlatt(); importSlsRotNegativ(); importSlsNegativ(); importZwischenplatten(); importRunddauben(); importJahreskennbuchstaben(); } @Test public void importJahreskennbuchstaben() throws IOException, ParseException { List<String> readLines = FileUtils.readLines(new File("data/jahreskennbuchstaben.csv"), "UTF-8"); for (String line : readLines) { DateFormat format = new SimpleDateFormat("dd.MM.yyyy", Locale.GERMAN); String[] split = line.split(",", -1); Date year = format.parse(split[0]); String character = split[1]; String remark = split[2]; List<YearCharacter> category = yearCharRepository.findByYear(year); if (category.isEmpty()) { log.info(String.format("Add new YearCharacter '%s' for year '%s'", character, year)); YearCharacter item = new YearCharacter(); item.setYearCharacter(character); item.setYear(year); item.setRemark(remark); item = yearCharRepository.save(item); log.info("Created YearCharacter for RegNr '" + character + "': " + item); } } } @Test public void importStoecke() throws ParseException, IOException { importFromFile("data/eisstockkoerper.csv", (long) 1); } @Test public void importSchuelerstoecke() throws ParseException, IOException { importFromFile("data/schuelerstoecke.csv", (long) 2); } @Test public void importStiele() throws ParseException, IOException { importFromFile("data/stiele.csv", (long) 3); } @Test public void importWinterlaufsohlen() throws ParseException, IOException { importFromFile("data/winterlaufsohlen.csv", (long) 4); } @Test public void importGrundplatten() throws ParseException, IOException { importFromFile("data/grundplatten.csv", (long) 5); } @Test public void importSlsGlatt() throws ParseException, IOException { importFromFile("data/slsGlatt.csv", (long) 6); } @Test public void importSlsRotNegativ() throws ParseException, IOException { importFromFile("data/slsRotNegativ.csv", (long) 7); } @Test public void importSlsNegativ() throws ParseException, IOException { importFromFile("data/slsNegativ.csv", (long) 8); } @Test public void importZwischenplatten() throws ParseException, IOException { importFromFile("data/zwischenplatten.csv", (long) 9); } @Test public void importRunddauben() throws ParseException, IOException { importFromFile("data/runddauben.csv", (long) 10); } public void importFromFile(String fileName, long categoryId) throws ParseException, IOException { List<String> readLines = FileUtils.readLines(new File(fileName), "UTF-8"); for (String line : readLines) { DateFormat format = new SimpleDateFormat("dd.MM.yyyy", Locale.GERMAN); String[] split = line.split(",", -1); String regNr = split[0]; String ifipartnerid = split[1]; String producerName = split[2]; Date registrationDate = format.parse(split[3]); Date prodCeasedDate = split[4].isEmpty() ? null : format.parse(split[4]); Date sellingProhibitedDate = split[5].isEmpty() ? null : format.parse(split[5]); Date expiredDate = split[6].isEmpty() ? null : format.parse(split[6]); Date sellingCeasedDate = split[7].isEmpty() ? null : format.parse(split[7]); String remark = split[8]; // 1 Eisstockkrper // 2 Eisstockkrper fr Schler // 3 Stiele // 4 Winterlaufsohlen // 5 Grundplatten // 6 Sommerlaufsohlen mit glatter Laufflche // 7 Sommerlaufsohle - rot - Negativprofil (sehr schnel... // 8 Sommerlaufsohlen mit Negativprofil // 9 Zwischenplatten // 10 Runddauben Category category = categoryRepository.findOne(categoryId); Producer producer; Item item; List<Producer> findByIfiPartnerId = producerRepository.findByIfiPartnerIdAndName(ifipartnerid, producerName); int partnerListSize = findByIfiPartnerId.size(); if (partnerListSize > 0) { if (partnerListSize > 1) { System.err.println("Many partners for IFI partner ID '" + ifipartnerid + "': " + findByIfiPartnerId.toString()); } producer = findByIfiPartnerId.get(0); log.info("Found Producer for ifiPartnerId '" + ifipartnerid + "': " + producer); } else { log.info("Did not find Producer for ifiPartnerId '" + ifipartnerid + "' and name '" + producerName + "'"); producer = new Producer(); producer.setIfiPartnerId(ifipartnerid); producer.setName(producerName); producer.setUpdated(new Date()); producer = producerRepository.save(producer); log.info("Created Producer for ifiPartnerId '" + ifipartnerid + "': " + producer); } List<Item> findByRegNumber = itemRepository.findByRegNumber(regNr); if (findByRegNumber.size() > 0) { if (findByRegNumber.size() > 1) { throw new RuntimeException("more than 1 item with regNr '" + regNr + "'"); } item = findByRegNumber.get(0); log.info("Found Item for RegNr '" + regNr + "': " + item); } else { item = new Item(); item.setCategory(category); item.setProducer(producer); item.setRegNumber(regNr); item.setRemark(remark); item.setRegistrationDate(registrationDate); item.setExpired(expiredDate); item.setProductionCeased(prodCeasedDate); item.setSellingCeased(sellingCeasedDate); item.setSellingProhibited(sellingProhibitedDate); item.setUpdated(new Date()); item = itemRepository.save(item); log.info("Created Item for RegNr '" + regNr + "': " + item); } } } }