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

Java tutorial

Introduction

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

    private UserLevel userLevel;

    @Autowired
    private IUserLevelDAO userLevelDAO;
    @Autowired
    private IUserDAO userDAO;

    @Override
    @Transactional
    public void confirmDelete(UserLevel userLevel) {
        try {
            userLevelDAO.remove(userLevel.getId());
        } catch (InstanceNotFoundException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public UserLevel getUserLevel() {
        return userLevel;
    }

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

    @Override
    @Transactional
    public void save(UserLevel ul) {
        int l = ul.getLevel();
        if (l < UserLevel.HIGHESTLEVEL)
            l = UserLevel.HIGHESTLEVEL;
        if (l > UserLevel.LOWESTLEVEL)
            l = UserLevel.LOWESTLEVEL;
        ul.setLevel(l);
        userLevelDAO.save(ul);

        userLevelDAO.flush();
    }

    private void validate(UserLevel userLevel) {
    }

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

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

    private UserLevel getFromDB(UserLevel userLevel) {
        if (!userLevel.isNewObject()) {
            try {
                return userLevelDAO.find(userLevel.getId());
            } catch (InstanceNotFoundException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        return userLevel;
    }

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

    @Override
    @Transactional
    public UserLevel findUserLevel(User user, LocalDate date) {
        return userLevelDAO.findByUser(user, date);
    }

    @Override
    @Transactional
    public List<UserLevel> getLevelHistories(User user) {
        return userLevelDAO.getLevelHistories(user);
    }
}