blankd.acme.pet.licensing.Application.java Source code

Java tutorial

Introduction

Here is the source code for blankd.acme.pet.licensing.Application.java

Source

package blankd.acme.pet.licensing;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import blankd.acme.pet.licensing.model.Account;
import blankd.acme.pet.licensing.model.License;
import blankd.acme.pet.licensing.model.Pet;
import blankd.acme.pet.licensing.model.emnu.AccountTypes;
import blankd.acme.pet.licensing.repo.AccountRepository;
import blankd.acme.pet.licensing.repo.LicenseRepository;
import blankd.acme.pet.licensing.repo.PetRepository;

@SpringBootApplication //(exclude = org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class)
public class Application implements CommandLineRunner {
    private static final Logger log = LoggerFactory.getLogger(Application.class);
    private static final Integer ZERO = Integer.valueOf(0);

    @Autowired
    private LicenseRepository repo;

    @Autowired
    private AccountRepository aRepo;

    @Autowired
    private PetRepository pRepo;

    @Value("${run.loader.load.data}")
    private String loadData;

    @Value("${run.loader.names}")
    private String petNames;

    @Value("${run.loader.animals}")
    private String species;

    @Value("${run.loader.assigned.max}")
    private Integer maxAssigned;

    @Value("${run.loader.licenses.max}")
    private Integer maxLicense;

    @Value("${run.loader.random.split}")
    private Double randomSplit;

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... arg0) throws Exception {
        Boolean runLoad = Boolean.valueOf(loadData);
        Account adminOwner = new Account("blankd", "Nothing", "Nothing", "Nothing", "Nothing", AccountTypes.ADMIN);
        Account clerkOwner = new Account("workerblank", "Nothing", "Nothing", "Nothing", "Nothing",
                AccountTypes.CLERK);
        Account petOwner = new Account("superblank", "Nothing", "Nothing", "Nothing", "Nothing", AccountTypes.USER);
        aRepo.save(adminOwner);
        aRepo.save(clerkOwner);
        aRepo.save(petOwner);
        if (runLoad) {
            List<License> inserting = new ArrayList<License>();
            String[] names = petNames.split(",");
            String[] animals = species.split(",");
            Integer assignedLicenses = ZERO;
            for (Integer i = ZERO; i < maxLicense; i++) {
                String license = Integer.toOctalString(i);
                Boolean assignLicense = this.randomBoolean();
                if (assignedLicenses < this.maxAssigned && assignLicense) {
                    String assignName = names[this.randomNumber(names.length)];
                    String assignSpecies = animals[this.randomNumber(animals.length)];
                    log.debug("Assigning Name " + assignName);
                    log.debug("Assigning Species " + assignSpecies);
                    assignedLicenses++;
                    Pet thePet = new Pet(assignName, assignSpecies, Pet.NONE);
                    pRepo.save(thePet);
                    log.debug("pet id is " + thePet.getId());
                    repo.save(new License(license, thePet, this.generateExpirationDate(), petOwner));
                } else {
                    log.debug("Generating License " + license);
                    repo.save(new License(license, null, null, null));
                }
            }
            log.debug("Assigned " + assignedLicenses + " licenses");

            Account find = aRepo.findByUsername("blankd");
            log.debug(find.getUsername() + " has " + find.getLicences().size() + " pets");
        }
    }

    private Date generateExpirationDate() {
        Calendar cal = Calendar.getInstance();
        Boolean expired = this.randomBoolean();
        if (expired) {
            cal.add(Calendar.YEAR, -1);
        } else {
            cal.add(Calendar.YEAR, 1);
        }

        return cal.getTime();
    }

    private Boolean randomBoolean() {
        return Math.random() > randomSplit;
    }

    private Integer randomNumber(Integer max) {
        Random random = new Random();
        return random.nextInt(max);
    }

}