BrightSide Workbench Full Report + Source Code
BrightSide/elephant-dossier/src/main/java/org/turro/dossier/task/Tasks.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.dossier.task;
20 
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.stream.Stream;
24 import org.turro.collections.FlatList;
25 import org.turro.dossier.db.DossierPU;
26 import org.turro.dossier.entity.Dossier;
27 import org.turro.dossier.entity.Issue;
28 import org.turro.dossier.entity.IssuePredecessor;
29 import org.turro.dossier.entity.IssueStatus;
30 import org.turro.jpa.Dao;
31 import org.turro.sql.SqlClause;
32 
37 public class Tasks {
38 
39  public double getPercentTasksDone() {
40  Collection<TaskItem> subtasks = getFilledFlat();
41 // if(issue != null) {
42 // subtasks.removeIf(ti -> Objects.equals(ti.getIssue().getId(), issue.getId()));
43 // }
44  if(subtasks.isEmpty()) {
45  return 0;
46  } else {
47  double done = subtasks.stream().filter(ti -> ti.getIssue().getStatus().isFinished()).count();
48  return (done / subtasks.size()) * 100.0;
49  }
50  }
51 
52  public Collection<TaskItem> getFilledFlat() {
53  return getFlat(getFilledRoots());
54  }
55 
56  public Collection<TaskItem> getFilledRoots() {
57  Collection<TaskItem> model = getRoots();
58  model.forEach((task) -> fillSubtasks(task));
59  return model;
60  }
61 
62  public Collection<TaskItem> getRoots() {
63  if(issue != null) {
64  return List.<TaskItem>of(new TaskItem(issue, null, 1));
65  } else {
66  try(Stream<Issue> stream = SqlClause.select("i").from("Issue i")
67  .where().equal("i.dossier", dossier)
68  .startIf(onlyOpen)
69  .and().in("i.status", IssueStatus.selectBy(null, null, null, false))
70  .endIf()
71  .and().empty("i.targets")
72  .dao(dao)
73  .stream(Issue.class)) {
74  return stream.map(i -> new TaskItem(i, null, 1)).toList();
75  }
76  }
77  }
78 
79  public Collection<TaskItem> getSubtasks(TaskItem item) {
80  try(Stream<IssuePredecessor> stream = SqlClause.select("p").from("IssuePredecessor p")
81  .where().equal("p.target", item.getIssue())
82  .startIf(onlyOpen)
83  .and().in("p.target.status", IssueStatus.selectBy(null, null, null, false))
84  .endIf()
85  .startIf(sameDossier)
86  .and("p.target.dossier = p.source.dossier")
87  .endIf()
88  .dao(dao)
89  .stream(IssuePredecessor.class)) {
90  return stream.map(p -> new TaskItem(p.getSource(), p.getType(), item.getLevel() + 1))
91  .toList();
92  }
93  }
94 
95  public Collection<TaskItem> getFlat(Collection<TaskItem> items) {
96  FlatList list = new FlatList<TaskItem>() {
97  @Override
98  protected Collection<TaskItem> subItems(TaskItem item) {
99  return item.getSubtasks();
100  }
101  };
102  items.forEach((item) -> {
103  list.addItem(item);
104  });
105  return list;
106  }
107 
108  /* Fill */
109 
110  private void fillSubtasks(TaskItem task) {
111  getSubtasks(task).stream().forEach((subtask) -> {
112  task.addSubtask(subtask);
113  fillSubtasks(subtask);
114  });
115  }
116 
117  /* Factory */
118 
119  public static Tasks from(Dossier dossier, boolean onlyOpen, boolean sameDossier) {
120  return new Tasks(dossier, null, onlyOpen, sameDossier);
121  }
122 
123  public static Tasks from(Issue issue, boolean onlyOpen, boolean sameDossier) {
124  return new Tasks(null, issue, onlyOpen, sameDossier);
125  }
126 
127  private final Dao dao;
128  private final Dossier dossier;
129  private final Issue issue;
130  private final boolean onlyOpen, sameDossier;
131 
132  private Tasks(Dossier dossier, Issue issue, boolean onlyOpen, boolean sameDossier) {
133  this.dao = new DossierPU();
134  this.dossier = dossier;
135  this.issue = issue;
136  this.onlyOpen = onlyOpen;
137  this.sameDossier = sameDossier;
138  }
139 
140 }
void addSubtask(TaskItem subtask)
Definition: TaskItem.java:60
Collection< TaskItem > getSubtasks()
Definition: TaskItem.java:56
static Tasks from(Dossier dossier, boolean onlyOpen, boolean sameDossier)
static Tasks from(Issue issue, boolean onlyOpen, boolean sameDossier)
static List< IssueStatus > selectBy(Boolean seen, Boolean working, Boolean standBy, Boolean finished)