19 package org.turro.timetracker;
21 import java.time.Duration;
22 import java.time.Instant;
23 import java.util.ArrayList;
24 import java.util.Date;
25 import java.util.EnumSet;
26 import java.util.List;
28 import java.util.TreeSet;
29 import java.util.stream.Stream;
30 import org.amic.util.date.CheckDate;
31 import org.turro.elephant.db.ElephantPU;
32 import org.turro.elephant.db.WhereClause;
33 import org.turro.elephant.entities.db.TimeTracker;
34 import org.turro.elephant.entities.db.TimeTrackerPK;
35 import org.turro.elephant.entities.db.TimeTrackerType;
36 import org.turro.jpa.Dao;
37 import org.turro.sql.SqlClause;
45 private final String entityPath, trackedPath;
49 return stream().noneMatch(e -> {
50 boolean match = !e.getTrackType().isPreviousValid(previousType[0]);
51 previousType[0] = e.getTrackType();
57 if(isEmpty())
return true;
73 tt = getDao().saveObject(tt);
104 wc.
addClause(
"select tt from TimeTracker tt");
105 wc.
addClause(
"where tt.trackedPath = :trackedPath");
107 wc.
addClause(
"and tt.trackType in (:types)");
110 wc.
addClause(
"select max(t2.timeTrack) from TimeTracker t2");
111 wc.
addClause(
"where tt.entityPath = t2.entityPath");
112 wc.
addClause(
"and tt.trackedPath = t2.trackedPath");
114 return getDao().getResultList(
TimeTracker.class, wc);
121 private void setHours() {
122 Date from = SqlClause.select(
"min(tt.timeTrack)").from(
"TimeTracker tt")
124 .and().equal(
"tt.hours", 0.0)
125 .and().equal(
"entityPath", entityPath)
126 .and().equal(
"trackedPath", trackedPath)
128 .singleResult(Date.class);
129 List<TimeTracker> toSave =
new ArrayList<>();
131 SqlClause.select(
"tt").from(
"TimeTracker tt")
132 .where().greater(
"tt.timeTrack",
new CheckDate(from).addDays(-1).getDate())
133 .and().equal(
"entityPath", entityPath)
134 .and().equal(
"trackedPath", trackedPath)
135 .orderBy(
"tt.timeTrack asc")
138 if(tt.getTrackType().isStarts()) {
139 model.assignFrom(tt);
141 tt.setHours(model.getFrom(tt));
145 getDao().saveCollection(toSave);
150 wc.
addClause(
"select tt from TimeTracker tt");
151 wc.
addClause(
"where tt.entityPath = :entityPath");
153 wc.
addClause(
"and tt.trackedPath = :trackedPath");
155 wc.
addClause(
"order by tt.timeTrack ASC");
157 try(Stream<TimeTracker> entries = getDao().stream(
TimeTracker.class, wc)) {
158 return entries.noneMatch(e -> {
159 boolean match = !e.getTrackType().isPreviousValid(previousType[0]);
160 previousType[0] = e.getTrackType();
168 wc.
addClause(
"select tt from TimeTracker tt");
169 wc.
addClause(
"where tt.entityPath = :entityPath");
171 wc.
addClause(
"and tt.trackedPath = :trackedPath");
173 wc.
addClause(
"order by tt.timeTrack ASC");
175 try(Stream<TimeTracker> entries = getDao().stream(
TimeTracker.class, wc)) {
176 entries.forEach(e -> {
177 if(!e.isPreviousValid(previous[0])) {
178 repair(max, previous[0], e);
198 ttq.loadLastActiveTrack();
204 ttq.loadLastHoursEntries(hours);
209 this.entityPath = entityPath;
210 this.trackedPath = trackedPath;
213 private void loadLastActiveTrack() {
214 WhereClause wc =
new WhereClause();
215 wc.addClause(
"select tt from TimeTracker tt");
216 wc.addClause(
"where tt.entityPath = :entityPath");
217 wc.addNamedValue(
"entityPath", entityPath);
218 wc.addClause(
"and tt.trackedPath = :trackedPath");
219 wc.addNamedValue(
"trackedPath", trackedPath);
220 wc.addClause(
"and tt.timeTrack >= (");
221 wc.addClause(
"select max(t2.timeTrack) from TimeTracker t2");
222 wc.addClause(
"where tt.entityPath = t2.entityPath");
223 wc.addClause(
"and tt.trackedPath = t2.trackedPath");
224 wc.addClause(
"and t2.trackType = :type");
225 wc.addNamedValue(
"type", TimeTrackerType.TRACK_START);
227 addAll(getDao().getResultList(TimeTracker.class, wc));
230 private void loadLastHoursEntries(
int hours) {
231 WhereClause wc =
new WhereClause();
232 wc.addClause(
"select tt from TimeTracker tt");
233 wc.addClause(
"where tt.entityPath = :entityPath");
234 wc.addNamedValue(
"entityPath", entityPath);
235 wc.addClause(
"and tt.trackedPath = :trackedPath");
236 wc.addNamedValue(
"trackedPath", trackedPath);
237 wc.addClause(
"and tt.timeTrack > :date");
238 wc.addNamedValue(
"date", Date.from(Instant.now().minus(Duration.ofHours(hours))));
239 addAll(getDao().getResultList(TimeTracker.class, wc));
246 private Dao getDao() {
248 _dao =
new ElephantPU();
void addClause(String clause)
void addNamedValue(String name, Object value)
static TimeTrackerPK from(TimeTracker tt)
void setEntityPath(String entityPath)
void setTrackType(TimeTrackerType trackType)
void setTimeTrack(Date timeTrack)
void setTrackedPath(String trackedPath)
TimeTrackerType getTrackType()
void deleteObject(Object obj)
static TimeTrackerQueue load(String entityPath, String trackedPath, int hours)
List< TimeTracker > getInactiveTracks()
List< TimeTracker > getActiveTracks()
boolean hasFullIntegrity()
void repairWith(Duration max)
boolean correctDate(CheckDate dateTrack)
TimeTrackerType getLastType()
Set< TimeTrackerType > getNextValids()
TimeTracker addTrack(TimeTrackerType type, Date dateTrack)
List< TimeTracker > getTracksByStatus(Set< TimeTrackerType > types)
static TimeTrackerQueue load(String entityPath, String trackedPath)
boolean isPreviousValid(TimeTrackerType previousType)
static EnumSet< TimeTrackerType > getValidNexts(TimeTrackerType type)
static EnumSet< TimeTrackerType > getEndTypes()
static EnumSet< TimeTrackerType > getInactiveTypes()
static EnumSet< TimeTrackerType > getActiveTypes()
TimeTrackerType getValidRepair(TimeTracker previous)