Clover Coverage Report - itracker
Coverage timestamp: Tue May 1 2012 16:42:12 CEST
91   235   28   9.1
18   169   0.31   10
10     2.8  
1    
 
 
  UserDAOImpl       Line # 27 91 28 0% 0.0
 
No Tests
 
1    package org.itracker.persistence.dao;
2   
3    import java.util.ArrayList;
4    import java.util.Arrays;
5    import java.util.Collection;
6    import java.util.HashMap;
7    import java.util.HashSet;
8    import java.util.Iterator;
9    import java.util.List;
10    import java.util.Map;
11    import java.util.Set;
12   
13    import org.hibernate.Criteria;
14    import org.hibernate.HibernateException;
15    import org.hibernate.Query;
16    import org.hibernate.criterion.DetachedCriteria;
17    import org.hibernate.criterion.Expression;
18    import org.hibernate.criterion.Order;
19    import org.hibernate.criterion.Restrictions;
20    import org.itracker.model.Permission;
21    import org.itracker.model.PermissionType;
22    import org.itracker.model.User;
23   
24    /**
25    *
26    */
 
27    public class UserDAOImpl extends BaseHibernateDAOImpl<User> implements UserDAO {
28   
 
29  0 toggle public User findByPrimaryKey(Integer userId) {
30  0 User user;
31   
32  0 try {
33  0 user = (User)getSession().get(User.class, userId);
34    } catch (HibernateException ex) {
35  0 throw convertHibernateAccessException(ex);
36    }
37  0 return user;
38    }
39   
 
40  0 toggle public User findByLogin(String login) {
41  0 User user;
42   
43  0 try {
44  0 Query query = getSession().getNamedQuery("UserByLoginQuery");
45  0 query.setString("login", login);
46  0 user = (User)query.uniqueResult();
47    } catch (HibernateException ex) {
48  0 throw convertHibernateAccessException(ex);
49    }
50  0 return user;
51    }
52   
 
53  0 toggle @SuppressWarnings("unchecked")
54    public List<User> findAll() {
55  0 List<User> users;
56   
57  0 try {
58  0 Query query = getSession().getNamedQuery("UsersAllQuery");
59  0 users = query.list();
60    } catch (HibernateException ex) {
61  0 throw convertHibernateAccessException(ex);
62    }
63  0 return users;
64    }
65   
 
66  0 toggle @SuppressWarnings("unchecked")
67    public List<User> findActive() {
68  0 List<User> users;
69   
70  0 try {
71  0 Query query = getSession().getNamedQuery("UsersActiveQuery");
72  0 users = query.list();
73    } catch (HibernateException ex) {
74  0 throw convertHibernateAccessException(ex);
75    }
76  0 return users;
77    }
78   
 
79  0 toggle @SuppressWarnings("unchecked")
80    public List<User> findByStatus(int status) {
81  0 List<User> users;
82   
83  0 try {
84  0 Query query = getSession().getNamedQuery("UsersByStatusQuery");
85  0 query.setInteger("userStatus", status);
86  0 users = query.list();
87    } catch (HibernateException ex) {
88  0 throw convertHibernateAccessException(ex);
89    }
90  0 return users;
91    }
92   
 
93  0 toggle @SuppressWarnings("unchecked")
94    public List<User> findSuperUsers() {
95  0 List<User> users;
96   
97  0 try {
98  0 Query query = getSession().getNamedQuery("UsersSuperQuery");
99  0 users = query.list();
100    } catch (HibernateException ex) {
101  0 throw convertHibernateAccessException(ex);
102    }
103  0 return users;
104    }
105   
 
106  0 toggle @SuppressWarnings("unchecked")
107    public List<User> findByRegistrationType(int registrationType) {
108  0 List<User> users;
109   
110  0 try {
111  0 Query query = getSession().getNamedQuery("UsersByRegistrationTypeQuery");
112  0 query.setInteger("registrationType", registrationType);
113  0 users = query.list();
114    } catch (HibernateException ex) {
115  0 throw convertHibernateAccessException(ex);
116    }
117  0 return users;
118    }
119   
120    /**
121    * Searches all permissions for the given user and sorts it by project. The
122    * <code>HashMap</code> returned has the project ids as key (<code>Integer</code>)
123    * and a <code>HashSet</code> as values. The <code>HashSet</code> holds a set of
124    * string representation of the permission
125    *
126    * @param user The user of interest
127    * @param requestSource requested by
128    * @return HashMap of permission keyed by project ids
129    */
 
130  0 toggle @SuppressWarnings("unchecked")
131    public Map<Integer, Set<PermissionType>> getUsersMapOfProjectsAndPermissionTypes(User user) {
132   
133    // create hashMap to hold permission by project id as key
134  0 final Map<Integer, Set<PermissionType>> permissionsByProjectId =
135    new HashMap<Integer, Set<PermissionType>>();
136   
137  0 try {
138    // load user bean
139  0 User userBean = (User) getSession().load(User.class, user.getId());
140    // create criteria
141  0 Criteria criteria = getSession().createCriteria(Permission.class);
142  0 criteria.add( Expression.eq ("user" , userBean) );
143  0 criteria.addOrder( Order.asc( "project" ));
144    // perform search
145  0 List<Permission> permissionsList = criteria.list();
146   
147  0 for (int i = 0; i < permissionsList.size(); i++) {
148  0 Permission permission = permissionsList.get(i);
149   
150    // Super user has access to all projects, which is indicated by the "null" project.
151  0 final Integer projectId = (permission.getProject() == null)
152    ? null : permission.getProject().getId();
153   
154  0 Set<PermissionType> projectPermissions = permissionsByProjectId.get(projectId);
155   
156  0 if (projectPermissions == null) {
157    // First permission for the project.
158  0 projectPermissions = new HashSet<PermissionType>();
159  0 permissionsByProjectId.put(projectId, projectPermissions);
160    } //else { // Add the permission to the existing set of permissions for the project. }
161   
162  0 PermissionType permissionType = PermissionType.fromCode(permission.getPermissionType());
163  0 projectPermissions.add(permissionType);
164    }
165    } catch (HibernateException ex) {
166  0 throw convertHibernateAccessException(ex);
167    }
168  0 return permissionsByProjectId;
169    }
170   
 
171  0 toggle @SuppressWarnings("unchecked")
172    public List<User> findUsersForProjectByAllPermissionTypeList(Integer projectID, Integer[] permissionTypes) {
173   
174  0 List<User> users = new ArrayList<User>();
175   
176  0 try {
177   
178  0 DetachedCriteria userCriteria = DetachedCriteria.forClass(User.class);
179  0 userCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
180  0 DetachedCriteria permissionCriteria = userCriteria.createCriteria("permissions");
181  0 permissionCriteria.add(Restrictions.in("permissionType", permissionTypes));
182  0 permissionCriteria.add(Restrictions.eq("project.id", projectID));
183   
184  0 List<User> userList = userCriteria.getExecutableCriteria(getSession()).list();
185   
186  0 for (User user : userList) {
187  0 if( isSamePermission( user.getPermissions(), permissionTypes)) {
188  0 users.add(user);
189    }
190    }
191   
192    } catch (HibernateException ex) {
193  0 throw convertHibernateAccessException(ex);
194    }
195   
196  0 return users;
197   
198    }
199   
 
200  0 toggle private boolean isSamePermission(Collection<Permission> permissions, Integer[] permissionTypes) {
201   
202  0 boolean retVal = true;
203   
204  0 if( permissions.size() != permissionTypes.length ) {
205  0 return false;
206    }
207   
208  0 Iterator<Permission> permsIt = permissions.iterator();
209  0 while (permsIt.hasNext()) {
210   
211  0 boolean found = false;
212  0 Permission permission = permsIt.next();
213   
214   
215  0 Iterator<Integer> pTypesIt = Arrays.asList(permissionTypes).iterator();
216  0 while (pTypesIt.hasNext()) {
217   
218  0 if (pTypesIt.next().equals(permission.getPermissionType())) {
219  0 found = true;
220  0 break;
221    }
222    }
223   
224  0 if( !found ) {
225  0 retVal = false;
226  0 break;
227    }
228   
229    }
230   
231  0 return retVal;
232   
233    }
234   
235    }