001    package org.hackystat.projectbrowser.page.dailyprojectdata.build;
002    
003    import java.io.Serializable;
004    import java.util.ArrayList;
005    import java.util.HashMap;
006    import java.util.List;
007    import java.util.Map;
008    import java.util.logging.Logger;
009    
010    import org.hackystat.dailyprojectdata.client.DailyProjectDataClient;
011    import org.hackystat.dailyprojectdata.client.DailyProjectDataClientException;
012    import org.hackystat.dailyprojectdata.resource.build.jaxb.MemberData;
013    import org.hackystat.dailyprojectdata.resource.build.jaxb.BuildDailyProjectData;
014    import org.hackystat.projectbrowser.ProjectBrowserApplication;
015    import org.hackystat.projectbrowser.ProjectBrowserSession;
016    import org.hackystat.projectbrowser.page.dailyprojectdata.DailyProjectDataSession;
017    import org.hackystat.sensorbase.resource.projects.jaxb.Project;
018    import org.hackystat.utilities.tstamp.Tstamp;
019    
020    /**
021     * The data model for Build DPD display.  This data model accommodates multiple Projects.
022     * For each project, the data model indicates the number of classes whose method-level percentage
023     * falls into each of five buckets, from 0-20% to 80-100%.
024     * @author Philip Johnson
025     * @author Shaoxuan Zhang
026     *
027     */
028    public class BuildDataModel implements Serializable {
029    
030      /** Support serialization. */
031      private static final long serialVersionUID = 1L;
032      
033      /** Holds the build data, organized by Project.*/
034      private Map<Project, BuildData> buildDataMap = new HashMap<Project, BuildData>();
035      
036      /**
037       * The default BuildDataModel, which contains no build information.
038       */
039      public BuildDataModel() {
040        // Do nothing
041      }
042      
043      /**
044       * Updates this data model to reflect the build information associated with the selected 
045       * projects.
046       */
047      public void update() {
048        this.clear();
049        DailyProjectDataClient dpdClient = ProjectBrowserSession.get().getDailyProjectDataClient();
050        DailyProjectDataSession session = ProjectBrowserSession.get().getDailyProjectDataSession();
051        List<Project> projects = session.getSelectedProjects();
052        
053        for (Project project : projects) {
054          Logger logger = ((ProjectBrowserApplication)ProjectBrowserApplication.get()).getLogger();
055          logger.fine("Getting Build DPD for project: " + project.getName());
056          try {
057            BuildDailyProjectData classData = dpdClient.getBuild(project.getOwner(),
058                project.getName(), Tstamp.makeTimestamp(session.getDate().getTime()));
059            logger.fine("Finished getting Build DPD for project: " + project.getName());
060            // Create a BuildData instance for this project.
061            BuildData buildData = this.getBuildData(project);
062            for (MemberData data : classData.getMemberData()) {
063              buildData.addEntry(data);
064            }
065          }
066          catch (DailyProjectDataClientException e) {
067            session.setFeedback("Exception when getting build DPD for project " + project + ": " +
068                e.getMessage());
069          }
070        }
071      }
072      
073      
074      /**
075       * Sets this model to its empty state. 
076       */
077      public void clear() {
078        this.buildDataMap.clear();
079      }
080      
081    
082      /**
083       * Returns true if this data model contains no information.
084       * Used to figure out if the associated panel should be visible. 
085       * @return True if the data model is empty. 
086       */
087      public boolean isEmpty() {
088        return this.buildDataMap.isEmpty();
089      }
090      
091      /**
092       * Return the BuildData instance associated with the specified project.
093       * Creates and returns a new BuildData instance if one is not yet present.
094       * @param project The project. 
095       * @return The BuildData instance for this project.  
096       */
097      public BuildData getBuildData(Project project) {
098        if (!buildDataMap.containsKey(project)) {
099          buildDataMap.put(project, new BuildData(project));
100        }
101        return buildDataMap.get(project);
102      }
103      
104      /**
105       * Returns the list of BuildData instances, needed for markup.
106       * @return The list of BuildData instances. 
107       */
108      public List<BuildData> getBuildDataList() {
109        return new ArrayList<BuildData>(this.buildDataMap.values());
110      }
111      
112    }