BrightSide Workbench Full Report + Source Code
provider/Projects.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2023 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.provider;
20 
21 import org.turro.alliance.content.control.Participations;
22 import org.turro.alliance.db.AlliancePU;
23 import org.turro.alliance.db.entities.AxParticipation;
24 import org.turro.alliance.db.entities.AxProject;
25 import org.turro.alliance.db.entities.RelationId;
26 import org.turro.jpa.Dao;
27 import org.turro.json.Jsons;
28 import org.turro.server.db.entities.AxAllianceParticipation;
29 import org.turro.server.db.entities.AxParticipationRequest;
30 import org.turro.sql.SqlClause;
31 import org.turro.string.Strings;
32 import org.turro.util.Cached;
33 import org.turro.ws.WsMember;
34 
39 public class Projects {
40 
41  public Jsons project(String projectId) {
42  AxProject project = SqlClause.select("p").from("AxProject p")
43  .where().equal("id", projectId)
44  .dao(dao.get())
45  .singleResult(AxProject.class);
46  return Jsons.read(project.toJson());
47  }
48 
49  public Jsons participations(String projectId) {
50  Jsons participations = Jsons.array();
51  SqlClause.select("p").from("AxParticipation p")
52  .where().equal("id", projectId)
53  .dao(dao.get())
54  .resultList(AxParticipation.class)
55  .forEach(p -> {
56  participations.addValue(Jsons.read(p.toJson()));
57  });
58  return participations;
59  }
60 
61  public Jsons axParticipations(String projectId, String as) {
62  Jsons participations = Jsons.array();
63  SqlClause.select("p").from("AxAllianceParticipation p")
64  .where().equal("id", correctId(projectId))
65  .startIf(!Strings.isBlank(as))
66  .and().isTrue(Strings.lowerCase(as))
67  .endIf()
68  .dao(dao.get())
69  .resultList(AxAllianceParticipation.class)
70  .forEach(p -> {
71  participations.addValue(Jsons.read(p.toJson()));
72  });
73  return participations;
74  }
75 
76  public Jsons axPendingParticipations() {
77  Jsons participations = Jsons.array();
78  SqlClause.select("p").from("AxParticipationRequest p")
79  .where().equal("mainMemberId", member.getMemberId())
80  .dao(dao.get())
81  .resultList(AxParticipationRequest.class)
82  .forEach(p -> {
83  participations.addValue(Jsons.read(p.toJson()));
84  });
85  return participations;
86  }
87 
88  public Jsons axAllParticipations(String projectId) {
89  Jsons participations = Jsons.array();
90  SqlClause.select("p").from("AxParticipationRequest p")
91  .where().equal("id", correctId(projectId))
92  .dao(dao.get())
93  .resultList(AxParticipationRequest.class)
94  .forEach(p -> {
95  participations.addValue(Jsons.read(p.toJson()));
96  });
97  SqlClause.select("p").from("AxAllianceParticipation p")
98  .where().equal("id", correctId(projectId))
99  .dao(dao.get())
100  .resultList(AxAllianceParticipation.class)
101  .forEach(p -> {
102  participations.addValue(Jsons.read(p.toJson()));
103  });
104  return participations;
105  }
106 
107  public Jsons axFullParticipations() {
108  Jsons participations = Jsons.array();
109  SqlClause.select("p").from("AxParticipationRequest p")
110  .where().equal("mainMemberId", member.getMemberId())
111  .or().equal("relatedMemberId", member.getMemberId())
112  .orderBy("p.creation desc")
113  .dao(dao.get())
114  .resultList(AxParticipationRequest.class)
115  .forEach(p -> {
116  participations.addValue(Jsons.read(p.toJson()));
117  });
118  SqlClause.select("p").from("AxAllianceParticipation p")
119  .where().equal("mainMemberId", member.getMemberId())
120  .or().equal("relatedMemberId", member.getMemberId())
121  .orderBy("p.creation desc")
122  .dao(dao.get())
123  .resultList(AxAllianceParticipation.class)
124  .forEach(p -> {
125  participations.addValue(Jsons.read(p.toJson()));
126  });
127  return participations;
128  }
129 
130  public void axAskParticipate(Jsons request) {
132  RelationId ri = new RelationId();
133  ri.setRelatedEntityId(request.getString("contactId"));
134  ri.setRelatedMemberId(member.getMemberId());
135  axp.setRelation(ri);
136  axp.setId(request.getString("axId"));
137  switch(request.getString("as")) {
138  case "Beneficiary" -> axp.setBeneficiary(true);
139  case "Offerer" -> axp.setOfferer(true);
140  case "Consortium" -> axp.setConsortium(true);
141  }
142  axp.setName(request.getString("contactName"));
143  axp.setFace(request.getString("contactFace"));
144  axp.setCompany(request.getString("companyName"));
145  axp.setCompanyFace(request.getString("companyFace"));
146  axp.setMemberName(member.getContact().getTradeName());
147  Participations.from(axp).add();
148  }
149 
150  public boolean isParticipant(Jsons request) {
151  return !SqlClause.select("p").from("AxAllianceParticipation p")
152  .where().equal("id", request.getString("axId"))
153  .and().equal("relatedEntityId", request.getString("contactId"))
154  .and().equal("relatedMemberId", member.getMemberId())
155  .and().isTrue(request.getString("as").toLowerCase())
156  .dao(dao.get())
157  .resultList(AxAllianceParticipation.class).isEmpty();
158  }
159 
160  public boolean hasRequested(Jsons request) {
161  return !SqlClause.select("p").from("AxParticipationRequest p")
162  .where().equal("id", request.getString("axId"))
163  .and().equal("relatedEntityId", request.getString("contactId"))
164  .and().equal("relatedMemberId", member.getMemberId())
165  .and().isTrue(request.getString("as").toLowerCase())
166  .dao(dao.get())
167  .resultList(AxParticipationRequest.class).isEmpty();
168  }
169 
170  public boolean save(Jsons request) {
171  AxAllianceParticipation ap = AxAllianceParticipation.fromJson(request.asJsonValue());
172  dao.get().saveObject(ap);
173  return true;
174  }
175 
176  public void axValidatePending(Jsons axPending) {
177  AxParticipationRequest pr = AxParticipationRequest.fromJson(axPending.asJsonValue());
179  dao.get().deleteObject(pr);
180  }
181 
182  public void axDeleteParticipation(Jsons axRequest) {
183  if(axRequest.getBoolean("validated")) {
184  AxAllianceParticipation ap = AxAllianceParticipation.fromJson(axRequest.asJsonValue());
185  dao.get().deleteObject(ap);
186  } else {
187  AxParticipationRequest pr = AxParticipationRequest.fromJson(axRequest.asJsonValue());
188  dao.get().deleteObject(pr);
189  }
190  }
191 
192  /* Utils */
193 
194  private String correctId(String axId) {
195  if(!Strings.isBlank(axId) && !axId.contains("##")) {
196  axId += "##" + member.getMemberId();
197  }
198  return axId;
199  }
200 
201  /* Dao */
202 
203  private final Cached<Dao> dao = Cached.instance(() -> new AlliancePU());
204 
205  /* Factory */
206 
207  public static Projects from(WsMember member) {
208  return new Projects(member);
209  }
210 
211  private final WsMember member;
212 
213  private Projects(WsMember member) {
214  this.member = member;
215  }
216 
217 }
static Participations from(AxParticipationRequest request)
void setRelatedMemberId(Long relatedMemberId)
Definition: RelationId.java:70
void setRelatedEntityId(String relatedEntityId)
Definition: RelationId.java:62
Jsons axParticipations(String projectId, String as)
static Projects from(WsMember member)
Jsons axAllParticipations(String projectId)
static AxAllianceParticipation fromJson(JsonValue value)
static AxParticipationRequest fromJson(JsonValue value)
IContact getContact()
Definition: WsMember.java:130