package model;
import java.io.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.EventObject;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
public class Pegelmodel {
private String[][] data;
private List<ModelChangedListener> listener;
public Pegelmodel() {
data = getDataFromFile();
listener = new ArrayList<ModelChangedListener>();
}
public void addModelChangedListener(ModelChangedListener ml) {
listener.add(ml);
}
public void removeModelChangedListener(ModelChangedListener ml) {
listener.remove(ml);
}
public String[][] getData() {
return data;
}
public void refresh() {
getDataFromFile();
}
private String[][] getDataFromFile() {
String[][] array = null;
ArrayList<String> forArray = new ArrayList<String>();
ArrayList<String> gaugeStations = new ArrayList<String>();
BufferedReader bufferConfig = null;
BufferedReader bufferGaugeStation = null;
String path = null;
File config = new File("C:/Users/Vaio/Documents/Ferialjobs/Land Oberösterreich/Applet für Hydro/Config.txt");
try {
bufferConfig = new BufferedReader(new FileReader(config));
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "Config-File not found", "Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
try {
path = bufferConfig.readLine();
} catch (IOException e) {
e.printStackTrace();
System.exit(2);
}
String read = null;
int length = 0;
try {
read = bufferConfig.readLine();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Can't read from config-file",
"Error", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
System.exit(2);
}
while(read != null) {
gaugeStations.add(read);
length++;
try {
read = bufferConfig.readLine();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Can't read from config-file",
"Error", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
System.exit(2);
}
}
try {
bufferConfig.close();
} catch (IOException e1) {
JOptionPane.showMessageDialog(null,
"Error occured while closeing the config-file",
"Error", JOptionPane.ERROR_MESSAGE);
e1.printStackTrace();
System.exit(3);
}
String[][]gauge = new String[length][4];
Pattern p = Pattern.compile(" ");
for(int i = 0; i < length; i++) {
gauge[i] = p.split(gaugeStations.get(i));
}
array = new String[gauge.length][5];
File gaugeStation = null;
String pathNew = null;
for(int i = 0; i<gauge.length; i++) {
pathNew = path + gauge[i][1];
gaugeStation = new File (pathNew);
try {
bufferGaugeStation = new BufferedReader(new FileReader(gaugeStation));
} catch(FileNotFoundException e) {
JOptionPane.showMessageDialog(null,
"Datei der Pegelstelle " + gauge[i][0] + " nicht gefunden!" ,
"Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
try {
read = bufferGaugeStation.readLine();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Can't read from file " + gauge[i][1],
"Error", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
System.exit(2);
}
if(read == null) {
JOptionPane.showMessageDialog(null, "Pegelstellen " + gauge[i][0] + ": Keine Daten vorhanden!",
"Warning", JOptionPane.WARNING_MESSAGE);
forArray.add(gauge[i][0] + ";00:00:00;00:00:00;-9999;" + gauge[i][3]);
} else {
forArray.add(gauge[i][0] + ";" + read);
}
try {
bufferGaugeStation.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Error occured while closeing the config-file",
"Error", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
System.exit(3);
}
}
Pattern p1;
Date nowDate = new Date();
Calendar now = new GregorianCalendar();
now.setTime(nowDate);
Calendar time = Calendar.getInstance();
time.setTime(nowDate);
String[] timeString = new String[3];
Pattern pTime = Pattern.compile(":");
long differenz;
long minutes;
for(int i = 0; i < gauge.length; i++) {
p1 = Pattern.compile(gauge[i][2]);
array[i] = p1.split(forArray.get(i));
if(Integer.parseInt(array[i][3]) >0) {
timeString = pTime.split(array[i][2]);
time.set(Calendar.HOUR, Integer.parseInt(timeString[0]));
time.set(Calendar.MINUTE, Integer.parseInt(timeString[1]));
time.set(Calendar.SECOND, Integer.parseInt(timeString[2]));
differenz = now.getTimeInMillis() - time.getTimeInMillis();
minutes = Math.round((double)differenz / (60. * 1000.));
if(minutes > 30) {
array[i][3] = "-9999";
}
}
}
return array;
}