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

Java tutorial

Introduction

Here is the source code for org.libreplan.web.users.hierarchy.UserSuperiorModel.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.users.daos.IUserDAO;
import org.libreplan.business.users.daos.IUserSuperiorDAO;
import org.libreplan.business.users.entities.User;
import org.libreplan.business.users.entities.UserSuperior;
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 UserSuperior}
 * 
 * @author Dergen Lee
 */
@Service
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@OnConcurrentModification(goToPage = "/userSuperior/superior.zul")
public class UserSuperiorModel implements IUserSuperiorModel {

    private UserSuperior superior;

    @Autowired
    private IUserSuperiorDAO userSuperiorDAO;
    @Autowired
    private IUserDAO userDAO;
    @Autowired
    private IDepartmentDAO departmentDAO;

    @Override
    @Transactional
    public void confirmDelete(UserSuperior superior) {
        try {
            userSuperiorDAO.remove(superior.getId());
        } catch (InstanceNotFoundException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public UserSuperior getUserSuperior() {
        return superior;
    }

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

    @Override
    @Transactional
    public void save(UserSuperior uh) {
        validate(superior);
        userSuperiorDAO.save(uh);
        userSuperiorDAO.flush();
    }

    private void validate(UserSuperior superior) {
        User sup = superior.getSuperior();
        if (sup == null) {
            throw new ValidationException(
                    invalidValue(_("Superior cannot be empty"), "name", superior.getId(), superior));
        }
        User user = superior.getUser();
        if (userSuperiorDAO.isSuperior(user, sup, superior.getFromDate())) {
            throw new ValidationException(invalidValue(_("{0} is superior of {1}, cannot be subordinate of {1}",
                    user.getFullName(), sup.getFullName()), "superior", sup.getId(), superior));
        }
    }

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

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

    private UserSuperior getFromDB(UserSuperior superior) {
        if (!superior.isNewObject()) {
            try {
                return userSuperiorDAO.find(superior.getId());
            } catch (InstanceNotFoundException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        return superior;
    }

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

    @Override
    @Transactional
    public UserSuperior findUserSuperior(User user, LocalDate date) {
        return userSuperiorDAO.findByUser(user, date);
    }

    @Override
    @Transactional
    public List<UserSuperior> getSuperiorHistories(User user) {
        return userSuperiorDAO.getSuperiorHistories(user);
    }
}