podd.resources.util.view.ProjectListPopulator.java Source code

Java tutorial

Introduction

Here is the source code for podd.resources.util.view.ProjectListPopulator.java

Source

/*
 * Copyright (c) 2009 - 2010. School of Information Technology and Electrical
 * Engineering, The University of Queensland.  This software is being developed
 * for the "Phenomics Ontoogy Driven Data Management Project (PODD)" project.
 * PODD is a National e-Research Architecture Taskforce (NeAT) project
 * co-funded by ANDS and ARCS.
 *
 * PODD 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, or
 * (at your option) any later version.
 *
 * PODD 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 PODD.  If not, see <http://www.gnu.org/licenses/>.
 */

package podd.resources.util.view;

import info.aduna.collections.iterators.CloseableIterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import podd.dataaccess.EntityDAO;
import podd.dataaccess.PoddObjectDAO;
import podd.dataaccess.UserProjectRoleBundleDAO;
import podd.exception.DataAccessException;
import podd.model.project.Project;
import podd.model.user.ProjectRole;
import podd.model.user.User;
import podd.model.user.UserProjectRoleBundle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static podd.model.project.Project.ProjectPublicationStatus.PUBLISHED;
import static podd.util.common.Constants.STATE_ACTIVE;

/**
 * @author Yuan-Fang Li
 * @version $Id$
 */
public class ProjectListPopulator {
    public static final class FreemarkerProjectHelper {
        private Project project;
        private String authenticatedUserRole;

        public FreemarkerProjectHelper(Project project, String authenticatedUserRole) {
            this.project = project;
            this.authenticatedUserRole = authenticatedUserRole;
        }

        public Project getProject() {
            return project;
        }

        public String getAuthenticatedUserRole() {
            return authenticatedUserRole;
        }
    }

    private static final Logger LOGGER = LoggerFactory.getLogger(ProjectListPopulator.class);

    private EntityDAO pidHolderDao;
    private PoddObjectDAO objectDao;
    private UserProjectRoleBundleDAO projectRoleDao;

    public ProjectListPopulator(EntityDAO pidHolderDao, PoddObjectDAO objectDao,
            UserProjectRoleBundleDAO projectRoleDao) {
        this.pidHolderDao = pidHolderDao;
        this.objectDao = objectDao;
        this.projectRoleDao = projectRoleDao;
    }

    public List<ProjectListPopulator.FreemarkerProjectHelper> getUsersProjectList(User user, int max, int first)
            throws DataAccessException {
        List<ProjectListPopulator.FreemarkerProjectHelper> projectList = new ArrayList<ProjectListPopulator.FreemarkerProjectHelper>();
        for (UserProjectRoleBundle bundle : projectRoleDao.getUserProjectRole(user.getUserName(), max, first)) {
            addOneProject(projectList, bundle);
        }
        return projectList;
    }

    private void addOneProject(List<FreemarkerProjectHelper> projectList, UserProjectRoleBundle bundle) {
        try {
            Project project = (Project) objectDao.load(bundle.getProject());
            // only return active projects
            if (null != project && STATE_ACTIVE.equals(project.getState())) {
                projectList.add(new FreemarkerProjectHelper(project, bundle.getProjectRole()));
            }
        } catch (DataAccessException e) {
            LOGGER.error("Error loading project: " + bundle.getProject() + " for user: " + bundle.getUser(), e);
        }
    }

    public List<FreemarkerProjectHelper> getProjects(Project.ProjectPublicationStatus status,
            boolean includeUserProjects, User user, int max, int first) throws DataAccessException {
        // get all FreemarkerProjectHelper published projects which are not in the users' project list
        Set<Criterion> criteria = new HashSet<Criterion>();
        criteria.add(Restrictions.like("publicationStatus", status));
        // only return active projects
        criteria.add(Restrictions.eq("state", STATE_ACTIVE));
        if (!includeUserProjects) {
            for (String pid : getUserProjectPidList(user)) {
                criteria.add(Restrictions.not(Restrictions.like("pid", pid)));
            }
        }
        return getProjectList(user, pidHolderDao.getAll(Project.class, criteria, max, first));
    }

    public List<FreemarkerProjectHelper> getAllProjects(User user, int max, int first) throws DataAccessException {
        // get all FreemarkerProjectHelper projects
        return getProjectList(user, pidHolderDao.getAll(Project.class, null, max, first));
    }

    public int getProjectCount(User user) {
        return getUserProjectPidList(user).size();
    }

    public int getProjectCount() throws DataAccessException {
        return pidHolderDao.getCount(Project.class, null);
    }

    public int getProjectCount(Project.ProjectPublicationStatus status, User user, boolean includeUserProjects)
            throws DataAccessException {
        Set<Criterion> criteria = new HashSet<Criterion>();
        criteria.add(Restrictions.like("publicationStatus", status));
        criteria.add(Restrictions.eq("state", STATE_ACTIVE));
        if (!includeUserProjects) {
            for (String pid : getUserProjectPidList(user)) {
                criteria.add(Restrictions.not(Restrictions.like("pid", pid)));
            }
        }
        return pidHolderDao.getCount(Project.class, criteria);
    }

    private List<ProjectListPopulator.FreemarkerProjectHelper> getProjectList(User user,
            CloseableIterator<Project> projectIterator) throws DataAccessException {

        List<ProjectListPopulator.FreemarkerProjectHelper> projectList = new ArrayList<FreemarkerProjectHelper>();
        try {
            while (projectIterator.hasNext()) {
                Project project = projectIterator.next();
                projectList.add(new FreemarkerProjectHelper(project, getUserRole(user, project)));
            }
            return projectList;
        } finally {
            if (null != projectIterator) {
                projectIterator.close();
            }
        }
    }

    /**
     * Converts a collection of projects to FreemarkerProjects
     *
     * @param projects
     * @param user
     * @return
     */
    public List<ProjectListPopulator.FreemarkerProjectHelper> convertToFreemakerProjects(
            Collection<Project> projects, User user) {
        List<ProjectListPopulator.FreemarkerProjectHelper> freemarkerProjects = new ArrayList<ProjectListPopulator.FreemarkerProjectHelper>();
        if (projects != null) {
            try {
                for (Project project : projects) {
                    freemarkerProjects.add(
                            new ProjectListPopulator.FreemarkerProjectHelper(project, getUserRole(user, project)));
                }
            } catch (DataAccessException e) {
                LOGGER.error("Found exception", e);
            }
        }

        return freemarkerProjects;
    }

    private String getUserRole(User user, Project project) throws DataAccessException {
        // get the user role to display, set to 'Public Access' for
        //  published projects unless the user has a more specific role
        String authenticatedUserRole = "";
        if (project.getPublicationStatus().equals(PUBLISHED)) {
            authenticatedUserRole = "Public Access";
        }
        if (null != user) {
            ProjectRole role = projectRoleDao.getUserProjectRole(user.getUserName(), project.getPid());
            if (null != role) {
                authenticatedUserRole = role.toString();
            }
        }
        return authenticatedUserRole;
    }

    private List<String> getUserProjectPidList(User user) {
        ArrayList<String> userProjectList = new ArrayList<String>();
        try {
            for (UserProjectRoleBundle bundle : projectRoleDao.getUserProjectRole(user.getUserName())) {
                loadOneProject(userProjectList, bundle);
            }
        } catch (DataAccessException e) {
            LOGGER.error("Error getting project role bundle for user: " + user, e);
        }
        return userProjectList;
    }

    private void loadOneProject(ArrayList<String> userProjectList, UserProjectRoleBundle bundle) {
        try {
            Project project = (Project) objectDao.load(bundle.getProject());
            // only return active projects
            if (null != project && STATE_ACTIVE.equals(project.getState())) {
                userProjectList.add(project.getPid());
            }
        } catch (DataAccessException e) {
            LOGGER.error("Error loading project: " + bundle.getProject() + " for user: " + bundle.getUser(), e);
        }
    }
}