001 package org.hackystat.projectbrowser.page.sensordata; 002 003 import java.io.Serializable; 004 005 import org.hackystat.projectbrowser.ProjectBrowserSession; 006 import org.hackystat.sensorbase.client.SensorBaseClient; 007 import org.hackystat.sensorbase.client.SensorBaseClientException; 008 import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData; 009 import org.hackystat.sensorbase.resource.sensordata.jaxb.Property; 010 import org.hackystat.sensorbase.resource.sensordata.jaxb.Properties; 011 import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataRef; 012 import org.hackystat.utilities.stacktrace.StackTrace; 013 import org.hackystat.utilities.tstamp.Tstamp; 014 015 /** 016 * A representation of a SensorData instance. 017 * If constructed with a SensorDataRef, will do lazy loading. 018 * @author Philip Johnson 019 */ 020 public class SensorDataDetails implements Serializable { 021 022 /** For serialization. */ 023 private static final long serialVersionUID = 1L; 024 private long timestamp = 0; 025 private long runtime = 0; 026 private String sdtName = null; 027 private String resource = null; 028 private String owner = null; 029 private String tool = null; 030 private String properties = null; 031 private SensorDataRef ref = null; 032 033 /** 034 * Create a SensorData instance representation suitable for display in Wicket. 035 * @param data The SensorData instance from the SensorBase. 036 */ 037 public SensorDataDetails(SensorData data) { 038 populateFields(data); 039 } 040 041 /** 042 * Creates a SensorDataDetails from a SensorDataRef. The ref will 043 * be used to retrieve a SensorData object the first time that an accessor on 044 * this instance is invoked. 045 * @param ref The SensorDataRef. 046 */ 047 public SensorDataDetails(SensorDataRef ref) { 048 this.ref = ref; 049 } 050 051 /** 052 * Populates the internal fields given a SensorData instance. 053 * @param data The instance. 054 */ 055 private void populateFields(SensorData data) { 056 this.timestamp = data.getTimestamp().toGregorianCalendar().getTimeInMillis(); 057 this.runtime = data.getRuntime().toGregorianCalendar().getTimeInMillis(); 058 this.sdtName = data.getSensorDataType(); 059 this.resource = data.getResource(); 060 this.owner = data.getOwner(); 061 this.tool = data.getTool(); 062 StringBuffer buff = new StringBuffer(); 063 Properties props = data.getProperties(); 064 if (props != null) { 065 for (Property prop : data.getProperties().getProperty()) { 066 String key = prop.getKey(); 067 String value = prop.getValue(); 068 buff.append(key).append(" = ").append(value).append(','); 069 } 070 } 071 this.properties = buff.toString(); 072 } 073 074 /** 075 * Returns a SensorData instance, given the reference. 076 * Can also return null if an error occurs during retrieval. 077 * @param ref The sensor data reference. 078 * @return The sensor data instance. 079 */ 080 private SensorData getSensorData(SensorDataRef ref) { 081 SensorBaseClient client = ProjectBrowserSession.get().getSensorBaseClient(); 082 try { 083 return client.getSensorData(ref); 084 } 085 catch (SensorBaseClientException e) { 086 System.out.println("Error getting sensor data: " + StackTrace.toString(e)); 087 } 088 return null; 089 } 090 091 /** 092 * Checks to see whether this SensorDataDetails instance contains only a reference, 093 * and if so, retrieves the actual sensor data instance and populates the fields with it. 094 */ 095 private void dereference() { 096 if ((this.timestamp == 0) && (this.ref != null)) { 097 SensorData data = getSensorData(this.ref); 098 if (data != null) { 099 this.populateFields(data); 100 } 101 } 102 } 103 104 /** 105 * Return the timestamp. 106 * @return The timestamp. 107 */ 108 public String getTimeStamp() { 109 dereference(); 110 return Tstamp.makeTimestamp(this.timestamp).toString(); 111 } 112 113 /** 114 * Return the runtime. 115 * @return The runtime. 116 */ 117 public String getRuntime() { 118 dereference(); 119 return Tstamp.makeTimestamp(this.runtime).toString(); 120 } 121 122 /** 123 * Return the SDT name. 124 * @return The sdt name. 125 */ 126 public String getSdtName() { 127 dereference(); 128 return this.sdtName; 129 } 130 131 /** 132 * Return the resource. 133 * @return The resource. 134 */ 135 public String getResource() { 136 dereference(); 137 return this.resource; 138 } 139 140 /** 141 * Return the owner. 142 * @return The owner. 143 */ 144 public String getOwner() { 145 dereference(); 146 return this.owner; 147 } 148 149 /** 150 * Return the tool. 151 * @return The tool. 152 */ 153 public String getTool() { 154 dereference(); 155 return this.tool; 156 } 157 158 /** 159 * Return the properties. 160 * @return The properties. 161 */ 162 public String getProperties() { 163 dereference(); 164 return this.properties; 165 } 166 }