org.hwo.pulscounter/src/org/hwo/pulscounter/elements/WorkShiftRecord.java

132 lines
3.1 KiB
Java

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<ChannelRecord> values;
public ChannelRecords(int channel){
this.channel = channel;
this.values = new LinkedList<WorkShiftRecord.ChannelRecords.ChannelRecord>();
}
public void addValue(DateTime timestamp,int value){
this.values.add(new ChannelRecord(timestamp, value));
}
public void sort(){
/* List<ChannelRecord> sorted = new LinkedList<WorkShiftRecord.ChannelRecords.ChannelRecord>();
for (ChannelRecord r: values){
if (values.size() == 0)
sorted.add(r);
}
this.values = sorted;
*/
}
public List<ChannelRecord> getRecords(){
return this.values;
}
}
WorkShift workShift;
Date date;
Hashtable<Integer, ChannelRecords>
records;
public WorkShiftRecord(WorkShift shift,Date date){
workShift = shift;
this.date = date;
records = new Hashtable<Integer, WorkShiftRecord.ChannelRecords>();
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);
}
}