/*
* 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.gui.model;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.ToggleGroup;
import mytime.be.Group;
import mytime.be.Location;
import mytime.be.Person;
import mytime.bll.BLLManager;
import mytime.bll.SortingStrategyFirstName;
import mytime.bll.SortingStrategyLastName;
import mytime.bll.Sortings;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
*
* @author Stefan-VpcEB3J1E
*/
public class ManagerModel
{
private static ManagerModel INSTANCE;
private Node rightSideView;
private BooleanProperty showRightPanel, reloadNodes;
private BLLManager bllMgr;
private List<Node> allPersons;
private List<Node> allPersonsGuildTab;
private List<Node> allPersonsInGuild;
private StringProperty textfieldSearchText;
private StringProperty queryAllPersons;
private StringProperty queryPersonsInGuild;
private ObservableList<String> snackbarMessage;
private final ObservableList<Group> allGroups;
private ContextMenu contextMenu;
private List<ToggleGroup> groupSorts;
private int currentToggleIdx;
private ObjectProperty objectProperty;
private ObjectProperty clickedVolunteer;
private Person currentPerson;
private Group currentlyChosenGuild;
/**
* Singleton
*/
private ManagerModel()
{
//slam
currentToggleIdx = 10;
groupSorts = new ArrayList<>();
contextMenu = new ContextMenu();
snackbarMessage = FXCollections.observableArrayList();
showRightPanel = new SimpleBooleanProperty(false);
reloadNodes = new SimpleBooleanProperty(false);
allPersons = Collections.synchronizedList(FXCollections.observableArrayList());
allPersonsGuildTab = Collections.synchronizedList(FXCollections.observableArrayList());
allPersonsInGuild = Collections.synchronizedList(FXCollections.observableArrayList());
textfieldSearchText = new SimpleStringProperty();
queryAllPersons = new SimpleStringProperty();
queryPersonsInGuild = new SimpleStringProperty();
objectProperty = new SimpleObjectProperty();
clickedVolunteer = new SimpleObjectProperty();
allGroups = FXCollections.observableArrayList();
try
{
allGroups.addAll(Model.getInstance().getAllGroups());
} catch (SQLException ex)
{
ex.printStackTrace();
}
}
/**
* Singleton
*
* @return
*/
public static ManagerModel getInstance()
{
if (INSTANCE == null)
{
INSTANCE = new ManagerModel();
}
return INSTANCE;
}
/**
* Sets the node for the side panel
*
* @param rightSideView
*/
public void setRightSideNode(Node rightSideView)
{
this.rightSideView = rightSideView;
}
/**
* Boolean property if the panel should be shown.
*
* @return
*/
public BooleanProperty getShowRightPanel()
{
return showRightPanel;
}
/**
* Returns the node for teh side panel.
*
* @return
*/
public Node getRightSideView()
{
return rightSideView;
}
/**
* Loads all the persons from the database
*
* @return
* @throws SQLException
*/
public List<Person> loadAllPersons() throws SQLException
{
return bllMgr.getAllVolunteers();
}
/**
* Sets the bllMgr
*
* @param bllMgr
*/
public void setBllManager(BLLManager bllMgr)
{
this.bllMgr = bllMgr;
}
/**
* Gets the person nodes as fx components
*
* @return
*/
public List<Node> getAllPersonNodes()
{
return allPersons;
}
/**
* Sets the list
*
* @param allPersons
*/
public void setAllPersons(List<Node> allPersons)
{
this.allPersons = allPersons;
}
/**
* Applies the Sorting that the user choose.
*
* @param sortings
* @param nodesToSort
* @return
*/
public List<Node> applySortingStrategy(Sortings sortings, List<Node> nodesToSort)
{
if (nodesToSort == null)
{
return allPersons = sortings.applyStrategies(allPersons);
} else
{
return sortings.applyStrategies(nodesToSort);
}
}
/**
* @return the String property used for filtering the volunteers in the
* managerview
*/
public StringProperty getTextfieldSearchText()
{
return textfieldSearchText;
}
/**
* @param query
* @return filters the allPersons list and returns the filtered list
*/
public List<Node> filterList(String query)
{
//System.out.println(bllMgr.filterList(query, loginPersonNodes));
return bllMgr.filterList(query, allPersons);
}
/**
* Deletes a volunteer from the database
*
* @param volunteerid
* @throws SQLException
*/
public void deleteVolunteer(Person p) throws SQLException
{
bllMgr.deleteVolunteer(p);
}
/**
* If the person nodes should be loaded in the view again or not.
*
* @return
*/
public BooleanProperty getReloadNodes()
{
return reloadNodes;
}
/**
* Gets the context menu that should be set for one button.
*
* @return
*/
public ContextMenu getContextMenu()
{
return contextMenu;
}
/**
* Sets the toggle index of the sorts selected
*
* @param i
*/
public void setCurrentToggle(int i)
{
currentToggleIdx = i;
}
/**
* gets the toggle idx
*
* @return
*/
public int getCurrentToggleIdx()
{
return currentToggleIdx;
}
/**
* Sets the new string the snackbar should show
*
* @param string
*/
public void showSnackbar(String string)
{
snackbarMessage.add(string);
}
/**
* Gets the property of the message.
*
* @return
*/
public ObservableList<String> getSnackbarMessage()
{
return snackbarMessage;
}
/**
* Undo the last deleted person in the view
*
* @return
* @throws SQLException
*/
public Person undoLastDeletedPerson() throws SQLException
{
return bllMgr.undoLastDeletedPerson();
}
/**
*
* @return
*/
public ObjectProperty getObjectProperty()
{
return objectProperty;
}
/**
* Clears teh saved location on this computer.
*
* @throws IOException
*/
public void clearSavedLocationInFile() throws IOException
{
bllMgr.clearSelectionOfLocation();
}
/**
* @return observablelist holding all groups
*/
public ObservableList<Group> getAllGroups()
{
return allGroups;
}
public void reloadVolunteerNodes()
{
reloadNodes.set(true);
}
public List<Person> getPersonsInGroup(Group group) throws SQLException
{
return bllMgr.getPersonsInGroup(group);
}
/**
* @return the objectproperty for clicked volunteer. Used for storing the
* volunteer clicked in the masonrypane in the volunteer tab in the
* manager-view
*/
public ObjectProperty getClickedVolunteer()
{
return clickedVolunteer;
}
/**
* @return the current person chosen in the volunteertab
*/
public Person getCurrentPerson()
{
return currentPerson;
}
/**
* Sets the current person chosen in the volunteertab
*
* @param currentPerson
*/
public void setCurrentPerson(Person currentPerson)
{
this.currentPerson = currentPerson;
}
/**
* Sets the currently chosen group. Getting setted when a guild is chosen in
* guild tab
*
* @param group
*/
public void setCurrentlyChosenGroup(Group group)
{
this.currentlyChosenGuild = group;
}
/**
* Gets currently chosen guild
*
* @return
*/
public Group getCurrentlyChosenGuild()
{
return currentlyChosenGuild;
}
/**
* Creates a new guild
*
* @param name
* @param location
* @return
* @throws SQLException
*/
public Group createNewGuild(String name, Location location) throws SQLException
{
return bllMgr.createNewGuild(name, location);
}
/**
* Retruns list of nodes for the MasonrayPane of all persons, in the guild
* tab Used for filtering list
*
* @return
*/
public List<Node> getAllPersonsGuildTab()
{
return allPersonsGuildTab;
}
/**
* Retruns list of nodes for the MasonrayPane of all persons in one guild,
* in the guild tab Used for filtering list
*
* @return
*/
public List<Node> getAllPersonsInGuild()
{
return allPersonsInGuild;
}
/**
* The property used for filtering the second list of nodes(All persons) in
* the guild tab
*
* @return
*/
public StringProperty getQueryAllPersons()
{
return queryAllPersons;
}
/**
* The property used for filtering the first list of nodes(All persons in
* guild) in the guild tab
*
* @return
*/
public StringProperty getQueryPersonsInGuild()
{
return queryPersonsInGuild;
}
/**
* Gets hours worked on a guild for a volunteer
*
* @param volunteerId
* @param guildId
* @return
* @throws SQLException
*/
public int getHoursWorkedOnOneGuildByVolunteer(int volunteerId, int guildId) throws SQLException
{
return bllMgr.getHoursWorkedOnOneGuildByVolunteer(volunteerId, guildId);
}
public void saveXlsDocument(HSSFWorkbook generateXlsDocument, File selectedFile)
{
bllMgr.saveXlsDocument(generateXlsDocument, selectedFile);
}
}