/**
* TNTConcept Easy Enterprise Management by Autentia Real Bussiness Solution S.L.
* Copyright (C) 2007 Autentia Real Bussiness Solution S.L.
*
* 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 3 of the License.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
package com.autentia.tnt.manager.admin;
import com.autentia.tnt.businessobject.Contact;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.autentia.tnt.businessobject.Department;
import com.autentia.tnt.dao.DataAccException;
import com.autentia.tnt.dao.SortCriteria;
import com.autentia.tnt.dao.hibernate.ContactDAO;
import com.autentia.tnt.dao.hibernate.DepartmentDAO;
import com.autentia.tnt.dao.search.DepartmentSearch;
import com.autentia.tnt.tracking.EntityChange;
import com.autentia.tnt.tracking.hibernate.dao.EntityChangeDAO;
import com.autentia.tnt.util.SpringUtils;
public class DepartmentManager {
/* Department - generated by stajanov (do not edit/delete) */
/** Logger */
private static final Log log = LogFactory.getLog(DepartmentManager.class);
/** Department DAO **/
private DepartmentDAO departmentDAO;
private ContactDAO contactDAO;
public void setContactDAO(ContactDAO contactDAO) {
this.contactDAO = contactDAO;
}
/**
* Get default DepartmentManager as defined in Spring's configuration file.
* @return the default singleton DepartmentManager
*/
public static DepartmentManager getDefault()
{
return (DepartmentManager)SpringUtils.getSpringBean("managerDepartment");
}
/**
* Empty constructor needed by CGLIB (Spring AOP)
*/
protected DepartmentManager()
{
}
/**
* Default constructor
* @deprecated do not construct managers alone: use Spring's declared beans
*/
public DepartmentManager( DepartmentDAO departmentDAO )
{
this.departmentDAO = departmentDAO;
}
/**
* List departments.
* @param search search filter to apply
* @param sort sorting criteria
* @return the list of all departments sorted by requested criterion
*/
public List<Department> getAllEntities(DepartmentSearch search, SortCriteria sort){
return departmentDAO.search( search, sort );
}
/**
* Get department by primary key.
* @return department selected by id.
*/
public Department getEntityById(int id){
final Department department = departmentDAO.getById(id);
department.initChanges();
return department;
}
/**
* Insert department.
*/
public void insertEntity(Department department) {
departmentDAO.insert(department);
}
/**
* Update department.
*/
public void updateEntity(Department department) {
updateEntityWithoutTrack(department);
this.trackContactChanges(department);
}
public void updateEntityWithoutTrack(Department department) {
departmentDAO.update(department);
}
/**
* Delete department.
*/
public void deleteEntity(Department department) throws DataAccException {
List<Contact> contacts = departmentDAO.getContactsForDepartment(department);
if (contacts != null && !contacts.isEmpty()) {
throw new DataAccException("Can't delete departments wiht contacts.");
}
List<Department> children = departmentDAO.getChildrenDepartments(department);
if (children != null && !children.isEmpty()) {
throw new DataAccException("Can't delete department with children departments");
}
departmentDAO.delete(department);
}
private void trackContactChanges(Department department) {
if (!department.getName().equals(department.getChanges().getName()))
{
final List<Contact> contacts = departmentDAO.getContactsForDepartment(department);
for (Contact contact : contacts) {
contact.addEntityChange(Contact.FIELD_DEPARTMENT,
department.getChanges().getName(), department.getName(),department.getId().toString());
contactDAO.update(contact);
}
}
}
/* Department - generated by stajanov (do not edit/delete) */
}