001    package org.hackystat.projectbrowser.page.sensordata;
002    
003    import java.io.Serializable;
004    import java.util.ArrayList;
005    import java.util.List;
006    
007    import javax.xml.datatype.XMLGregorianCalendar;
008    
009    import org.hackystat.projectbrowser.ProjectBrowserSession;
010    import org.hackystat.sensorbase.client.SensorBaseClient;
011    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataIndex;
012    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataRef;
013    import org.hackystat.utilities.stacktrace.StackTrace;
014    import org.hackystat.utilities.tstamp.Tstamp;
015    
016    /**
017     * A model for the sensor data list. 
018     * @author Philip Johnson
019     */
020    public class SensorDataDetailsModel implements Serializable {
021      /** For serialization. */
022      private static final long serialVersionUID = 1L;
023      private List<SensorDataDetails> detailsList = new ArrayList<SensorDataDetails>();
024      
025      /**
026       * Creates an empty SensorDataDetailsModel.
027       */
028      public SensorDataDetailsModel() {
029        // Don't do anything here. 
030      }
031      
032      /**
033       * Returns the list of sensor data details instances. 
034       * @return The list of sensor data details. 
035       */
036      public List<SensorDataDetails> getDetails() {
037        return this.detailsList;
038      }
039      
040      /**
041       * Sets the SensorDetailsModel given an sdtName and a Tool.  
042       * This is typically called from a SensorDataPanelLink.
043       * @param sdtName The name of the SDT whose instances are to be retrieved.
044       * @param tool The tool for the SDTs.
045       */
046      public void setSensorDetailsModel(String sdtName, String tool) {
047        detailsList.clear();
048        SensorBaseClient client = ProjectBrowserSession.get().getSensorBaseClient();
049        SensorDataSession session = ProjectBrowserSession.get().getSensorDataSession();
050        String projectName = session.getProject().getName();
051        String owner = session.getProject().getOwner();
052        XMLGregorianCalendar startTime = Tstamp.makeTimestamp(); // needs to be changed.
053        XMLGregorianCalendar endTime = Tstamp.incrementDays(startTime, 1);
054        SensorDataIndex index = null;
055        try {
056          // Retrieve all sensor data instances if SdtName is "Total".
057          if ("Total".equals(sdtName)) {
058            index = client.getProjectSensorData(owner, projectName, startTime, endTime);
059          }
060          // Retrieve the sensor data instances for the specified tool
061          else {
062            index = client.getProjectSensorData(owner, projectName, startTime, endTime, sdtName, tool);
063          }
064          for (SensorDataRef ref : index.getSensorDataRef()) {
065            detailsList.add(new SensorDataDetails(ref));
066            // HACK.  Replace above call to get an index with just the tool's sensor data. 
067            // if ("All".equals(tool) || tool.equals(ref.getTool())) {
068            //   detailsList.add(new SensorDataDetails(ref));
069            // }
070          }
071        }
072        catch (Exception e) {
073          System.out.println("Error getting sensor data: " + StackTrace.toString(e));
074        }
075      }
076      
077      /**
078       * Returns true if this model does not contain any data. 
079       * @return True if no data. 
080       */
081      public boolean isEmpty() {
082        return detailsList.isEmpty();
083      }
084      
085    
086    }