/*
* 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.util.bean;
import org.apache.log4j.Logger;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import sk.netmap.bo.Group;
import sk.netmap.bo.Server;
import sk.netmap.bo.SharedFolder;
import sk.netmap.bo.UsedSystemLetters;
import sk.netmap.util.LettersComparator;
/**
*
* @author Matej Zachar, $Date: 2006-04-23 22:48:52 +0200 (ne, 23 IV 2006) $
*
*/
public class DataService {
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(DataService.class);
private IDAO dao;
/**
*
* @param dao
*/
public void setDao(IDAO dao){
this.dao = dao;
}
// Group data controller
/**
* Save or update Group to database
*
* @param group
*/
public void saveOrUpdateGroup(Group group) {
dao.saveOrUpdate(group);
}
/**
* Remove Group from databse
*
* @param bo
*/
public void removeGroup(Group group) {
dao.delete(group);
}
public Group getGroup(long id) {
return (Group)dao.get(Group.class, id);
}
/**
* @return all groups
*/
@SuppressWarnings("unchecked")
public List<Group> getAllGroups() {
return dao.getAll(Group.class);
}
// Server data contoroler
/**
* Save or update Server to database
*
* @param server
*/
public void saveOrUpdateServer(Server server) {
dao.saveOrUpdate(server);
}
public void removeServer(Server server) {
server.getGroup().getServers().remove(server);
dao.delete(server);
}
// SharedFolder
/**
* Save or update SharedFolder to database
*
* @param server
*/
public void saveOrUpdateSharedFolder(SharedFolder folder) {
dao.saveOrUpdate(folder);
}
public void removeSharedFolder(SharedFolder folder) {
folder.getServer().getSharedFolders().remove(folder);
dao.delete(folder);
}
// Used system letters
/**
* Save or update Server to database
*
* @param server
*/
public void saveOrUpdateUsedSystemLetters(UsedSystemLetters letters) {
if (logger.isDebugEnabled()){
logger.debug("Saving UsedSystemLetter:"+letters.getId()+" Size:"+letters.getLetters().size());
}
dao.saveOrUpdate(letters);
}
public void saveOrUpdateObject(Object o){
dao.saveOrUpdate(o);
}
public void saveOrUpdateSystemLetter(String letter){
dao.saveOrUpdate(letter);
}
/**
* @return free letters which can by used to map networkDrive in group
*/
public Set<String> getFreeLettersInGroup(Group group) {
Set<String> letters = getAllLettersSet(LettersComparator.Z_A_ORDER);
// remove system leteres
for (String usedSystemLetter : getUsedSystemLetters().getLetters()) {
if (letters.contains(usedSystemLetter)) {
letters.remove(usedSystemLetter);
}
}
// remove mapedLetters
for (String mappedLetter : getUsedLettersInGroup(group)) {
if (mappedLetter != null) {
if (letters.contains(mappedLetter)) {
letters.remove(mappedLetter);
}
}
}
return letters;
}
/**
* @return used letters in selected group
*/
public Set<String> getUsedLettersInGroup(Group group) {
Set<String> letters = new TreeSet<String>(new LettersComparator(LettersComparator.Z_A_ORDER));
for (Server server : group.getServers()) {
for (SharedFolder folder : server.getSharedFolders()) {
letters.add(folder.getLetter());
}
}
return letters;
}
public Set<String> getFreeLettersInUsedSystemLetters() {
Set<String> letters = getAllLettersSet(LettersComparator.A_Z_ORDER);
// remove UsedSystemLeteres
for (String usedSystemLetter : getUsedSystemLetters().getLetters()) {
if (usedSystemLetter != null) {
if (letters.contains(usedSystemLetter)) {
letters.remove(usedSystemLetter);
}
}
}
return letters;
}
public Set<String> getUsedLettersInUsedSystemLetters() {
return getUsedSystemLetters().getLetters();
}
/**
* @return used letters by system
*/
@SuppressWarnings("unchecked")
public UsedSystemLetters getUsedSystemLetters() {
UsedSystemLetters systemLetters = (UsedSystemLetters)dao.get(UsedSystemLetters.class, 1L);
if (systemLetters == null){
systemLetters = new UsedSystemLetters();
dao.saveOrUpdate(systemLetters);
logger.debug("Created new UsedSystemLetters id:"+systemLetters.getId());
}
return systemLetters;
}
/**
* fill letters from A-Z
*
* @return Set
*/
private Set<String> getAllLettersSet(int letersComparatorOrder) {
Set<String> letters = new TreeSet<String>(new LettersComparator(letersComparatorOrder));
for (int i = 65; i <= 90; i++) {
letters.add("" + (char) i);
}
return letters;
}
}