BrightSide Workbench Full Report + Source Code
IssueGraph.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.graph;
20 
21 import java.util.Set;
22 import org.jgrapht.Graph;
23 import org.jgrapht.Graphs;
24 import org.jgrapht.graph.DefaultEdge;
25 import org.jgrapht.graph.SimpleDirectedGraph;
26 import org.turro.dossier.db.DossierPU;
27 import org.turro.dossier.entity.Dossier;
28 import org.turro.dossier.entity.Issue;
29 import org.turro.dossier.entity.IssuePredecessor;
30 import org.turro.jpa.Dao;
31 import org.turro.sql.SqlClause;
32 import org.turro.util.Cached;
33 
38 public class IssueGraph extends SimpleDirectedGraph<IssueVertex, IssueEdge> {
39 
40  /* Initialization */
41 
42  private void addVertices() {
43  SqlClause.select("i").from("Issue i")
44  .startIf(onlyPublishable)
45  .where().equal("publishable", true)
46  .endIf()
47  .startIf(onlyMilestones)
48  .whereOrAnd().equal("i.milestone", true)
49  .endIf()
50  .startIf(dossier != null)
51  .whereOrAnd().equal("i.dossier", dossier)
52  .endIf()
53  .dao(dao.get())
54  .resultList(Issue.class)
55  .forEach(issue -> {
56  addVertex(milestoneOf(issue));
57  });
58  }
59 
60  private void addEdges() {
61  // Full graph
62  Graph<Long, DefaultEdge> predecessorGraph = new SimpleDirectedGraph<>(DefaultEdge.class);
63  SqlClause.select("i").from("IssuePredecessor i")
64  .startIf(dossier != null)
65  .where().equal("i.source.dossier", dossier)
66  .and().equal("i.target.dossier", dossier)
67  .endIf()
68  .dao(dao.get())
69  .resultList(IssuePredecessor.class)
70  .forEach(predecessor -> {
71  Graphs.addEdgeWithVertices(predecessorGraph, predecessor.getSource().getId(), predecessor.getTarget().getId());
72  });
73  if(onlyMilestones) {
74  // Simplify for current milestones
75  Set.copyOf(predecessorGraph.vertexSet()).forEach(vertex -> {
76  if(!containsVertex(milestoneOf(vertex))) {
77  Graphs.removeVertexAndPreserveConnectivity(predecessorGraph, vertex);
78  }
79  });
80  }
81  // Add final edge model
82  vertexSet().forEach(m1 -> {
83  vertexSet().forEach(m2 -> {
84  if(predecessorGraph.containsEdge(m1.getItem().getIssueId(), m2.getItem().getIssueId())) {
85  Graphs.addEdgeWithVertices(this, m1, m2);
86  }
87  });
88  });
89  }
90 
91  private IssueVertex milestoneOf(Issue issue) {
92  return new IssueVertex(this, new IssueItem(issue));
93  }
94 
95  private IssueVertex milestoneOf(Long issueId) {
96  return new IssueVertex(this, new IssueItem(dao, issueId));
97  }
98 
99  private final Cached<Dao> dao = Cached.instance(() -> new DossierPU());
100 
101  /* Factory */
102 
103  private final Dossier dossier;
104  private final boolean onlyMilestones, onlyPublishable;
105 
106  public static IssueGraph load(Dossier dossier) {
107  return new IssueGraph(dossier, false, false);
108  }
109 
110  public static IssueGraph loadPublishable(Dossier dossier) {
111  return new IssueGraph(dossier, false, true);
112  }
113 
114  public static IssueGraph milestones(Dossier dossier) {
115  return new IssueGraph(dossier, true, false);
116  }
117 
118  public static IssueGraph publishableMilestones(Dossier dossier) {
119  return new IssueGraph(dossier, true, false);
120  }
121 
122  public static IssueGraph all() {
123  return new IssueGraph(false);
124  }
125 
126  public static IssueGraph publishable() {
127  return new IssueGraph(true);
128  }
129 
130  private IssueGraph(boolean onlyPublishable) {
131  super(IssueEdge.class);
132  this.dossier = null;
133  this.onlyMilestones = true;
134  this.onlyPublishable = onlyPublishable;
135  }
136 
137  private IssueGraph(Dossier dossier, boolean onlyMilestones, boolean onlyPublishable) {
138  super(IssueEdge.class);
139  this.dossier = dossier;
140  this.onlyMilestones = onlyMilestones;
141  this.onlyPublishable = onlyPublishable;
142  addVertices();
143  addEdges();
144  }
145 
146 }
static IssueGraph milestones(Dossier dossier)
static IssueGraph loadPublishable(Dossier dossier)
static IssueGraph load(Dossier dossier)
static IssueGraph publishableMilestones(Dossier dossier)