/*
* 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 mytime.dal.controller;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.util.List;
import mytime.be.Group;
import mytime.be.Person;
import mytime.be.Volunteer;
import mytime.bll.TransactionException;
import mytime.dal.dao.ConnectionManager;
import mytime.dal.dao.VolunteerDAO;
/**
*
* @author Stefan-VpcEB3J1E
*/
public class VolunteerController implements IVolunteer
{
private VolunteerDAO dao;
private ConnectionManager cm;
public VolunteerController() throws IOException
{
dao = new VolunteerDAO();
cm = new ConnectionManager();
}
/**
* Get all volunteers in a given group(guild)
*
* @return
* @throws SQLException
*/
@Override
public List<Person> getAllVolunteersInGuild(Group group) throws SQLException
{
try (Connection con = cm.getConnection())
{
return dao.getAllVolunteersInGuild(con, group);
}
}
/**
* Creates and adds a volunteer to the database
*
* @param firstname
* @param lastname
* @param email
* @param phonenumber
* @param description
* @param profilepicture
* @param groupid id's of the desired groups to be assigned to the person.
* Don't pass anything here if the person shouldnt be a member of any
* guilds.
* @return
* @throws SQLException
* @throws mytime.bll.TransactionException
*/
@Override
public Person createVolunteer(String firstname, String lastname, String email, String phonenumber, String description, String profilepicture, int... groupid) throws TransactionException, SQLException
{
try (Connection con = cm.getConnection())
{
try
{
con.setAutoCommit(false);
Person person = dao.createVolunteer(con, firstname, lastname, email, phonenumber, description, profilepicture);
for (int i : groupid)
{
dao.addVolunteerToGuild(con, person.getId().get(), i);
System.out.println("Added to guild id: " + i);
}
con.commit();
return person;
} catch (SQLException ex)
{
con.rollback();
throw new TransactionException("Badabing");
}
}
}
/**
* Documents volunteer-hours in the database. Method is called to store
* hours worked by given volunteer-id at given guild-id. The date when this
* method is called is also saved in the database
*
* @param volunteerid
* @param guildid
* @param hours
* @throws SQLException
*/
@Override
public void addHoursForVolunteer(int volunteerid, int guildid, int hours) throws SQLException
{
try (Connection con = cm.getConnection())
{
dao.addHoursForVolunteer(con, volunteerid, guildid, hours);
}
}
/**
*
* @param volunteerid
* @return the amount of hours one volunteer has worked in total. The
* volunteer is defined by id
* @throws SQLException
*/
@Override
public int getTotalHoursOneVolunteer(int volunteerid) throws SQLException
{
try (Connection con = cm.getConnection())
{
return dao.getTotalHoursOneVolunteer(con, volunteerid);
}
}
/**
* @param volunteerid
* @param guildid
* @return amount of hours one person worked on one guild, as an int.
* @throws SQLException
*/
@Override
public int getHoursWorkedOnOneGuildByVolunteer(int volunteerid, int guildid) throws SQLException
{
try (Connection con = cm.getConnection())
{
return dao.getHoursWorkedOnOneGuildByVolunteer(con, volunteerid, guildid);
}
}
/**
* Assign a volunteer to a guild
*
* @param volunteerid
* @param guildid
* @throws SQLException
*/
@Override
public void addVolunteerToGuild(int volunteerid, int guildid) throws SQLException
{
try (Connection con = cm.getConnection())
{
dao.addVolunteerToGuild(cm.getConnection(), volunteerid, guildid);
}
}
/**
* Undo the lastest documented hours.
*
* @throws SQLException
*/
@Override
public void undoLastDocumentedHours() throws SQLException
{
try (Connection c = cm.getConnection())
{
dao.undoLastChange(c);
}
}
/**
* @return all volunteers
* @throws SQLException
*/
@Override
public List<Person> getAllVolunteers() throws SQLException
{
try (Connection c = cm.getConnection())
{
return dao.getAllVolunteers(c);
}
}
/**
* Deletes a volunteer from the database
*
* @param volunteerid
* @throws SQLException
*/
@Override
public void deleteVolunteer(Person p) throws SQLException
{
try (Connection c = cm.getConnection())
{
dao.deleteVolunteer(c, p);
}
}
/**
* Undo the last deleted person
*
* @return
* @throws SQLException
*/
@Override
public Person undoLastDeletedPerson() throws SQLException
{
try (Connection c = cm.getConnection())
{
return dao.undoLastDeletedPerson(c);
}
}
/**
* Update a persons informaion
*
* @param person
* @throws SQLException
*/
@Override
public void updatePerson(Person person) throws SQLException
{
try(Connection c = cm.getConnection())
{
dao.updatePerson(c, person);
}
}
/**
* Assign person to group (Volunteer to guild)
* @param person
* @param group
* @throws SQLException
*/
@Override
public void addPersonToGroup(Person person, Group group) throws SQLException
{
try(Connection c = cm.getConnection())
{
dao.addVolunteerToGuild(c, person.getId().get(), group.getId().get());
}
}
/**
* Removes a person from a group
* @param person
* @param group
* @throws SQLException
*/
@Override
public void removePersonFromGroup(Person person, Group group) throws SQLException
{
try(Connection c = cm.getConnection())
{
dao.removePersonFromGroup(c, person, group);
}
}
/**
* Get sum of hours worked in each guild by one volunteer
* @param person
* @param groupAmount
* @return int[guild id][hours]
* @throws SQLException
*/
@Override
public int[][] getHoursWorkedInEachGuildOnByPerson(Person person, int groupAmount) throws SQLException
{
try(Connection c = cm.getConnection())
{
return dao.getHoursWorkedInEachGuildOnByPerson(c, person, groupAmount);
}
}
}