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

import dao.*;
import model.Event;
import request.EventRequest;
import result.EventResult;
import result.EventsResult;
import result.PersonResult;
import result.PersonsResult;

/**
 * Implementation of the /event and /event/[eventID] API routes
 */
public class EventService
{
    /**
     * Returns a single Event object with the specified ID
     * @param r - the eventRequest
     * @return the result of the event operation with a specified ID
     */
    public EventResult getEvent(EventRequest r) throws DataAccessException
    {
        Database db = new Database();

        EventResult result = new EventResult();

        try
        {
            db.openConnection();

            EventDAO eDao = new EventDAO(db.getConnection());
            AuthTokenDAO aDao = new AuthTokenDAO(db.getConnection());

            if (eDao.getEventByID(r.getEventID()) != null)
            {
                Event event = eDao.getEventByID(r.getEventID());

                if (aDao.getUsername(r.getAuthToken()).equals(event.getUsername()))
                {
                    db.closeConnection(true);

                    result.setAssociatedUsername(event.getUsername());
                    result.setEventID(event.getEventID());
                    result.setPersonID(event.getPersonID());
                    result.setLatitude(event.getLatitude());
                    result.setLongitude(event.getLongitude());
                    result.setCountry(event.getCountry());
                    result.setCity(event.getCity());
                    result.setEventType(event.getEventType());
                    result.setYear(event.getYear());
                    result.setSuccess(true);
                    return result;
                }
                else
                {
                    throw new DataAccessException("Event does not belong to the given user");
                }
            }
            else
            {
                throw new DataAccessException("Couldn't find event in database");
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            db.closeConnection(false);

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

    /**
     * Returns all events for all family members of the current user
     * @param r - the EventRequest
     * @return the result of the event operation without a specified ID
     */
    public EventsResult getEvents(EventRequest r) throws DataAccessException
    {
        Database db = new Database();

        EventsResult result = new EventsResult();

        try
        {
            db.openConnection();

            EventDAO eDao = new EventDAO(db.getConnection());
            AuthTokenDAO aDao = new AuthTokenDAO(db.getConnection());
            String username = aDao.getUsername(r.getAuthToken());

            if (eDao.findEventsForUser(username) != null)
            {
                if (eDao.findEventsForUser(username).get(0).getUsername().equals(aDao.getUsername(r.getAuthToken())))
                {
                    result.setData(eDao.findEventsForUser(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;
        }
    }
}