001    package org.hackystat.dailyprojectdata.resource.commit;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    import org.hackystat.sensorbase.resource.sensordata.jaxb.Property;
007    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
008    
009    /**
010     * The data container that abstracts the data retrieval of Commit information.
011     * 
012     * To Do: (1) change from a List to a Map[Owner, SensorData]. 
013     *   
014     * @author aito
015     * 
016     */
017    public class CommitDataContainer {
018      
019      /** The list of commit data. */
020      private List<CommitData> data = new ArrayList<CommitData>();
021    
022      /**
023       * Adds the specified SensorData instance to this container if it contains the
024       * three Commit properties (linesDeleted, linesAdded, totalLines) and if these property
025       * values are integers. 
026       * @param data the specified data instance.
027       */
028      public void addCommitData(SensorData data) {
029        if (isValidCommitData(data)) {
030          this.data.add(new CommitData(data));
031        }
032      }
033      
034      /**
035       * Returns true if the SensorData instance has the Commit properties, false otherwise.
036       * Note that linesModified is an optional property. 
037       * @param data The sensor data instance. 
038       * @return True if the sensor data instance is a Commit instance. 
039       */
040      private boolean isValidCommitData(SensorData data) {
041        try {
042          Integer.valueOf(this.getProperty(data, "linesDeleted"));
043          Integer.valueOf(this.getProperty(data, "linesAdded"));
044        }
045        catch (Exception e) {
046          return false;
047        }
048        return true;
049      }
050      
051      /**
052       * Returns the String value associated with property key.
053       * If no such property exists, returns null.
054       * 
055       * @param data The sensor data instance.
056       * @param propertyName the property name to search for.
057       * @return The property value, or null.
058       */
059      private String getProperty(SensorData data, String propertyName) {
060        List<Property> propertyList = data.getProperties().getProperty();
061        for (Property property : propertyList) {
062          if (propertyName.equals(property.getKey())) {
063            return property.getValue();
064          }
065        }
066        return null;
067      }
068    
069      /**
070       * Returns a set of owners of the wrapped SensorData.
071       * @return the list of sensor data owners.
072       */
073      public List<String> getOwners() {
074        List<String> owners = new ArrayList<String>();
075        for (CommitData data : this.data) {
076          if (!owners.contains(data.getOwner())) {
077            owners.add(data.getOwner());
078          }
079        }
080        return owners;
081      }
082    
083      /**
084       * Returns the total lines added by the specified owner.
085       * @param owner the specified owner.
086       * @return the total lines added.
087       */
088      public int getLinesAdded(String owner) {
089        int totalLinesAdded = 0;
090        for (CommitData data : this.data) {
091          if (data.getOwner().equals(owner)) {
092            totalLinesAdded += data.getLinesAdded();
093          }
094        }
095        return totalLinesAdded;
096      }
097    
098      /**
099       * Returns the total lines deleted by the specified owner.
100       * @param owner the specified owner.
101       * @return the total lines deleted.
102       */
103      public int getLinesDeleted(String owner) {
104        int totalLinesDeleted = 0;
105        for (CommitData data : this.data) {
106          if (data.getOwner().equals(owner)) {
107            totalLinesDeleted += data.getLinesDeleted();
108          }
109        }
110        return totalLinesDeleted;
111      }
112      
113      /**
114       * Returns the total lines modified by the specified owner.
115       * Not all CM systems track lines modified. SVN does not, Perforce does. 
116       * @param owner The specified owner.
117       * @return The total lines modified.
118       */
119      public int getLinesModified(String owner) {
120        int totalLinesModified = 0;
121        for (CommitData data : this.data) {
122          if (data.getOwner().equals(owner)) {
123            totalLinesModified += data.getLinesModified();
124          }
125        }
126        return totalLinesModified;
127      }
128    
129    
130      /**
131       * Returns the total commits made by the specified owner.
132       * @param owner the specified owner.
133       * @return the total commits.
134       */
135      public int getCommits(String owner) {
136        int numCommits = 0;
137        for (CommitData data : this.data) {
138          if (data.getOwner().equals(owner)) {
139            numCommits++;
140          }
141        }
142        return numCommits;
143      }
144    
145      /**
146       * Returns a copy of the list containing all of the added SensorData
147       * instances.
148       * @return the SensorData list copy.
149       */
150      public List<CommitData> getData() {
151        return new ArrayList<CommitData>(this.data);
152      }
153    }