Commit 5a1a0def authored by Ondra's avatar Ondra
Browse files

Uprava repozitaru, intenterface, dedicnosti, zakladnich datovych struktur

parent e504c084
......@@ -4,6 +4,7 @@ package cz.cvut.fel.skoumond.entities;
import cz.cvut.fel.skoumond.sql.TrainingDiary;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Objects;
/**
* Trida reprezentujici jeden treninkovy zaznam v diari,
......@@ -33,8 +34,9 @@ public class RunEntity extends BaseEntity implements Serializable {
* Prumerna tepova frekvence pri treninku
*/
private int avrgHeartRate;
private TypeEntity type;
/**
* Konstruktor zaznamu, inicilaizuje vsechny sve hodnoty
*
......@@ -44,14 +46,14 @@ public class RunEntity extends BaseEntity implements Serializable {
* @param distInKm
* @param avrgHeartRate
*/
public RunEntity(int id, Calendar dateTime, String desc, int sessionTime,
float distInKm, int avrgHeartRate) {
public RunEntity(int id, Calendar dateTime, String desc, int sessionTime, float distInKm, int avrgHeartRate, TypeEntity type) {
super(id);
this.dateTime = dateTime;
this.desc = desc;
this.sessionTime = sessionTime;
this.distInKm = distInKm;
this.avrgHeartRate = avrgHeartRate;
this.type = type;
}
/**
......@@ -64,6 +66,7 @@ public class RunEntity extends BaseEntity implements Serializable {
this.sessionTime = 0;
this.distInKm = 0;
this.avrgHeartRate = 0;
this.type = new TypeEntity(0, "");
}
/**
......@@ -155,7 +158,56 @@ public class RunEntity extends BaseEntity implements Serializable {
public void setAvrgHeartRate(int avrgHeartRate) {
this.avrgHeartRate = avrgHeartRate;
}
public TypeEntity getType() {
return type;
}
public void setType(TypeEntity type) {
this.type = type;
}
@Override
public int hashCode() {
int hash = 3;
hash = 37 * hash + Objects.hashCode(this.dateTime);
hash = 37 * hash + Objects.hashCode(this.desc);
hash = 37 * hash + this.sessionTime;
hash = 37 * hash + Float.floatToIntBits(this.distInKm);
hash = 37 * hash + this.avrgHeartRate;
hash = 37 * hash + Objects.hashCode(this.type);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final RunEntity other = (RunEntity) obj;
if (!Objects.equals(this.dateTime, other.dateTime)) {
return false;
}
if (!Objects.equals(this.desc, other.desc)) {
return false;
}
if (this.sessionTime != other.sessionTime) {
return false;
}
if (Float.floatToIntBits(this.distInKm) != Float.floatToIntBits(other.distInKm)) {
return false;
}
if (this.avrgHeartRate != other.avrgHeartRate) {
return false;
}
if (!Objects.equals(this.type, other.type)) {
return false;
}
return true;
}
/**
* Prepis metody toString
*
......
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cz.cvut.fel.skoumond.sql;
import java.util.List;
......
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cz.cvut.fel.skoumond.sql;
import cz.cvut.fel.skoumond.entities.BaseEntity;
......@@ -9,6 +6,7 @@ import cz.cvut.fel.skoumond.exceptions.NoConnectionException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
/**
*
......@@ -16,6 +14,12 @@ import java.sql.SQLException;
*/
public abstract class Repository<T extends BaseEntity> implements IRepository<T> {
protected static HashMap<Integer, BaseEntity> records;
public Repository() {
records = new HashMap();
}
protected static Connection getConnection() {
try {
......
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cz.cvut.fel.skoumond.sql;
import cz.cvut.fel.skoumond.entities.RunEntity;
import cz.cvut.fel.skoumond.exceptions.DataEntryException;
import cz.cvut.fel.skoumond.exceptions.NoConnectionException;
import static cz.cvut.fel.skoumond.sql.Repository.getConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
/**
......@@ -13,28 +18,101 @@ import java.util.List;
*/
public class RunRepository extends Repository<RunEntity> {
private final String FINDBYID = "SELECT * FROM APP.RUN WHERE R_ID = ?";
private final String ALL = "SELECT * APP.RUN";
private final String INSERT = "INSERT INTO APP.RUN VALUES (?, ?, ?, ?, ?, ?, ?)";
private final String UPDATE = "UPDATE APP.RUN SET R_CALENDAR = ? R_DESCRIPTION = ? R_SESSIONTIME = ? R_DISTANCE = ? R_AVRGHEARTRATE = ? R_TYPE = ? WHERE R_ID = ?";
private final String DELETE = "DELETE FROM APP.RUN WHERE R_ID = ?";
@Override
public RunEntity findById(int index) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
public RunEntity findById(int index) throws NoConnectionException {
try (Connection con = getConnection(); PreparedStatement ps = con.prepareStatement(FINDBYID)) {
ps.setInt(1, index);
ResultSet rs = ps.executeQuery();
rs.next();
return new RunEntity(rs.getInt(1), (Calendar) rs.getObject(2), rs.getString(3), rs.getInt(4), rs.getFloat(5), rs.getInt(6), new TypeRepository().findById(rs.getInt(7)));
} catch (SQLException e) {
System.err.println("Chyba při komunikaci s databází: " + e.getMessage());
throw new NoConnectionException();
}
}
@Override
public List<RunEntity> all() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
public List<RunEntity> all() throws NoConnectionException {
List<RunEntity> arrayList = new ArrayList();
try (Connection con = getConnection(); PreparedStatement ps = con.prepareStatement(ALL)) {
ResultSet rs = ps.executeQuery();
while (rs.next()) {
arrayList.add(new RunEntity(rs.getInt(1), (Calendar) rs.getObject(2), rs.getString(3), rs.getInt(4), rs.getFloat(5), rs.getInt(6), new TypeRepository().findById(rs.getInt(7))));
}
return arrayList;
} catch (SQLException e) {
System.err.println("Chyba při komunikaci s databází: " + e.getMessage());
throw new NoConnectionException();
}
}
@Override
public void save(RunEntity entity) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
public void save(RunEntity entity) throws NoConnectionException {
try (Connection con = getConnection(); PreparedStatement ps = con.prepareStatement(INSERT)) {
ps.setInt(1, entity.getId());
ps.setObject(2, entity.getDateTime());
ps.setString(3, entity.getDesc());
ps.setInt(4, entity.getSessionTime());
ps.setFloat(5, entity.getDistInKm());
ps.setInt(6, entity.getAvrgHeartRate());
ps.setInt(7, entity.getType().getId());
ps.executeUpdate();
} catch (SQLException e) {
System.err.println("Chyba při komunikaci s databází: " + e.getMessage());
throw new NoConnectionException();
}
}
@Override
public void update(RunEntity entity) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
public void update(RunEntity entity) throws NoConnectionException {
try (Connection con = getConnection();) {
con.setAutoCommit(false);
try (PreparedStatement ps = con.prepareStatement(UPDATE)) {
ps.setObject(1, entity.getDateTime());
ps.setString(2, entity.getDesc());
ps.setInt(3, entity.getSessionTime());
ps.setFloat(4, entity.getDistInKm());
ps.setInt(5, entity.getAvrgHeartRate());
ps.setInt(6, entity.getType().getId());
ps.setInt(7, entity.getId());
ps.executeUpdate();
con.commit();
con.setAutoCommit(true);
} catch (SQLException e) {
con.rollback();
con.setAutoCommit(true);
System.err.println("Chyba při práci s databází: " + e.getMessage());
throw new DataEntryException();
}
} catch (SQLException e) {
System.err.println("Chyba při komunikaci s databází: " + e.getMessage());
throw new NoConnectionException();
}
}
@Override
public void delete(RunEntity entity) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void delete(RunEntity entity) throws NoConnectionException {
try (Connection con = getConnection(); PreparedStatement ps = con.prepareStatement(DELETE)) {
ps.setInt(1, entity.getId());
ps.executeUpdate();
} catch (SQLException e) {
System.err.println("Chyba při komunikaci s databází: " + e.getMessage());
throw new NoConnectionException();
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cz.cvut.fel.skoumond.sql;
import cz.cvut.fel.skoumond.entities.TypeEntity;
......@@ -27,6 +24,9 @@ public class TypeRepository extends Repository<TypeEntity> {
private final String UPDATE = "UPDATE APP.TYPE SET T_TYPE = ? WHERE T_ID = ?";
private final String DELETE = "DELETE FROM APP.TYPE WHERE T_ID = ?";
public TypeRepository() {
}
@Override
public TypeEntity findById(int index) throws NoConnectionException {
......@@ -73,7 +73,7 @@ public class TypeRepository extends Repository<TypeEntity> {
}
@Override
public void update(TypeEntity entity) throws NoConnectionException, DataEntryException{
public void update(TypeEntity entity) throws NoConnectionException, DataEntryException {
try (Connection con = getConnection();) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment