BrightSide Workbench Full Report + Source Code
Subtask.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.Date;
22 import org.turro.dossier.entity.Issue;
23 import org.turro.dossier.entity.IssuePredecessor;
24 import org.turro.dossier.entity.IssuePredecessorType;
25 import org.turro.elephant.util.Toasts;
26 import org.turro.jpa.Dao;
27 
32 public class Subtask {
33 
34  private final Issue main;
35  private final IssuePredecessorType type;
36 
37  public Subtask(Issue main, IssuePredecessorType type) {
38  this.main = main;
39  this.type = type;
40  }
41 
42  public IssuePredecessor save(Dao dao, Issue task) {
43  IssuePredecessor predecessor = new IssuePredecessor();
44  predecessor.setSource(task);
45  predecessor.setTarget(main);
46  predecessor.setType(type);
47  predecessor.setCreation(new Date());
48  if(checkRelation(predecessor)) {
49  return dao.saveObject(predecessor);
50  } else {
51  Toasts.message("Invalid relation").show();
52  return null;
53  }
54  }
55 
56  private boolean checkRelation(IssuePredecessor ip) {
57  if(ip.getSource() != null && ip.getTarget() != null) {
58  if(ip.getSource().getId().equals(ip.getTarget().getId())) {
59  return false;
60  }
61  if(!ip.getSource().getId().equals(main.getId()) && followUp(ip.getSource())) {
62  return false;
63  }
64  if(!ip.getTarget().getId().equals(main.getId()) && followDown(ip.getTarget())) {
65  return false;
66  }
67  return true;
68  }
69  return false;
70  }
71 
72  private boolean followUp(Issue source) {
73  for(IssuePredecessor ip : source.getSources()) {
74  if(ip.getSource().getId().equals(main.getId())) {
75  return true;
76  } else {
77  followUp(ip.getSource());
78  }
79  }
80  return false;
81  }
82 
83  private boolean followDown(Issue target) {
84  for(IssuePredecessor ip : target.getTargets()) {
85  if(ip.getTarget().getId().equals(main.getId())) {
86  return true;
87  } else {
88  followUp(ip.getTarget());
89  }
90  }
91  return false;
92  }
93 
94 }
void setType(IssuePredecessorType type)
Subtask(Issue main, IssuePredecessorType type)
Definition: Subtask.java:37
IssuePredecessor save(Dao dao, Issue task)
Definition: Subtask.java:42
static Toasts message(String message)
Definition: Toasts.java:114