BrightSide Workbench Full Report + Source Code
AxClient.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.service;
20 
21 import java.util.HashSet;
22 import java.util.Set;
23 import org.turro.action.Contacts;
24 import org.turro.alliance.db.AlliancePU;
25 import org.turro.alliance.db.entities.AxCenter;
26 import org.turro.alliance.db.entities.AxChallenge;
27 import org.turro.alliance.db.entities.AxContact;
28 import org.turro.alliance.db.entities.AxProject;
29 import org.turro.alliance.db.entities.AxProjectGrant;
30 import org.turro.alliance.db.entities.AxStudent;
31 import org.turro.alliance.db.entities.AxTag;
32 import org.turro.collections.KeyValueMap;
33 import org.turro.dossier.db.DossierPU;
34 import org.turro.dossier.entity.Dossier;
35 import org.turro.dossier.entity.ParticipantRole;
36 import org.turro.dossier.entity.ProjectGrant;
37 import org.turro.elephant.context.Application;
38 import org.turro.jpa.Dao;
39 import org.turro.json.Jsons;
40 import org.turro.member.db.entities.AxChallengeCategory;
41 import org.turro.member.db.entities.AxProjectCategory;
42 import org.turro.member.db.entities.AxProjectGrantPublic;
43 import org.turro.sql.SqlClause;
44 import org.turro.students.db.StudentsPU;
45 import org.turro.students.entities.Challenge;
46 import org.turro.ws.WsServer;
47 import org.turro.ws.service.IWsService;
48 import org.turro.ws.service.member.SecuredClientService;
49 import org.turro.ws.service.WsService;
50 
55 @WsService(path = AxClient.CLIENT_SERVICE, name = "Alliance Member")
56 public class AxClient extends SecuredClientService implements IWsService {
57 
58  public static final String CLIENT_SERVICE = "/axclient";
59 
60  @Override
61  protected void doProcess(Application application, KeyValueMap map, WsServer server, Jsons json, String reason) {
62  if(AxConstants.USERS.equals(reason)) {
63  long memberId = json.getInt("memberId");
64  Jsons users = Jsons.array();
65  getParticipantsIds().forEach(id -> {
66  users.addValue(Jsons.read(AxContact.from(memberId, id).toJson()));
67  });
68  writeResponse(application, users.build());
69  } else if(AxConstants.PROJECTS.equals(reason)) {
70  long memberId = json.getInt("memberId");
71  Jsons projects = Jsons.array();
72  Dao dao = new DossierPU();
73  SqlClause.select("pc").from("AxProjectCategory pc")
74  .where().equal("pc.serverDomain", server.getServerDomain())
75  .dao(new AlliancePU())
76  .resultList(AxProjectCategory.class).forEach(pc -> {
77  Dossier dossier = dao.find(Dossier.class, Long.valueOf(pc.getProjectId()));
78  if(dossier != null && dossier.getProject() != null) {
79  projects.addValue(Jsons.read(AxProject.from(memberId, server, dossier, pc).toJson()));
80  }
81  });
82  writeResponse(application, projects.build());
83  } else if(AxConstants.PROJECT_GRANTS.equals(reason)) {
84  long memberId = json.getInt("memberId");
85  Jsons projectGrants = Jsons.array();
86  Dao dao = new DossierPU();
87  SqlClause.select("pc").from("AxProjectGrantPublic pc")
88  .where().equal("pc.serverDomain", server.getServerDomain())
89  .dao(new AlliancePU())
90  .resultList(AxProjectGrantPublic.class).forEach(pc -> {
91  ProjectGrant projectGrant = dao.find(ProjectGrant.class, Long.valueOf(pc.getProjectGrantId()));
92  if(projectGrant != null) {
93  projectGrants.addValue(Jsons.read(AxProjectGrant.from(memberId, projectGrant).toJson()));
94  }
95  });
96  writeResponse(application, projectGrants.build());
97  } else if(AxConstants.CENTERS.equals(reason)) {
98  long memberId = json.getInt("memberId");
99  Jsons centers = Jsons.array();
100  Contacts.getCenters().forEach(center -> {
101  centers.addValue(Jsons.read(AxCenter.from(memberId, center).toJson()));
102  });
103  writeResponse(application, centers.build());
104  } else if(AxConstants.STUDENTS.equals(reason)) {
105  long memberId = json.getInt("memberId");
106  Jsons students = Jsons.array();
107  Contacts.getStudents().forEach(student -> {
108  if(student.isStudent() && !student.isWorker()) {
109  students.addValue(Jsons.read(AxStudent.from(memberId, student).toJson()));
110  }
111  });
112  writeResponse(application, students.build());
113  } else if(AxConstants.CHALLENGES.equals(reason)) {
114  long memberId = json.getInt("memberId");
115  Jsons challenges = Jsons.array();
116  Dao dao = new StudentsPU();
117  SqlClause.select("pc").from("AxChallengeCategory pc")
118  .where().equal("pc.serverDomain", server.getServerDomain())
119  .dao(new AlliancePU())
120  .resultList(AxChallengeCategory.class).forEach(pc -> {
121  Challenge challenge = dao.find(Challenge.class, Long.valueOf(pc.getChallengeId()));
122  if(challenge != null) {
123  challenges.addValue(Jsons.read(AxChallenge.from(memberId, server, challenge, pc).toJson()));
124  }
125  });
126  writeResponse(application, challenges.build());
127  } else if(AxConstants.TAGS.equals(reason)) {
128  long memberId = json.getInt("memberId");
129  Jsons tags = Jsons.array();
130  Dao dao = new DossierPU();
131  SqlClause.select("pc").from("AxProjectCategory pc")
132  .where().equal("pc.serverDomain", server.getServerDomain())
133  .dao(new AlliancePU())
134  .resultList(AxProjectCategory.class).forEach(pc -> {
135  Dossier dossier = dao.find(Dossier.class, Long.valueOf(pc.getProjectId()));
136  if(dossier != null && dossier.getProject() != null) {
137  AxTag.from(memberId, dossier).forEach(tag -> {
138  tags.addValue(Jsons.read(tag.toJson()));
139  });
140  }
141  });
142  SqlClause.select("pc").from("AxProjectGrantPublic pc")
143  .where().equal("pc.serverDomain", server.getServerDomain())
144  .dao(new AlliancePU())
145  .resultList(AxProjectGrantPublic.class).forEach(pc -> {
146  ProjectGrant projectGrant = dao.find(ProjectGrant.class, Long.valueOf(pc.getProjectGrantId()));
147  if(projectGrant != null) {
148  AxTag.from(memberId, projectGrant).forEach(tag -> {
149  tags.addValue(Jsons.read(tag.toJson()));
150  });
151  }
152  });
153  writeResponse(application, tags.build());
154  }
155  }
156 
157  private Set<String> getParticipantsIds() {
158  Set<String> ids = new HashSet<>();
159  ids.addAll(SqlClause.select("distinct p.idContact").from("Participant p")
160  .where().notEqual("role", ParticipantRole.PARTICIPANT_SUBJECT)
161  .dao(new DossierPU())
162  .resultList(String.class));
163  ids.addAll(SqlClause.select("distinct p.idContact").from("CategoryParticipant p")
164  .where().notEqual("role", ParticipantRole.PARTICIPANT_SUBJECT)
165  .dao(new DossierPU())
166  .resultList(String.class));
167  return ids;
168  }
169 
170 }
static List< IContact > getCenters()
Definition: Contacts.java:146
static List< IContact > getStudents()
Definition: Contacts.java:150
static AxCenter from(long memberId, String contactId)
Definition: AxCenter.java:120
static AxContact from(long memberId, String contactId)
Definition: AxContact.java:162
static Set< AxTag > from(long memberId, Dossier dossier)
Definition: AxTag.java:66
static final String CLIENT_SERVICE
Definition: AxClient.java:58
void doProcess(Application application, KeyValueMap map, WsServer server, Jsons json, String reason)
Definition: AxClient.java:61
String getServerDomain()
Definition: WsServer.java:52