#27 implementation of task template with task generation

This commit is contained in:
Gian-Andrea Hutter 2022-10-31 12:24:54 +01:00
parent 8120071348
commit 6273b0e59a
2 changed files with 105 additions and 4 deletions

View File

@ -1,8 +1,106 @@
package ch.zhaw.gartenverwaltung.gardenplan;
import ch.zhaw.gartenverwaltung.io.GardenPlan;
import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException;
import ch.zhaw.gartenverwaltung.io.TaskDatabase;
import ch.zhaw.gartenverwaltung.io.PlantDatabase;
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;
// liste von crops
//task generieren
//plant holen, task template mit tasktemplateklasse tasks erstellen
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, HardinessZoneNotSetException {
//TaskListModel.generateTasksForCrop(crop)
long plantId = crop.getPlantId();
Optional plant;
Task task;
Plant plantL = plant;
plant = plantDatabase.getPlantById(HardinessZone.ZONE_8A, plantId);
if(plant.isPresent()){
List<GrowthPhase> growthPhases = plantL.lifecycleForGroup(0);
GrowthPhase growthPhase = growthPhases.get(0);
List<TaskTemplate> taskTemplatesForLyfecycle= growthPhase.taskTemplates();
task = taskTemplatesForLyfecycle.get(0).generateTask(crop.getStartDate());
taskDatabase.saveTask(task);
} else {
}
//TODO need to get the plants templates, or through the Growphase
// plant.getPlantTemplates
// CreateTaskWithTasktemplate
}
}

View File

@ -5,6 +5,7 @@ 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;
@ -34,6 +35,8 @@ public class JsonTaskDatabase implements TaskDatabase{
static {
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateDeserializer dateDeserializer = new LocalDateDeserializer(dateFormat);
//TODO Serializer
LocalDateSerializer dateSerializer = new LocalDateSerializer(dateFormat);
timeModule.addDeserializer(LocalDate.class, dateDeserializer);
}