From 707707f7b95c21821143c0c878986597f6b8de13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Henke?= Date: Thu, 5 May 2022 18:02:03 +0200 Subject: [PATCH] add and remove work times --- .../timetrack/spring/done/DoneBean.java | 92 ++++++++++++++++++- .../timetrack/spring/done/DoneController.java | 26 +++++- .../timetrack/spring/done/IDoneService.java | 4 + .../spring/done/impl/DoneGateway.java | 82 ++++++++++++++++- .../spring/done/impl/DoneService.java | 32 ++++++- src/main/resources/templates/done/item.html | 72 +++++++++++++++ src/main/resources/templates/done/list.html | 10 +- 7 files changed, 303 insertions(+), 15 deletions(-) create mode 100644 src/main/resources/templates/done/item.html diff --git a/src/main/java/de/jottyfan/timetrack/spring/done/DoneBean.java b/src/main/java/de/jottyfan/timetrack/spring/done/DoneBean.java index 1eb9509..f380e1c 100644 --- a/src/main/java/de/jottyfan/timetrack/spring/done/DoneBean.java +++ b/src/main/java/de/jottyfan/timetrack/spring/done/DoneBean.java @@ -32,6 +32,10 @@ public class DoneBean implements Serializable, Comparable { private TModuleRecord module; private TJobRecord activity; private TBillingRecord billing; + private Integer fkProject; + private Integer fkModule; + private Integer fkJob; + private Integer fkBilling; public DoneBean() { } @@ -45,8 +49,12 @@ public class DoneBean implements Serializable, Comparable { this.module = moduleMap.get(r.getFkModule()); this.activity = jobMap.get(r.getFkJob()); this.billing = billingMap.get(r.getFkBilling()); + this.fkProject = project.getPk(); + this.fkModule = module.getPk(); + this.fkJob = activity.getPk(); + this.fkBilling = billing.getPk(); } - + @Override public final String toString() { StringBuilder buf = new StringBuilder("DoneBean{"); @@ -105,6 +113,22 @@ public class DoneBean implements Serializable, Comparable { return String.format("%02d:%02d", diff.toHours(), diff.toMinutes() % 60); } + /** + * try to find out what date this entry is for; if not found, use the current + * date + * + * @return a local date + */ + public LocalDate getLocalDate() { + if (timeFrom != null) { + return timeFrom.toLocalDate(); + } else if (timeUntil != null) { + return timeUntil.toLocalDate(); + } else { + return LocalDate.now(); + } + } + /** * get local date time from s * @@ -146,15 +170,15 @@ public class DoneBean implements Serializable, Comparable { public String getJobName() { return activity == null ? "" : activity.getName(); } - + public String getBillingName() { return billing == null ? "" : billing.getName(); } - + public String getBillingShortcut() { return billing == null ? "" : billing.getShortcut(); } - + public String getBillingCsskey() { return billing == null ? "" : billing.getCsskey(); } @@ -205,6 +229,7 @@ public class DoneBean implements Serializable, Comparable { public void setProject(TProjectRecord project) { this.project = project; + this.fkProject = project != null ? project.getPk() : null; } public TModuleRecord getModule() { @@ -213,6 +238,7 @@ public class DoneBean implements Serializable, Comparable { public void setModule(TModuleRecord module) { this.module = module; + this.fkModule = module != null ? module.getPk() : null; } public TJobRecord getActivity() { @@ -221,6 +247,7 @@ public class DoneBean implements Serializable, Comparable { public void setActivity(TJobRecord activity) { this.activity = activity; + this.fkJob = activity != null ? activity.getPk() : null; } /** @@ -235,5 +262,62 @@ public class DoneBean implements Serializable, Comparable { */ public void setBilling(TBillingRecord billing) { this.billing = billing; + this.fkBilling = billing != null ? billing.getPk() : null; + } + + /** + * @return the fkProject + */ + public Integer getFkProject() { + return fkProject; + } + + /** + * @param fkProject the fkProject to set + */ + public void setFkProject(Integer fkProject) { + this.fkProject = fkProject; + } + + /** + * @return the fkModule + */ + public Integer getFkModule() { + return fkModule; + } + + /** + * @param fkModule the fkModule to set + */ + public void setFkModule(Integer fkModule) { + this.fkModule = fkModule; + } + + /** + * @return the fkJob + */ + public Integer getFkJob() { + return fkJob; + } + + /** + * @param fkJob the fkJob to set + */ + public void setFkJob(Integer fkJob) { + this.fkJob = fkJob; + } + + /** + * @return the fkBilling + */ + public Integer getFkBilling() { + return fkBilling; + } + + /** + * @param fkBilling the fkBilling to set + */ + public void setFkBilling(Integer fkBilling) { + this.fkBilling = fkBilling; } } diff --git a/src/main/java/de/jottyfan/timetrack/spring/done/DoneController.java b/src/main/java/de/jottyfan/timetrack/spring/done/DoneController.java index 908dcd5..59359cb 100644 --- a/src/main/java/de/jottyfan/timetrack/spring/done/DoneController.java +++ b/src/main/java/de/jottyfan/timetrack/spring/done/DoneController.java @@ -63,8 +63,30 @@ public class DoneController { bean = new DoneBean(); // the add case } model.addAttribute("doneBean", bean); -// model.addAttribute("types", Arrays.asList(EnumNotetype.values())); -// model.addAttribute("categories", Arrays.asList(EnumCategory.values())); + model.addAttribute("projectList", doneService.getProjects()); + model.addAttribute("moduleList", doneService.getModules()); + model.addAttribute("jobList", doneService.getJobs()); + model.addAttribute("billingList", doneService.getBillings()); return "done/item"; } + + @RolesAllowed("timetrack_user") + @RequestMapping(value = "/done/upsert", method = RequestMethod.POST) + public String doUpsert(Model model, @ModelAttribute DoneBean bean) { + String username = doneService.getCurrentUser(request); + Integer amount = doneService.doUpsert(bean, username); + DoneModel doneModel = new DoneModel(); + doneModel.setDay(bean.getLocalDate()); + return amount.equals(1) ? getList(doneModel, model) : toItem(bean.getPk(), model); + } + + @RolesAllowed("timetrack_user") + @GetMapping(value = "/done/delete/{id}") + public String doDelete(@PathVariable Integer id, Model model) { + DoneBean bean = doneService.getBean(id); + Integer amount = doneService.doDelete(id); + DoneModel doneModel = new DoneModel(); + doneModel.setDay(bean.getLocalDate()); + return amount.equals(1) ? getList(doneModel, model) : toItem(id, model); + } } diff --git a/src/main/java/de/jottyfan/timetrack/spring/done/IDoneService.java b/src/main/java/de/jottyfan/timetrack/spring/done/IDoneService.java index 336bb69..3797d39 100644 --- a/src/main/java/de/jottyfan/timetrack/spring/done/IDoneService.java +++ b/src/main/java/de/jottyfan/timetrack/spring/done/IDoneService.java @@ -29,4 +29,8 @@ public interface IDoneService { public List getJobs(); public List getBillings(); + + public Integer doUpsert(DoneBean bean, String username); + + public Integer doDelete(Integer id); } diff --git a/src/main/java/de/jottyfan/timetrack/spring/done/impl/DoneGateway.java b/src/main/java/de/jottyfan/timetrack/spring/done/impl/DoneGateway.java index 9f001d2..c4e7e8f 100644 --- a/src/main/java/de/jottyfan/timetrack/spring/done/impl/DoneGateway.java +++ b/src/main/java/de/jottyfan/timetrack/spring/done/impl/DoneGateway.java @@ -19,9 +19,11 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jooq.DSLContext; import org.jooq.DeleteConditionStep; +import org.jooq.InsertValuesStep7; import org.jooq.Record7; import org.jooq.Result; import org.jooq.SelectConditionStep; +import org.jooq.UpdateConditionStep; import org.jooq.exception.DataAccessException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @@ -51,7 +53,7 @@ public class DoneGateway { public DSLContext getJooq() { return this.jooq; } - + /** * get the user id of the user with username * @@ -248,4 +250,82 @@ public class DoneGateway { LOGGER.debug("{}", sql.toString()); return sql.execute(); } + + /** + * get the done bean of the pk + * + * @param pk the ID of the data set + * @return the bean if found; null otherwise + * @throws SQLException + * @throws ClassNotFoundException + * @throws DataAccessException + */ + public DoneBean getBean(Integer pk) throws DataAccessException, ClassNotFoundException, SQLException { + SelectConditionStep> sql = getJooq() + // @formatter:off + .select(T_DONE.PK, + T_DONE.TIME_FROM, + T_DONE.TIME_UNTIL, + T_DONE.FK_PROJECT, + T_DONE.FK_MODULE, + T_DONE.FK_JOB, + T_DONE.FK_BILLING) + .from(T_DONE) + .where(T_DONE.PK.eq(pk)); + // @formatter:on + LOGGER.debug("{}", sql.toString()); + Map projectMap = getProjectMap(); + Map moduleMap = getModuleMap(); + Map jobMap = getJobMap(); + Map billingMap = getBillingMap(); + for (Record7 r : sql.fetch()) { + DoneBean bean = new DoneBean(); + bean.setPk(r.get(T_DONE.PK)); + bean.setTimeFrom(r.get(T_DONE.TIME_FROM)); + bean.setTimeUntil(r.get(T_DONE.TIME_UNTIL)); + bean.setProject(projectMap.get(r.get(T_DONE.FK_PROJECT))); + bean.setModule(moduleMap.get(r.get(T_DONE.FK_MODULE))); + bean.setActivity(jobMap.get(r.get(T_DONE.FK_JOB))); + bean.setBilling(billingMap.get(r.get(T_DONE.FK_BILLING))); + return (bean); + } + return (null); + } + + public Integer upsert(DoneBean bean, Integer userId) { + return bean.getPk() != null ? update(bean) : insert(bean, userId); + } + + private Integer insert(DoneBean bean, Integer userId) { + InsertValuesStep7 sql = getJooq() + // @formatter:off + .insertInto(T_DONE, + T_DONE.TIME_FROM, + T_DONE.TIME_UNTIL, + T_DONE.FK_PROJECT, + T_DONE.FK_MODULE, + T_DONE.FK_JOB, + T_DONE.FK_BILLING, + T_DONE.FK_LOGIN) + .values(bean.getTimeFrom(), bean.getTimeUntil(), bean.getFkProject(), bean.getFkModule(), bean.getFkJob(), bean.getFkBilling(), userId); + // @formatter:on + LOGGER.debug(sql.toString()); + return sql.execute(); + } + + private Integer update(DoneBean bean) { + UpdateConditionStep sql = getJooq() + // @formatter:off + .update(T_DONE) + .set(T_DONE.TIME_FROM, bean.getTimeFrom()) + .set(T_DONE.TIME_UNTIL, bean.getTimeUntil()) + .set(T_DONE.FK_PROJECT, bean.getFkProject()) + .set(T_DONE.FK_MODULE, bean.getFkModule()) + .set(T_DONE.FK_JOB, bean.getFkJob()) + .set(T_DONE.FK_BILLING, bean.getFkBilling()) + .where(T_DONE.PK.eq(bean.getPk())); + // @formatter:on + LOGGER.debug(sql.toString()); + return sql.execute(); + } } diff --git a/src/main/java/de/jottyfan/timetrack/spring/done/impl/DoneService.java b/src/main/java/de/jottyfan/timetrack/spring/done/impl/DoneService.java index 94b83ba..ac4b15d 100644 --- a/src/main/java/de/jottyfan/timetrack/spring/done/impl/DoneService.java +++ b/src/main/java/de/jottyfan/timetrack/spring/done/impl/DoneService.java @@ -59,12 +59,16 @@ public class DoneService implements IDoneService { @Override public DoneBean getBean(Integer id) { - // TODO Auto-generated method stub - return null; + try { + return new DoneGateway(dsl).getBean(id); + } catch (Exception e) { + LOGGER.error(e); + return null; + } } @Override - public List getProjects() { + public List getProjects() { try { return new DoneGateway(dsl).getAllProjects(); } catch (Exception e) { @@ -103,4 +107,26 @@ public class DoneService implements IDoneService { } } + @Override + public Integer doUpsert(DoneBean bean, String username) { + try { + DoneGateway gw = new DoneGateway(dsl); + Integer userId = gw.getUserId(username); + return gw.upsert(bean, userId); + } catch (Exception e) { + LOGGER.error(e); + return -1; + } + } + + @Override + public Integer doDelete(Integer id) { + try { + return new DoneGateway(dsl).delete(id); + } catch (Exception e) { + LOGGER.error(e); + return -1; + } + } + } diff --git a/src/main/resources/templates/done/item.html b/src/main/resources/templates/done/item.html new file mode 100644 index 0000000..e1e1a2a --- /dev/null +++ b/src/main/resources/templates/done/item.html @@ -0,0 +1,72 @@ + + + +Arbeitszeit aktualisieren + + +
    +
+
+
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
Änderung
+
+ + + +
+
+
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/done/list.html b/src/main/resources/templates/done/list.html index 0e6c0bd..952fae8 100644 --- a/src/main/resources/templates/done/list.html +++ b/src/main/resources/templates/done/list.html @@ -48,11 +48,11 @@ - - - - - + + + + +