BrightSide Workbench Full Report + Source Code
ImportFromMembers.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.importer;
20 
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
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.AxDescriptor;
29 import org.turro.alliance.db.entities.AxParticipation;
30 import org.turro.alliance.db.entities.AxProject;
31 import org.turro.alliance.db.entities.AxProjectGrant;
32 import org.turro.alliance.db.entities.AxResponse;
33 import org.turro.alliance.db.entities.AxStudent;
34 import org.turro.alliance.db.entities.AxTag;
35 import org.turro.alliance.service.AxConstants;
36 import org.turro.alliance.service.AxClient;
37 import org.turro.jpa.Dao;
38 import org.turro.json.Jsons;
39 import org.turro.log.WebLoggers;
40 import org.turro.sql.SqlClause;
41 import org.turro.tags.Tags;
42 import org.turro.ws.WsMember;
43 import org.turro.ws.service.server.Clients;
44 
49 public class ImportFromMembers {
50 
51  public static void connect() {
52  Clients.getMembers(AxClient.CLIENT_SERVICE).forEach(member -> {
53  if(Clients.checkClientService(member.getMemberDomain(), member.getService())) {
54  connect(member);
55  }
56  });
57  }
58 
59  public static void connect(WsMember member) {
60  try {
62  ifm.removeContactsFromMember(member);
63  ifm.importContactsFromJson(member, Clients.getData(member, AxConstants.USERS));
64  ifm.removeProjectsFromMember(member);
66  ifm.removeDescriptorsFromMember(member);
67  ifm.importProjectsFromJson(member, Clients.getData(member, AxConstants.PROJECTS));
69  ifm.importProjectGrantsFromJson(member, Clients.getData(member, AxConstants.PROJECT_GRANTS));
70  ifm.removeCentersFromMember(member);
71  ifm.importCentersFromJson(member, Clients.getData(member, AxConstants.CENTERS));
72  ifm.removeStudentsFromMember(member);
73  ifm.importStudentsFromJson(member, Clients.getData(member, AxConstants.STUDENTS));
74  ifm.removeChallengesFromMember(member);
75  ifm.importChallengesFromJson(member, Clients.getData(member, AxConstants.CHALLENGES));
76  ifm.removeTagsFromMember(member);
77  ifm.importTagsFromJson(member, Clients.getData(member, AxConstants.TAGS));
78  } catch(Exception ex) {
80  }
81  }
82 
83  public void importContactsFromJson(WsMember member, Jsons json) {
84  json.getStructure().asArray().forEach(contact -> {
85  getDao().saveEntity(AxContact.fromJson(contact));
86  });
87  }
88 
89  public void importProjectsFromJson(WsMember member, Jsons json) {
90  json.getStructure().asArray().forEach(project -> {
91  AxProject tmp = AxProject.fromJson(project);
92  Set<AxDescriptor> descriptors = new HashSet<>();
93  descriptors.addAll(tmp.getDescriptors());
94  tmp.setDescriptors(null);
95  getDao().saveEntity(tmp);
96  for(AxDescriptor descriptor : descriptors) {
97  getDao().persistObject(descriptor);
98  }
99  });
100  }
101 
102  public void importProjectGrantsFromJson(WsMember member, Jsons json) {
103  json.getStructure().asArray().forEach(projectGrant -> {
104  AxProjectGrant tmp = AxProjectGrant.fromJson(projectGrant);
105  getDao().saveEntity(tmp);
106  });
107  }
108 
109  public void importCentersFromJson(WsMember member, Jsons json) {
110  json.getStructure().asArray().forEach(center -> {
111  AxCenter tmp = AxCenter.fromJson(center);
112  getDao().saveEntity(tmp);
113  });
114  }
115 
116  public void importStudentsFromJson(WsMember member, Jsons json) {
117  json.getStructure().asArray().forEach(student -> {
118  AxStudent tmp = AxStudent.fromJson(student);
119  getDao().saveEntity(tmp);
120  });
121  }
122 
123  public void importChallengesFromJson(WsMember member, Jsons json) {
124  json.getStructure().asArray().forEach(challenge -> {
125  AxChallenge tmp = AxChallenge.fromJson(challenge);
126  Set<AxResponse> responses = new HashSet<>();
127  responses.addAll(tmp.getResponses());
128  tmp.setResponses(null);
129  getDao().saveEntity(tmp);
130  for(AxResponse response : responses) {
131  getDao().persistObject(response);
132  }
133  });
134  }
135 
136  public void importTagsFromJson(WsMember member, Jsons json) {
137  json.getStructure().asArray().forEach(tag -> {
138  AxTag axt = AxTag.fromJson(tag);
139  Tags.addTag(axt.getEntityRoot() + axt.stringifyId(), axt.getTagName());
140  });
141  }
142 
143  public void importContactsFromRegister(WsMember member, List<ImporterRegister> data) {
144  for(ImporterRegister register : data) {
145  getDao().saveEntity(AxContact.from(member.getMemberId(), register));
146  }
147  }
148 
149  public void importProjectsFromRegister(WsMember member, List<ImporterRegister> data) {
150  for(ImporterRegister register : data) {
151  getDao().saveEntity(AxProject.from(member.getMemberId(), register));
152  }
153  }
154 
155  public void importParticipationsFromRegister(WsMember member, List<ImporterRegister> data) {
156  for(ImporterRegister register : data) {
157  getDao().saveEntity(AxParticipation.from(member.getMemberId(), register));
158  }
159  }
160 
161  public void importTagsFromRegister(WsMember member, List<ImporterRegister> data) {
162  for(ImporterRegister register : data) {
163  AxTag axt = AxTag.from(member.getMemberId(), register);
164  Tags.addTag(axt.getEntityRoot() + axt.stringifyId(), axt.getTagName());
165  }
166  }
167 
168  public void removeContactsFromMember(WsMember member) {
169  SqlClause.delete("AxContact")
170  .where().equal("contactId.memberId", member.getMemberId())
171  .dao(getDao())
172  .execute();
173  }
174 
175  public void removeProjectsFromMember(WsMember member) {
176  SqlClause.delete("AxProject")
177  .where().equal("projectId.memberId", member.getMemberId())
178  .dao(getDao())
179  .execute();
180  }
181 
183  SqlClause.delete("AxParticipation")
184  .where().equal("relation.mainMemberId", member.getMemberId())
185  .or().equal("relation.relatedMemberId", member.getMemberId())
186  .dao(getDao())
187  .execute();
188  }
189 
190  public void removeDescriptorsFromMember(WsMember member) {
191  SqlClause.delete("AxDescriptor")
192  .where().equal("memberId", member.getMemberId())
193  .dao(getDao())
194  .execute();
195  }
196 
198  SqlClause.delete("AxProjectGrant")
199  .where().equal("projectGrantId.memberId", member.getMemberId())
200  .dao(getDao())
201  .execute();
202  }
203 
204  public void removeCentersFromMember(WsMember member) {
205  SqlClause.delete("AxCenter")
206  .where().equal("centerId.memberId", member.getMemberId())
207  .dao(getDao())
208  .execute();
209  }
210 
211  public void removeStudentsFromMember(WsMember member) {
212  SqlClause.delete("AxStudent")
213  .where().equal("studentId.memberId", member.getMemberId())
214  .dao(getDao())
215  .execute();
216  }
217 
218  public void removeChallengesFromMember(WsMember member) {
219  SqlClause.delete("AxChallenge")
220  .where().equal("challengeId.memberId", member.getMemberId())
221  .dao(getDao())
222  .execute();
223  SqlClause.delete("AxResponse")
224  .where().equal("relation.mainMemberId", member.getMemberId())
225  .or().equal("relation.relatedMemberId", member.getMemberId())
226  .dao(getDao())
227  .execute();
228  }
229 
230  public void removeTagsFromMember(WsMember member) {
232  }
233 
234  /* Dao */
235 
236  private Dao _dao;
237 
238  private Dao getDao() {
239  if(_dao == null) {
240  _dao = new AlliancePU();
241  }
242  return _dao;
243  }
244 
245 }
static AxCenter fromJson(JsonValue value)
Definition: AxCenter.java:169
static AxChallenge fromJson(JsonValue value)
void setResponses(Set< AxResponse > responses)
static AxContact fromJson(JsonValue value)
Definition: AxContact.java:243
static AxContact from(long memberId, String contactId)
Definition: AxContact.java:162
static Set< AxParticipation > from(long memberId, Dossier dossier)
static AxProjectGrant fromJson(JsonValue value)
static AxProject from(long memberId, WsServer server, Dossier dossier, AxProjectCategory pc)
Definition: AxProject.java:311
void setDescriptors(Set< AxDescriptor > descriptors)
Definition: AxProject.java:172
static AxProject fromJson(JsonValue value)
Definition: AxProject.java:371
static AxStudent fromJson(JsonValue value)
Definition: AxStudent.java:183
static AxTag fromJson(JsonValue value)
Definition: AxTag.java:98
static Set< AxTag > from(long memberId, Dossier dossier)
Definition: AxTag.java:66
static final String CLIENT_SERVICE
Definition: AxClient.java:58
void importStudentsFromJson(WsMember member, Jsons json)
void removeParticipationsFromMember(WsMember member)
void removeProjectsFromMember(WsMember member)
void removeChallengesFromMember(WsMember member)
void importTagsFromRegister(WsMember member, List< ImporterRegister > data)
void importProjectGrantsFromJson(WsMember member, Jsons json)
void importProjectsFromJson(WsMember member, Jsons json)
void removeContactsFromMember(WsMember member)
void importContactsFromRegister(WsMember member, List< ImporterRegister > data)
void removeProjectGrantsFromMember(WsMember member)
void importProjectsFromRegister(WsMember member, List< ImporterRegister > data)
static void connect(WsMember member)
void removeStudentsFromMember(WsMember member)
void importTagsFromJson(WsMember member, Jsons json)
void removeDescriptorsFromMember(WsMember member)
void importChallengesFromJson(WsMember member, Jsons json)
void importParticipationsFromRegister(WsMember member, List< ImporterRegister > data)
void importContactsFromJson(WsMember member, Jsons json)
void importCentersFromJson(WsMember member, Jsons json)
void persistObject(Object obj)
Definition: Dao.java:127
WebLoggers message(String text, Object... parameters)
Definition: WebLoggers.java:34
static WebLoggers severe(Object entity)
Definition: WebLoggers.java:51
WebLoggers exception(Throwable throwable)
Definition: WebLoggers.java:29
static void addTag(Object entity, String tagName)
Definition: Tags.java:188
static void removeMemberTags(long member)
Definition: Tags.java:389
String getMemberDomain()
Definition: WsMember.java:75