001    package org.hackystat.projectbrowser.page.dailyprojectdata.unittest;
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.unittest.jaxb.MemberData;
013    import org.hackystat.dailyprojectdata.resource.unittest.jaxb.UnitTestDailyProjectData;
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 UnitTest DPD display.  This data model accommodates multiple Projects.
022     * For each project, the data model indicates the number of passing and failing unit tests.  
023     * @author Philip Johnson
024     * @author Shaoxuan Zhang
025     *
026     */
027    public class UnitTestDataModel implements Serializable {
028    
029      /** Support serialization. */
030      private static final long serialVersionUID = 1L;
031      
032      /** Holds the unittest data, organized by Project.*/
033      private Map<Project, UnitTestData> unittestDataMap = new HashMap<Project, UnitTestData>();
034      
035      /**
036       * The default UnitTestDataModel, which contains no unittest information.
037       */
038      public UnitTestDataModel() {
039        // Do nothing
040      }
041      
042      /**
043       * Updates this data model to reflect the unittest information associated with the selected 
044       * projects.
045       */
046      public void update() {
047        this.clear();
048        DailyProjectDataClient dpdClient = ProjectBrowserSession.get().getDailyProjectDataClient();
049        DailyProjectDataSession session = ProjectBrowserSession.get().getDailyProjectDataSession();
050        List<Project> projects = session.getSelectedProjects();
051        
052        for (Project project : projects) {
053          Logger logger = ((ProjectBrowserApplication)ProjectBrowserApplication.get()).getLogger();
054          logger.fine("Getting UnitTest DPD for project: " + project.getName());
055          try {
056            UnitTestDailyProjectData classData = dpdClient.getUnitTest(project.getOwner(),
057                project.getName(), Tstamp.makeTimestamp(session.getDate().getTime()));
058            logger.fine("Finished getting UnitTest DPD for project: " + project.getName());
059            // Create a UnitTestData instance for this project.
060            UnitTestData unittestData = this.getUnitTestData(project);
061            for (MemberData data : classData.getMemberData()) {
062              unittestData.addEntry(data.getSuccess().intValue(), data.getFailure().intValue());
063            }
064          }
065          catch (DailyProjectDataClientException e) {
066            session.setFeedback("Exception when getting unittest DPD for project " + project + ": " +
067                e.getMessage());
068          }
069        }
070      }
071      
072      
073      /**
074       * Sets this model to its empty state. 
075       */
076      public void clear() {
077        this.unittestDataMap.clear();
078      }
079      
080    
081      /**
082       * Returns true if this data model contains no information.
083       * Used to figure out if the associated panel should be visible. 
084       * @return True if the data model is empty. 
085       */
086      public boolean isEmpty() {
087        return this.unittestDataMap.isEmpty();
088      }
089      
090      /**
091       * Return the UnitTestData instance associated with the specified project.
092       * Creates and returns a new UnitTestData instance if one is not yet present.
093       * @param project The project. 
094       * @return The UnitTestData instance for this project.  
095       */
096      public UnitTestData getUnitTestData(Project project) {
097        if (!unittestDataMap.containsKey(project)) {
098          unittestDataMap.put(project, new UnitTestData(project));
099        }
100        return unittestDataMap.get(project);
101      }
102      
103      /**
104       * Returns the list of UnitTestData instances, needed for markup.
105       * @return The list of UnitTestData instances. 
106       */
107      public List<UnitTestData> getUnitTestDataList() {
108        return new ArrayList<UnitTestData>(this.unittestDataMap.values());
109      }
110      
111    }