001: /**
002: *
003: */package org.emforge.projectmanager.dao;
004:
005: import java.util.Collection;
006: import java.util.List;
007:
008: import org.acegisecurity.userdetails.UserDetails;
009: import org.emforge.projectmanager.base.ProjectDO;
010: import org.emforge.projectmanager.base.ProjectUserRole;
011: import org.hibernate.criterion.DetachedCriteria;
012: import org.hibernate.criterion.Expression;
013: import org.hibernate.criterion.Projections;
014: import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
015:
016: import ru.emdev.EmForge.security.dao.Role;
017:
018: /**
019: */
020: public class ProjectDAO extends HibernateDaoSupport {
021: @SuppressWarnings("unchecked")
022: public Collection<ProjectDO> getProjects() {
023: return getHibernateTemplate().loadAll(ProjectDO.class);
024: }
025:
026: public ProjectDO getProjectById(Long id) {
027: return (ProjectDO) getHibernateTemplate().get(ProjectDO.class,
028: id);
029: }
030:
031: @SuppressWarnings("unchecked")
032: public ProjectDO getProjectByName(String i_name) {
033: List<ProjectDO> projects = getHibernateTemplate()
034: .findByNamedQuery("Project.findProjectByName", i_name);
035:
036: if (projects.size() == 0) {
037: // no any project with specified name found
038: return null;
039: } else if (projects.size() > 0) {
040: //@todo Later we will check for multiple projects selected
041: return projects.get(0);
042: }
043:
044: return null;
045: }
046:
047: public ProjectDO saveProject(ProjectDO project) {
048: getHibernateTemplate().saveOrUpdate(project);
049: return project;
050: }
051:
052: public void deleteProject(ProjectDO project) {
053: getHibernateTemplate().delete(project);
054: }
055:
056: public void addUser(ProjectDO i_project, UserDetails i_user,
057: Role i_role) {
058: ProjectUserRole projectUserRole = new ProjectUserRole();
059:
060: if (!hasRole(i_project, i_user, i_role)) {
061: projectUserRole.setProject(i_project);
062: projectUserRole.setName(i_user.getUsername());
063: projectUserRole.setRole(i_role);
064:
065: getHibernateTemplate().save(projectUserRole);
066: }
067: }
068:
069: public void deleteUserRole(ProjectDO i_project, UserDetails i_user,
070: Role i_role) {
071: ProjectUserRole projectUserRole = getProjectUserRole(i_project,
072: i_user, i_role);
073:
074: if (projectUserRole != null) {
075: getHibernateTemplate().delete(projectUserRole);
076: }
077: }
078:
079: @SuppressWarnings("unchecked")
080: public Collection<ProjectUserRole> getUsers(ProjectDO i_project,
081: Role i_role) {
082: DetachedCriteria criteria = DetachedCriteria
083: .forClass(ProjectUserRole.class);
084: criteria.add(Expression.eq("project", i_project));
085: criteria.add(Expression.eq("role", i_role));
086:
087: return getHibernateTemplate().findByCriteria(criteria);
088: }
089:
090: @SuppressWarnings("unchecked")
091: public Collection<String> getAllProjectUsers(ProjectDO i_project) {
092: DetachedCriteria criteria = DetachedCriteria
093: .forClass(ProjectUserRole.class);
094: criteria.add(Expression.eq("project", i_project));
095: // @todo - Not very good - since it is got all entries - and only then filtered them.
096: criteria.setProjection(Projections.groupProperty("userName"));
097: return getHibernateTemplate().findByCriteria(criteria);
098: }
099:
100: public boolean hasRole(ProjectDO i_project, UserDetails i_user,
101: Role i_role) {
102: ProjectUserRole projectUserRole = getProjectUserRole(i_project,
103: i_user, i_role);
104:
105: return projectUserRole != null;
106: }
107:
108: @SuppressWarnings("unchecked")
109: protected ProjectUserRole getProjectUserRole(ProjectDO i_project,
110: UserDetails i_user, Role i_role) {
111: DetachedCriteria criteria = DetachedCriteria
112: .forClass(ProjectUserRole.class);
113: criteria.add(Expression.eq("project", i_project));
114: criteria.add(Expression.eq("userName", i_user.getUsername()));
115: criteria.add(Expression.eq("role", i_role));
116:
117: List<ProjectUserRole> result = getHibernateTemplate()
118: .findByCriteria(criteria);
119:
120: if (result.size() == 1) {
121: return result.get(0);
122: }
123:
124: return null;
125: }
126:
127: @SuppressWarnings("unchecked")
128: public Collection<Role> getAvialableRoles(ProjectDO i_project) {
129: List<Role> projects = getHibernateTemplate().findByNamedQuery(
130: "Project.findProjectRoles", i_project.getId());
131: return projects;
132: }
133: }
|