package org.hwo.pulscounter.elements; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Hashtable; import java.util.LinkedList; import java.util.List; import org.hwo.datetime.Date; import org.hwo.datetime.DateTime; import org.hwo.pulscounter.PulsCounter; public class WorkShiftRecord { public class ChannelRecords{ public class ChannelRecord { DateTime timestamp; Integer value; public ChannelRecord(DateTime timestamp,int value){ this.timestamp = timestamp; this.value = value; } public DateTime getTimestamp() { return timestamp; } public void setTimestamp(DateTime timestamp) { this.timestamp = timestamp; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } } int channel; List values; public ChannelRecords(int channel){ this.channel = channel; this.values = new LinkedList(); } public void addValue(DateTime timestamp,int value){ this.values.add(new ChannelRecord(timestamp, value)); } public void sort(){ /* List sorted = new LinkedList(); for (ChannelRecord r: values){ if (values.size() == 0) sorted.add(r); } this.values = sorted; */ } public List getRecords(){ return this.values; } } WorkShift workShift; Date date; Hashtable records; public WorkShiftRecord(WorkShift shift,Date date){ workShift = shift; this.date = date; records = new Hashtable(); loadRecords(); } void loadRecords(){ try { PreparedStatement stat = PulsCounter.getInspectorApplication().getConnection().prepareStatement("SELECT tstamp,channel,chvalue FROM rawvalues WHERE tstamp >= ? AND tstamp < ? ORDER BY channel,tstamp"); stat.setTimestamp(1, workShift.getShiftBegins(date).getTimeStamp()); stat.setTimestamp(2, workShift.getShiftEnds(date).getTimeStamp()); ResultSet result = stat.executeQuery(); while (result.next()){ int channel = result.getInt("channel"); if (!records.containsKey(channel)){ records.put(channel, new ChannelRecords(channel)); } // System.err.println("Record: " + result.getString("tstamp") + " [" + result.getString("channel") + "] " + result.getString("chvalue") ); records.get(channel).addValue(new DateTime(result.getTimestamp("tstamp")),result.getInt("chvalue")); } result.close(); stat.close(); for (WorkShiftRecord.ChannelRecords record: records.values().toArray(new WorkShiftRecord.ChannelRecords[0])){ record.sort(); } } catch (SQLException e) { e.printStackTrace(); } } public Integer[] getChannels(){ return (Integer[]) this.records.keySet().toArray(new Integer[0]); } public ChannelRecords getChannelRecords(int channel){ return records.get(channel); } }