001: package com.technoetic.xplanner.metrics;
002:
003: import com.technoetic.xplanner.db.hibernate.ThreadSession;
004: import net.sf.hibernate.Session;
005: import org.apache.log4j.Logger;
006:
007: import java.util.*;
008:
009: public class UserStoryMetrics {
010: private Logger log = Logger.getLogger(getClass());
011: private HashMap developerMetrics = new HashMap();
012: private double totalHours;
013: private int userStoryId;
014: private double maxDeveloperHours;
015:
016: public void setUserStoryId(int userStoryId) {
017: this .userStoryId = userStoryId;
018: }
019:
020: public void analyze() {
021: HashMap names = new HashMap();
022: try {
023: // Session session = GlobalSessionFactory.get().openSession();
024: Session session = ThreadSession.get();
025: try {
026: names.clear();
027: List nameResults = session.getNamedQuery("namesQuery")
028: .list();
029: Iterator iter = nameResults.iterator();
030: while (iter.hasNext()) {
031: Object[] result = (Object[]) iter.next();
032: names.put(result[1], result[0]);
033: }
034:
035: developerMetrics.clear();
036: List acceptedTasks = session.getNamedQuery(
037: "acceptedTaskInStoryQuery").setInteger(
038: "storyId", userStoryId).list();
039: Iterator acceptedTaskIter = acceptedTasks.iterator();
040: while (acceptedTaskIter.hasNext()) {
041: Object[] result = (Object[]) acceptedTaskIter
042: .next();
043: double acceptedHours = toDouble(result[2]);
044: if (acceptedHours > 0.0) {
045: getDeveloperMetrics(
046: (String) names.get(result[1]),
047: toInt(result[1]), userStoryId)
048: .setAcceptedTaskHours(acceptedHours);
049: }
050: }
051:
052: totalHours = 0.0;
053: maxDeveloperHours = 0.0;
054: List hoursResults = session.getNamedQuery(
055: "storyHoursWorkedQuery").setInteger("storyId",
056: userStoryId).list();
057: Iterator hoursIterator = hoursResults.iterator();
058: while (hoursIterator.hasNext()) {
059: Object[] result = (Object[]) hoursIterator.next();
060: int person1Id = toInt(result[0]);
061: int person2Id = toInt(result[1]);
062: Date startTime = (Date) result[2];
063: Date endTime = (Date) result[3];
064: double duration = toDouble(result[5]);
065: if ((endTime != null && startTime != null)
066: || duration != 0) {
067: double hours = duration == 0 ? (endTime
068: .getTime() - startTime.getTime()) / 3600000.0
069: : duration;
070: boolean isPaired = person1Id != 0
071: && person2Id != 0;
072: if (person1Id != 0) {
073: updateWorkedHours(userStoryId,
074: (String) names.get(result[0]),
075: person1Id, hours, isPaired);
076: totalHours += hours;
077: }
078: if (person2Id != 0) {
079: updateWorkedHours(userStoryId,
080: (String) names.get(result[1]),
081: person2Id, hours, isPaired);
082: totalHours += hours;
083: }
084: }
085: }
086: } catch (Exception ex) {
087: if (session.isConnected()) {
088: session.connection().rollback();
089: }
090: log.error("error", ex);
091: } finally {
092: session.close();
093: }
094: } catch (Exception ex) {
095: log.error("error", ex);
096: }
097: }
098:
099: private int toInt(Object object) {
100: return ((Integer) object).intValue();
101: }
102:
103: private double toDouble(Object object) {
104: return ((Double) object).doubleValue();
105: }
106:
107: private DeveloperMetrics getDeveloperMetrics(String name, int id,
108: int userStoryId) {
109: DeveloperMetrics dm = (DeveloperMetrics) developerMetrics
110: .get(name);
111: if (dm == null) {
112: dm = new DeveloperMetrics();
113: dm.setId(id);
114: dm.setName(name);
115: dm.setIterationId(userStoryId);
116: developerMetrics.put(name, dm);
117: }
118: return dm;
119: }
120:
121: private void updateWorkedHours(int userStoryId, String name,
122: int id, double hours, boolean isPaired) {
123: DeveloperMetrics dm = getDeveloperMetrics(name, id, userStoryId);
124: dm.setHours(dm.getHours() + hours);
125: if (dm.getHours() > maxDeveloperHours) {
126: maxDeveloperHours = dm.getHours();
127: }
128: if (isPaired) {
129: dm.setPairedHours(dm.getPairedHours() + hours);
130: }
131: }
132:
133: public double getTotalHours() {
134: return totalHours;
135: }
136:
137: public void setTotalHours(double totalHours) {
138: this .totalHours = totalHours;
139: }
140:
141: public Collection getDeveloperTotalTime() {
142: ArrayList metrics = new ArrayList(developerMetrics.values());
143: Collections.sort(metrics, new Comparator() {
144: public int compare(Object object1, Object object2) {
145: DeveloperMetrics dm1 = (DeveloperMetrics) object1;
146: DeveloperMetrics dm2 = (DeveloperMetrics) object2;
147: return (dm1.getHours() < dm2.getHours()) ? 1 : (dm1
148: .getHours() == dm2.getHours() ? 0 : -1);
149: }
150: });
151: return metrics;
152: }
153:
154: public double getMaxTotalTime() {
155: double maxTotalTime = 0;
156: Iterator itr = developerMetrics.values().iterator();
157: while (itr.hasNext()) {
158: double hours = ((DeveloperMetrics) itr.next()).getHours();
159: if (hours > maxTotalTime) {
160: maxTotalTime = hours;
161: }
162: }
163: return maxTotalTime;
164: }
165:
166: public Collection getDeveloperAcceptedTime() {
167: ArrayList metrics = new ArrayList(developerMetrics.values());
168: Collections.sort(metrics, new Comparator() {
169: public int compare(Object object1, Object object2) {
170: DeveloperMetrics dm1 = (DeveloperMetrics) object1;
171: DeveloperMetrics dm2 = (DeveloperMetrics) object2;
172: return (dm1.getAcceptedHours() < dm2.getAcceptedHours()) ? 1
173: : (dm1.getAcceptedHours() == dm2
174: .getAcceptedHours() ? 0 : -1);
175: }
176: });
177: return metrics;
178: }
179:
180: public double getMaxAcceptedTime() {
181: double maxAcceptedTime = 0;
182: Iterator itr = developerMetrics.values().iterator();
183: while (itr.hasNext()) {
184: double hours = ((DeveloperMetrics) itr.next())
185: .getAcceptedHours();
186: if (hours > maxAcceptedTime) {
187: maxAcceptedTime = hours;
188: }
189: }
190: return maxAcceptedTime;
191: }
192:
193: public int getUserStoryId() {
194: return userStoryId;
195: }
196:
197: }
|