01: package com.technoetic.xplanner.domain.repository;
02:
03: import java.util.ArrayList;
04: import java.util.Date;
05: import java.util.List;
06:
07: import net.sf.hibernate.HibernateException;
08: import net.sf.hibernate.Query;
09: import net.sf.hibernate.Session;
10:
11: import com.technoetic.xplanner.domain.Iteration;
12: import com.technoetic.xplanner.domain.UserStory;
13: import com.technoetic.xplanner.security.AuthenticationException;
14: import com.technoetic.xplanner.security.auth.Authorizer;
15: import com.technoetic.xplanner.util.TimeGenerator;
16:
17: //DOORDIE Test THIS!
18:
19: public class UserStoryRepository {
20: private Session session;
21: private int personId;
22: private Authorizer authorizer;
23: private IterationRepository iterationRepository;
24: protected static final String USER_STORY_WE_CAN_MOVE_TASKS_TO_QUERY = "com.technoetic.xplanner.domain.StoriesOfCurrentAndFutureIterationOfAllVisibleProjects";
25:
26: public UserStoryRepository(Session session, Authorizer authorizer,
27: int personId) {
28: this .session = session;
29: this .personId = personId;
30: this .authorizer = authorizer;
31: iterationRepository = new IterationRepository(session,
32: authorizer, personId);
33: }
34:
35: public Iteration getIteration(UserStory story)
36: throws HibernateException {
37: return iterationRepository.getIterationForStory(story);
38: }
39:
40: public List fetchStoriesWeCanMoveTasksTo(int actualStoryId)
41: throws HibernateException, AuthenticationException {
42: TimeGenerator timeGenerator = new TimeGenerator();
43: Date currentDate = timeGenerator.getTodaysMidnight();
44: Query query = getSession().getNamedQuery(
45: USER_STORY_WE_CAN_MOVE_TASKS_TO_QUERY);
46: query.setParameter("currentDate", currentDate);
47: query.setParameter("actualStoryId", new Integer(actualStoryId));
48: List allStories = query.list();
49: List acceptedStories = new ArrayList();
50: for (int i = 0; i < allStories.size(); i++) {
51: UserStory it = (UserStory) allStories.get(i);
52: if (accept(it)) {
53: acceptedStories.add(it);
54: }
55: }
56: return acceptedStories;
57: }
58:
59: public UserStory getUserStory(int storyId)
60: throws HibernateException {
61: return (UserStory) session.get(UserStory.class, new Integer(
62: storyId));
63: }
64:
65: private boolean accept(UserStory story)
66: throws AuthenticationException, HibernateException {
67: return authorizer.hasPermission(getIteration(story)
68: .getProjectId(), personId, story, "edit");
69: }
70:
71: protected Session getSession() {
72: return session;
73: }
74:
75: }
|