001: package com.technoetic.xplanner.domain.repository;
002:
003: import java.util.*;
004: import java.util.Date;
005:
006: import com.technoetic.xplanner.domain.*;
007: import com.technoetic.xplanner.security.AuthenticationException;
008: import com.technoetic.xplanner.security.auth.Authorizer;
009: import com.technoetic.xplanner.db.hibernate.ThreadSession;
010: import net.sf.hibernate.*;
011: import org.apache.log4j.Logger;
012:
013: //TODO Should be turn into a Query object
014: //TODO Create a spring context including IterationRepository and others for every web session (logged in user)
015: public class IterationRepository {
016: private static Logger log = Logger
017: .getLogger(IterationRepository.class);
018: public static final String EDITABLE_ITERATIONS_QUERY_STRING = "select i from "
019: + Iteration.class.getName()
020: + " i, "
021: + Project.class.getName()
022: + " p "
023: + "where p.hidden = false and i.projectId = p.id order by p.name, i.startDate";
024: private Session session;
025: private int loggedInUser;
026: private Authorizer authorizer;
027:
028: public IterationRepository(Session session, Authorizer authorizer,
029: int loggedInUser) {
030: this .session = session;
031: this .loggedInUser = loggedInUser;
032: this .authorizer = authorizer;
033: }
034:
035: private boolean canUserEditIteration(Iteration iteration)
036: throws AuthenticationException {
037: return authorizer.hasPermission(iteration.getProjectId(),
038: loggedInUser, iteration, "edit");
039: }
040:
041: public List fetchEditableIterations() throws HibernateException,
042: AuthenticationException {
043: List allIterations = getSession()
044: .getNamedQuery(
045: "com.technoetic.xplanner.domain.GetEditableIterationQuery")
046: .list();
047: List acceptedIterations = new ArrayList(allIterations.size());
048: for (int i = 0; i < allIterations.size(); i++) {
049: Iteration it = (Iteration) allIterations.get(i);
050: if (canUserEditIteration(it)) {
051: acceptedIterations.add(it);
052: }
053: }
054: return acceptedIterations;
055: }
056:
057: public List fetchEditableIterations(int projectId,
058: Date startingAfter) throws HibernateException,
059: AuthenticationException {
060: List allEditableIterations = fetchEditableIterations();
061: List acceptedIterations = new ArrayList(allEditableIterations
062: .size());
063: for (int i = 0; i < allEditableIterations.size(); i++) {
064: Iteration it = (Iteration) allEditableIterations.get(i);
065: if (it.getProjectId() == projectId
066: && it.getStartDate().after(startingAfter))
067: acceptedIterations.add(it);
068: }
069: return acceptedIterations;
070: }
071:
072: protected Session getSession() {
073: return session;
074: }
075:
076: public Iteration getIterationForStory(UserStory story)
077: throws HibernateException {
078: return getIteration(story.getIterationId());
079: }
080:
081: public Iteration getIteration(int iterationId)
082: throws HibernateException {
083: return (Iteration) session.load(Iteration.class, new Integer(
084: iterationId));
085: }
086:
087: public static Iteration getCurrentIteration(int projectId) {
088: java.sql.Date now = new java.sql.Date(System
089: .currentTimeMillis());
090: try {
091: Query getCurrentIterationQuery = ThreadSession
092: .get()
093: .getNamedQuery(
094: "com.technoetic.xplanner.domain.GetCurrentIterationQuery");
095: getCurrentIterationQuery.setParameter(0, new Integer(
096: projectId), Hibernate.INTEGER);
097: getCurrentIterationQuery.setParameter(1, now,
098: Hibernate.DATE);
099: List results = getCurrentIterationQuery.list();
100: return (results.size() > 0) ? (Iteration) results.get(0)
101: : null;
102: } catch (Exception e) {
103: log.error("error", e);
104: }
105: return null;
106: }
107: }
|