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;
/**
* The class <code>Pegelmodel</code> is the model and the calculating class
* for the UI <code>View</code>. The class <code>PegelkartMain</code> contains
* an instance of it also the class <code>View</code>.
*
* @see ui.View
* @see PegelkarteMain
* @author Ehrentraud Hager
* @version 1.0
* @since 13.7.2012 - Java 1.6.0_24
*/
public class Pegelmodel {
private String[][] data;
private List<ModelChangedListener> listener;
/**
* Constructor
*/
public Pegelmodel() {
data = getDataFromFile();
listener = new ArrayList<ModelChangedListener>();
}
/*Methods for adding and removing listeners to the model*/
/**
* @param ml -> {@link ModelChangedListener}<br>
* <br> Adds a listener from the {@code ArrayList} <b>listener</b>
*/
public void addModelChangedListener(ModelChangedListener ml) {
listener.add(ml);
}
/**
* @param ml -> {@link ModelChangedListener}<br>
* <br> Removes a listener from the {@code ArrayList} <b>listener</b>
*/
public void removeModelChangedListener(ModelChangedListener ml) {
listener.remove(ml);
}
/**
* @return {@code String[][]} <b>data</b><br><br>
* Gives the array back where following values are saved:<br>
*
* name | date | time | water-level
*/
public String[][] getData() {
return data;
}
/**
* Method which loads the entire array of gauge-stations and
* the water-level again
*/
public void refresh() {
getDataFromFile();
// fireModelChanged();
}
/*________________PRIVATE_SECTION________________*/
/**
* @return {@code String[][]} <b>array</b>
*
* <br>Array of gauge stations with
* their name, date, time and water level
*/
private String[][] getDataFromFile() {
String[][] array = null; //return-array
ArrayList<String> forArray = new ArrayList<String>(); //list for return-array
ArrayList<String> gaugeStations = new ArrayList<String>(); //list for reading from config-file
/*--------Buffer-------*/
BufferedReader bufferConfig = null;
BufferedReader bufferGaugeStation = null;
/*---------------------*/
String path = null;//path to the directory of the single Files
//get configuration-file with paths to the single data-files for the gauge-stations
File config = new File("C:/Users/Vaio/Documents/Ferialjobs/Land Oberösterreich/Applet für Hydro/Config.txt");
//TODO: get the real Pathname
//create the file-reader for the configuration-file
try {
bufferConfig = new BufferedReader(new FileReader(config));
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "Config-File not found", "Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
//read in the path for all other gauge-files
try {
path = bufferConfig.readLine();
} catch (IOException e) {
e.printStackTrace();
System.exit(2);
}
//Create buffer for reading from the gauge-file
//read the configuration-file in the StringArray for adaption
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);
}
}
//Closing the buffer and the config-file
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);
}
//split the elements of the array-list and save to another array
// 0 | 1 | 2 | 3
// name|path|separator | parameter
String[][]gauge = new String[length][4];
Pattern p = Pattern.compile(" ");
for(int i = 0; i < length; i++) {
gauge[i] = p.split(gaugeStations.get(i));
}
/* load the data from the path and save it to the array-list*/
array = new String[gauge.length][5];
File gaugeStation = null;
String pathNew = null;
for(int i = 0; i<gauge.length; i++) {
//get the new File
pathNew = path + gauge[i][1];
gaugeStation = new File (pathNew);
//Create buffer for reading from the gauge-file
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);
}
//reading the first line from buffer
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);
}
//checking if the gauge-file contains something
if(read == null) {
JOptionPane.showMessageDialog(null, "Pegelstellen " + gauge[i][0] + ": Keine Daten vorhanden!",
"Warning", JOptionPane.WARNING_MESSAGE);
//adding an additional line to the array list
forArray.add(gauge[i][0] + ";00:00:00;00:00:00;-9999;" + gauge[i][3]);
} else {
//adding the read line to the array list
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);
}
}
/*split the single elements from the array-list with the Pattern p1 and
* save it to the return-array
* 0 | 1 | 2 | 3 | 4
* name|date|time|value|parameter
*/
//new pattern for separator which is saved in the gauge-array
Pattern p1;
//system time
Date nowDate = new Date();
Calendar now = new GregorianCalendar();
now.setTime(nowDate);
//preparations to get the time from the entry in the return-array
Calendar time = Calendar.getInstance();
time.setTime(nowDate);
String[] timeString = new String[3]; //array for hours, minutes, seconds -> String
Pattern pTime = Pattern.compile(":");
long differenz;
long minutes;
for(int i = 0; i < gauge.length; i++) {
//makes a new pattern for the separator-character
p1 = Pattern.compile(gauge[i][2]);
array[i] = p1.split(forArray.get(i));
if(Integer.parseInt(array[i][3]) >0) { //if there is no data the value is already -9999
//get the time of the array-entry
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]));
//check if the time is valid (not older than half an hour)
differenz = now.getTimeInMillis() - time.getTimeInMillis();
minutes = Math.round((double)differenz / (60. * 1000.));
if(minutes > 30) {
array[i][3] = "-9999"; //Time is not valid -> maybe technical damage
}
}
}
return array;
}