Family-Map-Project / FamilyMapServer / FamilyMapServerStudent-master / src / services / FillService.java
FillService.java
Raw
package services;


import dao.*;
import model.Event;
import model.Person;
import model.User;
import request.FillRequest;
import result.FillResult;

import java.util.List;

/**
 * Implementation of the fill/[username]/{generations} API route
 */
public class FillService
{
    /**
     * Populates the server's database with generated data for the specified username. The user must
     * already be registered with the server and any data that already exists in the database for the user is deleted.
     * The optional "generations" parameter specifies the number of generations to be created and must be a non-negative integer
     * (default is 4)
     * @param r - theFillRequest
     * @return the result of the fill operation
     */
    public FillResult fill(FillRequest r) throws DataAccessException
    {
        Database db = new Database();

        FillResult result = new FillResult();

        try
        {
            db.openConnection();

            if (r.getGenerations() < 0)
            {
                throw new DataAccessException("Invalid generations given");
            }

            UserDAO uDao = new UserDAO(db.getConnection());
            PersonDAO pDao = new PersonDAO(db.getConnection());
            EventDAO eDao = new EventDAO(db.getConnection());

            if(uDao.getUserByID(r.getUsername()) != null)
            {
                pDao.removeUserPersons(r.getUsername());
                eDao.removeUserEvents(r.getUsername());

                User user = uDao.getUserByID(r.getUsername());

                DataGenerator generator = new DataGenerator();

                Person person = generator.createGenerations(user, user.getGender(), r.getGenerations(), db.getConnection());

                Person userPerson = person;

                List<Event> eventsToTransfer = new EventDAO(db.getConnection()).findEventsForPerson(userPerson.getPersonID());

                eDao.removePersonEvents(person.getPersonID());
                pDao.removePersonByID(person.getPersonID());

                userPerson.setFirstName(user.getFirstName());
                userPerson.setLastName(user.getLastName());
                userPerson.setGender(user.getGender());
                userPerson.setPersonID(user.getPersonID());

                for (int i = 0; i < eventsToTransfer.size(); i++)
                {
                   eventsToTransfer.get(i).setPersonID(userPerson.getPersonID());
                   new EventDAO(db.getConnection()).createEvent(eventsToTransfer.get(i));
                }

                pDao.createPerson(userPerson);

                db.closeConnection(true);

                result.setMessage("Successfully added " + generator.numPersons + " persons and " + generator.numEvents + " events to the database");
                result.setSuccess(true);
                return result;
            }
            else
            {
                throw new DataAccessException("User was not found in the database");
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            db.closeConnection(false);

            result.setMessage("Error: " + e.getMessage());
            result.setSuccess(false);
            return result;
        }
    }
}