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