DATABASEMANAGER.JAVA
package com.dcu.project.server;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import com.dcu.project.model.NMEAData;
public class DatabaseManager {
public NMEAData GetEntryFromDB() {
Session session = HibernateUtil.beginTransaction();
// Comment this next line if you want to stop dropping and recreating tables every execution
HibernateUtil.recreateDatabase();
// First create some customer entities using a method we have created
CreateNMEAData(session, "123456", "789123", 54.32, "N", 8.95, "W", 40, "345678");
NMEAData data = new NMEAData();
data = GetData(session);
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
return data;
}
/**
* This method creates a new Customer entity in the database using the
* fields provided as arguments
*/
public static void CreateNMEAData(Session session, String Time,
String Date, double Latitude, String Lat_Dir, double Longitude, String Long_Dir, double Speed,
String Course) {
System.out.println("Creating new data");
NMEAData gpsdata = new NMEAData();
gpsdata.setTime(Time);
gpsdata.setDate(Date);
gpsdata.setLat(Latitude);
gpsdata.setLat_Dir(Lat_Dir);
gpsdata.setLongitude(Longitude);
gpsdata.setLong_Dir(Long_Dir);
gpsdata.setSpeed(Speed);
gpsdata.setCourse(Course);
session.save(gpsdata);
System.out.println("NMEA Data Saved!");
}
/**
* Final method to print out all of our Customers with all their fields
* Demonstrates Retrieval only
*/
@SuppressWarnings("unchecked")
public static void DisplayCustomers(Session session) {
List alldata = null;
Query queryResult = session.createQuery("from NMEAData");
alldata = (List) queryResult.list();
for (int i = 0; i < alldata.size(); i++) {
NMEAData data = (NMEAData) alldata.get(i);
System.out.println(data.getID() + ", " + data.getTime());
}
}
public static NMEAData GetData(Session session){
int idVariable = 1;
// First Retrive the particular customer...let's assume we know the username
String queryString = "from NMEAData where ID = :ID";
Query query = session.createQuery(queryString);
query.setLong("ID", idVariable);
NMEAData data = (NMEAData) query.uniqueResult();
System.out.println(data.getID());
return data;
}
}
DATASERVIMPL.JAVA
package com.dcu.project.server;
import com.dcu.project.client.DataService;
import com.dcu.project.dto.GPSDataDTO;
import com.dcu.project.model.NMEAData;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
public class DataServiceImpl extends RemoteServiceServlet implements DataService {
private static final long serialVersionUID = 1L;
public GPSDataDTO getData() {
DatabaseManager a = new DatabaseManager();
NMEAData data = new NMEAData();
data = a.GetEntryFromDB();
System.out.println(data.getLat());
GPSDataDTO myData = new GPSDataDTO();
myData.setID(data.getID());
myData.setDate(data.getDate());
myData.setTime(data.getTime());
myData.setLat(data.getLat());
myData.setLat_Dir(data.getLat_Dir());
myData.setLong(data.getLongitude());
myData.setLong_Dir(data.getLong_Dir());
myData.setSpeed(data.getSpeed());
myData.setCourse(data.getCourse());
/*
myData.setID(1);
myData.setDate("111111");
myData.setTime("123456");
myData.setLat(53.3);
myData.setLat_Dir("N");
myData.setLong(-8.9);
myData.setLong_Dir("W");
myData.setSpeed(50.2);
myData.setCourse("123455");
*/
return myData;
}
}
NMEADATA.JAVA
package com.dcu.project.model;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table (name="barrytable")
public class NMEAData implements Serializable {
private static final long serialVersionUID = 1L;
protected int ID;
protected String Time;
protected String Date;
protected double Lat;
protected String Lat_Dir;
protected double Longitude;
protected String Long_Dir;
protected double Speed;
protected String Course;
public NMEAData(){
}
public NMEAData(int theID, String theTime, String theDate, double theLat, double theLat_Dir,
double theLongitude, String theLongDir, double theSpeed, String theCourse)
{
ID = theID;
Time = theTime;
Date = theDate;
Lat = theLat;
Longitude = theLongitude;
Long_Dir = theLongDir;
Speed = theSpeed;
Course = theCourse;
}
@Id
@GeneratedValue
@Column(name="id")
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
@Column(length=30)
public String getTime() {
return Time;
}
public void setTime(String time) {
Time = time;
}
@Column(length=30)
public String getDate() {
return Date;
}
public void setDate(String date) {
Date = date;
}
@Column(length=30)
public double getLat() {
return Lat;
}
public void setLat(double lat) {
Lat = lat;
}
@Column(length=30)
public String getLat_Dir() {
return Lat_Dir;
}
public void setLat_Dir(String latDir) {
Lat_Dir = latDir;
}
@Column(length=30)
public double getLongitude() {
return Longitude;
}
public void setLongitude(double longitude) {
Longitude = longitude;
}
@Column(length=30)
public String getLong_Dir() {
return Long_Dir;
}
public void setLong_Dir(String longDir) {
Long_Dir = longDir;
}
@Column(length=30)
public double getSpeed() {
return Speed;
}
public void setSpeed(double speed) {
Speed = speed;
}
@Column(length=30)
public String getCourse() {
return Course;
}
public void setCourse(String course) {
Course = course;
}
}
GPSDATADTO.JAVA
package com.dcu.project.dto;
import com.google.gwt.user.client.rpc.IsSerializable;
public class GPSDataDTO implements IsSerializable {
private static final long serialVersionUID = 1L;
protected int ID;
protected String myTime;
protected String myDate;
protected double Lat;
protected String Lat_Dir;
protected double myLong;
protected String Long_Dir;
protected double Speed;
protected String Course;
public GPSDataDTO(){
}
public GPSDataDTO(int theID, String theTime, String theDate, double theLat, double theLat_Dir,
double theLongitude, String theLongDir, double theSpeed, String theCourse)
{
ID = theID;
myTime = theTime;
myDate = theDate;
Lat = theLat;
myLong = theLongitude;
Long_Dir = theLongDir;
Speed = theSpeed;
Course = theCourse;
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getTime() {
return myTime;
}
public void setTime(String time) {
myTime = time;
}
public String getDate() {
return myDate;
}
public void setDate(String date) {
myDate = date;
}
public double getLat() {
return Lat;
}
public void setLat(double lat) {
Lat = lat;
}
public String getLat_Dir() {
return Lat_Dir;
}
public void setLat_Dir(String latDir) {
Lat_Dir = latDir;
}
public double getLongitude() {
return myLong;
}
public void setLong(double along) {
myLong = along;
}
public String getLong_Dir() {
return Long_Dir;
}
public void setLong_Dir(String longDir) {
Long_Dir = longDir;
}
public double getSpeed() {
return Speed;
}
public void setSpeed(double speed) {
Speed = speed;
}
public String getCourse() {
return Course;
}
public void setCourse(String course) {
Course = course;
}
}
|