001    package org.hackystat.projectbrowser.page.dailyprojectdata.commit;
002    
003    import java.io.Serializable;
004    import java.util.List;
005    import java.util.Map;
006    import java.util.TreeMap;
007    
008    import org.hackystat.dailyprojectdata.resource.commit.jaxb.MemberData;
009    import org.hackystat.sensorbase.resource.projects.jaxb.Project;
010    
011    /**
012     * Represents commit and churn data for a given project and day.
013     * @author Philip Johnson
014     *
015     */
016    public class CommitData implements Serializable {
017      
018      /** Support serialization. */
019      private static final long serialVersionUID = 1L;
020      
021      /** The project whose data is kept in this instance. */
022      private Project project;
023      
024      /** Holds the total Commits. */
025      private int totalCommit = 0;
026      
027      /** Holds the total Churn. */
028      private int totalChurn = 0;
029      
030      /** Maintains the (user, commit/churn) data. */
031      private Map<String, String> user2commit = new TreeMap<String, String>(); 
032    
033      /**
034       * Constructs a CommitData instance with member data. 
035       * @param project The project associated with this Commit data. 
036       * @param memberData The individual member data. 
037       */
038      public CommitData(Project project, List<MemberData> memberData) {
039        this.project = project;
040        for (MemberData data : memberData) {
041          String member = convertUriToEmail(data.getMemberUri());
042          int commits = data.getCommits();
043          int churn = 0;
044          this.totalCommit += commits;
045          try {
046            churn += data.getLinesAdded();
047            churn += data.getLinesAdded();
048            churn += data.getLinesModified();
049          }
050          catch (Exception e) { //NOPMD
051            // do nothing, it's because lines modified was null.
052          }
053          this.totalChurn += churn;
054          user2commit.put(member, String.format("%d/%d", commits, churn));
055        }
056      }
057      
058      /**
059       * Constructs an empty CommitData instance.
060       * Used to initialize the session state.  
061       * @param project The project.
062       */
063      public CommitData(Project project) {
064        this.project = project;
065      }
066      
067      /**
068       * Returns the project associated with this instance. 
069       * @return The project. 
070       */
071      public Project getProject() {
072        return this.project;
073      }
074      
075      /**
076       * Returns the total aggregate Commit for this project and day. 
077       * @return The total Commit. 
078       */
079      public int getTotalCommit () {
080        return this.totalCommit;
081      }
082      
083      /**
084       * Returns the total aggregate Churn for this project and day. 
085       * @return The total Churn. 
086       */
087      public int getTotalChurn () {
088        return this.totalChurn;
089      }
090      
091      /**
092       * Returns a String listing each project member with their commit and churn totals.
093       * @return A String of Commit info for this project and day. 
094       */
095      public String getCommitData() {
096        StringBuffer buff = new StringBuffer();
097        for (Map.Entry<String, String> entry : user2commit.entrySet()) {
098          buff.append(String.format("%s(%s) ", entry.getKey(), entry.getValue()));
099        }
100        return buff.toString();
101      }
102      
103      /**
104       * Converts a project member string to an email address.
105       * The member string might be a URI (starting with http) or the desired email address. 
106       * @param member The member string. 
107       * @return The email address corresponding to the member string. 
108       */
109      private String convertUriToEmail(String member) {
110        if (member.startsWith("http:")) {
111          int lastSlash = member.lastIndexOf('/');
112          if (lastSlash < 0) {
113            throw new IllegalArgumentException("Could not convert owner to URI");
114          }
115          return member.substring(lastSlash + 1); 
116        }
117        // Otherwise owner is already the email. 
118        return member;
119      }
120    }