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

import dao.*;
import model.Person;
import request.PersonRequest;
import result.PersonResult;
import result.PersonsResult;

/**
 * Implementation of the /person and /person/[personID] API routes
 */
public class PersonService
{
    /**
     * Returns the single Person object with the specified ID
     * @param r - the personRequest
     * @return the result of the person operation with a specified personID
     */
    public PersonResult getPerson(PersonRequest r) throws DataAccessException
    {
        Database db = new Database();

        PersonResult result = new PersonResult();

        try
        {
            db.openConnection();

            PersonDAO pDao = new PersonDAO(db.getConnection());
            AuthTokenDAO aDao = new AuthTokenDAO(db.getConnection());

            if (pDao.getPersonByID(r.getPersonID()) != null)
            {
                Person person = pDao.getPersonByID(r.getPersonID());

                if (aDao.getUsername(r.getAuthToken()).equals(person.getAssociatedUsername()))
                {
                    result.setAssociatedUsername(person.getAssociatedUsername());
                    result.setPersonID(person.getPersonID());
                    result.setFirstName(person.getFirstName());
                    result.setLastName(person.getLastName());
                    result.setGender(person.getGender());

                    if (person.getFatherID() != null)
                    {
                        result.setFatherID(person.getFatherID());
                    }
                    if (person.getMotherID() != null)
                    {
                        result.setMotherID(person.getMotherID());
                    }
                    if (person.getSpouseID() != null)
                    {
                        result.setSpouseID(person.getSpouseID());
                    }

                    result.setSuccess(true);
                    db.closeConnection(true);
                    return result;
                }
                else
                {
                    throw new DataAccessException("Person does not belong to the given user");
                }
            }
            else
            {
                throw new DataAccessException("Couldn't find person in database");
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            db.closeConnection(false);

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

    /**
     * Returns all family members of the current user. The current user is determined from the provided authToken
     * @param r - the personRequest
     * @return the result of the person operation without a specified personID
     */
    public PersonsResult getPersons(PersonRequest r) throws DataAccessException
    {
        Database db = new Database();

        PersonsResult result = new PersonsResult();

        try
        {
            db.openConnection();

            PersonDAO pDao = new PersonDAO(db.getConnection());
            AuthTokenDAO aDao = new AuthTokenDAO(db.getConnection());
            String username = aDao.getUsername(r.getAuthToken());

            if (pDao.findPersonsForUser(username) != null)
            {
                if (pDao.findPersonsForUser(username).get(0).getAssociatedUsername().equals(aDao.getUsername(r.getAuthToken())))
                {
                    result.setData(pDao.findPersonsForUser(username));
                    result.setSuccess(true);
                    db.closeConnection(true);
                    return result;
                }
                else
                {
                    throw new DataAccessException("Couldn't find persons for the given user");
                }
            }
            else
            {
                throw new DataAccessException("Couldn't find persons for the given user");
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            db.closeConnection(false);

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