Compare commits

..
4 changed files with 192 additions and 47 deletions
@@ -1,7 +1,93 @@
package ch.zhaw.gartenverwaltung.gardenplan;
import ch.zhaw.gartenverwaltung.io.*;
import ch.zhaw.gartenverwaltung.types.*;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
public class Gardenplanmodel {
// private JasonGardenplan gardenplan;
private GardenPlan gardenplan;
private TaskDatabase taskDatabase;
private PlantDatabase plantDatabase;
// private TaskListmodel;
private List<Crop> cropList = Collections.emptyList();
public Gardenplanmodel() {
try {
cropList = gardenplan.getCrops();
} catch (IOException ioException){
System.err.println("The task can not be created!");
}
}
public void plantCrop(Crop crop){
cropList.add(crop);
try {
gardenplan.saveCrop(crop);
createGardeningTasks(crop);
} catch (IOException ioException){
System.err.println("The task can not be created!");
}
}
public void removeCrop(Crop crop){
cropList.remove(crop);
try {
gardenplan.removeCrop(crop);
//TaskListModel.removeTasksForCrop(crop)
} catch (IOException ioException){
System.err.println("The task can not be created!");
}
}
public List<Crop> getCrops(){
if(cropList.isEmpty()){
try {
cropList = gardenplan.getCrops();
} catch (IOException ioException){
System.err.println("Crops could not be loaded");
}
}
return cropList;
}
public Optional<Crop> getCrop(Long id){
try {
return gardenplan.getCropById(id);
} catch (IOException ioException){
System.err.println("The task can not be created!");
}
return null;
}
private void createGardeningTasks(Crop crop) throws IOException {
//TaskListModel.generateTasksForCrop(crop)
long plantId = crop.getPlantId();
Optional plant;
try {
plant = plantDatabase.getPlantById(HardinessZone.ZONE_8A, plantId);
} catch (IOException ioException){
} catch (HardinessZoneNotSetException hardinessZoneNotSetException){
System.out.println("Hardinesszone not set!");
}
//TODO need to get the plants templates
// plant.getPlantTemplates
// CreateTaskWithTasktemplate
//taskDatabase.saveTask(task);
}
// liste von crops
//task generieren
//plant holen, task template mit tasktemplateklasse tasks erstellen
@@ -5,9 +5,11 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
@@ -24,6 +26,7 @@ import java.util.Map;
public class JsonTaskDatabase implements TaskDatabase{
IdProvider idProvider;
private final URL dataSource = getClass().getResource("taskdb.json");
private final static String INVALID_DATASOURCE_MSG = "Invalid datasource specified!";
private Map<Long, Task> taskMap = Collections.emptyMap();
@@ -34,7 +37,10 @@ public class JsonTaskDatabase implements TaskDatabase{
static {
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateDeserializer dateDeserializer = new LocalDateDeserializer(dateFormat);
LocalDateSerializer dateSerializer = new LocalDateSerializer(dateFormat);
timeModule.addDeserializer(LocalDate.class, dateDeserializer);
timeModule.addSerializer(LocalDate.class, dateSerializer);
}
/**
@@ -95,12 +101,16 @@ public class JsonTaskDatabase implements TaskDatabase{
* @throws IOException If the database cannot be accessed
*/
private void writeTaskListToFile() throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(timeModule)
.registerModule(new Jdk8Module());
if(dataSource != null) {
mapper.writeValue(new File(dataSource.getFile()), taskMap);
try {
new ObjectMapper()
.registerModule(timeModule)
.registerModule(new Jdk8Module())
.writeValue(new File(dataSource.toURI()), taskMap.values());
} catch (URISyntaxException e) {
throw new IOException(INVALID_DATASOURCE_MSG, e);
}
}
}
@@ -112,7 +122,8 @@ public class JsonTaskDatabase implements TaskDatabase{
private void loadTaskListFromFile() throws IOException {
if (dataSource != null) {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(timeModule);
mapper.registerModule(timeModule)
.registerModule(new Jdk8Module());
List<Task> result;
result = mapper.readerForListOf(Task.class).readValue(dataSource);
@@ -1,50 +1,50 @@
[
{
"id" : 1,
"name" : "sow plant",
"description": "Plant the seeds, crops in de bed.",
"startDate" : "2022-05-01",
"endDate" : "2022-05-01",
"interval" : 0
"id": 1,
"name": "sow plant",
"description": "Plant the seeds, crops in de bed.",
"startDate": "2022-05-01",
"endDate": "2022-05-01",
"interval": 0
},
{
"id" : 2,
"name" : "water plant",
"id": 2,
"name": "water plant",
"description": "water the plant, so that the soil is wet around the plant.",
"startDate" : "2022-05-01",
"endDate" : "2022-09-01",
"interval" : 2
"startDate": "2022-05-01",
"endDate": "2022-09-01",
"interval": 2
},
{
"id" : 3,
"name" : "fertilize plant",
"id": 3,
"name": "fertilize plant",
"description": "The fertilizer has to be mixed with water. Then fertilize the plants soil with the mixture",
"startDate" : "2022-06-01",
"endDate" : "2022-08-01",
"interval" : 28
"startDate": "2022-06-01",
"endDate": "2022-08-01",
"interval": 28
},
{
"id" : 4,
"name" : "covering plant",
"id": 4,
"name": "covering plant",
"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",
"endDate" : "2022-07-01",
"interval" : 0
"startDate": "2022-07-01",
"endDate": "2022-07-01",
"interval": 0
},
{
"id" : 5,
"name" : "look after plant",
"id": 5,
"name": "look after plant",
"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",
"endDate" : "2022-09-01",
"interval" : 5
"startDate": "2022-05-01",
"endDate": "2022-09-01",
"interval": 5
},
{
"id" : 6,
"name" : "harvest plant",
"id": 6,
"name": "harvest plant",
"description": "Pull the ripe vegetables out from the soil. Clean them with clear, fresh water. ",
"startDate" : "2022-09-01",
"endDate" : "2022-09-01",
"interval" : 0
"startDate": "2022-09-01",
"endDate": "2022-09-01",
"interval": 0
}
]
]
@@ -6,32 +6,80 @@ 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;
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("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(formatter.parse("01.05.2022"), formatter.parse("01.08.2022"));
taskList = testDatabase.getTaskList(LocalDate.parse("30.04.2022",formatter),
LocalDate.parse("31.05.2022",formatter));
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ParseException e) {
throw new RuntimeException(e);
}
Assertions.assertTrue(taskList.size()>0);
*/
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);
}
}
@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);
}
}
}