BrightSide Workbench Full Report + Source Code
TaskModel.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.ArrayList;
22 import java.util.Collection;
23 import java.util.function.Consumer;
24 import java.util.stream.Collectors;
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.elephant.db.WhereClause;
31 import org.turro.jpa.Dao;
32 
37 public class TaskModel {
38 
39  private final Dao dao;
40  private final Dossier dossier;
41  private final boolean onlyOpen;
42 
43  public TaskModel(Dossier dossier, boolean onlyOpen) {
44  this.dao = new DossierPU();
45  this.dossier = dossier;
46  this.onlyOpen = onlyOpen;
47  }
48 
49  public Collection<TaskItem> getRoots() {
50  WhereClause wc = new WhereClause();
51  wc.addClause("select issue from Issue issue");
52  wc.addClause("where issue.dossier = :dossier");
53  wc.addNamedValue("dossier", dossier);
54  if(onlyOpen) {
55  wc.addClause("and issue.status in (:status)");
56  wc.addNamedValue("status", IssueStatus.selectBy(null, null, null, false));
57  }
58  wc.addClause("and issue.sources is empty");
59  return dao.getResultList(Issue.class, wc).stream().map((issue) -> {
60  return new TaskItem(issue, null, 1);
61  }).collect(Collectors.toList());
62  }
63 
64  public Collection<TaskItem> getSubtasks(TaskItem item) {
65  WhereClause wc = new WhereClause();
66  wc.addClause("select predecessor from IssuePredecessor predecessor");
67  wc.addClause("where predecessor.source = :issue");
68  wc.addNamedValue("issue", item.getIssue());
69  wc.addClause("and predecessor.source.dossier = :dossier");
70  wc.addNamedValue("dossier", dossier);
71  if(onlyOpen) {
72  wc.addClause("and predecessor.target.status in (:status)");
73  wc.addNamedValue("status", IssueStatus.selectBy(null, null, null, false));
74  }
75  return dao.getResultList(IssuePredecessor.class, wc).stream().map((predecessor) -> {
76  return new TaskItem(predecessor.getTarget(), predecessor.getType(), item.getLevel() + 1);
77  }).collect(Collectors.toList());
78  }
79 
80  public Collection<TaskItem> getFlat(Collection<TaskItem> items) {
81  ArrayList<TaskItem> list = new ArrayList<>();
82  Consumer<TaskItem> addItem = item -> { list.add(item); item.getSubtasks().forEach((subtask) -> list.add(subtask));};
83  items.forEach((item) -> {
84  addItem.accept(item);
85  });
86  return list;
87  }
88 
89 }
Collection< TaskItem > getFlat(Collection< TaskItem > items)
Definition: TaskModel.java:80
Collection< TaskItem > getSubtasks(TaskItem item)
Definition: TaskModel.java:64
TaskModel(Dossier dossier, boolean onlyOpen)
Definition: TaskModel.java:43
Collection< TaskItem > getRoots()
Definition: TaskModel.java:49
void addNamedValue(String name, Object value)
static List< IssueStatus > selectBy(Boolean seen, Boolean working, Boolean standBy, Boolean finished)