BrightSide Workbench Full Report + Source Code
GrantMatchCriteria.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.entity;
20 
21 import java.io.Serializable;
22 import java.util.Collections;
23 import java.util.List;
24 import javax.persistence.Embeddable;
25 import org.turro.dossier.db.DossierPU;
26 import org.turro.elephant.db.WhereClause;
27 import org.turro.entities.Entities;
28 import org.turro.entities.EntityListAdapter;
29 import org.turro.entities.IElephantEntity;
30 import org.turro.path.Path;
31 import org.turro.tags.Tags;
32 
37 @Embeddable
38 public class GrantMatchCriteria implements Serializable {
39 
40  private boolean hiring, idi, investment, startUp, formation;
41 
42  public boolean isHiring() {
43  return hiring;
44  }
45 
46  public void setHiring(boolean hiring) {
47  this.hiring = hiring;
48  }
49 
50  public boolean isIdi() {
51  return idi;
52  }
53 
54  public void setIdi(boolean idi) {
55  this.idi = idi;
56  }
57 
58  public boolean isInvestment() {
59  return investment;
60  }
61 
62  public void setInvestment(boolean investment) {
63  this.investment = investment;
64  }
65 
66  public boolean isStartUp() {
67  return startUp;
68  }
69 
70  public void setStartUp(boolean startUp) {
71  this.startUp = startUp;
72  }
73 
74  public boolean isFormation() {
75  return formation;
76  }
77 
78  public void setFormation(boolean formation) {
79  this.formation = formation;
80  }
81 
82  /* Helpers */
83 
84  public boolean isEmpty() {
85  return !(hiring | idi | investment | startUp);
86  }
87 
88  public List<IElephantEntity> getMatchingsFor(Object entity) {
89  if(isEmpty()) return Collections.EMPTY_LIST;
91  }
92 
93  public List<IElephantEntity> getMatchingsFor(String entityPath) {
94  if(isEmpty()) return Collections.EMPTY_LIST;
95  Path path = Path.pathFrom(entityPath);
96  if("dossier".equals(path.getRoot())) {
97  return getMatchingsForDossier(path.getNodeAs(1, Long.class));
98  } else if("project-grant".equals(path.getRoot())) {
99  return getMatchingsForProjectGrant(path.getNodeAs(1, Long.class));
100  }
101  return Collections.EMPTY_LIST;
102  }
103 
104  public List<IElephantEntity> getMatchingsForDossier(Long dossierId) {
105  WhereClause wc = new WhereClause();
106  wc.addClause("select e from ProjectGrant e");
107  wc.addClause("where (e.recurrent = TRUE or (e.endDate is null or e.endDate > now()))");
108  addMatchCheck(wc);
109  addEmptyCheck(wc);
110  addTagIds(wc, "e.id", "/dossier/" + dossierId, "project-grant");
111  return new EntityListAdapter(new DossierPU().getResultList(wc));
112  }
113 
114  public List<IElephantEntity> getMatchingsForProjectGrant(Long projectGrantId) {
115  WhereClause wc = new WhereClause();
116  wc.addClause("select d from Dossier d");
117  wc.addClause("join d.project e");
118  wc.addClause("where 1=1");
119  addMatchCheck(wc);
120  addEmptyCheck(wc);
121  addTagIds(wc, "d.id", "/project-grant/" + projectGrantId, "dossier");
122  return new EntityListAdapter(new DossierPU().getResultList(wc));
123  }
124 
125  private void addMatchCheck(WhereClause wc) {
126  if(isEmpty()) {
127  wc.addClause("and 1=2");
128  return;
129  }
130  wc.addClause("and (");
131  String sep = "";
132  if(hiring) {
133  wc.addClause(sep);
134  wc.addClause("e.matchCriteria.hiring = :hiring");
135  wc.addNamedValue("hiring", hiring);
136  sep = "or";
137  }
138  if(idi) {
139  wc.addClause(sep);
140  wc.addClause("e.matchCriteria.idi = :idi");
141  wc.addNamedValue("idi", idi);
142  sep = "or";
143  }
144  if(investment) {
145  wc.addClause(sep);
146  wc.addClause("e.matchCriteria.investment = :investment");
147  wc.addNamedValue("investment", investment);
148  sep = "or";
149  }
150  if(startUp) {
151  wc.addClause(sep);
152  wc.addClause("e.matchCriteria.startUp = :startUp");
153  wc.addNamedValue("startUp", startUp);
154  sep = "or";
155  }
156  if(startUp) {
157  wc.addClause(sep);
158  wc.addClause("e.matchCriteria.formation = :formation");
159  wc.addNamedValue("formation", formation);
160  sep = "or";
161  }
162  wc.addClause(")");
163  }
164 
165  private void addEmptyCheck(WhereClause wc) {
166  wc.addClause("and (e.matchCriteria.hiring = TRUE");
167  wc.addClause("or e.matchCriteria.idi = TRUE");
168  wc.addClause("or e.matchCriteria.investment = TRUE");
169  wc.addClause("or e.matchCriteria.startUp = TRUE");
170  wc.addClause("or e.matchCriteria.formation = TRUE)");
171  }
172 
173  private void addTagIds(WhereClause wc, String field, String source, String rootTarget) {
174  List<Long> ids = Tags.getCommonLongIdentifiers(source, rootTarget);
175  if(ids != null && !ids.isEmpty()) {
176  wc.addIn("and", field, ids);
177  }
178  }
179 
180 }
List< IElephantEntity > getMatchingsFor(Object entity)
List< IElephantEntity > getMatchingsForProjectGrant(Long projectGrantId)
List< IElephantEntity > getMatchingsFor(String entityPath)
List< IElephantEntity > getMatchingsForDossier(Long dossierId)
void addNamedValue(String name, Object value)
static IElephantEntity getController(String path)
Definition: Entities.java:78