001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package info.jtrac.domain;
018:
019: import java.io.Serializable;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: /**
024: * Object that holds statistics for items within a single space
025: * a map of these would serve as the model for the dashboard view
026: * contains logic for totalling etc.
027: */
028: public class Counts implements Serializable {
029:
030: public static final int ASSIGNED_TO_ME = 1;
031: public static final int LOGGED_BY_ME = 2;
032: public static final int TOTAL = 3;
033:
034: private Map<Integer, Map<Integer, Long>> typeCounts = new HashMap<Integer, Map<Integer, Long>>();
035:
036: private boolean detailed;
037:
038: public boolean isDetailed() {
039: return detailed;
040: }
041:
042: public Counts(boolean detailed) {
043: this .detailed = detailed;
044: for (int i = 1; i < 4; i++) {
045: typeCounts.put(i, new HashMap<Integer, Long>());
046: }
047: }
048:
049: public void addLoggedByMe(int state, long count) {
050: add(LOGGED_BY_ME, state, count);
051: }
052:
053: public void addAssignedToMe(int state, long count) {
054: add(ASSIGNED_TO_ME, state, count);
055: }
056:
057: public void addTotal(int state, long count) {
058: add(TOTAL, state, count);
059: }
060:
061: protected void add(int type, int state, long count) {
062: Map<Integer, Long> stateCounts = typeCounts.get(type);
063: Long i = stateCounts.get(state);
064: if (i == null) {
065: stateCounts.put(state, count);
066: } else {
067: stateCounts.put(state, i + count);
068: }
069: }
070:
071: protected int getTotalForType(int type) {
072: Map<Integer, Long> stateCounts = typeCounts.get(type);
073: if (stateCounts == null) {
074: return 0;
075: }
076: int total = 0;
077: for (Map.Entry<Integer, Long> entry : stateCounts.entrySet()) {
078: total += entry.getValue();
079: }
080: return total;
081: }
082:
083: public int getLoggedByMe() {
084: return getTotalForType(LOGGED_BY_ME);
085: }
086:
087: public int getAssignedToMe() {
088: return getTotalForType(ASSIGNED_TO_ME);
089: }
090:
091: public int getTotal() {
092: return getTotalForType(TOTAL);
093: }
094:
095: public Map<Integer, Long> getLoggedByMeMap() {
096: return typeCounts.get(LOGGED_BY_ME);
097: }
098:
099: public Map<Integer, Long> getAssignedToMeMap() {
100: return typeCounts.get(ASSIGNED_TO_ME);
101: }
102:
103: public Map<Integer, Long> getTotalMap() {
104: return typeCounts.get(TOTAL);
105: }
106:
107: // return string for easier rendering on dashboard screen
108: public String getLoggedByMeForState(int stateKey) {
109: Long i = typeCounts.get(LOGGED_BY_ME).get(stateKey);
110: return i == null ? "" : i.toString();
111: }
112:
113: public String getAssignedToMeForState(int stateKey) {
114: Long i = typeCounts.get(ASSIGNED_TO_ME).get(stateKey);
115: return i == null ? "" : i.toString();
116: }
117:
118: public String getTotalForState(int stateKey) {
119: Long i = typeCounts.get(TOTAL).get(stateKey);
120: return i == null ? "" : i.toString();
121: }
122:
123: }
|