diff --git a/src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java b/src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java index 24757bd..c2cabee 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/gardenplan/Gardenplanmodel.java @@ -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 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 getCrops(){ + if(cropList.isEmpty()){ + try { + cropList = gardenplan.getCrops(); + } catch (IOException ioException){ + System.err.println("Crops could not be loaded"); + } + + } + return cropList; + } + + public Optional 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 growthPhases = plantL.lifecycleForGroup(0); + GrowthPhase growthPhase = growthPhases.get(0); + List 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 + + } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java index e16a74d..82d5812 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonTaskDatabase.java @@ -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); }