Merge pull request #49 from schrom01/feature_gardenplan-model_M2
Feature gardenplan model m2
This commit is contained in:
@@ -1,8 +1,92 @@
|
||||
package ch.zhaw.gartenverwaltung.gardenplan;
|
||||
|
||||
import ch.zhaw.gartenverwaltung.io.GardenPlan;
|
||||
import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException;
|
||||
import ch.zhaw.gartenverwaltung.io.JsonGardenPlan;
|
||||
import ch.zhaw.gartenverwaltung.taskList.PlantNotFoundException;
|
||||
import ch.zhaw.gartenverwaltung.taskList.TaskListModel;
|
||||
import ch.zhaw.gartenverwaltung.types.Crop;
|
||||
import ch.zhaw.gartenverwaltung.types.Plant;
|
||||
import ch.zhaw.gartenverwaltung.types.Task;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
/**
|
||||
* The Gardenplan model manages the crops in the gardenplan.
|
||||
*/
|
||||
public class Gardenplanmodel {
|
||||
// private JasonGardenplan gardenplan;
|
||||
// liste von crops
|
||||
//task generieren
|
||||
//plant holen, task template mit tasktemplateklasse tasks erstellen
|
||||
private GardenPlan gardenPlan;
|
||||
private List<Crop> cropList;
|
||||
private TaskListModel taskListModel;
|
||||
private Object IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Constructor of Gardenplan model
|
||||
*
|
||||
* @param taskListModel holds a reference to the task list object.
|
||||
*/
|
||||
public Gardenplanmodel(TaskListModel taskListModel) throws IOException {
|
||||
this.taskListModel = taskListModel;
|
||||
gardenPlan = new JsonGardenPlan();
|
||||
cropList = new ArrayList<>();
|
||||
cropList = gardenPlan.getCrops();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Crop with a {@link Plant} and the planting date of the plant. Then let the Tasklistmodel create the
|
||||
* gardening {@link Task} for the crop. Store the crop in the gardenplan file and the cache.
|
||||
*
|
||||
* @param plant The plant which is wnated to be planted
|
||||
* @param plantingDate The date, when the plant is planted
|
||||
* @throws IOException If the database cannot be accessed
|
||||
* @throws HardinessZoneNotSetException If the hardinesszone could not be added
|
||||
* @throws PlantNotFoundException If the plant is not found in the database.
|
||||
*/
|
||||
public void plantAsCrop(Plant plant, LocalDate plantingDate) throws IOException, HardinessZoneNotSetException, PlantNotFoundException {
|
||||
Crop crop = new Crop(plant.id(), plantingDate);
|
||||
//TODO Add Area to Plant
|
||||
//crop.withArea(0);
|
||||
gardenPlan.saveCrop(crop);
|
||||
taskListModel.planTasksForCrop(crop);
|
||||
cropList = gardenPlan.getCrops();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a {@link Crop} from the file and the cache
|
||||
*
|
||||
* @param crop The plant which is wnated to be planted
|
||||
* @throws IOException If the database cannot be accessed
|
||||
*/
|
||||
public void removeCrop(Crop crop) throws IOException {
|
||||
gardenPlan.removeCrop(crop);
|
||||
taskListModel.removeTasksForCrop(crop.getCropId().orElseThrow(IllegalArgumentException::new));
|
||||
cropList = gardenPlan.getCrops();
|
||||
}
|
||||
/**
|
||||
* Returns a list of {@link Crop}s which are currently in the gardenplan.
|
||||
*
|
||||
* @throws IOException If the database cannot be accessed
|
||||
*/
|
||||
public List<Crop> getCrops() throws IOException {
|
||||
if(!cropList.isEmpty()){
|
||||
cropList = gardenPlan.getCrops();
|
||||
}
|
||||
return cropList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an Optional of {@link Crop} if the crop is the gardenplan
|
||||
*
|
||||
* @param cropId The date, when the plant is planted
|
||||
* @throws IOException If the database cannot be accessed
|
||||
*/
|
||||
public Optional<Crop> getCrop(Long cropId) throws IOException {
|
||||
return gardenPlan.getCropById(cropId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,11 +18,11 @@ public interface GardenPlan {
|
||||
/**
|
||||
* Attempts to retrieve the {@link Crop} with the specified cropId.
|
||||
*
|
||||
* @param id The {@link Crop#cropId} to look for
|
||||
* @param cropId The {@link Crop} to look for
|
||||
* @return {@link Optional} of the found {@link Crop}, {@link Optional#empty()} if no entry matched the criteria
|
||||
* @throws IOException If there is a problem reading from or writing to the database
|
||||
*/
|
||||
Optional<Crop> getCropById(long id) throws IOException;
|
||||
Optional<Crop> getCropById(long cropId) throws IOException;
|
||||
|
||||
/**
|
||||
* Saves a Crop to the Database.
|
||||
|
||||
@@ -6,9 +6,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;
|
||||
@@ -25,6 +27,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();
|
||||
|
||||
@@ -35,7 +38,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);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -121,12 +127,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,7 +148,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);
|
||||
@@ -148,5 +159,8 @@ public class JsonTaskDatabase implements TaskDatabase{
|
||||
(res, task) -> res.put(task.getId(), task),
|
||||
(existing, replacement) -> {});
|
||||
}
|
||||
|
||||
Long maxId = taskMap.isEmpty() ? 0L : Collections.max(taskMap.keySet());
|
||||
idProvider = new IdProvider(maxId);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user