Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Dev #16

Merged
merged 9 commits into from
Oct 11, 2023
Merged

Dev #16

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@
<artifactId>derbytools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>4.0.1</version>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/no/ntnu/erbj/tds/TdsApplication.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package no.ntnu.erbj.tds;

import java.util.Scanner;

import no.ntnu.erbj.tds.ui.cli.utilities.TdsLogger;
import no.ntnu.erbj.tds.ui.gui.GuiLauncher;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.shell.command.annotation.CommandScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
* The main class of the application. This class is responsible for starting the application.
Expand All @@ -19,7 +20,9 @@
* @author Erik Bjørnsen
*/
@SpringBootApplication
@EnableTransactionManagement
@CommandScan(basePackages = "no.ntnu.erbj.tds.ui.cli.commands")
@EntityScan(basePackages = "no.ntnu.erbj.tds.model")
public class TdsApplication {

/**
Expand All @@ -41,6 +44,7 @@ public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String choice = scanner.nextLine();


switch (choice) {
case "1":
SpringApplication.run(TdsApplication.class, args);
Expand Down
101 changes: 46 additions & 55 deletions src/main/java/no/ntnu/erbj/tds/dao/DAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,56 @@
import java.util.Optional;

/**
* Data Access Object used to access data from a database.
* Reused from assignment 2 in IDATG1002.
* Data Access Object used to access data from a database. Reused from assignment 2 in IDATG1002.
*
* @param <T> the type of the entity to be stored in the database.
* @version 1.1
* @author Erik Bjørnsen
*/
public interface DAO<T> extends Iterable<T> {
/**
* Adds a new instance of an entity to the database.
*
* @param t The entity to be added.
*/
void add(T t);

/**
* Removes an instance of an entity from the database.
*
* @param t The entity to be removed.
*/
void remove(T t);

/**
* Updates all fields of an entity in the database.
*
* @param t The entity to be updated.
*/
void update(T t);

/**
* Returns an iterator of all entity instances in the database.
*
* @return An iterator of all entities in the database.
*/
Iterator<T> iterator();

/**
* Finds a specific entity instance by using the entity's id.
*
* @param id The id as a String.
* @return The entity instance found.
*/
Optional<T> find(Long id);

/**
* Returns a List of all entity instances in the database.
*
* @return All entity instances in the database as a List.
*/
List<T> getAll();

/**
* Prints entity-specific details of all the entity instances in the database.
*/
void printAllDetails();

/**
* Closes the EntityManagerFactory and the EntityManager.
*/
void close();
/**
* Adds a new instance of an entity to the database.
*
* @param t The entity to be added.
*/
void add(T t);

/**
* Removes an instance of an entity from the database.
*
* @param t The entity to be removed.
*/
void remove(T t);

/**
* Updates all fields of an entity in the database.
*
* @param t The entity to be updated.
*/
void update(T t);

/**
* Returns an iterator of all entity instances in the database.
*
* @return An iterator of all entities in the database.
*/
Iterator<T> iterator();

/**
* Finds a specific entity instance by using the entity's id.
*
* @param id The id as a String.
* @return The entity instance found.
*/
Optional<T> find(Long id);

/**
* Returns a List of all entity instances in the database.
*
* @return All entity instances in the database as a List.
*/
List<T> getAll();

/** Prints entity-specific details of all the entity instances in the database. */
void printAllDetails();
}

77 changes: 77 additions & 0 deletions src/main/java/no/ntnu/erbj/tds/dao/DepartureDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package no.ntnu.erbj.tds.dao;

import jakarta.persistence.*;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import no.ntnu.erbj.tds.model.Departure;
import no.ntnu.erbj.tds.ui.cli.utilities.TdsLogger;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
* This class is a Data Access Object (DAO) for the Departure class. It provides methods for
* accessing the database.
*
* @version 1.1
* @author Erik
*/
@Repository
public class DepartureDAO implements DAO<Departure> {
@PersistenceContext private EntityManager em;

/** {@inheritDoc} */
@Override
@Transactional
public void add(Departure departure) {
if (getAll().contains(departure)) {
throw new IllegalArgumentException("Instance of departure already exists in the database.");
} else if (getAll().stream().anyMatch(dep -> dep.getTrain().equals(departure.getTrain()))) {
throw new IllegalArgumentException("Train already has a departure.");
}

this.em.persist(departure);
}

/** {@inheritDoc} */
@Override
@Transactional
public void remove(Departure departure) {
em.remove(em.contains(departure) ? departure : em.merge(departure));
}

/** {@inheritDoc} */
@Override
@Transactional
public void update(Departure departure) {
em.merge(departure);
}

/** {@inheritDoc} */
@Override
public Iterator<Departure> iterator() {
TypedQuery<Departure> query = this.em.createQuery("SELECT d FROM Departure d", Departure.class);
return query.getResultList().iterator();
}

/** {@inheritDoc} */
@Override
public Optional<Departure> find(Long id) {
return Optional.ofNullable(em.find(Departure.class, id));
}

/** {@inheritDoc} */
@Override
public List<Departure> getAll() {
return em.createQuery("SELECT d FROM Departure d", Departure.class).getResultList();
}

/** {@inheritDoc} */
@Override
public void printAllDetails() {
List<Departure> departureList = getAll();
for (Departure departure : departureList) {
TdsLogger.getInstance().info("Departure Details" + " :: " + departure.getId());
}
}
}
75 changes: 75 additions & 0 deletions src/main/java/no/ntnu/erbj/tds/dao/StationDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package no.ntnu.erbj.tds.dao;

import jakarta.persistence.*;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import no.ntnu.erbj.tds.model.Station;
import no.ntnu.erbj.tds.ui.cli.utilities.TdsLogger;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
* This class is a Data Access Object (DAO) for the Station class. It provides methods for
* accessing the database.
*
* @version 1.1
* @author Erik
*/
@Repository
public class StationDAO implements DAO<Station> {
@PersistenceContext private EntityManager em;

/** {@inheritDoc} */
@Override
@Transactional
public void add(Station station) {
if (getAll().contains(station)) {
throw new IllegalArgumentException("Instance of station already exists in the database.");
} else {
this.em.persist(station);
}
}

/** {@inheritDoc} */
@Override
@Transactional
public void remove(Station station) {
em.remove(em.contains(station) ? station : em.merge(station));
}

/** {@inheritDoc} */
@Override
@Transactional
public void update(Station station) {
em.merge(station);
}

/** {@inheritDoc} */
@Override
public Iterator<Station> iterator() {
TypedQuery<Station> query = this.em.createQuery("SELECT s FROM Station s", Station.class);
return query.getResultList().iterator();
}

/** {@inheritDoc} */
@Override
public Optional<Station> find(Long id) {
return Optional.ofNullable(em.find(Station.class, id));
}

/** {@inheritDoc} */
@Override
public List<Station> getAll() {
return em.createQuery("SELECT s FROM Station s", Station.class).getResultList();
}

/** {@inheritDoc} */
@Override
public void printAllDetails() {
List<Station> stationList = getAll();
for (Station station : stationList) {
TdsLogger.getInstance().info("Station Details" + " :: " + station.getId());
}
}
}
Loading
Loading