Merge remote-tracking branch 'origin/feature_taskList_m2' into feature_gardenplan-model_M2
# Conflicts: # src/test/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabaseTest.java
This commit is contained in:
commit
d45b8e116e
|
@ -0,0 +1,19 @@
|
|||
package ch.zhaw.gartenverwaltung;
|
||||
|
||||
import ch.zhaw.gartenverwaltung.types.HardinessZone;
|
||||
|
||||
public class Config {
|
||||
private static HardinessZone currentHardinessZone;
|
||||
|
||||
static {
|
||||
currentHardinessZone = HardinessZone.ZONE_8A;
|
||||
}
|
||||
|
||||
public static HardinessZone getCurrentHardinessZone() {
|
||||
return currentHardinessZone;
|
||||
}
|
||||
|
||||
public static void setCurrentHardinessZone(HardinessZone currentHardinessZone) {
|
||||
Config.currentHardinessZone = currentHardinessZone;
|
||||
}
|
||||
}
|
|
@ -58,14 +58,29 @@ public class JsonTaskDatabase implements TaskDatabase{
|
|||
return taskMap.values().stream().filter(task -> task.isInTimePeriode(start, end)).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method get all Tasks for a specific Crop
|
||||
* @param cropId the cropId
|
||||
* @return List of Tasks for given Crop
|
||||
*/
|
||||
@Override
|
||||
public List<Task> getTaskForCrop(Crop crop) {
|
||||
return null; //TODO implement
|
||||
public List<Task> getTaskForCrop(long cropId) throws IOException {
|
||||
if(taskMap.isEmpty()) {
|
||||
loadTaskListFromFile();
|
||||
}
|
||||
return taskMap.values().stream().filter(task -> task.getCropId() == cropId).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method remove all Tasks for a specific Crop
|
||||
* @param cropId the crop
|
||||
*/
|
||||
@Override
|
||||
public void removeTasksForCrop(Crop crop) {
|
||||
// TODO implement
|
||||
public void removeTasksForCrop(long cropId) throws IOException {
|
||||
if(taskMap.isEmpty()) {
|
||||
loadTaskListFromFile();
|
||||
}
|
||||
taskMap.values().removeIf(task -> task.getCropId() == cropId);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -25,9 +25,20 @@ public interface TaskDatabase {
|
|||
*/
|
||||
List<Task> getTaskList(LocalDate start, LocalDate end) throws IOException;
|
||||
|
||||
List<Task> getTaskForCrop(Crop crop); //TODO Javadoc
|
||||
/**
|
||||
* Method get all Tasks for a specific Crop
|
||||
* @param cropId the cropId
|
||||
* @return List of Tasks for given Crop
|
||||
* @throws IOException If the database cannot be accessed
|
||||
*/
|
||||
List<Task> getTaskForCrop(long cropId) throws IOException;
|
||||
|
||||
void removeTasksForCrop(Crop crop); // TODO Javadoc
|
||||
/**
|
||||
* Method remove all Tasks for a specific Crop
|
||||
* @param cropId the cropId
|
||||
* @throws IOException If the database cannot be accessed
|
||||
*/
|
||||
void removeTasksForCrop(long cropId) throws IOException;
|
||||
|
||||
/**
|
||||
* Saves the {@link Task} in the Cache.
|
||||
|
|
|
@ -1,68 +1,114 @@
|
|||
package ch.zhaw.gartenverwaltung.taskList;
|
||||
|
||||
import ch.zhaw.gartenverwaltung.Config;
|
||||
import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException;
|
||||
import ch.zhaw.gartenverwaltung.io.JsonTaskDatabase;
|
||||
import ch.zhaw.gartenverwaltung.io.PlantDatabase;
|
||||
import ch.zhaw.gartenverwaltung.io.TaskDatabase;
|
||||
import ch.zhaw.gartenverwaltung.types.Crop;
|
||||
import ch.zhaw.gartenverwaltung.types.HardinessZone;
|
||||
import ch.zhaw.gartenverwaltung.types.Plant;
|
||||
import ch.zhaw.gartenverwaltung.types.Task;
|
||||
import ch.zhaw.gartenverwaltung.types.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class TaskListModel {
|
||||
private TaskDatabase taskDatabase;
|
||||
private PlantDatabase plantDatabase;
|
||||
|
||||
/**
|
||||
* Comparators to create sorted Task List
|
||||
*/
|
||||
static final Comparator<Task> sortByStartDate = Comparator.comparing(Task::getStartDate);
|
||||
|
||||
public TaskListModel(){
|
||||
taskDatabase = new JsonTaskDatabase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor to create Database Objects.
|
||||
*/
|
||||
public TaskListModel(TaskDatabase taskDatabase, PlantDatabase plantDatabase) {
|
||||
this.taskDatabase = taskDatabase;
|
||||
this.plantDatabase = plantDatabase;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to save a new Task to Task Database
|
||||
* @param task the Task to save
|
||||
* @throws IOException If the database cannot be accessed
|
||||
*/
|
||||
public void addTask(Task task) throws IOException {
|
||||
taskDatabase.saveTask(task);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to add all Tasks for a new crop
|
||||
* @param crop the crop which is added
|
||||
* @throws PlantNotFoundException if the plantId in the crop doesn't exist in Plant Database
|
||||
* @throws HardinessZoneNotSetException If there is no Hardiness Zone Set in Plant Database
|
||||
* @throws IOException If the database cannot be accessed
|
||||
*/
|
||||
public void planTasksForCrop(Crop crop) throws PlantNotFoundException, HardinessZoneNotSetException, IOException {
|
||||
Plant plant = plantDatabase.getPlantById(HardinessZone.ZONE_8A, crop.getPlantId()).orElseThrow(PlantNotFoundException::new);
|
||||
// TODO new exception
|
||||
// TODO HArdiness Zone
|
||||
return;
|
||||
Plant plant = plantDatabase.getPlantById(Config.getCurrentHardinessZone(), crop.getPlantId()).orElseThrow(PlantNotFoundException::new);
|
||||
for (GrowthPhase growthPhase : plant.lifecycle()) {
|
||||
for (TaskTemplate taskTemplate : growthPhase.taskTemplates()) {
|
||||
addTask(taskTemplate.generateTask(crop.getStartDate(), crop.getCropId().orElse(0L)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void removeTasksForCrop(Crop crop) {
|
||||
//TODO implement
|
||||
taskDatabase.removeTasksForCrop(crop);
|
||||
/**
|
||||
* Method to remove all Tasks for a specific Crop
|
||||
* @param cropId The crop which is removed
|
||||
* @throws IOException If the database cannot be accessed
|
||||
*/
|
||||
public void removeTasksForCrop(long cropId) throws IOException {
|
||||
taskDatabase.removeTasksForCrop(cropId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to remove a Task from Database
|
||||
* @param task the Task to remove
|
||||
* @throws IOException If the database cannot be accessed
|
||||
*/
|
||||
public void removeTask(Task task) throws IOException {
|
||||
taskDatabase.removeTask(task);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get all Tasks
|
||||
* @return List of all Tasks
|
||||
* @throws IOException If the database cannot be accessed
|
||||
*/
|
||||
public List<Task> getTaskList() throws IOException {
|
||||
return getFilteredTaskList(LocalDate.MIN, LocalDate.MAX);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get all Tasks which are today or in future
|
||||
* @return List of Tasks
|
||||
* @throws IOException If the database cannot be accessed
|
||||
*/
|
||||
public List<Task> getFutureTasks() throws IOException {
|
||||
return getFilteredTaskList(LocalDate.now(), LocalDate.MAX);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get all Tasks which are today or in past
|
||||
* @return List of Tasks
|
||||
* @throws IOException If the database cannot be accessed
|
||||
*/
|
||||
public List<Task> getPastTasks() throws IOException {
|
||||
return getFilteredTaskList(LocalDate.MIN, LocalDate.now());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get an Array of 7 Tasklists for the next 7 days. Index 0 is Tasklist for Today.
|
||||
* @return Array with length 7 (List<Task>[])
|
||||
* @throws IOException If the database cannot be accessed
|
||||
*/
|
||||
public List<Task>[] getTasksUpcomingWeek() throws IOException {
|
||||
List<Task>[] listArray = new List[7];
|
||||
for(int i = 0; i < 7; i++) {
|
||||
|
@ -72,10 +118,23 @@ public class TaskListModel {
|
|||
return listArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get Tasklist filtered by date.
|
||||
* @param start the start date for the filter
|
||||
* @param end the end date for the filter
|
||||
* @return List of Tasks matched by the filter
|
||||
* @throws IOException If the database cannot be accessed
|
||||
*/
|
||||
public List<Task> getFilteredTaskList(LocalDate start, LocalDate end) throws IOException {
|
||||
return getSortedTaskList(taskDatabase.getTaskList(start, end), sortByStartDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to sort a Tasklist by a given Comparator
|
||||
* @param taskList The Tasklist to sort
|
||||
* @param comparator the comparator to sort
|
||||
* @return a sorted coppy of the given Tasklist
|
||||
*/
|
||||
private List<Task> getSortedTaskList(List<Task> taskList, Comparator<Task> comparator) {
|
||||
return taskList.stream().sorted(comparator).collect(Collectors.toList());
|
||||
}
|
||||
|
|
|
@ -15,30 +15,33 @@ public class Task {
|
|||
private final LocalDate startDate;
|
||||
private Integer interval;
|
||||
private LocalDate endDate;
|
||||
private Crop cropId;
|
||||
private long cropId;
|
||||
|
||||
/**
|
||||
* default constructor
|
||||
* (used by Json deserializer)
|
||||
*/
|
||||
public Task(){
|
||||
public Task(long cropId){
|
||||
name= "";
|
||||
description= "";
|
||||
startDate = LocalDate.now();
|
||||
this.cropId = cropId;
|
||||
}
|
||||
|
||||
public Task(String name, String description, LocalDate startDate) {
|
||||
public Task(String name, String description, LocalDate startDate, long cropId) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.startDate = startDate;
|
||||
this.cropId = cropId;
|
||||
}
|
||||
|
||||
public Task(String name, String description, LocalDate startDate, LocalDate endDate, int interval) {
|
||||
public Task(String name, String description, LocalDate startDate, LocalDate endDate, int interval, long cropId) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.startDate = startDate;
|
||||
this.endDate = endDate;
|
||||
this.interval = interval;
|
||||
this.cropId = cropId;
|
||||
}
|
||||
|
||||
// Builder-pattern-style setters
|
||||
|
@ -56,10 +59,7 @@ public class Task {
|
|||
}
|
||||
|
||||
public boolean isInTimePeriode(LocalDate searchStartDate, LocalDate searchEndDate){
|
||||
if(startDate.isAfter(searchStartDate) &&startDate.isBefore(searchEndDate)){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return startDate.isAfter(searchStartDate) && startDate.isBefore(searchEndDate);
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
@ -67,6 +67,7 @@ public class Task {
|
|||
public String getName() { return name; }
|
||||
public String getDescription() { return description; }
|
||||
public LocalDate getStartDate() { return startDate; }
|
||||
public long getCropId() { return cropId; }
|
||||
|
||||
public Optional<Integer> getInterval() {
|
||||
return Optional.ofNullable(interval);
|
||||
|
|
|
@ -44,8 +44,8 @@ public class TaskTemplate {
|
|||
this.relativeStartDate = relativeStartDate;
|
||||
}
|
||||
|
||||
public Task generateTask(LocalDate realStartDate) {
|
||||
Task task = new Task(name, description, realStartDate.plusDays(relativeStartDate));
|
||||
public Task generateTask(LocalDate realStartDate, long cropId) {
|
||||
Task task = new Task(name, description, realStartDate.plusDays(relativeStartDate), cropId);
|
||||
if (relativeEndDate != null) {
|
||||
task.withEndDate(realStartDate.plusDays(relativeEndDate));
|
||||
}
|
||||
|
|
|
@ -6,80 +6,42 @@ import org.junit.jupiter.api.*;
|
|||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class JsonTaskDatabaseTest {
|
||||
|
||||
TaskDatabase testDatabase;
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
|
||||
@BeforeEach
|
||||
void connectToDb() {
|
||||
testDatabase = new JsonTaskDatabase();
|
||||
// testDatabase = new JsonTaskDatabase();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@DisplayName("Check if results are retrieved completely")
|
||||
void getTasks(){
|
||||
|
||||
/*
|
||||
List<Task> taskList=null;
|
||||
try {
|
||||
taskList = testDatabase.getTaskList(LocalDate.parse("30.04.2022",formatter),
|
||||
LocalDate.parse("31.05.2022",formatter));
|
||||
taskList = testDatabase.getTaskList(formatter.parse("01.05.2022"), formatter.parse("01.08.2022"));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (ParseException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
Assertions.assertEquals(3,taskList.size());
|
||||
|
||||
}
|
||||
|
||||
//@Disabled("disabled until idProvider works")
|
||||
@Test
|
||||
@DisplayName("Add task.")
|
||||
void addTask(){
|
||||
Task task = new Task("Testtask","This is a test Task.",LocalDate.parse("01.05.2022",formatter));
|
||||
try {
|
||||
testDatabase.saveTask(task);
|
||||
List<Task> taskList=null;
|
||||
try {
|
||||
taskList = testDatabase.getTaskList(LocalDate.parse("30.04.2022",formatter),
|
||||
LocalDate.parse("31.05.2022",formatter));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
Assertions.assertEquals(4,taskList.size());
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
Assertions.assertTrue(taskList.size()>0);
|
||||
*/
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Remove task.")
|
||||
void removeTask(){
|
||||
Task task = new Task("Dummy","Dummy",LocalDate.parse("31.05.2022",formatter)).withId(2);
|
||||
try {
|
||||
testDatabase.removeTask(task);
|
||||
List<Task> taskList=null;
|
||||
try {
|
||||
taskList = testDatabase.getTaskList(LocalDate.parse("30.04.2022",formatter),
|
||||
LocalDate.parse("31.05.2022",formatter));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
Assertions.assertEquals(2,taskList.size());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
void getTaskForCrop() {
|
||||
// TODO implement Test
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void removeTasksForCrop() {
|
||||
// TODO implement Test
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue