Compare commits
No commits in common. "541217c2810a1ba389305ea16f73c7d7023334cd" and "25057d34f0a9d38e54e5f36974bd15f3b477311b" have entirely different histories.
541217c281
...
25057d34f0
|
@ -1,19 +0,0 @@
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,6 +1,5 @@
|
||||||
package ch.zhaw.gartenverwaltung.io;
|
package ch.zhaw.gartenverwaltung.io;
|
||||||
|
|
||||||
import ch.zhaw.gartenverwaltung.types.Crop;
|
|
||||||
import ch.zhaw.gartenverwaltung.types.Task;
|
import ch.zhaw.gartenverwaltung.types.Task;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
|
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
|
||||||
|
@ -52,31 +51,6 @@ public class JsonTaskDatabase implements TaskDatabase{
|
||||||
return taskMap.values().stream().filter(task -> task.isInTimePeriode(start, end)).toList();
|
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(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(long cropId) throws IOException {
|
|
||||||
if(taskMap.isEmpty()) {
|
|
||||||
loadTaskListFromFile();
|
|
||||||
}
|
|
||||||
taskMap.values().removeIf(task -> task.getCropId() == cropId);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If no data is currently loaded, data is loaded from {@link #dataSource}.
|
* If no data is currently loaded, data is loaded from {@link #dataSource}.
|
||||||
* If the {@link Task} has an id than the task is added to the {@link #taskMap}
|
* If the {@link Task} has an id than the task is added to the {@link #taskMap}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package ch.zhaw.gartenverwaltung.io;
|
package ch.zhaw.gartenverwaltung.io;
|
||||||
|
|
||||||
import ch.zhaw.gartenverwaltung.types.Crop;
|
|
||||||
import ch.zhaw.gartenverwaltung.types.HardinessZone;
|
import ch.zhaw.gartenverwaltung.types.HardinessZone;
|
||||||
import ch.zhaw.gartenverwaltung.types.Plant;
|
import ch.zhaw.gartenverwaltung.types.Plant;
|
||||||
import ch.zhaw.gartenverwaltung.types.Task;
|
import ch.zhaw.gartenverwaltung.types.Task;
|
||||||
|
@ -25,21 +24,6 @@ public interface TaskDatabase {
|
||||||
*/
|
*/
|
||||||
List<Task> getTaskList(LocalDate start, LocalDate end) throws IOException;
|
List<Task> getTaskList(LocalDate start, LocalDate end) throws IOException;
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
* Saves the {@link Task} in the Cache.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
package ch.zhaw.gartenverwaltung.taskList;
|
|
||||||
|
|
||||||
public class PlantNotFoundException extends Exception {
|
|
||||||
public PlantNotFoundException() {
|
|
||||||
super("The selected Plant was not found in Database!");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,11 +1,8 @@
|
||||||
package ch.zhaw.gartenverwaltung.taskList;
|
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.JsonTaskDatabase;
|
||||||
import ch.zhaw.gartenverwaltung.io.PlantDatabase;
|
|
||||||
import ch.zhaw.gartenverwaltung.io.TaskDatabase;
|
import ch.zhaw.gartenverwaltung.io.TaskDatabase;
|
||||||
import ch.zhaw.gartenverwaltung.types.*;
|
import ch.zhaw.gartenverwaltung.types.Task;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
@ -15,100 +12,39 @@ import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class TaskListModel {
|
public class TaskListModel {
|
||||||
private TaskDatabase taskDatabase;
|
private TaskDatabase taskDatabase;
|
||||||
private PlantDatabase plantDatabase;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Comparators to create sorted Task List
|
|
||||||
*/
|
|
||||||
static final Comparator<Task> sortByStartDate = Comparator.comparing(Task::getStartDate);
|
static final Comparator<Task> sortByStartDate = Comparator.comparing(Task::getStartDate);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public TaskListModel(){
|
public TaskListModel(){
|
||||||
taskDatabase = new JsonTaskDatabase();
|
taskDatabase = new JsonTaskDatabase();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public TaskListModel(TaskDatabase taskDatabase) {
|
||||||
* Constructor to create Database Objects.
|
|
||||||
*/
|
|
||||||
public TaskListModel(TaskDatabase taskDatabase, PlantDatabase plantDatabase) {
|
|
||||||
this.taskDatabase = taskDatabase;
|
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 {
|
public void addTask(Task task) throws IOException {
|
||||||
taskDatabase.saveTask(task);
|
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(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)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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 {
|
public void removeTask(Task task) throws IOException {
|
||||||
taskDatabase.removeTask(task);
|
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 {
|
public List<Task> getTaskList() throws IOException {
|
||||||
return getFilteredTaskList(LocalDate.MIN, LocalDate.MAX);
|
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 {
|
public List<Task> getFutureTasks() throws IOException {
|
||||||
return getFilteredTaskList(LocalDate.now(), LocalDate.MAX);
|
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 {
|
public List<Task> getPastTasks() throws IOException {
|
||||||
return getFilteredTaskList(LocalDate.MIN, LocalDate.now());
|
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 {
|
public List<Task>[] getTasksUpcomingWeek() throws IOException {
|
||||||
List<Task>[] listArray = new List[7];
|
List<Task>[] listArray = new List[7];
|
||||||
for(int i = 0; i < 7; i++) {
|
for(int i = 0; i < 7; i++) {
|
||||||
|
@ -118,23 +54,10 @@ public class TaskListModel {
|
||||||
return listArray;
|
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 {
|
public List<Task> getFilteredTaskList(LocalDate start, LocalDate end) throws IOException {
|
||||||
return getSortedTaskList(taskDatabase.getTaskList(start, end), sortByStartDate);
|
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) {
|
private List<Task> getSortedTaskList(List<Task> taskList, Comparator<Task> comparator) {
|
||||||
return taskList.stream().sorted(comparator).collect(Collectors.toList());
|
return taskList.stream().sorted(comparator).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,33 +15,29 @@ public class Task {
|
||||||
private final LocalDate startDate;
|
private final LocalDate startDate;
|
||||||
private Integer interval;
|
private Integer interval;
|
||||||
private LocalDate endDate;
|
private LocalDate endDate;
|
||||||
private long cropId;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* default constructor
|
* default constructor
|
||||||
* (used by Json deserializer)
|
* (used by Json deserializer)
|
||||||
*/
|
*/
|
||||||
public Task(long cropId){
|
public Task(){
|
||||||
name= "";
|
name= "";
|
||||||
description= "";
|
description= "";
|
||||||
startDate = LocalDate.now();
|
startDate = LocalDate.now();
|
||||||
this.cropId = cropId;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task(String name, String description, LocalDate startDate, long cropId) {
|
public Task(String name, String description, LocalDate startDate) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.startDate = startDate;
|
this.startDate = startDate;
|
||||||
this.cropId = cropId;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task(String name, String description, LocalDate startDate, LocalDate endDate, int interval, long cropId) {
|
public Task(String name, String description, LocalDate startDate, LocalDate endDate, int interval) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.startDate = startDate;
|
this.startDate = startDate;
|
||||||
this.endDate = endDate;
|
this.endDate = endDate;
|
||||||
this.interval = interval;
|
this.interval = interval;
|
||||||
this.cropId = cropId;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Builder-pattern-style setters
|
// Builder-pattern-style setters
|
||||||
|
@ -59,7 +55,10 @@ public class Task {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isInTimePeriode(LocalDate searchStartDate, LocalDate searchEndDate){
|
public boolean isInTimePeriode(LocalDate searchStartDate, LocalDate searchEndDate){
|
||||||
return startDate.isAfter(searchStartDate) && startDate.isBefore(searchEndDate);
|
if(startDate.isAfter(searchStartDate) &&startDate.isBefore(searchEndDate)){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
|
@ -67,7 +66,6 @@ public class Task {
|
||||||
public String getName() { return name; }
|
public String getName() { return name; }
|
||||||
public String getDescription() { return description; }
|
public String getDescription() { return description; }
|
||||||
public LocalDate getStartDate() { return startDate; }
|
public LocalDate getStartDate() { return startDate; }
|
||||||
public long getCropId() { return cropId; }
|
|
||||||
|
|
||||||
public Optional<Integer> getInterval() {
|
public Optional<Integer> getInterval() {
|
||||||
return Optional.ofNullable(interval);
|
return Optional.ofNullable(interval);
|
||||||
|
|
|
@ -44,8 +44,8 @@ public class TaskTemplate {
|
||||||
this.relativeStartDate = relativeStartDate;
|
this.relativeStartDate = relativeStartDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task generateTask(LocalDate realStartDate, long cropId) {
|
public Task generateTask(LocalDate realStartDate) {
|
||||||
Task task = new Task(name, description, realStartDate.plusDays(relativeStartDate), cropId);
|
Task task = new Task(name, description, realStartDate.plusDays(relativeStartDate));
|
||||||
if (relativeEndDate != null) {
|
if (relativeEndDate != null) {
|
||||||
task.withEndDate(realStartDate.plusDays(relativeEndDate));
|
task.withEndDate(realStartDate.plusDays(relativeEndDate));
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,8 +5,7 @@
|
||||||
"description": "Plant the seeds, crops in de bed.",
|
"description": "Plant the seeds, crops in de bed.",
|
||||||
"startDate" : "2022-05-01",
|
"startDate" : "2022-05-01",
|
||||||
"endDate" : "2022-05-01",
|
"endDate" : "2022-05-01",
|
||||||
"interval" : 0,
|
"interval" : 0
|
||||||
"cropID" : 0
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id" : 2,
|
"id" : 2,
|
||||||
|
@ -14,8 +13,7 @@
|
||||||
"description": "water the plant, so that the soil is wet around the plant.",
|
"description": "water the plant, so that the soil is wet around the plant.",
|
||||||
"startDate" : "2022-05-01",
|
"startDate" : "2022-05-01",
|
||||||
"endDate" : "2022-09-01",
|
"endDate" : "2022-09-01",
|
||||||
"interval" : 2,
|
"interval" : 2
|
||||||
"cropID" : 0
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id" : 3,
|
"id" : 3,
|
||||||
|
@ -23,8 +21,7 @@
|
||||||
"description": "The fertilizer has to be mixed with water. Then fertilize the plants soil with the mixture",
|
"description": "The fertilizer has to be mixed with water. Then fertilize the plants soil with the mixture",
|
||||||
"startDate" : "2022-06-01",
|
"startDate" : "2022-06-01",
|
||||||
"endDate" : "2022-08-01",
|
"endDate" : "2022-08-01",
|
||||||
"interval" : 28,
|
"interval" : 28
|
||||||
"cropID" : 0
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id" : 4,
|
"id" : 4,
|
||||||
|
@ -32,8 +29,7 @@
|
||||||
"description": "Take a big enough coverage for the plants. Cover the whole plant with a bit space between the plant and the coverage",
|
"description": "Take a big enough coverage for the plants. Cover the whole plant with a bit space between the plant and the coverage",
|
||||||
"startDate" : "2022-07-01",
|
"startDate" : "2022-07-01",
|
||||||
"endDate" : "2022-07-01",
|
"endDate" : "2022-07-01",
|
||||||
"interval" : 0,
|
"interval" : 0
|
||||||
"cropID" : 0
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id" : 5,
|
"id" : 5,
|
||||||
|
@ -41,8 +37,7 @@
|
||||||
"description": "Look for pest or illness at the leaves of the plant. Check the soil around the plant, if the roots are enough covered with soil",
|
"description": "Look for pest or illness at the leaves of the plant. Check the soil around the plant, if the roots are enough covered with soil",
|
||||||
"startDate" : "2022-05-01",
|
"startDate" : "2022-05-01",
|
||||||
"endDate" : "2022-09-01",
|
"endDate" : "2022-09-01",
|
||||||
"interval" : 5,
|
"interval" : 5
|
||||||
"cropID" : 0
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id" : 6,
|
"id" : 6,
|
||||||
|
@ -50,7 +45,6 @@
|
||||||
"description": "Pull the ripe vegetables out from the soil. Clean them with clear, fresh water. ",
|
"description": "Pull the ripe vegetables out from the soil. Clean them with clear, fresh water. ",
|
||||||
"startDate" : "2022-09-01",
|
"startDate" : "2022-09-01",
|
||||||
"endDate" : "2022-09-01",
|
"endDate" : "2022-09-01",
|
||||||
"interval" : 0,
|
"interval" : 0
|
||||||
"cropID" : 0
|
|
||||||
}
|
}
|
||||||
]
|
]
|
|
@ -34,14 +34,4 @@ public class JsonTaskDatabaseTest {
|
||||||
Assertions.assertTrue(taskList.size()>0);
|
Assertions.assertTrue(taskList.size()>0);
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void getTaskForCrop() {
|
|
||||||
// TODO implement Test
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void removeTasksForCrop() {
|
|
||||||
// TODO implement Test
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue