001    package org.hackystat.projectbrowser.page.dailyprojectdata.build;
002    
003    import java.io.Serializable;
004    import java.util.ArrayList;
005    import java.util.List;
006    
007    import org.hackystat.dailyprojectdata.resource.build.jaxb.MemberData;
008    import org.hackystat.projectbrowser.page.dailyprojectdata.detailspanel.DailyProjectDetailsPanel;
009    import org.hackystat.sensorbase.resource.projects.jaxb.Project;
010    
011    /**
012     * Data structure for representing build information about a single project. 
013     * This representation includes the Project, plus Buckets containing 
014     * DPD Build MemberData instances for the passing and failing instances. 
015     * Bucket 0 contains MemberData instances with passing build info, and 
016     * Bucket 1 contains MemberData instances with failing build info.  Note
017     * that the same MemberData instance can appear in both buckets. 
018     * 
019     * @author Philip Johnson
020     * @author Shaoxuan Zhang
021     */
022    public class BuildData implements Serializable {
023    
024      /** Support serialization. */
025      private static final long serialVersionUID = 1L;
026      
027      /** The project whose data is kept in this instance. */
028      private Project project;
029      
030      /** The two buckets for this data. */
031      private List<MemberData> successes = new ArrayList<MemberData>();
032      private List<MemberData> failures = new ArrayList<MemberData>();
033      
034      /** The total number of builds across all buckets. */
035      private int total = 0;
036      
037      /**
038       * Creates a new BuildData instance.  
039       * @param name The name of the Project associated with this instance. 
040       */
041      public BuildData(Project name) {
042        this.project = name;
043      }
044      
045      /**
046       * Updates this BuildData instance with information about the numbers of passes and failures
047       * for a given instance in the Project.  
048       * @param memberData The data to be added.
049       */
050      public void addEntry(MemberData memberData) {
051        if (memberData.getSuccess() > 0) {
052          this.successes.add(memberData);
053        }
054        if (memberData.getFailure() > 0) {
055          this.failures.add(memberData);
056        }
057        total += memberData.getSuccess() + memberData.getFailure();
058      }
059      
060      /**
061       * Returns the current value of the specified bucket. 
062       * @param bucket The bucket number, where 0 is successes and 1 is failures. 
063       * @return The value inside the given bucket. 
064       */
065      public int getBucketValue(int bucket) {
066        List<MemberData> dataList = ((bucket == 0) ? successes : failures);
067        int count = 0;
068        for (MemberData data : dataList) {
069          count += ((bucket == 0) ? data.getSuccess() : data.getFailure()); 
070        }
071        return count;
072      }
073      
074      /**
075       * Returns the total number of entries across all buckets. 
076       * @return The total number of entries. 
077       */
078      public int getTotal() {
079        return this.total;
080      }
081      
082      /**
083       * Returns the total number of entries across all buckets as a string. 
084       * @return The total number of entries. 
085       */
086      public String getTotalString() {
087        return String.valueOf(this.total);
088      }
089      
090      /**
091       * Returns the bucket value as a percentage of the total number of entries across all buckets.
092       * Returns zero if there are no entries. 
093       * @param bucket The bucket whose percentage is to be returned.
094       * @return The bucket as a percentage.
095       */
096      public int getBucketPercentage(int bucket) {
097        if (getTotal() == 0) {
098          return 0;
099        }
100        else {
101          double percent = (double)getBucketValue(bucket) / (double)getTotal();
102          return ((int) (percent * 100));
103        }
104      }
105        
106      /**
107       * Returns the current value of the specified bucket as a string. 
108       * @param bucket The bucket number, where 0 is the first one and 4 is the last one. 
109       * @return The value inside the given bucket. 
110       */
111      public String getBucketCountString(int bucket) {
112        return String.valueOf(getBucketValue(bucket));
113      }
114      
115      /**
116       * Returns the bucket percentage as a string.
117       * @param bucket The bucket.
118       * @return Its percentage as a string.
119       */
120      public String getBucketPercentageString(int bucket) {
121        return getBucketPercentage(bucket) + "%";
122      }
123      
124     
125      /**
126       * Return the project associated with this data. 
127       * @return The project.
128       */
129      public Project getProject() {
130        return project;
131      }
132      
133      /**
134       * Returns a details panel containing information about this bucket.
135       * @param id The wicket id for this panel.
136       * @param bucket The bucket of interest. 
137       * @param isCount True if the count should be returned, false if percentage. 
138       * @return The DailyProjectDetailsPanel instance. 
139       */
140      public DailyProjectDetailsPanel getPanel(String id, int bucket, boolean isCount) {
141        boolean displaySuccesses = (bucket == 0);
142        List<MemberData> dataList = (displaySuccesses ? successes : failures);
143        DailyProjectDetailsPanel dpdPanel = 
144          new DailyProjectDetailsPanel(id, "Build Data", 
145              ((isCount) ? this.getBucketCountString(bucket) : this.getBucketPercentageString(bucket)));
146        dpdPanel.getModalWindow().setContent(
147            new BuildDetailsPanel(dpdPanel.getModalWindow().getContentId(),
148            dataList, displaySuccesses));
149            
150        return dpdPanel;
151      }
152    }