BrightSide Workbench Full Report + Source Code
AxProject.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.alliance.db.entities;
20 
21 import java.io.Serializable;
22 import java.util.Date;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Objects;
26 import java.util.Set;
27 import java.util.TreeSet;
28 import javax.json.JsonValue;
29 import javax.persistence.CascadeType;
30 import javax.persistence.EmbeddedId;
31 import javax.persistence.Entity;
32 import javax.persistence.FetchType;
33 import javax.persistence.JoinColumn;
34 import javax.persistence.JoinColumns;
35 import javax.persistence.OneToMany;
36 import javax.persistence.Temporal;
37 import org.turro.string.Phrases;
38 import org.turro.string.Strings;
39 import org.hibernate.annotations.Formula;
40 import org.turro.alliance.content.AlliancePhaseDefinitions;
41 import org.turro.alliance.db.AlliancePU;
42 import org.turro.dossier.entity.Dossier;
43 import org.turro.elephant.context.ElephantContext;
44 import org.turro.entities.Entities;
45 import org.turro.importer.ImporterRegister;
46 import org.turro.jpa.entity.IDaoEntity;
47 import org.turro.json.IJSONizable;
48 import org.turro.member.db.entities.AxProjectCategory;
49 import org.turro.phase.PhaseDefinition;
50 import org.turro.phase.PhaseDefinitions;
51 import org.turro.server.db.entities.AxAllianceParticipation;
52 import org.turro.sql.SqlClause;
53 import org.turro.ws.WsServer;
54 
59 @Entity
60 public class AxProject implements Serializable, IDaoEntity, IJSONizable {
61 
62  @EmbeddedId private ProcedenceId projectId;
63 
64  private Long categoryId;
65 
66  @Formula("concat(entityId,'##',memberId)")
67  private String id; /* Widgets compatibility */
68 
69  @Temporal(value = javax.persistence.TemporalType.TIMESTAMP)
70  private java.util.Date creation;
71 
72  private int phaseIndex;
73 
74  private String code, name, summary, searching;
75 
76  private String sourceLink;
77 
78  @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
79  @JoinColumns({
80  @JoinColumn(name="mainEntityId", referencedColumnName = "entityId", insertable = false, updatable = false),
81  @JoinColumn(name="mainMemberId", referencedColumnName = "memberId", insertable = false, updatable = false)
82  })
83  private Set<AxParticipation> participations;
84 
85  @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
86  @JoinColumns({
87  @JoinColumn(name="entityId", referencedColumnName = "entityId", insertable = false, updatable = false),
88  @JoinColumn(name="memberId", referencedColumnName = "memberId", insertable = false, updatable = false)
89  })
90  private Set<AxDescriptor> descriptors;
91 
93  return projectId;
94  }
95 
96  public void setProjectId(ProcedenceId projectId) {
97  this.projectId = projectId;
98  }
99 
100  public Long getCategoryId() {
101  return categoryId;
102  }
103 
104  public void setCategoryId(Long categoryId) {
105  this.categoryId = categoryId;
106  }
107 
108  public String getId() {
109  return id;
110  }
111 
112  public Date getCreation() {
113  return creation;
114  }
115 
116  public void setCreation(Date creation) {
117  this.creation = creation;
118  }
119 
120  public int getPhaseIndex() {
121  return phaseIndex;
122  }
123 
124  public void setPhaseIndex(int phaseIndex) {
125  this.phaseIndex = phaseIndex;
126  }
127 
128  public String getCode() {
129  return code;
130  }
131 
132  public void setCode(String code) {
133  this.code = code;
134  }
135 
136  public String getName() {
137  return name;
138  }
139 
140  public void setName(String name) {
141  this.name = name;
142  }
143 
144  public String getSummary() {
145  return summary;
146  }
147 
148  public void setSummary(String summary) {
149  this.summary = summary;
150  }
151 
152  public String getSearching() {
153  return searching;
154  }
155 
156  public void setSearching(String searching) {
157  this.searching = searching;
158  }
159 
160  public Set<AxParticipation> getParticipations() {
161  return participations;
162  }
163 
164  public void setParticipations(Set<AxParticipation> participations) {
165  this.participations = participations;
166  }
167 
168  public Set<AxDescriptor> getDescriptors() {
169  return descriptors;
170  }
171 
172  public void setDescriptors(Set<AxDescriptor> descriptors) {
173  this.descriptors = descriptors;
174  }
175 
177  return new AlliancePU().find(AxCategory.class, categoryId);
178  }
179 
180  public void setCategory(AxCategory category) {
181  categoryId = category.getCategoryId();
182  }
183 
184  public String getSourceLink() {
185  return sourceLink;
186  }
187 
188  public void setSourceLink(String sourceLink) {
189  this.sourceLink = sourceLink;
190  }
191 
192  public String getFriendlyName() {
193  return Phrases.start(code, name).toString();
194  }
195 
196  /* Descriptors */
197 
198  public Set<AxDescriptor> getSortedDescriptors() {
199  return new TreeSet<>(descriptors);
200  }
201 
202  /* Roles */
203 
204  public List<AxContact> getDrivers() {
205  return getParticipations().stream().filter(p -> p.isDriver())
206  .map(p -> p.getContact()).toList();
207  }
208 
209  public List<AxContact> getBeneficiaries() {
210  return getParticipations().stream().filter(p -> p.isBeneficiary())
211  .map(p -> p.getContact()).toList();
212  }
213 
214  public List<AxContact> getOfferers() {
215  return getParticipations().stream().filter(p -> p.isOfferer())
216  .map(p -> p.getContact()).toList();
217  }
218 
219  public List<AxContact> getConsortium() {
220  return getParticipations().stream().filter(p -> p.isConsortium())
221  .map(p -> p.getContact()).toList();
222  }
223 
224  public List<AxContact> getCoordinators() {
225  return getParticipations().stream().filter(p -> p.isCoordinator())
226  .map(p -> p.getContact()).toList();
227  }
228 
229  public List<AxContact> getFunding() {
230  return getParticipations().stream().filter(p -> p.isFunding())
231  .map(p -> p.getContact()).toList();
232  }
233 
234  public List<AxContact> getResearch() {
235  return getParticipations().stream().filter(p -> p.isResearch())
236  .map(p -> p.getContact()).toList();
237  }
238 
239  public List<AxContact> getSupport() {
240  return getParticipations().stream().filter(p -> p.isSupport())
241  .map(p -> p.getContact()).toList();
242  }
243 
244  public List<AxParticipation> getDriverParticipations() {
245  return getParticipations().stream().filter(p -> p.isDriver()).toList();
246  }
247 
248  public List<AxParticipation> getBeneficiaryParticipations() {
249  return getParticipations().stream().filter(p -> p.isBeneficiary()).toList();
250  }
251 
252  public List<AxParticipation> getOffererParticipations() {
253  return getParticipations().stream().filter(p -> p.isOfferer()).toList();
254  }
255 
256  public List<AxParticipation> getConsortiumParticipations() {
257  return getParticipations().stream().filter(p -> p.isConsortium()).toList();
258  }
259 
260  public List<AxParticipation> getCoordinatorParticipations() {
261  return getParticipations().stream().filter(p -> p.isCoordinator()).toList();
262  }
263 
264  public List<AxParticipation> getFundingParticipations() {
265  return getParticipations().stream().filter(p -> p.isFunding()).toList();
266  }
267 
268  public List<AxParticipation> getResearchParticipations() {
269  return getParticipations().stream().filter(p -> p.isResearch()).toList();
270  }
271 
272  public List<AxParticipation> getSupportParticipations() {
273  return getParticipations().stream().filter(p -> p.isSupport()).toList();
274  }
275 
276  /* Members */
277 
278  private transient List<AxAllianceParticipation> allianceParticipations;
279 
280  public List<AxAllianceParticipation> getAllianceParticipations() {
281  if(allianceParticipations == null) {
282  allianceParticipations = SqlClause.select("p").from("AxAllianceParticipation p")
283  .where().equal("mainEntityId", projectId.getEntityId())
284  .and().equal("mainMemberId", projectId.getMemberId())
285  .dao(new AlliancePU())
286  .resultList(AxAllianceParticipation.class);
287  }
288  return allianceParticipations;
289  }
290 
291  public List<AxAllianceParticipation> getBeneficiaryAxParticipations() {
292  return getAllianceParticipations().stream().filter(p -> p.isBeneficiary()).toList();
293  }
294 
295  public List<AxAllianceParticipation> getOffererAxParticipations() {
296  return getAllianceParticipations().stream().filter(p -> p.isOfferer()).toList();
297  }
298 
299  public List<AxAllianceParticipation> getConsortiumAxParticipations() {
300  return getAllianceParticipations().stream().filter(p -> p.isConsortium()).toList();
301  }
302 
303  /* Phase */
304 
306  return AlliancePhaseDefinitions.instance().get(phaseIndex);
307  }
308 
309  /* Factory */
310 
311  public static AxProject from(long memberId, WsServer server, Dossier dossier, AxProjectCategory pc) {
312  AxProject axp = new AxProject();
313  ProcedenceId id = new ProcedenceId();
314  id.setMemberId(memberId);
315  id.setEntityId(Long.toString(dossier.getId()));
316  axp.setProjectId(id);
317  axp.setCreation(dossier.getCreation());
319  axp.setCode(dossier.getProject().getProjectCode());
320  axp.setName(dossier.getProject().getProjectTitle());
321  axp.setSummary(dossier.getProject().getGoal());
322  axp.setSearching(dossier.getSearching());
323  axp.setCategoryId(pc.getCategoryId());
325  axp.setParticipations(AxParticipation.from(memberId, dossier));
326  axp.setDescriptors(AxDescriptor.from(axp, dossier));
327  return axp;
328  }
329 
330  public static AxProject from(long memberId, ImporterRegister register) {
331  AxProject axp = new AxProject();
332  ProcedenceId id = new ProcedenceId();
333  id.setMemberId(memberId);
334  id.setEntityId(register.getString("projectId"));
335  axp.setProjectId(id);
336  axp.setCreation(register.getDate("creation"));
337  axp.setPhaseIndex(register.getInteger("phaseIndex"));
338  axp.setCode(register.getString("projectCode"));
339  axp.setName(register.getString("name"));
340  axp.setSummary(register.getString("summary"));
341  axp.setCategoryId(register.getLong("categoryId"));
342  axp.setSourceLink(register.getString("source"));
343  return axp;
344  }
345 
346  /* IDaoEntity */
347 
348  @Override
349  public Object entityId() {
350  return projectId;
351  }
352 
353  @Override
354  public boolean isEmpty() {
355  return projectId.isEmpty() ||
356  Strings.isBlank(name);
357  }
358 
359  /* JSONizable */
360 
361  @Override
362  public String toJson() {
363  return toJson(this);
364  }
365 
366  @Override
367  public String toJson(Map<String, Object> properties) {
368  return toJson(this, properties);
369  }
370 
371  public static AxProject fromJson(JsonValue value) {
372  return IJSONizable.fromJson(value.toString(), AxProject.class);
373  }
374 
375  /* Utils */
376 
377  @Override
378  public int hashCode() {
379  int hash = 3;
380  hash = 79 * hash + Objects.hashCode(this.projectId);
381  return hash;
382  }
383 
384  @Override
385  public boolean equals(Object obj) {
386  if (this == obj) {
387  return true;
388  }
389  if (obj == null) {
390  return false;
391  }
392  if (getClass() != obj.getClass()) {
393  return false;
394  }
395  final AxProject other = (AxProject) obj;
396  return Objects.equals(this.projectId, other.projectId);
397  }
398 
399 }
static Set< AxDescriptor > from(AxProject project, Dossier dossier)
static Set< AxParticipation > from(long memberId, Dossier dossier)
List< AxParticipation > getCoordinatorParticipations()
Definition: AxProject.java:260
static AxProject from(long memberId, ImporterRegister register)
Definition: AxProject.java:330
List< AxParticipation > getSupportParticipations()
Definition: AxProject.java:272
String toJson(Map< String, Object > properties)
Definition: AxProject.java:367
List< AxParticipation > getConsortiumParticipations()
Definition: AxProject.java:256
static AxProject from(long memberId, WsServer server, Dossier dossier, AxProjectCategory pc)
Definition: AxProject.java:311
void setDescriptors(Set< AxDescriptor > descriptors)
Definition: AxProject.java:172
Set< AxParticipation > getParticipations()
Definition: AxProject.java:160
void setProjectId(ProcedenceId projectId)
Definition: AxProject.java:96
Set< AxDescriptor > getSortedDescriptors()
Definition: AxProject.java:198
List< AxParticipation > getFundingParticipations()
Definition: AxProject.java:264
List< AxParticipation > getResearchParticipations()
Definition: AxProject.java:268
void setSourceLink(String sourceLink)
Definition: AxProject.java:188
List< AxParticipation > getOffererParticipations()
Definition: AxProject.java:252
List< AxAllianceParticipation > getAllianceParticipations()
Definition: AxProject.java:280
void setCategory(AxCategory category)
Definition: AxProject.java:180
List< AxAllianceParticipation > getConsortiumAxParticipations()
Definition: AxProject.java:299
List< AxAllianceParticipation > getBeneficiaryAxParticipations()
Definition: AxProject.java:291
static AxProject fromJson(JsonValue value)
Definition: AxProject.java:371
List< AxParticipation > getDriverParticipations()
Definition: AxProject.java:244
void setParticipations(Set< AxParticipation > participations)
Definition: AxProject.java:164
List< AxParticipation > getBeneficiaryParticipations()
Definition: AxProject.java:248
List< AxAllianceParticipation > getOffererAxParticipations()
Definition: AxProject.java:295
static String getServerUrl(String scheme)
static IElephantEntity getController(String path)
Definition: Entities.java:78
int getMapping(int index, String serverId)
static PhaseDefinitions instance()
String getServerStrId()
Definition: WsServer.java:88