63 lines
2.0 KiB
Java
63 lines
2.0 KiB
Java
package eu.dnetlib.dnetrolemanagement.scripts;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.JsonArray;
|
|
import eu.dnetlib.dnetrolemanagement.entities.User;
|
|
import eu.dnetlib.dnetrolemanagement.services.RegistryService;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.boot.CommandLineRunner;
|
|
import org.springframework.context.annotation.Profile;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.PrintWriter;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
@Component
|
|
@Profile("export-managers")
|
|
public class TypeManagersExport implements CommandLineRunner {
|
|
|
|
private static class Manager {
|
|
String entity;
|
|
String email;
|
|
|
|
public Manager(String entity, String email) {
|
|
this.entity = entity;
|
|
this.email = email;
|
|
}
|
|
|
|
public String toCSV() {
|
|
return entity + "," + email;
|
|
}
|
|
}
|
|
|
|
@Autowired
|
|
RegistryService registryService;
|
|
private final static String TYPE = "community";
|
|
|
|
@Override
|
|
public void run(String... args) throws Exception {
|
|
File file = new File(TYPE + "-managers.csv");
|
|
List<Manager> managers = new ArrayList<>();
|
|
JsonArray cous = registryService.getCous(TYPE + ".");
|
|
cous.forEach(cou -> {
|
|
String entity = cou.getAsJsonObject().get("Name").getAsString().replace(TYPE + ".", "");
|
|
Integer couId = cou.getAsJsonObject().get("Id").getAsInt();
|
|
User[] emails = new Gson().fromJson(registryService.getUserEmailByCouId(couId, true), User[].class);
|
|
for(User email:emails) {
|
|
managers.add(new Manager(entity, email.getEmail()));
|
|
}
|
|
});
|
|
try (PrintWriter pw = new PrintWriter(file)) {
|
|
pw.println(TYPE + ",email");
|
|
managers.stream()
|
|
.map(Manager::toCSV)
|
|
.forEach(pw::println);
|
|
} catch (FileNotFoundException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|