/*
* 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.bll;
import com.microsoft.sqlserver.jdbc.SQLServerException;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javafx.scene.Node;
import javafx.scene.control.Button;
import mytime.be.Group;
import mytime.be.Location;
import mytime.be.Person;
import mytime.dal.DALFacade;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
*
* @author Stefan-VpcEB3J1E
*/
public class BLLManager
{
private final DALFacade dalFacade;
public BLLManager() throws IOException
{
dalFacade = DALFacade.getInstance();
}
/**
* Gets all the locations without the groups and persons
*
* @return
* @throws SQLException
*/
public List<Location> getAllLocations() throws SQLException
{
return dalFacade.getAllLocations();
}
/**
* Gets a Location with groups and persons by id
*
* @param locationId
* @return
* @throws SQLServerException
*/
public Location getSelectedLocation(int locationid) throws SQLException
{
return dalFacade.getSelectedLocation(locationid);
}
/**
* 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
*/
public void addHoursForVolunteer(int volunteerid, int guildid, int hours) throws SQLException
{
dalFacade.addHoursForVolunteer(volunteerid, guildid, hours);
}
/**
* Returns a list of guilds at a certain location which the given volunteer
* is a member of
*
* @param c
* @param volunteerid
* @param locationid
* @return
*/
public List<Group> getAMembersGuildsAtLocation(int volunteerid, int locationid) throws SQLException
{
return dalFacade.getAMembersGuildsAtLocation(volunteerid, locationid);
}
/**
* @param volunteerid
* @return the amount of hours one volunteer has worked in total, as an int.
* The volunteer is defined by id
* @throws SQLException
*/
public int getTotalHoursOneVolunteer(int volunteerid) throws SQLException
{
return dalFacade.getTotalHoursOneVolunteer(volunteerid);
}
/**
* @param volunteerid
* @param guildid
* @return amount of hours one person worked on one guild, as an int.
* @throws SQLException
*/
public int getHoursWorkedOnOneGuildByVolunteer(int volunteerid, int guildid) throws SQLException
{
return dalFacade.getHoursWorkedOnOneGuildByVolunteer(volunteerid, guildid);
}
/**
* @param volunteerid
* @return a list of all the groups a person is assigned to
* @throws SQLException
*/
public List<Group> getAllGroupsForPerson(int volunteerid) throws SQLException
{
return dalFacade.getAllGroupsForPerson(volunteerid);
}
public void undoLastDocumentedHours() throws SQLException
{
dalFacade.undoLastDocumentedHours();
}
/**
* Takes a list and a query. Returns a new list filtered with the query.
*
* @param query
* @param fullList
* @return
*/
public List<Node> filterList(String query, List<Node> fullList)
{
List<Node> filteredList = new ArrayList();
for (int i = 0; i < fullList.size(); i++)
{
//Button button = (Button) fullList.get(i);
Person person = (Person) fullList.get(i).getUserData();
String text = person.getFullName().toLowerCase();
//String[] array = text.split("\n");
// if (array.length > 1)
// {
// text = array[0] + " " + array[1];
// }
if (text.contains(query.toLowerCase()))
{
filteredList.add(fullList.get(i));
}
}
return filteredList;
}
/**
* Save the chosen location on the pc, so you dont have to choose everytime
* you open the application
*
* @param location
* @throws IOException
*/
public void saveLocationLocally(int locationid) throws IOException
{
dalFacade.saveLocationLocally(locationid);
}
/**
* Gets the store location
*
* @return
* @throws IOException
*/
public int getLocalLocation() throws IOException
{
return dalFacade.getLocalLocation();
}
/**
* @return all volunteers
* @throws SQLException
*/
public List<Person> getAllVolunteers() throws SQLException
{
return dalFacade.getAllVolunteers();
}
/**
* Creates and adds volunteer to the database
*
* @param name
* @param email
* @param phonenumber
* @throws SQLException
*/
public Person createVolunteer(String firstname, String lastname, String email, String phonenumber, String description, String profilepicture, int... groupid) throws SQLException, TransactionException
{
return dalFacade.createVolunteer(firstname, lastname, email, phonenumber, description, profilepicture, groupid);
}
/**
* Deletes a volunteer from the database
*
* @param volunteerid
* @throws SQLException
*/
public void deleteVolunteer(Person p) throws SQLException
{
dalFacade.deleteVolunteer(p);
}
/**
* Undo the last person that the manger have deleted.
*
* @return
* @throws SQLException
*/
public Person undoLastDeletedPerson() throws SQLException
{
return dalFacade.undoLastDeletedPerson();
}
/**
* Clears teh saved location on this computer
*
* @throws IOException
*/
public void clearSelectionOfLocation() throws IOException
{
dalFacade.clearLocationLocally();
}
/**
* @return all groups stored in the database
* @throws SQLException
*/
public List<Group> getAllGroups() throws SQLException
{
return dalFacade.getAllGroups();
}
public List<Person> getPersonsInGroup(Group group) throws SQLException
{
return dalFacade.getAllVolunteersInGuild(group);
}
/**
* Updates the information of a person.
*
* @param person
*/
public void updatePerson(Person person) throws SQLException
{
dalFacade.updatePerson(person);
}
/**
* Assign person to group (Volunteer to guild)
*
* @param person
* @param group
* @throws SQLException
*/
public void addPersonToGroup(Person person, Group group) throws SQLException
{
dalFacade.addVolunteerToGuild(person, group);
}
/**
* Removes a person from a group
*
* @param person
* @param group
* @throws SQLException
*/
public void removePersonFromGroup(Person person, Group group) throws SQLException
{
dalFacade.removePersonFromGroup(person, group);
}
/**
* Create a new group
* @param name The groups name
* @param location The location where the group should be associated to.
* @return
* @throws SQLException
*/
public Group createNewGuild(String name, Location location) throws SQLException
{
return dalFacade.createGuild(name, location.getId().get());
}
/**
* Get sum of hours worked in each guild by one volunteer
* @param person
* @param groupAmount
* @return int[guild id][hours]
* @throws SQLException
*/
public int[][] getHoursWorkedInEachGuildOnByPerson(Person person, int groupAmount) throws SQLException
{
return dalFacade.getHoursWorkedInEachGuildOnByPerson(person, groupAmount);
}
/**
* Method for saving a the spreadsheet as a XLS document.
* @param generateXlsDocument Spreadsheet
* @param selectedFile Destination file to save to.
*/
public void saveXlsDocument(HSSFWorkbook generateXlsDocument, File selectedFile)
{
dalFacade.saveXlsDocument(generateXlsDocument, selectedFile);
}
}