org.libreplan.web.users.hierarchy.UserDepartmentModel.java Source code

Java tutorial

Introduction

Here is the source code for org.libreplan.web.users.hierarchy.UserDepartmentModel.java

Source

/*
 * This file is part of LibrePlan
 *
 * Copyright (C) 2009-2010 Fundacin para o Fomento da Calidade Industrial e
 *                         Desenvolvemento Tecnolxico de Galicia
 * Copyright (C) 2010-2012 Igalia, S.L.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.libreplan.web.users.hierarchy;

import static org.libreplan.web.I18nHelper._;
import static org.libreplan.business.common.exceptions.ValidationException.invalidValue;

import java.util.List;

import org.apache.commons.lang.Validate;
import org.joda.time.LocalDate;
import org.libreplan.business.common.exceptions.InstanceNotFoundException;
import org.libreplan.business.common.exceptions.ValidationException;
import org.libreplan.business.departments.daos.IDepartmentDAO;
import org.libreplan.business.departments.entities.Department;
import org.libreplan.business.users.daos.IUserDAO;
import org.libreplan.business.users.daos.IUserDepartmentDAO;
import org.libreplan.business.users.entities.User;
import org.libreplan.business.users.entities.UserDepartment;
import org.libreplan.web.common.concurrentdetection.OnConcurrentModification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/*
 * Model for  UI operations related to {@link UserDepartment}
 * 
 * @author Dergen Lee
 */
@Service
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@OnConcurrentModification(goToPage = "/userDepartment/dept.zul")
public class UserDepartmentModel implements IUserDepartmentModel {

    private UserDepartment dept;

    @Autowired
    private IUserDepartmentDAO userDepartmentDAO;
    @Autowired
    private IUserDAO userDAO;
    @Autowired
    private IDepartmentDAO departmentDAO;

    @Override
    @Transactional
    public void confirmDelete(UserDepartment dept) {
        try {
            userDepartmentDAO.remove(dept.getId());
        } catch (InstanceNotFoundException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public UserDepartment getUserDepartment() {
        return dept;
    }

    @Override
    @Transactional
    public void confirmSave() throws ValidationException {
        save(dept);
    }

    @Override
    @Transactional
    public void save(UserDepartment ud) {
        validate(dept);
        userDepartmentDAO.save(ud);
        userDepartmentDAO.flush();
    }

    private void validate(UserDepartment udept) {
        Department dept = udept.getDept();
        if (dept == null) {
            throw new ValidationException(
                    invalidValue(_("Department cannot be empty"), "department", udept.getId(), udept));
        }
    }

    @Override
    public void initCreate(User user) {
        dept = UserDepartment.create();
        dept.setUser(user);
        dept.setFromDate(LocalDate.now());
    }

    @Override
    @Transactional(readOnly = true)
    public void initEdit(UserDepartment dept) {
        Validate.notNull(dept);
        this.dept = getFromDB(dept);
    }

    private UserDepartment getFromDB(UserDepartment dept) {
        if (!dept.isNewObject()) {
            try {
                return userDepartmentDAO.find(dept.getId());
            } catch (InstanceNotFoundException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        return dept;
    }

    /*
     * @Override
     * 
     * @Transactional(readOnly = true) public List<UserDepartment>
     * getUserHierachies() { return userDepartmentDAO.list(UserDepartment.class);
     * }
     */
    @Override
    public UserDepartment findUserDepartment(User user) {
        return findUserDepartment(user, LocalDate.now());
    }

    @Override
    @Transactional
    public UserDepartment findUserDepartment(User user, LocalDate date) {
        return userDepartmentDAO.findByUser(user, date);
    }

    @Override
    @Transactional
    public List<UserDepartment> getDepartmentHistories(User user) {
        return userDepartmentDAO.getDepartmentHistories(user);
    }
}