Skip to content
Snippets Groups Projects
Commit 894f94e8 authored by Ondřej Hrdlička's avatar Ondřej Hrdlička
Browse files

Made Category use CategoryTime for its timeSpent

parent 8214d3e5
No related branches found
No related tags found
2 merge requests!52Final project MR,!45Activating most of the app
Pipeline #
...@@ -7,6 +7,9 @@ import org.joda.time.format.PeriodFormatter; ...@@ -7,6 +7,9 @@ import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder; import org.joda.time.format.PeriodFormatterBuilder;
import java.awt.Color; import java.awt.Color;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects; import java.util.Objects;
public class Category implements Identifiable { public class Category implements Identifiable {
...@@ -14,16 +17,12 @@ public class Category implements Identifiable { ...@@ -14,16 +17,12 @@ public class Category implements Identifiable {
private Long id; private Long id;
private String name; private String name;
private Color color; private Color color;
private Duration timeSpent; private List<CategoryTime> timeSpents;
private final PeriodFormatter periodicFormatter; private final PeriodFormatter periodicFormatter;
public Category(String name, Color color) { public Category(String name, Color color) {
this(name, color, new Duration(0)); this.timeSpents = new ArrayList<>();
}
public Category(String name, Color color, Duration duration) {
timeSpent = duration;
this.name = name; this.name = name;
this.color = color; this.color = color;
this.periodicFormatter = createFormatter(); this.periodicFormatter = createFormatter();
...@@ -51,32 +50,28 @@ public class Category implements Identifiable { ...@@ -51,32 +50,28 @@ public class Category implements Identifiable {
} }
public String getTimeDurationFormatted() { public String getTimeDurationFormatted() {
final var dayTimePeriod = timeSpent.toPeriod().normalizedStandard(PeriodType.dayTime()); final var dayTimePeriod = getTotalDuration().toPeriod().normalizedStandard(PeriodType.dayTime());
return periodicFormatter.print(dayTimePeriod).strip(); return periodicFormatter.print(dayTimePeriod).strip();
} }
public void addTime(Duration duration) { public Duration getTotalDuration() {
timeSpent = timeSpent.plus(duration); return Duration.standardHours(this.timeSpents.stream().map(CategoryTime::getTimeInHours).reduce(0L, Long::sum));
} }
public void addTime(Long millis) { public void setTimeSpents(List<CategoryTime> timeSpents) {
addTime(Duration.millis(millis)); this.timeSpents = timeSpents;
} }
public void addTime(int hours) { public List<CategoryTime> getTimeSpents() {
addTime(Duration.standardHours(hours)); return timeSpents;
} }
public void subtractTime(int hours) { public void addTime(Long hours, Long taskId) {
timeSpent = timeSpent.minus(Duration.standardHours(hours)); timeSpents.add(new CategoryTime(LocalDate.now(), hours, taskId));
} }
public void resetTime() { public void subtractTime(Long taskId) {
timeSpent = new Duration(0); timeSpents.removeIf(categoryTime -> categoryTime.getTaskId().equals(taskId));
}
public Long getTimeInMillis() {
return timeSpent.getMillis();
} }
private PeriodFormatter createFormatter() { private PeriodFormatter createFormatter() {
...@@ -88,7 +83,6 @@ public class Category implements Identifiable { ...@@ -88,7 +83,6 @@ public class Category implements Identifiable {
.toFormatter(); .toFormatter();
} }
public Long getId() { public Long getId() {
return id; return id;
} }
......
...@@ -2,6 +2,7 @@ package cz.muni.fi.pv168.project.db.category; ...@@ -2,6 +2,7 @@ package cz.muni.fi.pv168.project.db.category;
import cz.muni.fi.pv168.project.data.category.Category; import cz.muni.fi.pv168.project.data.category.Category;
import cz.muni.fi.pv168.project.db.DataAccessException; import cz.muni.fi.pv168.project.db.DataAccessException;
import cz.muni.fi.pv168.project.db.categorytime.CategoryTimeDao;
import cz.muni.fi.pv168.project.db.interfaces.DataAccessObject; import cz.muni.fi.pv168.project.db.interfaces.DataAccessObject;
import javax.sql.DataSource; import javax.sql.DataSource;
...@@ -31,11 +32,10 @@ public class CategoryDao implements DataAccessObject<Category> { ...@@ -31,11 +32,10 @@ public class CategoryDao implements DataAccessObject<Category> {
try (var connection = dataSource.getConnection(); try (var connection = dataSource.getConnection();
var st = connection.prepareStatement( var st = connection.prepareStatement(
"INSERT INTO CATEGORY (TITLE, COLOR, TIME_SPENT) VALUES (?, ?, ?)", "INSERT INTO CATEGORY (TITLE, COLOR) VALUES (?, ?)",
Statement.RETURN_GENERATED_KEYS)) { Statement.RETURN_GENERATED_KEYS)) {
st.setString(1, entity.getName()); st.setString(1, entity.getName());
st.setInt(2, entity.getColor().getRGB()); st.setInt(2, entity.getColor().getRGB());
st.setLong(3, entity.getTimeInMillis());
st.executeUpdate(); st.executeUpdate();
...@@ -60,7 +60,7 @@ public class CategoryDao implements DataAccessObject<Category> { ...@@ -60,7 +60,7 @@ public class CategoryDao implements DataAccessObject<Category> {
@Override @Override
public Collection<Category> getAll() throws DataAccessException { public Collection<Category> getAll() throws DataAccessException {
try (var connection = dataSource.getConnection(); try (var connection = dataSource.getConnection();
var st = connection.prepareStatement("SELECT ID, TITLE, COLOR, TIME_SPENT FROM CATEGORY")) { var st = connection.prepareStatement("SELECT ID, TITLE, COLOR FROM CATEGORY")) {
List<Category> categories = new ArrayList<>(); List<Category> categories = new ArrayList<>();
try (var rs = st.executeQuery()) { try (var rs = st.executeQuery()) {
...@@ -71,7 +71,7 @@ public class CategoryDao implements DataAccessObject<Category> { ...@@ -71,7 +71,7 @@ public class CategoryDao implements DataAccessObject<Category> {
return categories; return categories;
} catch (SQLException ex) { } catch (SQLException ex) {
throw new DataAccessException(""); throw new DataAccessException("Failed to load all categories", ex);
} }
} }
...@@ -83,16 +83,22 @@ public class CategoryDao implements DataAccessObject<Category> { ...@@ -83,16 +83,22 @@ public class CategoryDao implements DataAccessObject<Category> {
try (var connection = dataSource.getConnection(); try (var connection = dataSource.getConnection();
var st = connection.prepareStatement( var st = connection.prepareStatement(
"UPDATE CATEGORY SET TITLE = ?, COLOR = ?, TIME_SPENT = ? WHERE ID = ?")) { "UPDATE CATEGORY SET TITLE = ?, COLOR = ? WHERE ID = ?")) {
st.setString(1, entity.getName()); st.setString(1, entity.getName());
st.setInt(2, entity.getColor().getRGB()); st.setInt(2, entity.getColor().getRGB());
st.setLong(3, entity.getTimeInMillis()); st.setLong(3, entity.getId());
st.setLong(4, entity.getId());
int rowsUpdated = st.executeUpdate(); int rowsUpdated = st.executeUpdate();
if (rowsUpdated == 0) { if (rowsUpdated == 0) {
throw new DataAccessException("Failed to update non-existing category: " + entity); throw new DataAccessException("Failed to update non-existing category: " + entity);
} }
// cleanup previous
categoryTimeDao.deleteAllEntitiesFor(entity.getId());
// add current
for (var subtask : entity.getTimeSpents()) {
categoryTimeDao.addEntityFor(entity.getId(), subtask);
}
} catch (SQLException ex) { } catch (SQLException ex) {
throw new DataAccessException("Failed to update category " + entity, ex); throw new DataAccessException("Failed to update category " + entity, ex);
} }
...@@ -121,7 +127,7 @@ public class CategoryDao implements DataAccessObject<Category> { ...@@ -121,7 +127,7 @@ public class CategoryDao implements DataAccessObject<Category> {
public Category fetch(Long id) { public Category fetch(Long id) {
try (var connection = dataSource.getConnection(); try (var connection = dataSource.getConnection();
var st = connection.prepareStatement( var st = connection.prepareStatement(
"SELECT ID, TITLE, COLOR, TIME_SPENT FROM CATEGORY WHERE ID = ?")) { "SELECT ID, TITLE, COLOR FROM CATEGORY WHERE ID = ?")) {
st.setLong(1, id); st.setLong(1, id);
try (var rs = st.executeQuery()) { try (var rs = st.executeQuery()) {
...@@ -142,13 +148,14 @@ public class CategoryDao implements DataAccessObject<Category> { ...@@ -142,13 +148,14 @@ public class CategoryDao implements DataAccessObject<Category> {
} }
private Category getFullCategoryFromDb(ResultSet rs) throws SQLException { private Category getFullCategoryFromDb(ResultSet rs) throws SQLException {
var id = rs.getLong("ID");
var category = new Category( var category = new Category(
rs.getString("TITLE"), rs.getString("TITLE"),
new Color(rs.getInt("COLOR")) new Color(rs.getInt("COLOR"))
); );
category.addTime(rs.getLong("TIME_SPENT")); category.setId(id);
category.setId(rs.getLong("ID")); category.setTimeSpents(categoryTimeDao.getAllEntitiesFor(id));
return category; return category;
} }
......
...@@ -33,8 +33,7 @@ public class CategoryManager implements DataAccessManager { ...@@ -33,8 +33,7 @@ public class CategoryManager implements DataAccessManager {
return "CREATE TABLE " + getTableName() + "(" + return "CREATE TABLE " + getTableName() + "(" +
"ID BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY," + "ID BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY," +
"COLOR INT," + "COLOR INT," +
"TITLE VARCHAR(256)," + "TITLE VARCHAR(256)" +
"TIME_SPENT BIGINT" +
")"; ")";
} }
} }
...@@ -139,12 +139,13 @@ public class TaskView implements Tab<Task> { ...@@ -139,12 +139,13 @@ public class TaskView implements Tab<Task> {
@Override @Override
public void updateTo() { public void updateTo() {
var selectedTask = headerList.getSelectedValue(); var selectedTask = headerList.getSelectedValue();
selectedTask.getCategories().forEach(category -> { selectedTask.getCategories().forEach(category -> {
if (selectedTask.getTaskStatus().equals(TaskStatus.FINISHED)) { if (selectedTask.getTaskStatus().equals(TaskStatus.FINISHED)) {
category.addTime(selectedTask.getEstimatedTime()); category.addTime(selectedTask.getEstimatedTime(), selectedTask.getId());
categoryDao.update(category); categoryDao.update(category);
} else if (selectedTask.getTaskStatus().equals(TaskStatus.PLANNED)) { } else if (selectedTask.getTaskStatus().equals(TaskStatus.PLANNED)) {
category.subtractTime(selectedTask.getEstimatedTime()); category.subtractTime(selectedTask.getId());
categoryDao.update(category); categoryDao.update(category);
} }
}); });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment