Skip to content

Commit 77b3053

Browse files
authored
feat(common,directory): #WB-1404, framework extension for actualites app requirements (#622)
* feat(common): #WB-1404, add a hook for running post-SQL scripts tasks * Add a method to get details on users' structures
1 parent b3fb44b commit 77b3053

File tree

5 files changed

+79
-19
lines changed

5 files changed

+79
-19
lines changed

common/src/main/java/org/entcore/common/http/BaseServer.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import io.vertx.core.Promise;
3737
import io.vertx.core.shareddata.LocalMap;
38+
3839
import org.entcore.common.cache.CacheFilter;
3940
import org.entcore.common.cache.CacheService;
4041
import org.entcore.common.cache.RedisCacheService;
@@ -62,6 +63,7 @@
6263
import org.entcore.common.utils.Config;
6364
import org.entcore.common.utils.Mfa;
6465
import org.entcore.common.utils.Zip;
66+
6567
import io.vertx.core.AsyncResult;
6668
import io.vertx.core.Handler;
6769
import io.vertx.core.buffer.Buffer;
@@ -71,6 +73,8 @@
7173
import java.io.File;
7274
import java.util.*;
7375

76+
import io.vertx.core.Future;
77+
7478
public abstract class BaseServer extends Server {
7579
public static final String ONDEPLOY_I18N = "ondeploy.i18n";
7680
private static String moduleName;
@@ -254,8 +258,10 @@ protected void initModulesHelpers(String node) {
254258
getEventBus(vertx),
255259
postgresConfig.getString("sqlAdminAdress", "sql.persistor.admin")
256260
);
261+
final String initScriptsPath = FileResolver.absolutePath(config.getString("init-scripts", "sql"));
257262
DB migration = new DB(vertx, sqlAdmin, schema);
258-
migration.loadScripts(FileResolver.absolutePath(config.getString("init-scripts", "sql")));
263+
migration.loadScripts(initScriptsPath)
264+
.compose(Void -> postSqlScripts());
259265
}
260266
if (config.getBoolean("elasticsearch", false)) {
261267
if (config.getJsonObject("elasticsearchConfig") != null) {
@@ -406,4 +412,13 @@ public String getSchema() {
406412
return schema;
407413
}
408414

415+
/**
416+
* An overridable hook allowing additional non-sql tasks to be done
417+
* after all SQL migration scripts have been applied.
418+
* @return a future
419+
*/
420+
protected Future<Void> postSqlScripts() {
421+
return Future.succeededFuture();
422+
}
423+
409424
}

common/src/main/java/org/entcore/common/sql/DB.java

+19-14
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,29 @@ public void handle(AsyncResult<List<String>> asyncResult) {
9999
final String filename = f.substring(f.lastIndexOf(File.separatorChar) + 1);
100100
if (!excludeFileNames.contains(filename)) {
101101
vertx.fileSystem().readFile(f, bufferAsyncResult -> {
102-
if (bufferAsyncResult.succeeded()) {
103-
String script = bufferAsyncResult.result().toString();
104-
script = script.replaceAll("\\-\\-\\s.*(\r|\n|$)", "").replaceAll("(\r|\n|\t)", " ");
105-
s.raw(script);
106-
newFiles.add(new JsonArray().add(filename));
107-
} else {
108-
log.error("Error reading file : " + f, bufferAsyncResult.cause());
109-
}
110-
if (count.decrementAndGet() == 0) {
111-
commit(schema, s, newFiles).onComplete(promise);
112-
}
113-
});
102+
if (bufferAsyncResult.succeeded()) {
103+
String script = bufferAsyncResult.result().toString();
104+
script = script.replaceAll("\\-\\-\\s.*(\r|\n|$)", "").replaceAll("(\r|\n|\t)", " ");
105+
s.raw(script);
106+
newFiles.add(new JsonArray().add(filename));
107+
} else {
108+
log.error("Error reading file : " + f, bufferAsyncResult.cause());
109+
}
110+
if (count.decrementAndGet() == 0) {
111+
commit(schema, s, newFiles).onComplete(promise);
112+
}
113+
});
114114
} else {
115115
count.decrementAndGet();
116116
}
117117
}
118-
if (count.get() == 0 && newFiles.size() > 0) {
119-
commit(schema, s, newFiles).onComplete(promise);
118+
if (count.get() == 0) {
119+
if(newFiles.size() > 0) {
120+
commit(schema, s, newFiles).onComplete(promise);
121+
} else {
122+
// No script has been played = success
123+
promise.complete();
124+
}
120125
}
121126
} else {
122127
log.error("Error reading sql directory : " + path, asyncResult.cause());

directory/src/main/java/org/entcore/directory/controllers/DirectoryController.java

+6
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,12 @@ public void directoryHandler(final Message<JsonObject> message) {
578578
JsonArray structuresToExclude = message.body().getJsonArray("structures-to-exclude");
579579
userService.getMainStructure(userId, structuresToExclude, BusResponseHandler.busResponseHandler(message));
580580
break;
581+
case "getUsersStructures" : {
582+
JsonArray userIds = message.body().getJsonArray("userIds", new JsonArray());
583+
JsonArray fields = message.body().getJsonArray("fields");
584+
userService.getUsersStructures(userIds, fields, busArrayHandler(message));
585+
break;
586+
}
581587
case "list-users":
582588
JsonArray userIds = message.body().getJsonArray("userIds", new JsonArray());
583589
JsonArray groupIds = message.body().getJsonArray("groupIds", new JsonArray());

directory/src/main/java/org/entcore/directory/services/UserService.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@
1919

2020
package org.entcore.directory.services;
2121

22+
import java.util.List;
23+
24+
import org.entcore.common.user.UserInfos;
25+
import org.entcore.directory.pojo.TransversalSearchQuery;
26+
2227
import fr.wseduc.webutils.Either;
2328
import io.vertx.core.Future;
2429
import io.vertx.core.Handler;
2530
import io.vertx.core.http.HttpServerRequest;
2631
import io.vertx.core.json.JsonArray;
2732
import io.vertx.core.json.JsonObject;
28-
import org.entcore.common.user.UserInfos;
29-
import org.entcore.directory.pojo.TransversalSearchQuery;
30-
31-
import java.util.List;
3233

3334
public interface UserService {
3435

@@ -161,6 +162,15 @@ void listByLevel(String levelContains, String levelNotContains, String profile,
161162

162163
void getMainStructure(String userId, JsonArray structuresToExclude, Handler<Either<String, JsonObject>> result);
163164

165+
/**
166+
* Get some details about the structures that a list of users are attached to.
167+
* @param userIds IDs of users
168+
* @param fields fields of the structure nodes, to be returned in details. Default to ["id"]
169+
* @return an array of JsonObjects, such as
170+
* { userId: "ID of the user", structures: [{id: "ID of the structure", ... other queried fields...}] }
171+
*/
172+
void getUsersStructures(JsonArray userIds, JsonArray fields, Handler<Either<String, JsonArray>> handler);
173+
164174
void getAttachmentSchool(String userId, JsonArray structuresToExclude, Handler<Either<String, JsonObject>> result);
165175

166176
Future<JsonObject> getUsersDisplayNames(JsonArray userIds);

directory/src/main/java/org/entcore/directory/services/impl/DefaultUserService.java

+24
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,30 @@ public void getMainStructure(String userId, JsonArray structuresToExclude, Handl
13211321
}
13221322

13231323

1324+
public void getUsersStructures(JsonArray userIds, JsonArray fields, Handler<Either<String, JsonArray>> handler) {
1325+
if (fields == null || fields.size() == 0) {
1326+
fields = new JsonArray().add("id");
1327+
}
1328+
final StringBuilder query = new StringBuilder(
1329+
"MATCH (u:User)-[:IN]->(ProfileGroup)-[:DEPENDS]->(s:Structure) " +
1330+
"WHERE u.id IN {userIds} " +
1331+
"RETURN u.id as userId, COLLECT(distinct {");
1332+
for (Object field : fields) {
1333+
query.append(field).append(": s.").append(field).append(",");
1334+
}
1335+
query.deleteCharAt(query.length() - 1).append("}) as structures ");
1336+
1337+
JsonObject params = new JsonObject().put("userIds", userIds);
1338+
neo.execute(query.toString(), params, validResultHandler(res->{
1339+
if (res.isRight()) {
1340+
final JsonArray result = res.right().getValue();
1341+
handler.handle(new Either.Right<>(result));
1342+
} else {
1343+
handler.handle(res);
1344+
}
1345+
}));
1346+
}
1347+
13241348
public void getAttachmentSchool(String userId, JsonArray structuresToExclude, Handler<Either<String, JsonObject>> handler) {
13251349
String query =
13261350
"MATCH (u:User {id : {userId}})-[:ADMINISTRATIVE_ATTACHMENT]->(s:Structure) WHERE NOT s.id IN {structuresIds} " +

0 commit comments

Comments
 (0)