001: package com.technoetic.xplanner.tags;
002:
003: import com.technoetic.xplanner.db.hibernate.ThreadSession;
004: import com.technoetic.xplanner.db.IdSearchHelper;
005: import com.technoetic.xplanner.domain.*;
006: import net.sf.hibernate.Hibernate;
007: import net.sf.hibernate.Session;
008: import net.sf.hibernate.HibernateException;
009:
010: import javax.servlet.ServletRequest;
011: import java.util.List;
012:
013: import org.apache.struts.action.ActionMapping;
014:
015: // todo - Get rid of this once we have a true object model with relationships
016:
017: public class DomainContext {
018: public static final String REQUEST_KEY = "domainContext";
019:
020: private String projectName;
021: private int projectId;
022: private String iterationName;
023: private int iterationId;
024: private String storyName;
025: private int storyId;
026: private String taskName;
027: private int taskId;
028: private String featureName;
029: private int featureId;
030: private Object targetObject;
031: private ActionMapping actionMapping;
032: public static IdSearchHelper idSearchHelper = new IdSearchHelper();
033:
034: public ActionMapping getActionMapping() {
035: return actionMapping;
036: }
037:
038: public void setActionMapping(ActionMapping actionMapping) {
039: this .actionMapping = actionMapping;
040: }
041:
042: public String getProjectName() {
043: return projectName;
044: }
045:
046: public void setProjectName(String projectName) {
047: this .projectName = projectName;
048: }
049:
050: public int getProjectId() {
051: return projectId;
052: }
053:
054: public void setProjectId(int projectId) {
055: this .projectId = projectId;
056: }
057:
058: public String getIterationName() {
059: return iterationName;
060: }
061:
062: public void setIterationName(String iterationName) {
063: this .iterationName = iterationName;
064: }
065:
066: public int getIterationId() {
067: return iterationId;
068: }
069:
070: public void setIterationId(int iterationId) {
071: this .iterationId = iterationId;
072: }
073:
074: public String getStoryName() {
075: return storyName;
076: }
077:
078: public void setStoryName(String storyName) {
079: this .storyName = storyName;
080: }
081:
082: public int getStoryId() {
083: return storyId;
084: }
085:
086: public void setStoryId(int storyId) {
087: this .storyId = storyId;
088: }
089:
090: public String getTaskName() {
091: return taskName;
092: }
093:
094: public void setTaskName(String taskName) {
095: this .taskName = taskName;
096: }
097:
098: public int getTaskId() {
099: return taskId;
100: }
101:
102: public String getFeatureName() {
103: return featureName;
104: }
105:
106: public void setFeatureName(String featureName) {
107: this .featureName = featureName;
108: }
109:
110: public int getFeatureId() {
111: return featureId;
112: }
113:
114: public void setFeatureId(int featureId) {
115: this .featureId = featureId;
116: }
117:
118: public void setTaskId(int taskId) {
119: this .taskId = taskId;
120: }
121:
122: public Object getTargetObject() {
123: return targetObject;
124: }
125:
126: public void populate(Object object) throws Exception {
127: Session session = ThreadSession.get();
128: targetObject = object;
129: if (object instanceof Project) {
130: populate((Project) object);
131: } else if (object instanceof Iteration) {
132: populate(session, (Iteration) object);
133: } else if (object instanceof UserStory) {
134: populate(session, (UserStory) object);
135: } else if (object instanceof Task) {
136: populate(session, (Task) object);
137: } else if (object instanceof Feature) {
138: populate(session, (Feature) object);
139: } else if (object instanceof Note) {
140: populate((Note) object);
141: } else if (object instanceof TimeEntry) {
142: populate(session, (TimeEntry) object);
143: }
144: }
145:
146: private void populate(Project project) throws Exception {
147: setProjectName(project.getName());
148: setProjectId(project.getId());
149: }
150:
151: private void populate(Session session, Iteration iteration)
152: throws Exception {
153: Project project = (Project) session.load(Project.class,
154: new Integer(iteration.getProjectId()));
155: populate(project);
156: setIterationName(iteration.getName());
157: setIterationId(iteration.getId());
158: }
159:
160: private void populate(Session session, UserStory story)
161: throws Exception {
162: List results = session
163: .find(
164: "select p.name, p.id, i.name, i.id from "
165: + "from p in class com.technoetic.xplanner.domain.Project, "
166: + "i in class com.technoetic.xplanner.domain.Iteration, "
167: + "s in class com.technoetic.xplanner.domain.UserStory "
168: + "where s.id = ? and s.iterationId = i.id and i.projectId = p.id",
169: new Integer(story.getId()), Hibernate.INTEGER);
170: if (results.size() > 0) {
171: Object[] result = (Object[]) results.iterator().next();
172: setProjectName((String) result[0]);
173: setProjectId(((Integer) result[1]).intValue());
174: setIterationName((String) result[2]);
175: setIterationId(((Integer) result[3]).intValue());
176: }
177: setStoryName(story.getName());
178: setStoryId(story.getId());
179: }
180:
181: private void populate(Session session, Task task) throws Exception {
182: List results = session
183: .find(
184: "select p.name, p.id, i.name, i.id, s.name, s.id "
185: + "from p in class com.technoetic.xplanner.domain.Project, "
186: + "i in class com.technoetic.xplanner.domain.Iteration, "
187: + "s in class com.technoetic.xplanner.domain.UserStory, "
188: + "t in class com.technoetic.xplanner.domain.Task "
189: + "where t.id = ? and t.story.id = s.id and s.iterationId = i.id and i.projectId = p.id",
190: new Integer(task.getId()), Hibernate.INTEGER);
191: if (results.size() > 0) {
192: Object[] result = (Object[]) results.iterator().next();
193: setProjectName((String) result[0]);
194: setProjectId(((Integer) result[1]).intValue());
195: setIterationName((String) result[2]);
196: setIterationId(((Integer) result[3]).intValue());
197: setStoryName((String) result[4]);
198: setStoryId(((Integer) result[5]).intValue());
199: }
200: setTaskName(task.getName());
201: setTaskId(task.getId());
202: }
203:
204: private void populate(Session session, TimeEntry timeEntry)
205: throws Exception {
206: List results = session
207: .find(
208: "select p.name, p.id, i.name, i.id, s.name, s.id, t.name "
209: + "from p in class com.technoetic.xplanner.domain.Project, "
210: + "i in class com.technoetic.xplanner.domain.Iteration, "
211: + "s in class com.technoetic.xplanner.domain.UserStory, "
212: + "t in class com.technoetic.xplanner.domain.Task "
213: + "where t.id = ? and t.story.id = s.id and s.iterationId = i.id and i.projectId = p.id",
214: new Integer(timeEntry.getTaskId()),
215: Hibernate.INTEGER);
216: if (results.size() > 0) {
217: Object[] result = (Object[]) results.iterator().next();
218: setProjectName((String) result[0]);
219: setProjectId(((Integer) result[1]).intValue());
220: setIterationName((String) result[2]);
221: setIterationId(((Integer) result[3]).intValue());
222: setStoryName((String) result[4]);
223: setStoryId(((Integer) result[5]).intValue());
224: setTaskName(((String) result[6]));
225: }
226: setTaskId(timeEntry.getId());
227: }
228:
229: private void populate(Session session, Feature feature)
230: throws Exception {
231: List results = session
232: .find(
233: "select p.name, p.id, i.name, i.id, s.name, s.id "
234: + "from p in class com.technoetic.xplanner.domain.Project, "
235: + "i in class com.technoetic.xplanner.domain.Iteration, "
236: + "s in class com.technoetic.xplanner.domain.UserStory, "
237: + "f in class com.technoetic.xplanner.domain.Feature "
238: + "where f.id = ? and f.story.id = s.id and s.iterationId = i.id and i.projectId = p.id",
239: new Integer(feature.getId()), Hibernate.INTEGER);
240: if (results.size() > 0) {
241: Object[] result = (Object[]) results.iterator().next();
242: setProjectName((String) result[0]);
243: setProjectId(((Integer) result[1]).intValue());
244: setIterationName((String) result[2]);
245: setIterationId(((Integer) result[3]).intValue());
246: setStoryName((String) result[4]);
247: setStoryId(((Integer) result[5]).intValue());
248: }
249: setFeatureName(feature.getName());
250: setFeatureId(feature.getId());
251: }
252:
253: private void populate(Note note) throws Exception {
254: populate(getNoteTarget(note.getAttachedToId()));
255: }
256:
257: public static DomainObject getNoteTarget(int attachedToId) {
258: try {
259: return idSearchHelper.search(attachedToId);
260: } catch (HibernateException e) {
261: return null;
262: }
263: }
264:
265: public void save(ServletRequest request) {
266: request.setAttribute(REQUEST_KEY, this );
267: }
268:
269: public static DomainContext get(ServletRequest request) {
270: return (DomainContext) request.getAttribute(REQUEST_KEY);
271: }
272: }
|