cimitero.rest.InitRESTService.java Source code

Java tutorial

Introduction

Here is the source code for cimitero.rest.InitRESTService.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 cimitero.rest;

import cimitero.entities.TBody;
import cimitero.entities.TCemetry;
import cimitero.entities.TCemetryGround;
import cimitero.entities.TCustomer;
import cimitero.entities.TInvoice;
import cimitero.entities.TReminder;
import cimitero.entities.TTomb;
import cimitero.entities.TTombRequest;
import cimitero.persistence.HibernateUtil;
import java.sql.Date;
import javax.ejb.Stateless;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.hibernate.Session;
import org.jboss.logging.Logger;

/**
 * REST Web Service
 *
 * @author Markus
 */
@Stateless
@Path("/init")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public class InitRESTService {

    Logger log = Logger.getLogger(InitRESTService.class);

    public InitRESTService() {

    }

    @GET
    @Produces({ MediaType.TEXT_PLAIN })
    public String init() {
        fillPersons();
        fillCemetries();
        fillCemetryGrounds();
        fillTombs();
        fillBodies();
        fillInvoices();
        fillReminders();
        fillTombRequest();
        return "database filled";
    }

    @GET
    @Path("/truncatedb")
    public void truncateTables() {
        log.info("truncate db");
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        //session.createQuery("delete from TUser").executeUpdate();
        session.createQuery("delete from TCoordinate").executeUpdate();
        session.createQuery("delete from TCemetryGround").executeUpdate();
        session.createQuery("delete from TCemetry").executeUpdate();
        session.createQuery("delete from TReminder").executeUpdate();
        session.createQuery("delete from TInvoice").executeUpdate();
        session.createQuery("delete from TTombRequest").executeUpdate();
        session.createQuery("delete from TPerson").executeUpdate();
        session.createQuery("delete from TTomb").executeUpdate();
        session.getTransaction().commit();
    }

    private void fillCemetries() {
        log.info("generating 100 cemetries");
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        for (int i = 1; i <= 100; i++) {
            TCemetry tmpCemetry = new TCemetry();
            tmpCemetry.setName("Cemetry " + i);
            tmpCemetry.setAddress("Cemetry Street " + i);
            tmpCemetry.setLatitute(1.0);
            tmpCemetry.setLongitude(1.0);
            session.saveOrUpdate(tmpCemetry);
        }
        session.getTransaction().commit();
    }

    private void fillCemetryGrounds() {
        log.info("generating 100 cemetry grounds");
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        for (int i = 1; i <= 100; i++) {
            TCemetryGround tmpCemetryGround = new TCemetryGround();
            tmpCemetryGround.setName("Cemetry " + i);
            tmpCemetryGround.setAddress("Cemetry Street " + i);
            tmpCemetryGround.setCemetry(null);
            session.saveOrUpdate(tmpCemetryGround);
        }
        session.getTransaction().commit();
    }

    private void fillTombs() {
        log.info("generating 100 tombs");
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        for (int i = 1; i <= 100; i++) {
            TTomb tmpTomb = new TTomb();
            tmpTomb.setTombNo(Integer.toString(i));
            tmpTomb.setCemetryGround(null);
            tmpTomb.setCustomer(null);
            session.saveOrUpdate(tmpTomb);
        }
        session.getTransaction().commit();
    }

    private void fillTombRequest() {
        log.info("generating 100 tomb request");
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        for (int i = 1; i <= 100; i++) {
            TTombRequest tmpTombRequest = new TTombRequest();
            tmpTombRequest.setRequestDate(new Date(1448912890));
            tmpTombRequest.setTomb(null);
            tmpTombRequest.setRequestText("Test Text");
            tmpTombRequest.setCustomer(null);
            session.saveOrUpdate(tmpTombRequest);
        }
        session.getTransaction().commit();
    }

    private void fillInvoices() {
        log.info("generating 100 invoices");
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        for (int i = 1; i <= 100; i++) {
            TInvoice tmpInvoice = new TInvoice();
            tmpInvoice.setCurrency("EUR");
            tmpInvoice.setTomb(null);
            tmpInvoice.setTotal((double) i * 10);
            tmpInvoice.setInvoiceDate(new Date(1448912890));
            tmpInvoice.setPaymentDate(new Date(1448912890));
            tmpInvoice.setInvoiceNumber("R00" + i);
            session.saveOrUpdate(tmpInvoice);
        }
        session.getTransaction().commit();
    }

    private void fillReminders() {
        log.info("generating 100 reminders");
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        for (int i = 1; i <= 100; i++) {
            TReminder tmpReminder = new TReminder();
            tmpReminder.setCurrency("EUR");
            tmpReminder.setInvoice(null);
            tmpReminder.setCharges((double) i * 10);
            tmpReminder.setInvoiceDate(new Date(1448912890));
            tmpReminder.setPaymentDate(new Date(1448912890));
            session.saveOrUpdate(tmpReminder);
        }
        session.getTransaction().commit();
    }

    private void fillPersons() {
        log.info("generating 100 persons");
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        for (int i = 1; i <= 100; i++) {
            TCustomer tmpPerson = new TCustomer();
            tmpPerson.setFirstName("First Name " + i);
            tmpPerson.setLastName("Last Name " + i);
            tmpPerson.setAddress("My Street " + i);
            tmpPerson.setGebDatum(Date.valueOf("01.01.1992"));
            tmpPerson.setOtherFirstNames("Other First Name");
            tmpPerson.setTelephoneNumber("0123456789");
            tmpPerson.setTitle("Title");
            session.saveOrUpdate(tmpPerson);
        }
        session.getTransaction().commit();
    }

    private void fillBodies() {
        log.info("generating 100 bodies");
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        for (int i = 1; i <= 100; i++) {
            TBody tmpBody = new TBody();
            tmpBody.setFirstName("First Name " + i);
            tmpBody.setLastName("Last Name " + i);
            tmpBody.setAddress("My Street " + i);
            tmpBody.setGebDatum(Date.valueOf("01.01.1992"));
            tmpBody.setOtherFirstNames("Other First Name");
            tmpBody.setTelephoneNumber("0123456789");
            tmpBody.setTitle("Title");
            tmpBody.setTomb(null);
            session.saveOrUpdate(tmpBody);
        }
        session.getTransaction().commit();
    }
}