/*
* GNU General Public License
*
* Copyright (C) 2006 Matej Zachar <mzachar@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id$
*/
package sk.netmap.gui.controller;
import org.apache.log4j.Logger;
import java.util.List;
import java.util.Set;
import sk.netmap.bo.Group;
import sk.netmap.bo.Server;
import sk.netmap.bo.SharedFolder;
import sk.netmap.bo.UsedSystemLetters;
import sk.netmap.gui.componets.AbstractPropertyChangeNotifier;
import sk.netmap.util.NetworkMap;
import sk.netmap.util.SpringUtil;
/**
* Main data controller for manipulation with data
* @author Matej Zachar $Date$
*/
public class DataController extends AbstractPropertyChangeNotifier{
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(DataController.class);
public static final String PROPERTY_GROUP_CHANGE = "Group change";
public static final String PROPERTY_SERVER_CHANGE = "Server change";
public static final String PROPERTY_SHARED_FOLDER_CHANGE = "Folder change";
public static final String PROPERTY_MAP_GROUP = "Map group";
public static final String PROPERTY_UNMAP_GROUP = "Unmap group";
private static DataController instance;
private Group lastMappedGroup;
private Group oldGroup = new Group();
private Server oldServer = new Server();
private SharedFolder oldSharedFolder = new SharedFolder();
public static DataController getInstance() {
if (instance == null){
instance = new DataController();
}
return instance;
}
public void fireDataChanged(){
firePropertyChange(PROPERTY_GROUP_CHANGE, oldGroup, oldServer);
}
public boolean isGroupMapped(Group group){
return group.equals(lastMappedGroup);
}
public void addGroup(Group newGroup) {
SpringUtil.getDataService().saveOrUpdateGroup(newGroup);
firePropertyChange(PROPERTY_GROUP_CHANGE, null, newGroup);
}
public void updateGroup(Group newGroup){
SpringUtil.getDataService().saveOrUpdateGroup(newGroup);
firePropertyChange(PROPERTY_GROUP_CHANGE, oldGroup, newGroup);
}
public void removeGroup(Group group){
SpringUtil.getDataService().removeGroup(group);
firePropertyChange(PROPERTY_GROUP_CHANGE, group, null);
}
public List<Group> getAllGroups() {
return SpringUtil.getDataService().getAllGroups();
}
public void addServer(Group currentGroup, Server newServer) {
currentGroup.addServer(newServer);
SpringUtil.getDataService().saveOrUpdateServer(newServer);
firePropertyChange(PROPERTY_SERVER_CHANGE, null, newServer);
}
public void updateServer(Server server){
SpringUtil.getDataService().saveOrUpdateServer(server);
firePropertyChange(PROPERTY_SERVER_CHANGE, oldServer, server);
}
public void removeServer(Server server) {
SpringUtil.getDataService().removeServer(server);
firePropertyChange(PROPERTY_SERVER_CHANGE, server, null);
}
public void addSharedFolder(Server currentServer, SharedFolder newSharedFoder) {
currentServer.addSharedFolder(newSharedFoder);
SpringUtil.getDataService().saveOrUpdateSharedFolder(newSharedFoder);
firePropertyChange(PROPERTY_SHARED_FOLDER_CHANGE, null, newSharedFoder);
}
public void updateSharedFolder(SharedFolder folder){
SpringUtil.getDataService().saveOrUpdateSharedFolder(folder);
firePropertyChange(PROPERTY_SHARED_FOLDER_CHANGE, oldSharedFolder, folder);
}
public void removeSharedFolder(SharedFolder folder) {
SpringUtil.getDataService().removeSharedFolder(folder);
firePropertyChange(PROPERTY_SHARED_FOLDER_CHANGE, folder, null);
}
public void addUsedSystemLetter(String letter){
logger.debug("Adding used System letter: '"+letter+"'");
UsedSystemLetters usedSystemLetters = SpringUtil.getDataService().getUsedSystemLetters();
usedSystemLetters.addLetter(letter);
SpringUtil.getDataService().saveOrUpdateUsedSystemLetters(usedSystemLetters);
}
public void removeUsedSystemLetter(String letter){
UsedSystemLetters usedSystemLetters = SpringUtil.getDataService().getUsedSystemLetters();
usedSystemLetters.getLetters().remove(letter);
SpringUtil.getDataService().saveOrUpdateUsedSystemLetters(usedSystemLetters);
}
public Set<String> getFreeLettersInGroup(Group group) {
return SpringUtil.getDataService().getFreeLettersInGroup(group);
}
public Set<String> getFreeLettersInUsedSyetemLetters() {
return SpringUtil.getDataService().getFreeLettersInUsedSystemLetters();
}
public Set<String> getUsedLettersInUsedSystemLetters() {
return SpringUtil.getDataService().getUsedLettersInUsedSystemLetters();
}
/**
* Map all drivers in database to system
*/
public void mapGroup(Group group) {
Group oldGroup = lastMappedGroup;
if (oldGroup != null){
logger.debug("------map group: "+oldGroup.getId()+" - "+group.getId());
}
NetworkMap.getInstance().mapGroup(oldGroup, group);
lastMappedGroup = group;
firePropertyChange(PROPERTY_MAP_GROUP, oldGroup, group);
}
public void mapGroup(long groupId) {
mapGroup(SpringUtil.getDataService().getGroup(groupId));
}
public void unMapGroup(Group group) {
NetworkMap.getInstance().unMapGroup(group);
if (lastMappedGroup.equals(group)){
lastMappedGroup = null;
}
firePropertyChange(PROPERTY_UNMAP_GROUP, null, group);
}
public void unMapLastMappedGroup(){
unMapGroup(lastMappedGroup);
lastMappedGroup = null;
}
public void forceUnMapAllSystemNetworkDrives() {
Group oldGroup = lastMappedGroup;
NetworkMap.getInstance().forceUnMapAllDrives();
lastMappedGroup = null;
firePropertyChange(PROPERTY_UNMAP_GROUP, null, oldGroup);
}
}