Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.acapulcoapp.alloggiatiweb; import com.acapulcoapp.AcapulcoappApp; import static com.acapulcoapp.alloggiatiweb.LayoutDescriptor.*; import com.acapulcoapp.config.Constants; import com.acapulcoapp.domain.CheckinRecord; import com.acapulcoapp.domain.CheckinType; import com.acapulcoapp.domain.Country; import com.acapulcoapp.domain.District; import com.acapulcoapp.domain.IdentityDocument; import com.acapulcoapp.domain.Person; import com.acapulcoapp.domain.enumeration.Gender; import com.acapulcoapp.repository.CheckinRecordRepository; import com.acapulcoapp.repository.CheckinTypeRepository; import com.acapulcoapp.repository.CountryRepository; import com.acapulcoapp.repository.DistrictRepository; import com.acapulcoapp.repository.IdentityDocumentRepository; import com.acapulcoapp.repository.PersonRepository; import com.acapulcoapp.repository.UserRepository; import java.io.File; import java.io.IOException; import java.net.UnknownHostException; import java.nio.charset.Charset; import java.time.LocalDate; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.InputMismatchException; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; import org.apache.commons.io.Charsets; import org.apache.commons.io.FileUtils; import org.springframework.boot.SpringApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.SimpleCommandLinePropertySource; import java.time.ZonedDateTime; import java.util.HashSet; import java.util.Set; import java.util.TreeSet; /** * * @author chiccomask */ public class FileReader { private static CountryRepository countryRepository; private static DistrictRepository districtRepository; private static IdentityDocumentRepository identityDocumentRepository; private static PersonRepository personRepository; private static CheckinTypeRepository checkinTypeRepository; private static CheckinRecordRepository checkinRecordRepository; public static void main(String[] args) throws UnknownHostException, IOException { // TODO code application logic here SpringApplication app = new SpringApplication(AcapulcoappApp.class); SimpleCommandLinePropertySource source = new SimpleCommandLinePropertySource(args); addDefaultProfile(app, source); ConfigurableApplicationContext context = app.run(args); initBeans(context); Map<LocalDate, List<List<String>>> map = new TreeMap<>(); List<File> files = new ArrayList<>(FileUtils.listFiles(new File("/Users/chiccomask/Downloads/ALLOGGIATI"), new String[] { "txt" }, true)); Collections.reverse(files); int count = 0; for (File file : files) { // List<String> allLines = FileUtils.readLines(file, "windows-1252"); List<String> allLines = FileUtils.readLines(file, "UTF-8"); for (int i = 0; i < allLines.size();) { count++; List<String> record = new ArrayList<>(); String line = allLines.get(i); String type = TIPO_ALLOGGIO.parse(line); switch (type) { case "16": record.add(line); i++; break; case "17": { record.add(line); boolean out = false; while (!out) { i++; if (i < allLines.size()) { String subline = allLines.get(i); String subtype = TIPO_ALLOGGIO.parse(subline); if (!subtype.equals("19")) { out = true; } else { record.add(subline); } } else { out = true; } } break; } case "18": { record.add(line); boolean out = false; while (!out) { i++; if (i < allLines.size()) { String subline = allLines.get(i); String subtype = TIPO_ALLOGGIO.parse(subline); if (!subtype.equals("20")) { out = true; } else { record.add(subline); } } else { out = true; } } break; } default: break; } LocalDate arrived = LocalDate.parse(DATA_ARRIVO.parse(line), DateTimeFormatter.ofPattern(DATE_PATTERN)); if (!map.containsKey(arrived)) { map.put(arrived, new ArrayList<>()); } map.get(arrived).add(record); } } for (LocalDate date : map.keySet()) { System.out.println(); System.out.println("process day " + date); for (List<String> record : map.get(date)) { System.out.println(); System.out.println("process record "); for (String line : record) { System.out.println(line); } CheckinRecord checkinRecord = new CheckinRecord(); //non lo setto per adesso String firstLine = record.get(0); String typeStr = TIPO_ALLOGGIO.parse(firstLine); CheckinType cht = checkinTypeRepository.find(typeStr); checkinRecord.setCheckinType(cht); int days = Integer.parseInt(PERMANENZA.parse(firstLine)); checkinRecord.setDays(days); checkinRecord.setArrived(date); boolean isMain = true; List<Person> others = new ArrayList<>(); for (String line : record) { Person p = extractPerson(line); if (p.getDistrictOfBirth() == null) { System.out.println("district of birth not found " + p); } List<Person> duplicates = personRepository.findDuplicates(p.getSurname(), p.getName(), p.getDateOfBirth()); if (duplicates.isEmpty()) { System.out.println("add new person " + p.getId() + " " + p); personRepository.saveAndFlush(p); } else if (duplicates.size() == 1) { Person found = duplicates.get(0); if (p.getIdentityDocument() != null) { //we sorted by date so we suppose //the file version is newer so we update the entity p.setId(found.getId()); System.out.println("update person " + p.getId() + " " + p); personRepository.saveAndFlush(p); } else if (found.getIdentityDocument() != null) { //on db there are more data so I use them. p = found; System.out.println("use already saved person " + p.getId() + " " + p); } else { p.setId(found.getId()); System.out.println("update person " + p.getId() + " " + p); personRepository.saveAndFlush(p); } } else { throw new RuntimeException("More duplicated for " + p.getName()); } if (isMain) { checkinRecord.setMainPerson(p); isMain = false; } else { others.add(p); } } checkinRecord.setOtherPeople(new HashSet<>(others)); if (checkinRecordRepository.alreadyExists(checkinRecord.getMainPerson(), date) != null) { System.out.println("already exists " + date + " p " + checkinRecord.getMainPerson()); } else { System.out.println("save record "); checkinRecordRepository.saveAndFlush(checkinRecord); } } } // // if (type.equals("16")) { // List<String> record = new ArrayList<>(); // record.add(line); // keepOpen = false; // } // // map.get(arrived).add(record); // map.values().forEach((list) -> { // // for (String line : list) { // // Person p = null; // // try { // // p = extractPerson(line); // // List<Person> duplicates = personRepository.findDuplicates(p.getSurname(), p.getName(), p.getDateOfBirth()); // // if (duplicates.isEmpty()) { // personRepository.saveAndFlush(p); // // } else if (duplicates.size() > 1) { // System.out.println(); // System.out.println("MULIPLE DUPLICATED"); // // for (Person dd : duplicates) { // System.out.println(dd); // } // System.out.println("* " + p); // throw new RuntimeException(); // } else { // //// if (!duplicates.get(0).getDistrictOfBirth().equals(p.getDistrictOfBirth())) { //// int index = 0; //// //// System.out.println(); //// System.out.println("DUPLICATED"); //// //// for (Person dd : duplicates) { //// System.out.println(dd); //// index++; //// } //// System.out.println("* " + p); //// System.out.println(file.getAbsolutePath() + " " + p); //// //// System.out.println(); //// System.out.println(); //// } //// duplicates.remove(0); //// personRepository.deleteInBatch(duplicates); //// System.out.println(); //// System.out.println("Seleziona scelta"); //// Scanner s = new Scanner(System.in); //// int selected; //// try { //// selected = s.nextInt(); //// } catch (InputMismatchException e) { //// selected = 0; //// } //// //// if (duplicates.size() <= selected) { //// personRepository.deleteInBatch(duplicates); //// personRepository.saveAndFlush(p); //// } else { //// duplicates.remove(selected); //// personRepository.deleteInBatch(duplicates); //// } // } // // } catch (Exception e) { // // System.out.println(); //// System.out.println("ERROR READING lineCount=" + allLines.indexOf(line) + " line=" + line); //// System.out.println(file.getAbsolutePath()); // System.out.println(p); // e.printStackTrace(); // System.out.println(); // } // } // }); context.registerShutdownHook(); System.exit(0); } /** * If no profile has been configured, set by default the "dev" profile. */ private static void addDefaultProfile(SpringApplication app, SimpleCommandLinePropertySource source) { if (!source.containsProperty("spring.profiles.active") && !System.getenv().containsKey("SPRING_PROFILES_ACTIVE")) { app.setAdditionalProfiles("dev"); //public static final String SPRING_PROFILE_DEVELOPMENT = "dev"; } } private static void initBeans(ConfigurableApplicationContext context) { countryRepository = context.getBean(CountryRepository.class); districtRepository = context.getBean(DistrictRepository.class); identityDocumentRepository = context.getBean(IdentityDocumentRepository.class); personRepository = context.getBean(PersonRepository.class); checkinTypeRepository = context.getBean(CheckinTypeRepository.class); checkinRecordRepository = context.getBean(CheckinRecordRepository.class); } public static Person extractPerson(String line) { Person p = new Person(); // String part = line.substring(start, start + COGNOME.getLength()).trim().toLowerCase(); // if (containsSpecialChar(part)) { // part = part.substring(0, part.length() - 1).trim().toLowerCase(); // start--; // } p.setSurname(COGNOME.parse(line)); p.setName(NOME.parse(line)); String part = SESSO.parse(line); p.setGender(part.equals("1") ? Gender.male : Gender.female); part = DATA_NASCITA.parse(line); p.setDateOfBirth(LocalDate.parse(part, DateTimeFormatter.ofPattern(DATE_PATTERN))); part = COMUNE_NASCITA.parse(line); District d = districtRepository.find(part); p.setDistrictOfBirth(d); part = STATO_NASCITA.parse(line); Country c = countryRepository.find(part); p.setCountryOfBirth(c); part = CITTADINANZA.parse(line); c = countryRepository.find(part); p.setCitizenship(c); part = TIPO_DOCUMENTO.parse(line); IdentityDocument id = identityDocumentRepository.find(part); p.setIdentityDocument(id); part = NUMERO_DOCUMENTO.parse(line); if (!part.isEmpty()) { p.setDocumentNumber(part); } part = LUOGO_RILASCIO.parse(line); d = districtRepository.find(part); p.setDocumentReleaseDistrict(d); if (d == null) { c = countryRepository.find(part); p.setDocumentReleaseCountry(c); } return p; } private static boolean containsSpecialChar(String part) { return part.contains("") || part.contains("") || part.contains("") || part.contains("") || part.contains("") || part.contains(""); } }