com.acapulcoapp.alloggiatiweb.AddRegion.java Source code

Java tutorial

Introduction

Here is the source code for com.acapulcoapp.alloggiatiweb.AddRegion.java

Source

/*
 * 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.domain.enumeration.Region;
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.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
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.Properties;
import java.util.Set;
import java.util.TreeSet;

/**
 *
 * @author chiccomask
 */
public class AddRegion {

    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);

        runTask();

        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);
    }

    private static void runTask() {
        Properties prop = new Properties();
        InputStream input = null;

        try {

            input = new FileInputStream("/Users/chiccomask/Downloads/Provincie.csv");

            // load a properties file
            prop.load(input);

            // get the property value and print it out
            System.out.println(prop.getProperty("TO"));
            System.out.println(prop.getProperty("VC"));
            System.out.println(prop.getProperty("AO"));

            List<District> all = districtRepository.findAll();

            for (District d : all) {
                if (d.getRegion() == null) {
                    String r = prop.getProperty(d.getProvince());

                    if (r == null) {
                        System.out.println("province not found " + d.getProvince());
                    }

                    Region region = Region.valueOf(r.toUpperCase());

                    //          System.out.println(d.getProvince() + " " + d.getDescription() + " " + region);

                    d.setRegion(region);

                    districtRepository.saveAndFlush(d);
                }
            }

        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}